SQL TRUNCATE

Write SQL TRUNCATE Query.

In SQL, the TRUNCATE TABLE statement is a Data Definition Language operation that marks the extent of a table for deallocation. The result of this operation quickly removes all data from a table,

Example


Table: Employee                             
+----+-------+--------+--------------+      
| Id | Name  | Salary | DepartmentId |      
+----+-------+--------+--------------+      
1  | Ram   | 85000  | 1            |      
2  | Henry | 80000  | 2            |      
3  | Sam   | 60000  | 2            |      
4  | Shyam | 60000  | 1            |
5  | Geeta | 90000  | 1            |
6  | Sheet | 90000  | 1            |
7  | Leela | 80000  | 1            |
8  | Geeta | 70000  | 1            |
+----+-------+--------+--------------+

Approach : 

Mysql: Truncate table without Foreign Key Constraints


TRUNCATE TABLE EMPLOYEE


Mysql: Truncate table with Foreign Key Constraints: You cannot TRUNCATE a table that has FK constraints applied oN it 

Approach 1:

  1. Remove constraints
  2. Perform TRUNCATE

Approach 2: Disabled the FOREIGN_KEY_CHECKS

SET FOREIGN_KEY_CHECKS = 0
TRUNCATE TABLE EMPLOYEE; 
SET FOREIGN_KEY_CHECKS = 1;


No comments:

Post a Comment