Sometimes we wish to get rid of all
the data in a table. Now SQL Tutorial will explain that one way of doing this
is with SQL DROP TABLE
command, which we saw in the
last lesson of out SQL Tutorial. But what if we wish to simply
get rid of the data only and not the entire table itself?
For this, we can use the SQL TRUNCATE TABLE command. With the help of this
command we can make the table empty from its data only.
The syntax for SQL TRUNCATE TABLE is as under
So, if we want to truncate the table of Employees that we created in
SQL CREATE TABLE
section of our
SQL Tutorail, we simply type this below query,
If we try to TRUNCATE a table, and the table is have a relationship with other table like Employees_Salary with
the constraint of SQL Foreign Key
then we can't TRUNCATE the table with this
SQL Truncate Table command, here we need to TRUNCATE the
Employees_Salary table first because Employees_Salary is depending on it and that is having some records. That's
why we need to TRUNCATE both tables and the sequence is to empty that table which is based on
SQL Foreign Key
or remove this
constraint first. Then we can TRUNCATE any table with SQL Truncate statement.