MariaDB DELETE
語(yǔ)句用于從MariaDB數(shù)據(jù)庫(kù)的表中刪除一個(gè)或多個(gè)記錄。
語(yǔ)法:
DELETE FROM table
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]]
[LIMIT number_rows];
下面演示如何使用指定的一個(gè)條件來(lái)刪除數(shù)據(jù)。
示例:
DELETE FROM Students
WHERE student_name = 'Blaba';
在執(zhí)行上面刪除語(yǔ)句后,現(xiàn)在查詢表中的數(shù)據(jù) -
假設(shè)數(shù)據(jù)庫(kù)表students
中的student_name
有兩個(gè)Maxsu
值,現(xiàn)在想刪除student_id
不是1
的所有Maxsu
值,參考以下語(yǔ)句 -
首先插入兩條件記錄 -
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');
查詢當(dāng)數(shù)據(jù)庫(kù)中的記錄 -
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 |
+------------+--------------+-----------------+----------------+
6 rows in set (0.00 sec)
刪除那些student_id
大于1
,并且student_name
為Maxsu
的記錄 -
DELETE FROM Students
WHERE student_name = 'Maxsu'
AND student_id > 1;
執(zhí)行上面刪除語(yǔ)句以后,現(xiàn)在查詢表中的記錄 -
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 |
+------------+--------------+-----------------+----------------+
5 rows in set (0.00 sec)
可以看到,student_id
為7
,并且student_name
值為Maxsu
的記錄被刪除了。