Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I truncate tables properly?

I'm using datamapper with ruby to store data to certain tables.

Several of the tables have very large amounts of information and I want to clear them out when the user 'rebuilds database' (it basically deletes everything and re-calculates data).

I originally tried Forum.all.destroy and did it for all the different tables, but I noticed some of them just weren't deleted from within phpmyadmin. I can only imagine it's because of foreign keys. Although I really don't know because my other table which foreing keys was successfully deleted. Not to mention, I'd rather just 'zero' it out anyway so the keys don't get to extraordinarily large numbers (like key #500,000).

I then tried running it with the code below, but it doesn't clear the tables out because of 'foreign key constraints'. I want to force it to work because I know for a fact I'm clearing out all the tables that rely on each other (I'm only not clearing out 2 tables, a settings table and a random storage table, neither of which use foreign keys).

So far I have...

adapter = DataMapper.repository(:default).adapter adapter.execute('TRUNCATE TABLE `forums`, `dates`, `remarks`'); 

That would be fine except the MySQL syntax is wrong apparently. So that's the first thing.

I did it 1 by 1 in phpmyadmin and when I did it that way it says

Cannot truncate a table referenced in a foreign key constraint 
like image 638
Tallboy Avatar asked Dec 27 '11 06:12

Tallboy


People also ask

How do I truncate data in a table?

The SQL TRUNCATE TABLE command is used to delete complete data from an existing table. You can also use DROP TABLE command to delete complete table but it would remove complete table structure form the database and you would need to re-create this table once again if you wish you store some data.

What does it mean to truncate a table?

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition to its data, use the DROP TABLE statement.

How do I truncate multiple tables at once?

To truncate multiple tables you can use T-SQL and iterate through table names to truncate each at a time. You can have all your table names comma separated in @tableList variable and yes you can truncate multiple tables from different schemas if they are prefixed.


2 Answers

Plan A:

SET FOREIGN_KEY_CHECKS = 0; -- Disable foreign key checking. TRUNCATE TABLE forums; TRUNCATE TABLE dates; TRUNCATE TABLE remarks; SET FOREIGN_KEY_CHECKS = 1; -- Enable foreign key checking. 

Plan B:

You should truncate child tables firstly, then parent tables.

Disabling foreign key checks risks entering rows into your tables that don't adhere to the constraints which can cause undefined behavior.

like image 52
Devart Avatar answered Oct 06 '22 01:10

Devart


Instead of using Disable foreign key checking.

You can use the below code.

DELETE FROM forums; ALTER TABLE forums AUTO_INCREMENT = 1;  DELETE FROM dates; ALTER TABLE dates AUTO_INCREMENT = 1;  DELETE FROM remarks; ALTER TABLE remarks AUTO_INCREMENT = 1; 

It will just delete all the rows and make id increment from 1 onwards.

like image 44
Phoenix Avatar answered Oct 06 '22 01:10

Phoenix