SQL Server (Transact-SQL) DELETE 语句用于从表中删除现有记录。 WHERE 子句可以与 DELETE 语句一起使用来删除所选行,否则所有记录将被删除。
语法
在 SQL Server (Transact-SQL) 中使用 DELETE 语句的语法如下:
DELETE FROM table_name
WHERE condition(s);
示例:
考虑一个数据库,其中包含一个名为 Employee 的表,其中包含以下记录:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
删除员工的记录EmpID为5,语句如下:
DELETE FROM Employee
WHERE EmpID = 5;
-- 查看结果
SELECT * from Employee;
现在Employee表将包含以下记录:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
6 | Huang | Beijing | 28 | 2800 |
同样,要删除城市以"New"开头的员工记录,语句为:
DELETE FROM Employee
WHERE City LIKE 'New%';
-- 查看结果
SELECT * from Employee;
现在是 Employee表将包含以下记录:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
6 | Huang | Beijing | 28 | 2800 |