?
Solved

MySQL Delete

Posted on 2010-09-21
5
Medium Priority
?
391 Views
Last Modified: 2012-05-10
Hi EE,

I am trying to delete a record from a MySQL parent table that has five foreign keys using workbench.  When I use the statement DELETE FROM table WHERE id = 24 I get this error - Error Code: 1451
Cannot delete or update a parent row: a foreign key constraint fails.  I am hoping that the delete from the parent table will remove the child table records as well. Any insights?

Thanks Runnuger
0
Comment
Question by:runnuger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Accepted Solution

by:
mmr159 earned 2000 total points
ID: 33723915
Foreign keys enforce relational integrity.  You must either remove the foreign keys (probably not a good idea), or first delete the records referenced by the foreign keys.
0
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 33723935
Hello,
Delete on child rows will be happened if you define cascade reference option in your FOREIGN KEY Constraints. This URL may be helped you.

http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

-FA
0
 
LVL 14

Expert Comment

by:john-formby
ID: 33723937
Hi,

You can use cascade delete for this.  Check out: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

Hope this helps,

John
0
 
LVL 2

Expert Comment

by:supersepp
ID: 33723953
you probably forgot to set your foreign key to ON DELETE CASCADE

you can fix that by using: ALTER TABLE referencingtable DROP FOREIGN KEY fk_symbol;

and then ALTER TABLE referencingtable
    ADD FOREIGN KEY idx_fk (idxcolname)
    REFERENCES table (idxcolname)
    ON DELETE CASCADE;

or you simply delete all rows from the other tables before deleting from the parent.
0
 
LVL 2

Expert Comment

by:Sandeepratan
ID: 33723996
to delete the records from the child table eaither you have to delete the same from parent table or desable the foreignkey constraint.  the automatic delete is depend on the enforced refrential integrity
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question