简体   繁体   中英

How to create a mysql table with multiple foreign keys and multiple columns

So I've been researching this for hours and been having no luck in finding something suitable for what I'm trying to do.

I'm currently having two issues: Creating a table with multiple foreign keys referencing the same primary key and splitting these attributes into multiple columns.

In the end, I would like my table to look something like this, where my O-Director_ID is the primary key in one column that correlates to all the other ID's (in this case FAD_ID, SAD_ID and SUD_ID) in the other column that are all foreign keys corresponding to the primary key from another table (Member_ID from crew_member, which already exists).

所需表

Here's what I'm currently trying to do, just to create multiple foreign keys (I don't even know how to go about creating that second column yet):

mysql> create table Other_Directing (
    -> O_Director_ID int (4) not null auto_increment,
    -> FAD_ID int (5) not null,
    -> SAD_ID int (5) not null,
    -> SUD_ID int <5> not null,
    -> primary key (O_Director_ID),
    -> foreign key (FAD_ID) references crew_member(Member_ID),
    -> foreign key (SAD_ID) references crew_member(Member_ID),
    -> foreign key (SUD_ID) references crew_member(Member_ID)
    -> );

However even this doesn't work, I get this error message:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 
'<5> not null,
primary key (O_Director_ID),
foreign key (FAD_ID) references crew_' 
at line 5

Thank you and I hope this makes sense.

Regards,

Michael

SUD_ID int <5> not null,更改为: SUD_ID int (5) not null,

Does your project really necessitate non-default sizes on your int columns? If so, you have a typo... one of your sizes is in <>'s instead of ()'s: -> SUD_ID int <5> not null should be -> SUD_ID int (5) not null

If you're sure you do want the sizes due to memory constraints:

create table Other_Directing (
    O_Director_ID int not null auto_increment,
    FAD_ID int not null,
    SAD_ID int not null,
    SUD_ID int not null,
    primary key (O_Director_ID),
    foreign key (FAD_ID) references crew_member(Member_ID),
    foreign key (SAD_ID) references crew_member(Member_ID),
    foreign key (SUD_ID) references crew_member(Member_ID)
);

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