Locke
Locke

Reputation: 671

Delete all data from table but not the columns

How can I delete all the data in an SQL table, but not delete the columns? I want all the column names and attributes to still be there after the data is deleted.

Upvotes: 1

Views: 3882

Answers (6)

rock321987
rock321987

Reputation: 11032

delete from tablename;

it can also work

truncate table tablename

I didn't read the question

EDIT I checked for

delete from tablename;

it is also working..am i right??downvoters can you please tell the reason

'truncate'..'truncate' is more efficient than 'delete'..

Upvotes: 0

Jojodmo
Jojodmo

Reputation: 23596

You could either use:

TRUNCATE TABLE `tableName`;

From the MySQL docs:

TRUNCATE TABLE empties a table completely. Logically, this is equivalent to a DELETE statement that deletes all rows, but there are practical differences under some circumstances.

You could also use DELETE FROM tableName, but using TRUNCATE TABLE tableName is better because:

Truncate operations drop and re-create the table, which is much faster than deleting rows one by one, particularly for large tables.

Upvotes: 3

VPR
VPR

Reputation: 77

Please use this syntax in your sql editor

Ex: truncate table tablename

table name: demo

truncate table demo

this will delete only data and not the columns

Upvotes: 3

OptimusCrime
OptimusCrime

Reputation: 14863

To delete all content of a table do:

TRUNCATE TABLE `name`

Upvotes: 3

sockmonk
sockmonk

Reputation: 4255

The syntax might vary slightly depending on your database, but you probably want something like:

TRUNCATE TABLE my_table_name;

Also, DELETE FROM my_table_name will also work, but generally be much slower than TRUNCATE.

Upvotes: 6

Allen Chak
Allen Chak

Reputation: 1950

Did you try the using TRUNCATE??

TRUNCATE TABLE YOUR_TABLE_NAME;

Upvotes: 3

Related Questions