MYSQL How to delete a row from a table with dependencies?

Asked

Viewed 1,452 times

0

Is it possible to delete a row from a table with dependencies? I tried the command

ALTER TABLE MinhaTabela NOCHECK CONSTRAINT ALL

But the error appears

Error Code: 1064. You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'NOCHECK CONSTRAINT ALL

What’s wrong?

  • This syntax is SQL Server and not Mysql, ideally you identify the records that reference it and take this reference before deleting, otherwise your database will be full of inconsistencies

  • Got it, thanks, but it’s possible to delete it anyway?

  • Yes, but if you delete only by disabling Fk validation, your database will be inconsistent forever, until you fix this data and it will only generate problems in the future

  • Okay, thanks, it’s more for learning effect

1 answer

3


The correct syntax to disable and enable Fks in Mysql is

To deactivate:

SET FOREIGN_KEY_CHECKS=0;

To activate:

SET FOREIGN_KEY_CHECKS=1;

The ideal is not to use this to delete the records, because your database will become inconsistent, but to search for the records that are related to the record you want to delete and remove this link, after that you can delete your record without problems.

  • Thank you for the reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.