how to write before update trigger? - sqlite

I have two tables namely table1 and table2.
When table1 is updated, I want to insert the row being updated to table2 so that table2 serves as a log.
table1 has column1,column2,column3,column4....column10
table2 has column1,column2,column3,column4
So while inserting into table2, I just want column1,column2,column3,column4 from table1.

This will be simple if you just want to add the record updated from table1 to table2 and you can use after update
delimiter //
create trigger update_table1 after update on table1
for each row
begin
insert into table2
(column1,column2,column3,column4)
values
(old.column1,old.column2,old.column3,old.column4);
end; //
delimiter;

Related

How can I UPDATE table using condition WHERE from another table in SQLite

I'm working with SQLite and I have two tables:
table1 table2
------------- --------------
id id
value condition
Column "id" contains the same data. And I need to:
UPDATE table1
SET table1.value = 'Some value'
WHERE table2.condition = 'Some condition"
I tried to use JOIN and FROM for linking tables with "id" column, but this isn`t working in SQLite. Please help with syntax.
You need a correlated subquery in the update, something like this:
UPDATE table1
SET value = 'Some value'
WHERE EXISTS (SELECT 1 FROM table2 t2
WHERE t2.id = table1.id AND
t2.condition = 'Some condition');
Method 1 : Considering the two tables you have are "Table1" and "Table2", Updating Table1 can be done using a nested Select statement in which you will be selecting the data you need to update from Table2. For example:
UPDATE Table1
SET StudentID =
(
SELECT RegNo
FROM Table2 t
WHERE StudentID = RegNo
);
You can have a look on this link which solves a similar question : https://dba.stackexchange.com/questions/206317/update-values-in-one-table-from-data-in-another
Method 2 : You can use a table join. Refer the same link given above.

Insert random row to table and then delete

I've got two tables: tableA and tableB. I would like to insert random row from tableA to tableB and then delete this row from tableA. How can i do it? Is it possible at all? Below is my insert code but i dont know how to delete this row.
INSERT INTO tableB
SELECT * FROM tableA ORDER BY RANDOM() LIMIT 1;
You can use rowid:
DELETE from tableA
WHERE elementA = (
SELECT elementA from tableB
WHERE rowid = (SELECT MAX(rowid) FROM tableB)
)
Provided that elementA is unique in both tables, if this is executed after your INSERT statement it will find elementA from the inserted row and delete the row from TableA with that elementA.

SQLite: updating column from another table

I have a old table and a new table. what i need is to copy the uuId of the old table to the new Table.
im following some answers from other references but i can`t get the ideal answer.
the closest answer i found is this:
update table1
set table1.uuid =
(select table2.uuid from table2 where table1.itemDescription = table2.itemDescription)
when i execute this query, it only saves the 1st found uuid of the old table to all the entry in the new Table.
Sample Table2 (old table):
uuid|itemDescription
1|item1
2|item2
3|item3
Sample Table1 (new Table):
uuid|itemDescription
Null|item1
Null|item2
Null|item3
Desired Output:
uuid|itemDescription
1|item1
2|item2
3|item3
what happens:
uuid|itemDescription
1|item1
1|item2
1|item3
In SQLite, you must not use the table name in the SET clause:
update table1
set uuid =
(select table2.uuid from table2 where table1.itemDescription = table2.itemDescription);
Try like this :
UPDATE table1
SET table1.uuid = table2.uuid
FROM table2 WHERE table1.itemDescription = table2.itemDescription
No need for subquery, otherwise you need to link item description in outer query also

sqlite: insert a row using sqlite_sequence record

I have a table in my database... I want to insert a row to table1 by reading from a record on sqlite_sequence... I know the sqlite_sequence table is for AUTOINCREMENT... and also my table1 has AUTOINCREMENT for _id as well...
I tried this:
INSERT INTO table1 (column_1, column_2) VALUES ((SELECT seq FROM sqlite_sequence WHERE name=other_table),`other_column`);
but after inserting, inserted row will be deleted in table1...
I want to know the last row id in other_table... and i tried with last_insert_rowid() instead of (SELECT seq FROM sqlite_sequence WHERE name=other_table)... but the result was same...
Where is my mistake?? thanks!

Sqlite Query replacing a column with a column from another table

I have 2 tables, one is indexing the other.
I am querying Table#1, and it has one column (string) that has an ID in it that corresponds to a unique row in Table#2. Im trying to write a query in Sqlite that allows me to retrieve the value from Table#2 if the column value in Table#1 is not an empty string.
Kinda like:
"SELECT TMake,TModel,TTrim,IYear,[%q] AS TPart1 FROM AppGuide WHERE TPart1 != ''"
But instead of retrieving the Index value (TPart1) Id like to get the string from Table#2.
Is this possible?
Any help is appreciated.
You could use a correlated subquery:
SELECT TMake,
TModel,
...,
(SELECT stringvalue
FROM Table2
WHERE Table2.ID = Table1.TPart1)
FROM Table1
WHERE Table1.TPart1 != ''
However, these are rather slow to execute, so you'd better use a join (this returns exactly the same result):
SELECT Table1.TMake,
Table1.TModel,
...,
Table2.stringvalue
FROM Table1 LEFT JOIN Table2 ON Table1.TPart1 = Table2.ID
WHERE Table1.TPart1 != ''
If you don't want to get records from Table1 that have no matching Table2 record, drop the LEFT.

Resources