简体   繁体   中英

How to join two rows from a mysql table into a third one

what I have to do is basiclly create 3 tables.

I have created the first two which are the following.

  • table 1

    • User_id
    • email
    • Password
  • table 2

    • Schedule_id
    • Time
    • Date
  • Table 3 has to be this

    • User_id <<<< that user_id has to be the same as the one mentioned in table number one, where if I was to change the user ID in table 1 it would also change in table number 3
    • number

What you need is a FOREIGN KEY with a ON UPDATE CASCADE definition, like this:

create table table_3 (
  user_id int(10) unsigned,
  CONSTRAINT fk_tb_1_user_id FOREIGN KEY (user_id) REFERENCES table_1(user_id) ON UPDATE CASCADE
);

int(10) unsigned is a type I invented to exemplify, but it should match exactly the user_id column type in table_1.

But pay attention, for the FOREIGN KEY to work, every user_id in table_3 MUST exist in table_1, you will not be able to insert a user_id in table_3 if it doesn't exist in table_1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM