Open In App

Truncate Table in MariaDB

MariaDB is an open-source relational database management system. It offers faster query execution and data manipulation as compared to other RDBMS. Also, it handles large datasets very efficiently. MariaDB Provides plugins for various functionalities like backupperformance monitoringsecurity, and data integration. MariaDB can easily integrate with other open-source software and cloud platforms.

In this article, We will learn about the Truncate Table in MariaDB along with its syntax, examples, and so on.



TRUNCATE TABLE in MariaDB

A TRUNCATE TABLE allows all records from a table to be deleted, resetting the table to an empty state. Compared with the DELETE statement, which deletes rows one by one and causes increased transaction logs, TRUNCATE TABLE is a more efficient method of clearing a table.

Syntax:



TRUNCATE TABLE [database_name.] table_name;

Explanation: In this syntax, database_name denotes the name of the database in which your table is present and table_name is the name of the table.

Examples of TRUNCATE TABLE in MariaDB

To understand the TRUNCATE TABLE in MariaDB, we need a table on which we will perform the TRUNCATE TABLE command. So here we will create a table and insert some data into it.

Query for Create Table

CREATE TABLE Students 
(
student_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
course VARCHAR(50),
);

Query for Insert Data:

INSERT INTO Students VALUES
(1, 'Ratnala', 'Harshavardha', 'CSE'),
(2, 'Maram', 'Harsha', 'Electrical'),
(3, 'Medishetty', 'Yashwanth', 'Chemical'),
(4, 'Sukumar', 'Reddy', 'Mechanical' ),
(5, 'Engula', 'Vamshi', 'Electronics');

We can check whether our data is inserted successfully or not using below command.

SELECT * FROM Students;

Output:

Records Inserted

Explanation: Suppose we have a table named Students, and we want to remove all students records from the table while keeping its structure. We can done this using the TRUNCATE TABLE command.

Query:

TRUNCATE TABLE Students;
SELECT * FROM STUDENTS;

Output:

Delete Table

Explanation: The TRUNCATE query is executed successfully. As we can see that the records of “Students” table have been deleted permanently.

Advantages of the TRUNCATE TABLE

Disadvantages of the TRUNCATE TABLE

While TRUNCATE TABLE is a powerful and efficient tool, there are certain considerations and limitations of TRUNCATE TABLE.

Conclusion

In MariaDB, TRUNCATE TABLE command is significantly important and remove all records in the table. Its good performance feature, low use of resources, and auto-increment reset capabilities make it an best option for some cases. We have saw in the advantage that TRUNCATE TABLE is faster than DELETE. But there is a limitations too which is it removes all records but DELETE can’t. While truncating the table it preserve the structure of Table.

Article Tags :