Pass data from one table to another in mysql - 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

Related

how to use primary key as custId number in second table

I have two tables, the first has an auto incrementing ID number, I want to use that as custId in the second table.
I am using an insert into the first table with all the basic info, name, address etc. Then in the second table only 3 things, custId, stocknum, and location. How can I write to these two tables kinda of simultaneously since stockNum may have several values, but always attached to one custId. I hope this makes sense even without putting code in here.
You can't insert into multiple tables at the same time. You have two options. You either do two inserts
INSERT INTO table1 (col1, col2) VALUES ('value1',value2);
/* Gets the id of the new row and inserts into the other table */
INSERT INTO table2 (cust_id, stocknum, location) VALUES (LAST_INSERT_ID(), 'value3', 'value4')
Or you can use a post-insert trigger
CREATE TRIGGER table2_auto AFTER INSERT ON `table1`
FOR EACH ROW
BEGIN
INSERT INTO table2 (cust_id, stocknum, location) VALUES (NEW.id, value3, 'value4')
END
Hope this helps.
After inserting in the first table, The identity field or Auto increment field generate an ID
Get this id Refer Here(LAST_INSERT_ID() MySQL)
Then use this id to store value in the other table

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

Trigger insert deleted row

Hello I have some difficulties with simple trigger in MSSQL 2008.
I want to insert deleted row to another table.
Here is my trigger code:
use databasex;
GO
CREATE TRIGGER DeleteEmployee
ON Employees
FOR DELETE
AS
BEGIN
INSERT INTO DeletedEmployees (Name, Surname, Function)
VALUES (deleted.Name, deleted.Surname, deleted.Function)
END
Employees table:
Name Surname Function ...
DeletedEmployees table:
Name Surname Function ...
I need somehow to specify that deleted columns are from deleted table, but how ?
I dont want to write subquery for every column.
How it being done ?
ps: is there any good tutorial on using triggers ? i believe i will need write more triggers in the future.
DELETED is a virtual table you select from (you may have deleted more than one row, so a single value is not enough)
Also, FUNCTION is a reserved word and needs to be escaped
CREATE TRIGGER DeleteEmployee
ON Employees
FOR DELETE
AS
BEGIN
INSERT INTO DeletedEmployees (Name, Surname, [Function])
SELECT Name, Surname, [Function] FROM deleted
END
An SQLfiddle to test with.

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.

MySQL generate sql insert for depended tables

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.