I created a trigger for copy values from table1 to table2:
INSERT INTO table_2 (destinationid, destinationrow1, destinationrow2)
SELECT originid, originrow1, originrow2 FROM table_1
WHERE rowtype='special_value'
Well, I don't understand how to create the trigger so that when a specific id is deleted in the Table 1 also do it in Table 2.
Related
I need to compare two tables that are linked by Foreign Key and add records from the master table that are not already in the linked table with foreign key. What's the best way to go about doing this please?
You can create a trigger on master table that will be triggered when there's a new insert to the master table.
DELIMITER //
CREATE TRIGGER master_after_insert
AFTER INSERT
ON linked_table FOR EACH ROW
BEGIN
-- Insert record into linked_table
INSERT INTO linked_table
( id,
some_value,
master_id)
VALUES
( 1,
'test value',
NEW.id ); // <-- using NEW you can get newly inserted record
END; //
this seemed to do the trick for me:
INSERT into table2 (foreignkey_id)
SELECT id FROM table1
WHERE id NOT IN (SELECT foreignkey_id FROM table2)
I am creating a query, wherein i have created select query from table 1 and then inserting values to table 2, but how can create a command that when new data comes in table 1 then update table 2 values
and this insertion happen daily with job.
I tried creating trigger but i don t know how i can relate this to my scenario
IF OBJECT_ID('TEMPDB..#temp')IS NOT NULL
DROP TABLE #temp
Select Getdate()[Data Till],Sum[Quantity] Qty
into #temp from XYZ w
here w.Date <=Getdate()
Insert into dbo.table1
Select [Data Till],Qty from #temp >
As of now data is coming in table 2 like
1)[Data Till]-2019/05/22 Qty-100
2)[Data Till]-2019/05/23 Qty-150
3)[Data Till]-2019/05/24 Qty-120
Now what if in 2019/05/22 back dated entry of qty -20 comes in table 1 and how will table 2 has to update values in table 2
1)[Data Till]-2019/05/22 Qty-80
2)[Data Till]-2019/05/23 Qty-150
3)[Data Till]-2019/05/24 Qty-120
CREATE TRIGGER [schema_name.]trigger_name
ON Table_name
AFTER INSERT
AS
BEGIN
-- write your update statement here
END
This is how you can write after update trigger that executes your statement between 'BEGIN' and 'END' when you insert new row into 'Table_name'.
AS MY ASSUMPTION
Update means you have the data in table 2 already. If the new data comes in table 1 what kind of update you want to do ?
is it existing data update or new record insert then update.
From your question if it is updated above two kinds of things can be happened
For this why you have two write insert trigger
create trigger abc
on table1
For insert
as
begin
--update table2
end
Hi i need to Mysql Auto create, update, delete table 2 from table 1
I have table 1 which column is:
id, title, category, sku, brand, price, last_update
And i have table 2 which column is:
id, title, sku, brand, code1, code2
What i need to if table a insert new value or update or delete will affect to table 2, how can i do that ?
I try this :
INSERT INTO table2 (title, sku, brand)
SELECT title, sku, brand
FROM table1
But this not good idea because when i run agaain sql much duplicate i have and i can not update it, please tell me how do that.
Iam using trigger to solved my problem :
CREATE TRIGGER table1insert after INSERT ON table1
FOR EACH ROW BEGIN
INSERT INTO table2 SET
title = NEW.title, sku=New.sku, brand=NEW.brand
ON DUPLICATE KEY UPDATE sku=New.sku;
END$$
and i make one again trigger if table 1 delete so table 2 deleted too.
CREATE TRIGGER table1delete after DELETE ON table1
FOR EACH ROW BEGIN
DELETE FROM table2
WHERE sku=old.sku;
END$$
maybe this can help everyone need it
Is this possible? For some context I am working with temporary tables and would like to shift values around. I am simply wondering if I can insert a value from a column of one table into the column of a new table, while deleting the original value from the first table (if that makes sense).
Table 1
|id| |Column_with_value|
---------------------------------
1 | blah |
---------------------------------
Table 2
(empty)
becomes:
Table 1
(empty)
Table 2
|id| |Column_with_value|
---------------------------------
1 | blah |
---------------------------------
I don't know one single operation which can do this. But there is really no need to put that into one operation as long as it stays in one transaction:
BEGIN;
SELECT * FROM table1 …;
UPDATE table2 …;
DELETE FROM table1 …;
COMMIT;
You can use a trigger on table 1 which listens for delete statements.
Something like this:
CREATE TRIGGER `my_insert_table2_trigger`
AFTER DELETE ON `table1`
FOR EACH ROW
BEGIN
INSERT INTO `table2` VALUES (OLD.id, OLD.Column_with_value)
END
With OLD you can access the values of the deleted row.
Try like this
CREATE TRIGGER Remove_Old
BEFORE DELETE ON Table_1
FOR EACH ROW
Insert Into Table_2
SELECt * From Table_1
Moving data between tables costs, complicates schema, and increases maintenance. Probably better if you had a single table with all of the data, with an additional column to tag it accordingly.
However
INSERT INTO tbl_1 (id, column)
SELECT id, column from tbl_2 where id=1;
DELETE FROM tbl1 where id=1;
Or if you just want to move everything from a full table to an empty table, try
RENAME TABLE tbl_1 TO tbl_2
I need create sql script for insert in many tables - total near 2000, but tables linked (it is an xml adjancency model) - so after have inserted in table1, receive id, add some data, insert into fk of table2 - previous id, and data, after repeat for table3, for table 4,...
Length of chain of linked tables - more than 20.
The question is: exists some code generator that can read relations between tables from information schema and - after point some table as root - generate all tree of insert query?
Thanks.
If you can't do it on application layer, I suggest trying to do it with Triggers.
Essentially, you would create an "AFTER INSERT" Trigger on each of the tables. Smth like that:
CREATE TRIGGER t1
AFTER INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO table2 (id, attrib2) VALUES(NEW.id, NEW.attrib2);
END
CREATE TRIGGER t2
AFTER INSERT ON table2
FOR EACH ROW
BEGIN
INSERT INTO table3 (id, attrib3) VALUES(NEW.id, NEW.attrib3);
END
Then, an insert into table1 would trigger an insert into table2, which would trigger an insert into table3 and so on.