[WHERE dieu_kien];
The full syntax of the DELETE command is as follows.
DELETE [TOP (giatri_dau) [PERCENT] ]
FROM bang
[WHERE dieu_kien
];
state
Table needs to delete the record.
WHERE dieu_kien
Option. Conditions that the record must meet to be deleted.
TOP (giatri_dau)
Option. If specifically, it will insert the first value of the row based on giatri_dau. For example, TOP (10) will insert the first 10 rows from the result set.
PERCENT
Option. If specified, the first rows are based on the percentage of giatri_dau of the result set. For example, TOP (10) PERCENT will insert 10% of the first value in the result set.
Note
There is no need to list fields in the DELETE statement because you will delete all rows in the table.
For example - use 1 condition
DELETE FROM nhanvien
WHERE ten = 'Sarah';
This command will delete all records in the table with the employee name Sarah.
If you want to check the number of deleted items, run the SELECT command before executing the delete command.
SELECT hasun (*)
FROM nhanvien
WHERE ten = 'Sar
ah';
For example - use 2 conditions
DELETE FROMnhanvien
WHERE ho = 'Johnson'
AND nhanvien_id >
= 80;
This command will delete all records in the table if the staff name is Johnson and nhanvien_id is greater than or equal to 80.
To know the number of rows deleted, run the SELECT statement below before running the DELETE command.
SELECT count (*)
FROM nhanvien
WHERE ho = 'Johnson'
AND nhanvien_id
> = 80;
For example - use the keyword TOP
DELETETOP(3)
FROM nhanvien
WHERE ho =
'Johnson';
This will delete the first 3 records in the table when the employee's family name is Johnson. If there are other records in this table that have a family name of Johnson, then they are not affected by this DELETE.
For example - use EXISTS clause
You can perform a more complex delete command, such as deleting records in a table based on values in another table, for example. Because it is impossible to render more than one table in the FROM clause in the DELETE statement, the EXISTS clause can be used as follows.
DELETE FROMnhanvien
WHERE EXISTS
(SELECT *
FROM danhba
WHERE danhba.danhba_id = nhanvien.nhanvien_id
AND danhba.danhba_id
<100);
This DELETE statement will delete all records in the table when there is a record in the list of names that listener_id is less than 100 and list_id matches nhanvien_id.
If you want to determine the number of deleted lines, run the SELECT command before deleting.
SELECTcount (*)
FROM nhanvien
WHERE EXITS
(SELECT *
FROM danhba
WHERE danhba.danhba_id = nhanvien.nhanvien_id
AND danhba.da
nhba_id <100);
Previous article: UPDATE command in SQL Server
Lesson: TRUNCATE TABLE command in SQL Server