The TRUNCATE statement in MySQL removes the complete data without removing its structure. It is a part of DDL or data definition language command. Generally, we use this command when we want to delete an entire data from a table without removing the table structure The TRUNCATE command works the same as a DELETE command without using a WHERE clause that deletes complete rows from a table. However, the TRUNCATE command is more efficient as compared to the DELETE command because it removes and recreates the table instead of deleting single records one at a time. Since this command internally drops the table and recreates it, the number of rows affected by the truncate statement is zero, unlike the delete statement that returns the number of deleted rows This command does not maintain the transaction log during the execution. It deallocates the data pages instead of rows and makes an entry for the deallocating pages instead of rows in transaction logs. This command also locks the pages instead of rows; thus, it requires fewer locks and resources The following points must be considered while using the TRUNCATE command - We cannot use the WHERE clause with this command so that filtering of records is not possible
- We cannot rollback the deleted data after executing this command because the log is not maintained while performing this operation
- We cannot use the truncate statement when a table is referenced by a foreign key or participates in an indexed view
- The TRUNCATE command doesn't fire DELETE triggers associated with the table that is being truncated because it does not operate on individual rows
cú phápThe following syntax explains the TRUNCATE command to remove data from the table In this syntax, first, we will specify the table name which data we are going to remove. The TABLE keyword in the syntax is not mandatory. But it's a good practice to use it to distinguish between the TRUNCATE() function and the TRUNCATE TABLE statement MySQL Truncate Table ExampleLet us demonstrate how we can truncate the table with the help of an example. First, we are going to create a table named "customer" using the below statement Next, we will add values to this table using the below statement Now, verify the table by executing the SELECT statement whether the records inserted or not We will get the output, as shown below Now, execute the following statement that truncates the table customer using the TRUNCATE syntax discussed above After the successful execution, we will get the following output As we can see, this query returns 0 rows are affected even if all the table records are deleted. We can verify the deletion of the data by executing the SELECT statement again. This command gives the following output that shows none of the records present in the table How to Truncate Table with Foreign key?If we perform the TRUNCATE operation for the table that uses a foreign key constraint, we will get the following error In that case, we need to log into the MySQL server and disable foreign key checks before executing the TRUNCATE statement as below Now, we are able to truncate tables. After execution, re-enable foreign key checks as given below How to truncate all tables in MySQL?The TRUNCATE statement in MySQL will delete only one table at a time. If we want to delete more than one table, we need to execute the separate TRUNCATE statement. The below example shows how to truncate multiple tables in MySQL We can also use the below SQL query that generates several TRUNCATE TABLE commands at once using the table names in our database Tóm lược. in this tutorial, you will learn how to use the MySQL CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement to delete all data in a tableIntroduction to the MySQL CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB;Code language: SQL (Structured Query Language) (sql)4 statementThe MySQL CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement allows you to delete all data in a tableLogically, the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement is like a CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 0 statement without a CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 1 clause that deletes all rows from a table, or a sequence of CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 2 and CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 3 statementsHowever, the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement is more efficient than the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 5 statement because it drops and recreates the table instead of deleting rows one by oneHere is the basic syntax of the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statementTRUNCATE [TABLE] table_name; Code language: SQL (Structured Query Language) (sql)
In this syntax, you specify the name of the table which you want to remove all data after the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 keywordsThe CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 8 keyword is optional. However, it is a good practice to use the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 8 keyword to distinguish between the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement and the DELIMITER $$
CREATE PROCEDURE load_book_data(IN num INT(4))
BEGIN
DECLARE counter INT(4) DEFAULT 0;
DECLARE book_title VARCHAR(255) DEFAULT '';
WHILE counter < num DO
SET book_title = CONCAT('Book title #',counter);
SET counter = counter + 1;
INSERT INTO books(title)
VALUES(book_title);
END WHILE;
END$$
DELIMITER ;
Code language: SQL (Structured Query Language) (sql) 1 functionIf there is any DELIMITER $$
CREATE PROCEDURE load_book_data(IN num INT(4))
BEGIN
DECLARE counter INT(4) DEFAULT 0;
DECLARE book_title VARCHAR(255) DEFAULT '';
WHILE counter < num DO
SET book_title = CONCAT('Book title #',counter);
SET counter = counter + 1;
INSERT INTO books(title)
VALUES(book_title);
END WHILE;
END$$
DELIMITER ;
Code language: SQL (Structured Query Language) (sql) 2 constraints from other tables which reference the table that you truncate, the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement will failBecause a truncate operation causes an implicit commit, therefore, it cannot be rolled back The CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement resets value in the AUTO_INCREMENT column to its start value if the table has an DELIMITER $$
CREATE PROCEDURE load_book_data(IN num INT(4))
BEGIN
DECLARE counter INT(4) DEFAULT 0;
DECLARE book_title VARCHAR(255) DEFAULT '';
WHILE counter < num DO
SET book_title = CONCAT('Book title #',counter);
SET counter = counter + 1;
INSERT INTO books(title)
VALUES(book_title);
END WHILE;
END$$
DELIMITER ;
Code language: SQL (Structured Query Language) (sql) 5 columnThe CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement does not fire CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 5 triggers associated with the table that is being truncatedUnlike a CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 5 statement, the number of rows affected by the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement is 0, which should be interpreted as no informationMySQL CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB;Code language: SQL (Structured Query Language) (sql)4 exampleLet’s take an example of using the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statementFirst, create a new table named CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 32 for the demonstrationCREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql)
Next, insert dummy data to the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 32 table by using the following stored procedure________số 8Then, load 10,000 rows into the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 32 table. It will take a whileCREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 3After that, check the data in the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 32 tableCREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 0Finally, use the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement to delete all rows from the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 32 tableCREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 2Note that you can compare the performance between the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 with the CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 5 statementIn this tutorial, you have learned how to use the MySQL CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
) ENGINE=INNODB; Code language: SQL (Structured Query Language) (sql) 4 statement to delete all data from a table efficiently, especially for a large table
What is TRUNC in MySQL?
The TRUNCATE() function truncates a number to the specified number of decimal places . Ghi chú. See also the FLOOR(), CEIL(), CEILING(), and ROUND() functions.
How to use TRUNC in MySQL?
The TRUNCATE statement in MySQL removes the complete data without removing its structure. It is a part of DDL or data definition language command. . TRUNCATE TABLE table_name1; TRUNCATE TABLE table_name2; TRUNCATE TABLE table_name3;
How to TRUNCATE a column in MySQL?
TRUNCATE() Function in MySQL
The TRUNCATE function in MySQL is used to truncate a number to a specified number of decimal places. Thông số. TRUNCATE() function accepts two parameters as mentioned above and described below. X –The number which to be truncated.
What is the use of TRUNCATE ()?
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. |