How To Clear A Table In Sql

Table of contents:

How To Clear A Table In Sql
How To Clear A Table In Sql

Video: How To Clear A Table In Sql

Video: How To Clear A Table In Sql
Video: The SQL DELETE Statement 2024, November
Anonim

Structured Query Language (SQL) was developed in the 1970s by two Americans (Raymond Boyce and Donald Chamberlin) from IBM. Its first version was officially adopted in 1986 and today it is the most common database management language. Of course, the operation of clearing tables from records is one of the basic ones in this language and can be done in several ways.

How to clear a table in sql
How to clear a table in sql

Necessary

Basic knowledge of the SQL language

Instructions

Step 1

Use the SQL truncate statement to flush tables, specifying the name of the table you are interested in in your query. For example, if you want to clear a table named TableToClear, then the entire query should look like this:

truncate table `TableToClear`

Step 2

Use the delete operator as an alternative to the truncate operator to delete data from a table row by row. The syntax for this command requires you to specify the name of the table and the condition under which a row should be removed from it. If you enter a condition that is known to be true, regardless of the content of the row, then all table records will be deleted. For example, for the TableToClear table, a query with this operator can be composed like this:

delete from `TableToClear` where 1

Unlike the truncate operator, this query will return the number of rows deleted. Another difference in the execution of this command is not locking the entire table, but only the record being processed at the moment. This option will take longer to execute, which will become noticeable when there are a large number of rows in the table being flushed.

Step 3

There are also more exotic options - for example, delete the table completely and recreate it in one Sql query. Use drop to delete and create to create. For example, if the TableToClear table consists of a 50-character Name text field and an integer Code field with non-zero values, then you can write the operations for deleting and recreating it as follows:

drop table `TableToClear`;

create table `TableToClear` (Code integer not null, Name char (50) not null);

Recommended: