Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Foreign Key (MySQL)

I'm extremely new to SQL and I'm trying to perform a simple ALTER TABLE to create a new column and use it as a foreign key to reference another simple table in my database. I've altered both tables to be InnoDB

However, when executing the ALTER TABLE code, I get the following error:

Error   1452    Cannot add or update a child row:  a foreign key constraint fails (`toys`.<result 2 when  explaining filename '#sql-6d4_6'>, CONSTRAINT  `#sql-6d4_6_ibfk_1` FOREIGN KEY (`toy_id`) REFERENCES `toys` (`toy_id`))     

Below are the DESC of both tables:

Table 1:

FIELD       TYPE     NULL   KEY     EXTRA toy_id      int(11)  NO     PRI     auto_increment toy varchar(50) YES          

Table 2:

FIELD       TYPE        NULL   KEY     EXTRA boy_id      int(11)     NO     PRI      auto_increment boy         varchar(50) YES  

And this is the ALTER query I was trying to perform:

    ALTER TABLE boys     ADD COLUMN toy_id INT NOT NULL,     ADD CONSTRAINT toys_toy_id_fk     FOREIGN KEY(toy_id)     REFERENCES toys(toy_id); 

I've looked all over trying to figure it out, but with no luck. Thanks in advance, and please be kind to this newbie :)

EDIT:

Here are the SHOW CREATE TABLE for both tables:

TABLE 1:

    CREATE TABLE `toys` (       `toy_id` int(11) NOT NULL AUTO_INCREMENT,       `toy` varchar(50) DEFAULT NULL,       PRIMARY KEY (`toy_id`)     ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 

TABLE 2:

    CREATE TABLE `boys` (       `boy_id` int(11) NOT NULL AUTO_INCREMENT,       `boy` varchar(50) DEFAULT NULL,       PRIMARY KEY (`boy_id`)     ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 
like image 207
KPM Avatar asked Mar 11 '13 21:03

KPM


People also ask

How do I manually add a foreign key in MySQL?

To add a foreign key, click the last row in the Foreign Key Name list. Enter a name for the foreign key and select the column or columns that you wish to index by checking the column name in the Column list. You can remove a column from the index by removing the check mark from the appropriate column.

How do I add a foreign key to an existing table?

ALTER TABLE students ADD FOREIGN KEY (student_id) REFERENCES points(id); To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax: ALTER TABLE students ADD CONSTRAINT fk_student_id FOREIGN KEY (student_id) REFERENCES points(id);

Can we add foreign key after creating table?

We can add a FOREIGN KEY constraint to a column of an existing MySQL table with the help of ALTER TABLE statement.

What is foreign key in MySQL?

A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.


1 Answers

You can't add a NOT NULL column to a table that has more than zero rows, when the column is constrained to values that match those in the parent table, and yet has only NULL values because it's a new, unpopulated column with no DEFAULT.

The workaround is to do it in stages: add the column, but don't declare it NOT NULL, and don't declare the foreign key yet.

ALTER TABLE boys  ADD COLUMN toy_id INT; 

Then populate it with valid data that matches some value(s) in your toys table.

UPDATE boys SET toy_id = ...; 

Then alter the column to be NOT NULL, and create the constraint:

ALTER TABLE boys MODIFY COLUMN toy_id INT NOT NULL,  ADD CONSTRAINT toys_toy_id_fk  FOREIGN KEY(toy_id)  REFERENCES toys(toy_id); 
like image 62
Bill Karwin Avatar answered Oct 07 '22 01:10

Bill Karwin