DELETE Table in SQL Server is a command which is used to remove one or more rows from a table completely, you use the DELETE statement which can delete one or more then one rows from table and also all data from the table, But it will not delete the structure of table.
Can we used Delete command to delete all records?
There are many people asked to can DELETE command to delete all records from huge table, then we should say it is better use TRUNCATE command then delete command, Truncate command is more faster then Delete command in SQL, But yes you can also use delete command if you want to delete few rows from tables.
Below are syntax for DELETE command
Syntax:
DELETE [ TOP (expression) ]
FROM table_name
WHERE condition;
Example #1: Delete rows based on condition
In Delete command, you need to use WHERE condition to delete particular rows from table, if you will not put any condition then it will delete all rows in that table
Lets take below example where we have applied the condition on Customer_name = ‘Jack Lebron’. So it will delete ‘Jack Lebron’ records only.
Important: Content value should be in one single quotes like in below example we have give on ‘Jack Lebron’.
DELETE FROM Customers_Tbl
WHERE Customer_Name = 'Jack Lebron'
Result:
Example #2: Delete all records from table
Lets take example how you can delete all records from table without WHERE clause in SQl query
DELETE * FROM Customers_Tbl;
Example #2: Delete Top 10 records from table
DELETE TOP (10) FROM Customers_Tbl;:
Result:
DELETE vs TRUNCATE
Truncate | Delete |
It locks the entire table | It locks the table row. |
Its DDL (Data Definition Language) Command | Its DML (Data Manipulation Language) command |
Can’t use WHERE clause with it. | Can use WHERE to filter data to delete |
Trigger is not fired while truncate | Trigger is fired |
Truncate reset identity of table | Delete does not reset identity of table |