0

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 ?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502

4 Answers4

1

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

Community
  • 1
  • 1
0

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.

0

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.

av1987
  • 493
  • 4
  • 12
0

Well, you could use triggers and insert the same userid in the cart table.

xception
  • 4,241
  • 1
  • 17
  • 27