Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL Delete

Posted on 2010-09-21
5
Medium Priority
?
394 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 Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…

610 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