MySQL generate sql insert for depended tables - mysql

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.

Related

MySQL Update Linked Table If Master Table Has New Records

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)

How to update values automatically in table 2, when new values occurs in table 1

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

Pass data from one table to another in mysql

I'm learning mysql and i try to pass data from one table to another.
In Details, i have table1 with columns id, firstname, lastname etc.
Then i have a table2 with columns id, firstname, lastname, bodyfat, bodyweight, matchid.
The thing that i want is to pass the data firstname and lastname from table1 to table2 and they should be matched on table1.id = table2.matchid. Also the procedure should happen every time that new records are inserted into the table1, so the specific values are also updated and displayed in table2.
Is there any way to achieve that?
Thanks, in advance!
Use an after insert trigger:
DELIMITER //
CREATE TRIGGER after_user_insert
AFTER INSERT
ON table1 FOR EACH ROW
BEGIN
INSERT INTO table2 (firstname, lastname, matchid)
VALUES
(NEW.firstname, NEW.lastname, NEW.id);
END; //
DELIMITER ;
This assumes that you only want to insert new records in table2 with the first and last name, which is all the available information in table1. You could include other columns in the insert, but you would have to specify how to do that.
Beyond this, if you needed to initially populate table2 from table1, you could do an INSERT INTO ... SELECT:
INSERT INTO table2 (firstname, lastname)
SELECT firstname, lastname
FROM table1
-- WHERE <conditions>
you can use a trigger on table 1. A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.
You can set a trigger on insert event in table 1. Here is a link https://dev.mysql.com/doc/refman/5.7/en/triggers.html that shows examples of trigger in mysql. Hope this helps
You can Create Trigger on Table1 with the insert or update statement into the Table2 using AFTER keyword, so that whenever a data is inserted in the Table 1 the trigger will be invoked and does the respective action based on your trigger definition.
For more details about how to create and use a trigger for MySQL refer the below link.
https://dev.mysql.com/doc/refman/5.7/en/triggers.html

How to copy-paste data from one table to another in MySQL?

I have crate a db in MySQL which has a lot of tables. I want the value of one table to be automatically saved on another table too.
For example I write something on: table1.lastname, I want this to be also stored in table2.lastname .
How is this called and how I can do that with PHP My Admin?
CREATE TABLE new_table_name LIKE old_table_name
Create trigger after_insert on new_table
like this
CREATE TRIGGER `AFTER_INSERT` AFTER INSERT ON `new_table_name` FOR EACH ROW BEGIN
insert into new_table_name (column_names) values (column_values) ;
END
For first you must create table for data store.
Then you must create trigger on wanted table for catch event and insert data in early created table.
This will do what you want:
INSERT INTO table2 (lastname)
SELECT lastname
FROM table1
If you want to include all rows from table1. Otherwise you can add a WHERE statement to the end if you want to add only a subset of table1.
I hope this helps.
If the table doesn't exist, you can create one with the same schema like so:
CREATE TABLE table2 LIKE table1;
Then, to copy the data over:
INSERT INTO table2 SELECT * FROM table1
Or If the tables have different structures you can also:
INSERT INTO table2 (`col1`,`col2`) SELECT `col1`,`col2` FROM table1;
EDIT: to constrain this..
INSERT INTO table2 (`col1_`,`col2_`) SELECT `col1`,`col2` FROM
table1 WHERE `foo`=1

BEFORE INSERT TRIGGER on one table but insert should be done on another table

I have two tables say table1 and table2.
fields of table1 - id and count
fields of table2 - id and count
both tables have same fields. Now I want to create BEFORE INSERT TRIGGER on table1 which will check if count = 2 then insert into table2 otherwise in table1.
I created the trigger but when I fire this query ..example-
insert into table1 (id, count) values (1323, 2);
then one row is inserted into table2 (this I want) and same row is inserted into table1 also.
I want row should be inserted in table1 or table not in both tables.
what I do to achieve this ?
Try with this:
delimiter $$
CREATE TRIGGER myTrigger
BEFORE INSERT ON table1
FOR EACH ROW
BEGIN
IF NEW.count = 2 THEN
insert into table2 (id, count) values (New.id,New.Count);
ELSE
<<no action>>
END IF;
END$$
I'm not sure why you want this, so I suspect I might be giving advice on some sort of solution that should be fixed differently, but here we go.
As you can see in this question here it is not possible to neatly stop an insert. You could try and make an error so you'll not actually insert, but I think that is really painful.
What you can do is make a fake table dummytable and put your insert trigger there. Your row will always insert in the dummy table, but you either insert it in table1 or table2 according to your wishes. The meat of the trigger would look like
IF NEW.count = 2 THEN
INSERT INTO table2 (id, count) VALUES (New.id,New.Count);
ELSE
INSERT INTO table2 (id, count) VALUES (New.id,New.Count);
END IF;
You'll have to clean the dummytable now and then.