Skip to main content

Posts

Showing posts with the label sql

How to truncate a foreign key constrained table

The truncate function in SQL deletes all table data. However when your database has foreign key constraints this will give an error. In this tutorial, we'll cover how to TRUNCATE a table with foreign keys.  Please note that this is not advisable if your website is already in production, so ONLY do it if you're sure it will not compromise the integrity of your data. If you're using phpMyAdmin simply uncheck/disable the ' Enable foreign key checks ' checkbox as shown below: Alternatively, you could use the following SQL command. SET FOREIGN_KEY_CHECKS = 0; TRUNCATE my_table_name; SET FOREIGN_KEY_CHECKS = 1; I hope you found this helpful