简体   繁体   中英

mysql foreign key i cant truncate my table

#1701 - Cannot truncate a table referenced in a foreign key constraint (`away_order_detail`, CONSTRAINT `away_order_detail_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `away_order_master` (`id`))

I am facing this problem can you give me any solution?

You cannot TRUNCATE a table that has FK constraints applied on it (truncate is not the same as delete).

To work around:

Option 1 which does not risk damage to data integrity:

Remove constraints Perform TRUNCATE Delete manually the rows that now have references to "nowhere" Create constraints

Option 2, which is bad practice, if you are OK risking damage to data integrity

SET FOREIGN_KEY_CHECKS=0; TRUNCATE table1; SET FOREIGN_KEY_CHECKS=1;

From truncate foreign key constrained table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM