Solved

MySQL Delete

Posted on 2010-09-21
5
390 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 500 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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 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