Itay Moav -Malimovka Itay Moav -Malimovka - 1 year ago 70
MySQL Question

MySql foreign key which is a composite key - what is the syntax?

Is it possible to have a composite key in mysql(5.1) and if so, what is the syntax?

table a:

column aa,bb

references table b

columns b_aa,b_bb

Answer Source

With InnoDB tables, you can, like:

create table YourTable (
     col1 int, 
     col2 int, 
     constraint foreign key (col1, col2) 
                references OtherTable (col1, col2) 
                on delete cascade
) type=InnoDB;

For MyISAM tables, foreign key constraint are silently ignored.

for complete tutorial:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download