Skip navigation links

MySQL Forums :: Database Administration & Monitoring :: #1452 - Cannot add or update a child row: a foreign key constraint fails?


Advanced Search

Re: #1452 - Cannot add or update a child row: a foreign key constraint fails?
Posted by: Saravanan K ()
Date: August 10, 2010 05:48AM

Hi,


you can insert rows in foreign key table(but you can insert only the values that matched with 'ID' in UserGroup)

The values in the foreign key column should be matched with primary key column.(the values can't be differ)

Follow this example you will get it.

mysql> create table pri(no int(4) auto_increment,name varchar(10),primary key(no));
Query OK, 0 rows affected (0.13 sec)


mysql> create table fr(no int(4),city varchar(10),constraint no_fk foreign key(no) referen
ces pri(no) on update cascade);
Query OK, 0 rows affected (0.09 sec)


mysql> select * from pri;
+----+-----------+
| no | name |
+----+-----------+
| 1 | saravanan |
| 2 | guru |
| 4 | rathish |
+----+-----------+
3 rows in set (0.02 sec)

mysql> select * from fr;
+------+---------+
| no | city |
+------+---------+
| 1 | chennai |
| 2 | salem |
| 4 | trichy |
+------+---------+
3 rows in set (0.02 sec)

mysql> insert into fr values(5,'chennai');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`sar
`.`fr`, CONSTRAINT `no_fk` FOREIGN KEY (`no`) REFERENCES `pri` (`no`) ON UPDATE CASCADE)

Here '5' is the value that not in the primary key column.


mysql> insert into fr values(4,'chennai');
Query OK, 1 row affected (0.09 sec)

mysql> select * from fr;
+------+---------+
| no | city |
+------+---------+
| 1 | chennai |
| 2 | salem |
| 4 | trichy |
| 4 | chennai |
+------+---------+
4 rows in set (0.00 sec)



you should not pass this query. when the table created with foreign key.

INSERT INTO `robertco_LC131`.`AccessControlAssociation`
SELECT *
FROM `robertco_LC`.`AccessControlAssociatio

Options: ReplyQuote


Subject Written By Posted
#1452 - Cannot add or update a child row: a foreign key constraint fails? Mike C 08/08/2010 01:46PM
Re: #1452 - Cannot add or update a child row: a foreign key constraint fails? Saravanan K 08/10/2010 05:48AM
Re: #1452 - Cannot add or update a child row: a foreign key constraint fails? Walter Stingl 10/25/2010 02:56AM


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.