CRUD 即增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写,是表的四种基本操作。
语法
INSERT INTO [表名] VALUES(参数1, 参数2, 参数3......);
示例
INSERT INTO stu VALUES(2, 15, "李梅", 20200811, 88.5, 90.5);
MariaDB [lee]> select * from stu;
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 2 | 15 | 李梅 | 2020-08-11 | 88.50 | 90.50 |
+------+------+--------+------------+-------+---------+
1 row in set (0.000 sec)
即只插入部分列内容
语法
INSERT INTO [表名](列1,列2,列3.......) values(参数1, 参数2, 参数3......);
示例
INSERT INTO stu(id, age, name) values(3, 16, "李华");
MariaDB [lee]> select * from stu;
+------+------+--------+-------+------+---------+
| id | age | name | birth | math | english |
+------+------+--------+-------+------+---------+
| 3 | 16 | 李华 | NULL | NULL | NULL |
+------+------+--------+-------+------+---------+
1 row in set (0.001 sec)
语法
SELECT * FROM [表名];
示例
SELECT * FROM stu;
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
| 2 | 15 | 李梅 | 2020-08-13 | 88.50 | 90.50 |
| 3 | 18 | 张三 | 2020-08-14 | 84.50 | 70.50 |
| 4 | 16 | 李四 | 2020-08-15 | 75.60 | 40.80 |
| 5 | 14 | 王五 | 2020-08-16 | 48.80 | 91.90 |
| 6 | 13 | 马六 | 2020-08-17 | 85.90 | 95.40 |
+------+------+--------+------------+-------+---------+
语法
SELECT 列1,列2,列3..... FROM [表名];
示例
SELECT id, age, name FROM stu;
+------+------+--------+
| id | age | name |
+------+------+--------+
| 1 | 11 | 李华 |
| 2 | 15 | 李梅 |
| 3 | 18 | 张三 |
| 4 | 16 | 李四 |
| 5 | 14 | 王五 |
| 6 | 13 | 马六 |
+------+------+--------+
语法
SELECT * FROM [表名] WHERE [条件];
示例
SELECT * FROM stu where name = "李华";
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
+------+------+--------+------------+-------+---------+
语法
SELECT * FROM [表名] LIMIT [每页条数] OFFSET [偏移条数];
示例
SELECT * FROM stu LIMIT 2;//限制每页两条
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
| 2 | 15 | 李梅 | 2020-08-13 | 88.50 | 90.50 |
+------+------+--------+------------+-------+---------+
SELECT * FROM stu LIMIT 2 OFFSET 2;//偏移两条,也就是查询第二页
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 3 | 18 | 张三 | 2020-08-14 | 84.50 | 70.50 |
| 4 | 16 | 李四 | 2020-08-15 | 75.60 | 40.80 |
+------+------+--------+------------+-------+---------+
语法
SELECT DISTINCT * FROM [表名];
示例
SELECT DISTINCT * FROM stu;
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
| 2 | 15 | 李梅 | 2020-08-13 | 88.50 | 90.50 |
| 3 | 18 | 张三 | 2020-08-14 | 84.50 | 70.50 |
| 4 | 16 | 李四 | 2020-08-15 | 75.60 | 40.80 |
| 5 | 14 | 王五 | 2020-08-16 | 48.80 | 91.90 |
| 6 | 13 | 马六 | 2020-08-17 | 85.90 | 95.40 |
+------+------+--------+------------+-------+---------+
语法
SELECT * FROM [表名] ORDER BY [排序依赖项] [DESC/ASC];
//DESC为降序排序,ASC为升序排序,默认为ASC
示例
SELECT * FROM stu ORDER BY math DESC;//按数学成绩降序
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 2 | 15 | 李梅 | 2020-08-13 | 88.50 | 90.50 |
| 6 | 13 | 马六 | 2020-08-17 | 85.90 | 95.40 |
| 3 | 18 | 张三 | 2020-08-14 | 84.50 | 70.50 |
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
| 4 | 16 | 李四 | 2020-08-15 | 75.60 | 40.80 |
| 5 | 14 | 王五 | 2020-08-16 | 48.80 | 91.90 |
+------+------+--------+------------+-------+---------+
语法
SELECT 列a,列b...... AS [别名] FROM [表名];
//DESC为降序排序,ASC为升序排序,默认为ASC
示例
SELECT name, math + english AS total FROM stu;//获取每个人的总分
+--------+--------+
| name | total |
+--------+--------+
| 李华 | 164.00 |
| 李梅 | 179.00 |
| 张三 | 155.00 |
| 李四 | 116.40 |
| 王五 | 140.70 |
| 马六 | 181.30 |
+--------+--------+
语法
UPDATE [表名] SET [修改项] = [修改结果]
示例
UPDATE stu SET math = 99.5 WHERE name = "李梅";
MariaDB [lee]> select * from stu;
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 2 | 15 | 李梅 | 2020-08-11 | 99.50 | 90.50 |
+------+------+--------+------------+-------+---------+
语法
DELETE FROM [表名];
示例
DELETE FROM stu where name = "李梅";
MariaDB [lee]> select * from stu;
Empty set (0.000 sec)