Reputation:
I have two tables called 'USER' and 'CART' the two tables contain the same column name 'USER_ID'.
If I am to insert a new 'USER_ID' into the 'USER' table, how can I make this 'USER_ID' be in the 'CART' table as well? Using only one command:
INSERT INTO USER (USER_ID) VALUES ('1');
Is there an easy way to do this ?
Upvotes: 0
Views: 161
Reputation: 75
Tanya, I guess it's not possible to insert into two tables with one query in mysql. You may find your question discussed/answered in the below link.
sql - insert into multiple tables in one query
Upvotes: 1
Reputation: 4287
Well, you could use triggers and insert the same userid in the cart table.
Upvotes: 0
Reputation: 493
If your User table is master table and Cart is child table, then you can use.
trigger. Insert after User table you need to insert into Cart table. here is the exact turorial you want.
trigger after insert tutorial.
Upvotes: 0
Reputation:
You can't, you can do two statements wrapped in one transaction to insure consistency.
You can use a trigger, but triggers are an anti-pattern at best and the side effects and complexities to say, undo their effects are almost NEVER worth it.
Upvotes: 0