0

I am using mysql.

When I add a foreign key to a table it is done without any errors.

When the table structure is described i see MUL under the key field like I see the the PRI for the columns set as the primary key.

But when I try to violate the foreign key constraint by adding a value absent in the parent table in the child table it is correctly inserted without any errors.With my little knowledge in SQL this is against the rules of the foreign key.

Any Ideas why this is happening or how I can fix it?

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56

1 Answers1

2

Probably you use the default myisam engine. It doesnt support foreign keys...

In this case put "engine=innodb" at the end of he create tables.

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • 1
    lighten up - The question is "Any Ideas why this is happening or how I can fix it?" Lajos answered it. In fact he answered both of them. – AgRizzo Sep 28 '13 at 23:40
  • 1
    @MarcelGwerder, this is the correct answer. Mysql surprises many people when it doesn't enforce FKs, and the fix is to use innodb instead of the default engine. – Mark Harrison Sep 28 '13 at 23:58
  • +1 and for what it's worth, InnoDB *is* the default engine since MySQL 5.5, which was released in 2010. – Bill Karwin Sep 29 '13 at 01:48
  • @BillKarwin: even with InnoDB you can define foreign keys that are silently ignored –  Dec 01 '13 at 18:43