Home Database Mysql Tutorial What is the difference between SQL commands delete and truncate?

What is the difference between SQL commands delete and truncate?

Feb 20, 2019 am 10:47 AM
delete sql command truncate

Both delete and truncate commands can be used to delete data (records) in SQL, so what is the difference between them? The following article will introduce to you the difference between delete and truncate commands. I hope it will be helpful to you.

What is the difference between SQL commands delete and truncate?

What is the delete command?

delete command helps in deleting records from the tables in the database. [Video tutorial recommendation: MySQL tutorial]

Example: The following is a student table, containing 3 fields: student_id, name, marks

What is the difference between SQL commands delete and truncate?

You can use the SQL delete command to delete all records in the student table:

delete from student;
Copy after login

You can also delete specific records in the student table:

delete from student where student_id =2;
Copy after login

The above SQL The command will delete the record with student_id 2. After executing this statement, the table will contain only two records: records with student_id 1 and 3.

What is the truncate command?

The truncate command helps in deleting all records from the table. It also removes the table structure from the database. So when the truncate command is executed on a specific table, it also initializes the primary key.

The following SQL command will delete all records in the student table. Additionally, it removes the table structure from the database.

truncate table student;
Copy after login

What is the difference between delete and truncate commands?

1. Command type

delete is a data manipulation language (DML) command; and truncate is a data definition language (DDL) command.

2. Function

The delete command deletes single, multiple or all records from the table according to the specified SQL statement; while the truncate command deletes all records from the database and Table Structure.

3. Where clause

delete command supports WHERE clause, you can use the where clause with DELETE to filter and delete specific records; and, truncate command WHERE clause is not supported.

4. Locking

The delete command uses row-level locking, and each row in the table is locked for deletion; the truncate command uses table-level locking, locking the entire table to delete all records.

5. Indexed view

The delete command can be used with indexed views; however, the truncate command cannot be used with indexed views.

6. Execution speed

Because the delete command maintains the log, the speed is very slow. However, since the truncate command maintains minimal logging records in the transaction log, it executes faster.

7. Table structure

The delete command will not affect the table structure, while the truncate command will delete the table structure from the database.

8. Transaction space

The delete command uses more transaction space than the truncate command.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is the difference between SQL commands delete and truncate?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the delete key on the keyboard? What is the delete key on the keyboard? Mar 16, 2023 pm 04:48 PM

There are two keyboard delete keys: del (delete) key and backspace key. Backspace is also called the backspace key. This key can delete the text content in front of the cursor; and the delete key can delete characters, files and selected objects. Each time you press the del key, a character to the right of the cursor will be deleted, and the character to the right of the cursor will move one frame to the left; when one or more files/folders are selected, press the Del key to quickly delete; in some applications Select an object in the program and press the Del key to quickly delete the selected object.

What is the function of delete key What is the function of delete key Mar 10, 2023 pm 06:07 PM

The functions of the delete key are: 1. Delete characters; each time the delete key is pressed, a character to the right of the cursor will be deleted, and the character to the right of the cursor will move one frame to the left. 2. Delete files; when one or more files/folders are selected, press the Delete key to quickly delete them (move to the Recycle Bin for recovery). 3. Delete the selected object; select an object in some applications and press the Delete key to quickly delete the selected object.

How to Control + Alt + Delete: Mac Tutorial How to Control + Alt + Delete: Mac Tutorial Apr 16, 2023 pm 12:37 PM

Control+Alt+Delete: "Mac" mode Ctrlaltdel is a common key combination used by Windows users to open Task Manager. They usually exit unwanted applications from the manager menu to free up some space on their computer. The Control+Alt+Delete Mac variant lets you open the Force Quit menu. If Mac users want to quit the program causing the problem or view open programs, they can interact with the menu to investigate further. How to perform ControlAltDelete on Mac? If you have any malfunctioning applications, you must use this key combination to

Can deleted files be recovered? Can deleted files be recovered? Feb 24, 2023 pm 03:49 PM

Files deleted by delete can be recovered; because when users use delete to delete files, these files will be moved to the recycle bin and are not completely deleted. Recovery method: 1. Open the "Recycle Bin", select the file you want to restore, and click "Restore this item"; 2. Open the "Recycle Bin", select the file you want to restore, and use the undo shortcut "ctrl+z". Can.

How to add, edit and delete table rows in jQuery? How to add, edit and delete table rows in jQuery? Sep 05, 2023 pm 09:49 PM

In today's era of web development, effective and efficient table management has become very important, especially when dealing with data-heavy web applications. The ability to dynamically add, edit, and delete rows from a table can significantly enhance the user experience and make applications more interactive. An effective way to achieve this is to leverage the power of jQuery. jQuery provides many features to help developers perform operations. Table rows A table row is a collection of interrelated data, represented by elements in HTML. It is used to group together cells (represented by elements) in a table. Each element is used to define a row in the table, and for multi-attribute tables, it usually contains one or more elements. Syntax$(selector).append(co

How to use PUT and Delete requests in SpringBoot How to use PUT and Delete requests in SpringBoot May 13, 2023 pm 12:16 PM

PUT and Delete requests are used in the Form form and only support get and post methods. In order to implement the put method, we can implement it through the following three steps: 1) Configure HiddenHttpMethodFilter in SpringMVC 2) Create a post form on the page 3) Create an input item, name ="_method", the value is the specified request method. Get the value of "_method" in the HiddenHttpMethodFilter class to get the new request method. The th tag is the thymeleaf template, which means that only when employee

How to use database Delete How to use database Delete Nov 13, 2023 am 11:50 AM

Database Delete usage: 1. Delete a single record; 2. Delete multiple records; 3. Delete all records; 4. Delete records with specific conditions.

What is the function of delete key What is the function of delete key Dec 31, 2020 pm 04:17 PM

The functions of the delete key are: 1. Delete text; 2. Delete files; 3. Delete selected objects. The delete key is usually located in the upper right corner of the keyboard. When we press the delete key, we can delete a character to the right of the cursor.

See all articles