views:

22

answers:

1

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

+1  A: 

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: http://dev.mysql.com/doc/refman/4.1/en/innodb-foreign-key-constraints.html

Andomar
As a side note, InnoDB doesn't understand things like `col1 int references OtherTable (col1)`... it has to be a separate constraint like done above even for single column foreign keys.
R. Bemrose
@R. Bemrose: Even worse, MySQL will silently ignore incorrect ways to write a foreign key, making you think you actually created one...
Andomar