Is it possible to insert in to two tables at once? I need to insert some data in to a table(contactinformation) and then based on the primary key insert in to a userstable and have the primarykey set as a field (foreign key) . Is this possible?
Thanks
You can write a procedure for this.
DELIMITER //
CREATE PROCEDURE `proc1` (contactinformation colums... usertable columns...)
BEGIN
INSERT INTO contactinformation values(contactinformation colums ...);
INSERT INTO usertable values(LAST_INSERT_ID(), contactinformation colums ...);
END//
DELIMITER ;
contactinformation colums...
means the column definitions of contactinformation table.usertable columns...
means the column definitions of usertable table.
After the first insert you can get the insert id if contactinformation table has any auto column. Then use that key in the second insert statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With