MySQL Forums
Forum List  »  Newbie

Re: Creating foreign keys
Posted by: Fred Riley
Date: September 10, 2004 11:37AM

Ok, looks like I'm answering my own question. After much hunting around, I came across an article called "Referential integrity in MySQL" which outlines in simple terms how to create foreign keys and maintain referential integrity - see http://www.databasejournal.com/features/mysql/article.php/2248101

Funny thing is, if I created identical fields in the referencing and referenced tables and ran something like:

ALTER TABLE RLO
ADD FOREIGN KEY(devt_stage_id) REFERENCES DEVT_STAGE(devt_stage_id);

the error:

"ERROR 1216: Cannot add or update a child row: a foreign key constraint fails"

would occur. This foxed me for a bit, until I realised that the foreign key had been created because in MySQL CC under the "KEY" column it has "MUL", then looking at MySQLFront I could see it was explicitly defined as a foreign key.

So here's the thing. If you don't create and index a foreign key field in your referencing table you'll get an error like:

"ERROR 1005: Can't create table './test/customer_sales.frm' (errno: 150)"

If you do create the field and add the foreign key, you'll get error 1216 but the foreign key will be created anyway. Hmmm ;-(

Ah, well, I'll get used to it in time, I'm sure.

Cheers

Fred

Options: ReplyQuote


Subject
Written By
Posted
September 10, 2004 08:15AM
Re: Creating foreign keys
September 10, 2004 11:37AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.