MariaDB删除数据

MariaDB DELETE语句用于从MariaDB数据库的表中删除一个或多个记录。

语法:

DELETE FROM table  
[WHERE conditions]  
[ORDER BY expression [ ASC | DESC ]]  
[LIMIT number_rows];

1. 使用条件删除数据

下面演示如何使用指定的一个条件来删除数据。

示例:

DELETE FROM Students  
WHERE student_name = 'Blaba';

在执行上面删除语句后,现在查询表中的数据 -

2. 使用多个条件删除数据

假设数据库表students中的student_name有两个Maxsu值,现在想删除student_id不是1的所有Maxsu值,参考以下语句 -

首先插入两条件记录 -

INSERT INTO Students  
(student_name, student_address, admission_date) 
VALUES('Maxsu','Sanya','2016-03-09 00:00:00');
-- 再插入另一条记录
INSERT INTO Students  
(student_name, student_address, admission_date) 
VALUES('Ajeet','Sanya','2015-12-07 00:00:00');

查询当数据库中的记录 -

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          3 | JMaster      | Beijing         | 2016-05-07     |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |
|          5 | 科比         | Haikou          | 2016-02-07     |
|          7 | Maxsu        | Sanya           | 2016-03-09     |
|          8 | Ajeet        | Sanya           | 2015-12-07     |
+------------+--------------+-----------------+----------------+
rows in set (0.00 sec)

删除那些student_id大于1,并且student_nameMaxsu的记录 -

DELETE FROM Students
WHERE student_name = 'Maxsu'
AND student_id > 1;

执行上面删除语句以后,现在查询表中的记录 -

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          3 | JMaster      | Beijing         | 2016-05-07     |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |
|          5 | 科比         | Haikou          | 2016-02-07     |
|          8 | Ajeet        | Sanya           | 2015-12-07     |
+------------+--------------+-----------------+----------------+
rows in set (0.00 sec)

可以看到,student_id7,并且student_name值为Maxsu的记录被删除了。


上一篇:MariaDB更新数据

下一篇:MariaDB截断表

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程