What is a statutory constraint?

How can I solve the "1701, 'Cannot truncate a table referenced in a foreign key constraint" issue in MySQL when trying to truncate a table?

  • I have two tables: CREATE TABLE IF NOT EXISTS `submissions` ( `submission_id` INT(11) NOT NULL AUTO_INCREMENT) CREATE TABLE IF NOT EXISTS `assessments` ( `assessment_id` INT NOT NULL AUTO_INCREMENT , `submission_id` INT NOT NULL , CONSTRAINT `submission` FOREIGN KEY (`submission_id` ) REFERENCES `submissions` (`submission_id` ) ON DELETE NO ACTION ON UPDATE NO ACTION) When I try to execute TRUNCATE submissions;, I get the error: "_mysql_exceptions.OperationalError: (1701, 'Cannot truncate a table referenced in a foreign key constraint (`assessments`, CONSTRAINT `submission` FOREIGN KEY (`submission_id`) REFERENCES `submissions` (`submission_id`))')" How can I circumvent it?

  • Answer:

    You can disable the foreign key check before truncating the table: SET FOREIGN_KEY_CHECKS=0; TRUNCATE submissions; SET FOREIGN_KEY_CHECKS=1; Beware that this might break the integrity of the database.

Franck Dernoncourt at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.