MariaDB DELETE
语句用于从MariaDB数据库的表中删除一个或多个记录。
语法:
DELETE FROM table [WHERE conditions] [ORDER BY expression [ ASC | DESC ]] [LIMIT number_rows];
下面演示如何使用指定的一个条件来删除数据。
示例:
DELETE FROM Students WHERE student_name = 'Blaba';
在执行上面删除语句后,现在查询表中的数据 -
假设数据库表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_name
为Maxsu
的记录 -
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_id
为7
,并且student_name
值为Maxsu
的记录被删除了。