3

I am having trouble adding Foreign Key constraints to my database.

I am running the InnoDB engine on all tables, and have insured the data type and size are the same in both tables.

Also, I have tried using the MySQL command line, phpMyAdmin and MySQL workbench, but no luck.

Since in MySQL workbench does not allow me to check the Index Columns, nor select the Referenced Column, there is another issue I am overlooking.

Is there something else I should be looking for?

I do have test data in almost all of the tables, would that matter?

EDIT: When I do see an error it is usually "#1005 - Can't create table 'db_name.#sql-43d_8e' (errno: 150)" , that talks about data mismatching, according to the documentation...Otherwise MySQL Workbench won't let me do it, so I do no know the problem.

8
  • 1
    If the data doesnt match up it might matter... Commented Jan 16, 2012 at 18:12
  • All the referencing data is of INT(10) type... Commented Jan 16, 2012 at 18:13
  • No i mean if the values dont match to the foreign tables. Commented Jan 16, 2012 at 18:13
  • What exactly do you mean with "having trouble"? An error message? If yes, which? Are FKs not enforced? Do you have the error when adding the FK or when insert/updating rows? Commented Jan 16, 2012 at 18:16
  • Yea, I think you were saying what boobiq posted below...I wasn't thinking about missing data. I'm going to try to remove all the data from the tables and try again. Thanks! Commented Jan 16, 2012 at 18:16

3 Answers 3

4

You should also check to make sure your columns match. The issue I was having is that I did not notice one was a signed int and the other unsigned.

Sign up to request clarification or add additional context in comments.

Comments

1

That would matter, if you have data, that can't be linked correctly with foreign keys, for example you have entry in "comments" with id_article = 10, but you don't have entry in "articles" with id = 10 .. in that case, you can't add foreign key

1 Comment

hmm let me clear the tables and try again... Since I added data with out the constraints to keep the data integrity, this might very well be the problem.
1

I had the same problem, datatype of the primary key in the referenced table and the foreign key were the same yet i couldn't create the second table because mysql couldn't add the foreign key constraint. for some reason it was the sequence in which i created the tables. the error occurred if i created the first table, added the data then created the second table, if i created the first table and second tables before i added the data to the first table there was no error message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.