SQLite DELETE 语句用于从表中删除现有记录。 WHERE 子句可以与 DELETE 语句一起使用来删除选定的行,否则所有记录将被删除。
语法
在 SQLite 中使用 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 |
同理,要删除 city 以"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 |