以下是阿鲤数据库基础查询的学习总结以及大量的练习,如果大家可以将此篇博客的练习做完,可以有校的加深大家对数据库的使用;如果你还不了解数据库,请浏览这篇博客数据库基础

一:create

二:retrieve

三:update

四:delete


一:create

语法:

INSERT [INTO] table_name [(column [, column] ...)] VALUES (value_list) [, (value_list)] ... value_list: value, [, value] ...

eg:

创建一张学生表

MariaDB [db1]> CREATE TABLE students ( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, sn INT NOT NULL UNIQUE COMMENT '学号', name VARCHAR(20) NOT NULL, qq VARCHAR(20)
);
Query OK, 0 rows affected (0.01 sec)

单行数据 + 全列插入

MariaDB [db1]> insert students values(100, 10000, '唐三藏', null);
Query OK, 1 row affected (0.00 sec)MariaDB [db1]> insert students values(101, 10001, '孙悟空', '43453532');
Query OK, 1 row affected (0.02 sec)MariaDB [db1]> select * from students;
+-----+-------+-----------+----------+
| id  | sn    | name      | qq       |
+-----+-------+-----------+----------+
| 100 | 10000 | 唐三藏    | NULL     |
| 101 | 10001 | 孙悟空    | 43453532 |
+-----+-------+-----------+----------+

多行数据+指定列插入

MariaDB [db1]> INSERT INTO students (id, sn, name) VALUES (102, 20001, '曹孟德'), (103, 20002, '孙仲谋');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0MariaDB [db1]> select * from students;
+-----+-------+-----------+----------+
| id  | sn    | name      | qq       |
+-----+-------+-----------+----------+
| 100 | 10000 | 唐三藏    | NULL     |
| 101 | 10001 | 孙悟空    | 43453532 |
| 102 | 20001 | 曹孟德    | NULL     |
| 103 | 20002 | 孙仲谋    | NULL     |
+-----+-------+-----------+----------+
4 rows in set (0.00 sec)

更新插入:有时候会因为主键或唯一键对应的值已经存在而导致插入失败

//主键冲突
MariaDB [db1]> insert into students(id, sn, name) values (100, 10010, '白龙马');
ERROR 1062 (23000): Duplicate entry '100' for key 'PRIMARY'
//唯一键冲突
MariaDB [db1]> insert into students(id, sn, name) values (104, 20001, '阿斗');
ERROR 1062 (23000): Duplicate entry '20001' for key 'sn'

如果需覆盖,可以执行插入时更新

语法

INSERT ... ON DUPLICATE KEY UPDATE column = value [, column = value] ...

eg

MariaDB [db1]> insert into students (id, sn, name) values(100, 10010, '白龙马')-> on duplicate key update sn = 10010, name = '白龙马';
Query OK, 2 rows affected (0.00 sec)//0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
//1 row affected: 表中没有冲突数据,数据被插入
//2 row affected: 表中有冲突数据,并且数据已经被更新MariaDB [db1]> select * from students;
+-----+-------+-----------+----------+
| id  | sn    | name      | qq       |
+-----+-------+-----------+----------+
| 100 | 10010 | 白龙马    | NULL     |
| 101 | 10001 | 孙悟空    | 43453532 |
| 102 | 20001 | 曹孟德    | NULL     |
| 103 | 20002 | 孙仲谋    | NULL     |
+-----+-------+-----------+----------+
4 rows in set (0.00 sec)//我们还可以查询收到影响的数据行数
SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

当然我们也可以直接替换掉冲突的主键或唯一键

MariaDB [db1]> replace into students(sn, name) values(20001, '阿斗');
Query OK, 2 rows affected (0.00 sec)MariaDB [db1]> select * from students;
+-----+-------+-----------+----------+
| id  | sn    | name      | qq       |
+-----+-------+-----------+----------+
| 100 | 10010 | 白龙马    | NULL     |
| 101 | 10001 | 孙悟空    | 43453532 |
| 103 | 20002 | 孙仲谋    | NULL     |
| 104 | 20001 | 阿斗      | NULL     |
+-----+-------+-----------+----------+

二:retrieve

语法:

SELECT[DISTINCT] {* | {column [, column] ...} [FROM table_name] [WHERE ...] [ORDER BY column [ASC | DESC], ...] LIMIT ...

eg

创建一个成绩表,并插入数据

//创建表结构
CREATE TABLE exam_result (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL COMMENT '同学姓名',
yuwen float DEFAULT 0.0 COMMENT '语文成绩',
shuxue float DEFAULT 0.0 COMMENT '数学成绩',
yingyu float DEFAULT 0.0 COMMENT '英语成绩'
);//插入测试数据
INSERT INTO exam_result (name, yuwen, shuxue, yingyu) VALUES
('唐三藏', 67, 98, 56),
('孙悟空', 87, 78, 77),
('猪悟能', 88, 98, 90),
('曹孟德', 82, 84, 67),
('刘玄德', 55, 85, 45),
('孙权', 70, 73, 78),
('宋公明', 75, 65, 30);
Query OK,
7 rows affected (0.00 sec) Record
s: 7 Duplicates: 0 Warnings: 0

全列查询

MariaDB [db1]> select * from exam_result;
+----+-----------+-------+--------+--------+
| id | name      | yuwen | shuxue | yingyu |
+----+-----------+-------+--------+--------+
|  1 | 唐三藏    |    67 |     98 |     56 |
|  2 | 孙悟空    |    87 |     78 |     77 |
|  3 | 猪悟能    |    88 |     98 |     90 |
|  4 | 曹孟德    |    82 |     84 |     67 |
|  5 | 刘玄德    |    55 |     85 |     45 |
|  6 | 孙权      |    70 |     73 |     78 |
|  7 | 宋公明    |    75 |     65 |     30 |
+----+-----------+-------+--------+--------+
//通常情况下不建议使用*查询,因为列数越多,数据量越大效率越低

指定列查询

MariaDB [db1]> select id,name,yingyu from exam_result;
+----+-----------+--------+
| id | name      | yingyu |
+----+-----------+--------+
|  1 | 唐三藏    |     56 |
|  2 | 孙悟空    |     77 |
|  3 | 猪悟能    |     90 |
|  4 | 曹孟德    |     67 |
|  5 | 刘玄德    |     45 |
|  6 | 孙权      |     78 |
|  7 | 宋公明    |     30 |
+----+-----------+--------+

查询字段为表达式

MariaDB [db1]> select id, name, yingyu + 10 from exam_result;
+----+-----------+-------------+
| id | name      | yingyu + 10 |
+----+-----------+-------------+
|  1 | 唐三藏    |          66 |
|  2 | 孙悟空    |          87 |
|  3 | 猪悟能    |         100 |
|  4 | 曹孟德    |          77 |
|  5 | 刘玄德    |          55 |
|  6 | 孙权      |          88 |
|  7 | 宋公明    |          40 |
+----+-----------+-------------+
7 rows in set (0.00 sec)MariaDB [db1]> select id, name, yuwen + shuxue + yingyu from exam_result;
+----+-----------+-------------------------+
| id | name      | yuwen + shuxue + yingyu |
+----+-----------+-------------------------+
|  1 | 唐三藏    |                     221 |
|  2 | 孙悟空    |                     242 |
|  3 | 猪悟能    |                     276 |
|  4 | 曹孟德    |                     233 |
|  5 | 刘玄德    |                     185 |
|  6 | 孙权      |                     221 |
|  7 | 宋公明    |                     170 |
+----+-----------+-------------------------+
7 rows in set (0.00 sec)

当然我们也可以为结果指定别名

语法:

SELECT column [AS] alias_name [...] FROM table_name;

eg:

MariaDB [db1]> select id, name, yuwen + shuxue + yingyu 总分 from exam_result;
+----+-----------+--------+
| id | name      | 总分   |
+----+-----------+--------+
|  1 | 唐三藏    |    221 |
|  2 | 孙悟空    |    242 |
|  3 | 猪悟能    |    276 |
|  4 | 曹孟德    |    233 |
|  5 | 刘玄德    |    185 |
|  6 | 孙权      |    221 |
|  7 | 宋公明    |    170 |
+----+-----------+--------+
7 rows in set (0.00 sec)

那么我们现在想要查询都有那些分数存在,所以我们要去重

MariaDB [db1]> select shuxue from exam_result;
+--------+
| shuxue |
+--------+
|     98 |
|     78 |
|     98 |
|     84 |
|     85 |
|     73 |
|     65 |
+--------+
7 rows in set (0.00 sec)MariaDB [db1]> select distinct shuxue from exam_result;
+--------+
| shuxue |
+--------+
|     98 |
|     78 |
|     84 |
|     85 |
|     73 |
|     65 |
+--------+
6 rows in set (0.00 sec)

当然我们在查询时候,也可以使用一些条件;where条件

where支持以下运算符号

运算符 说明
>, >=, <, <=
大于,大于等于,小于,小于等于
=
等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL
<=>
等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1)
!=, <>
不等于
between a0 and a1
范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1)
in(option, ...)
如果是 option 中的任意一个,返回 TRUE(1)
is_null 是null
in not null 不是null
like 模糊匹配,%表示任意多个任意字符;_表示任意一个字符
and 多个条件必须都为true(1),结果才是true(1)
or 任意一个条件为true(1),就果就是true(1)
not 条件为true(1),结果为false(0)

eg:

英语成绩不及格的同学及成绩

MariaDB [db1]> select name,yingyu from exam_result where yingyu < 60;
+-----------+--------+
| name      | yingyu |
+-----------+--------+
| 唐三藏    |     56 |
| 刘玄德    |     45 |
| 宋公明    |     30 |
+-----------+--------+
3 rows in set (0.00 sec)

语文成绩在[80,90]分的同学及语文成绩

MariaDB [db1]> SELECT name, yuwen from exam_result where yuwen >= 80 and yuwen <= 90;
+-----------+-------+
| name      | yuwen |
+-----------+-------+
| 孙悟空    |    87 |
| 猪悟能    |    88 |
| 曹孟德    |    82 |
+-----------+-------+
3 rows in set (0.00 sec)MariaDB [db1]> select name, yuwen from exam_result where yuwen between 80 and 90;
+-----------+-------+
| name      | yuwen |
+-----------+-------+
| 孙悟空    |    87 |
| 猪悟能    |    88 |
| 曹孟德    |    82 |
+-----------+-------+
3 rows in set (0.00 sec)
数学成绩是58或着59或着98或99的同学及成绩
MariaDB [db1]> select name, shuxue from exam_result;
+-----------+--------+
| name      | shuxue |
+-----------+--------+
| 唐三藏    |     98 |
| 孙悟空    |     78 |
| 猪悟能    |     98 |
| 曹孟德    |     84 |
| 刘玄德    |     85 |
| 孙权      |     73 |
| 宋公明    |     65 |
+-----------+--------+
7 rows in set (0.00 sec)MariaDB [db1]> select name, shuxue from exam_result-> where shuxue = 58-> or shuxue = 59-> or shuxue = 98-> or shuxue = 99;
+-----------+--------+
| name      | shuxue |
+-----------+--------+
| 唐三藏    |     98 |
| 猪悟能    |     98 |
+-----------+--------+
2 rows in set (0.00 sec)MariaDB [db1]> select name, shuxue from exam_result where shuxue in (58, 59, 98, 99);
+-----------+--------+
| name      | shuxue |
+-----------+--------+
| 唐三藏    |     98 |
| 猪悟能    |     98 |
+-----------+--------+
2 rows in set (0.00 sec)

匹配姓孙的同学

MariaDB [db1]> select name from exam_result where name like '孙%';
+-----------+
| name      |
+-----------+
| 孙悟空    |
| 孙权      |
+-----------+
2 rows in set (0.00 sec)

严格匹配二字孙姓同学

MariaDB [db1]> select name from exam_result where name like '孙%';
+-----------+
| name      |
+-----------+
| 孙悟空    |
| 孙权      |
+-----------+
2 rows in set (0.00 sec)

语文成绩好于英语成绩的同学

MariaDB [db1]> select name, yuwen, yingyu from exam_result where yuwen > yingyu;
+-----------+-------+--------+
| name      | yuwen | yingyu |
+-----------+-------+--------+
| 唐三藏    |    67 |     56 |
| 孙悟空    |    87 |     77 |
| 曹孟德    |    82 |     67 |
| 刘玄德    |    55 |     45 |
| 宋公明    |    75 |     30 |
+-----------+-------+--------+
5 rows in set (0.00 sec)

总分在200分以下的同学

MariaDB [db1]> select name, yuwen + shuxue + yingyu 总分 from exam_result-> where yuwen + shuxue + yingyu < 200;
+-----------+--------+
| name      | 总分   |
+-----------+--------+
| 刘玄德    |    185 |
| 宋公明    |    170 |
+-----------+--------+
2 rows in set (0.00 sec)

语文成绩大与80且不姓孙的同学

MariaDB [db1]> select name, yuwen from exam_result-> where yuwen > 80 and name not like '孙%';
+-----------+-------+
| name      | yuwen |
+-----------+-------+
| 猪悟能    |    88 |
| 曹孟德    |    82 |
+-----------+-------+
2 rows in set (0.00 sec)

孙某同学,总成绩>200,语文成绩  < 数学成绩,英语成绩 > 80

MariaDB [db1]> select name, yuwen, shuxue, yingyu, yuwen + shuxue + yingyu 总分-> from exam_result-> where name like '孙_' or(-> yuwen + shuxue +yingyu > 200 and yuwen < shuxue and yingyu > 80-> );
+-----------+-------+--------+--------+--------+
| name      | yuwen | shuxue | yingyu | 总分   |
+-----------+-------+--------+--------+--------+
| 猪悟能    |    88 |     98 |     90 |    276 |
| 孙权      |    70 |     73 |     78 |    221 |
+-----------+-------+--------+--------+--------+
2 rows in set (0.00 sec)

null的查询

MariaDB [db1]> select * from students;
+-----+-------+-----------+----------+
| id  | sn    | name      | qq       |
+-----+-------+-----------+----------+
| 100 | 10010 | 白龙马    | NULL     |
| 101 | 10001 | 孙悟空    | 43453532 |
| 103 | 20002 | 孙仲谋    | NULL     |
| 104 | 20001 | 阿斗      | NULL     |
+-----+-------+-----------+----------+
4 rows in set (0.00 sec)MariaDB [db1]> select sn, name, qq from students where qq is not null;
+-------+-----------+----------+
| sn    | name      | qq       |
+-------+-----------+----------+
| 10001 | 孙悟空    | 43453532 |
+-------+-----------+----------+
1 row in set (0.00 sec)

为查询结果排序

语法

//ASC 为升序(从小到大)
//DESC 为降序(从大到小)
//默认为 ASCASC SELECT ... FROM table_name [WHERE ...] ORDER BY column [ASC|DESC], [...];

eg:显示同学和数学成绩,并把数学成绩升序显示

MariaDB [db1]> select name, shuxue from exam_result order by shuxue;
+-----------+--------+
| name      | shuxue |
+-----------+--------+
| 宋公明    |     65 |
| 孙权      |     73 |
| 孙悟空    |     78 |
| 曹孟德    |     84 |
| 刘玄德    |     85 |
| 唐三藏    |     98 |
| 猪悟能    |     98 |
+-----------+--------+
7 rows in set (0.00 sec)

同学及qq号,按照qq号排序显示

MariaDB [db1]> select name, qq from students order by qq;
+-----------+----------+
| name      | qq       |
+-----------+----------+
| 白龙马    | NULL     |
| 孙仲谋    | NULL     |
| 阿斗      | NULL     |
| 孙悟空    | 43453532 |
+-----------+----------+
4 rows in set (0.00 sec)//降序
MariaDB [db1]> select name, qq from students order by qq desc;
+-----------+----------+
| name      | qq       |
+-----------+----------+
| 孙悟空    | 43453532 |
| 白龙马    | NULL     |
| 孙仲谋    | NULL     |
| 阿斗      | NULL     |
+-----------+----------+
4 rows in set (0.00 sec)

查询同学各门成绩,依次按照数学降序,英语升序,语文升序的方式显示

MariaDB [db1]> SELECT name, shuxue, yingyu, yuwen FROM exam_result ORDER BY shuxue DESC, yingyu, yuwen;
+-----------+--------+--------+-------+
| name      | shuxue | yingyu | yuwen |
+-----------+--------+--------+-------+
| 唐三藏    |     98 |     56 |    67 |
| 猪悟能    |     98 |     90 |    88 |
| 刘玄德    |     85 |     45 |    55 |
| 曹孟德    |     84 |     67 |    82 |
| 孙悟空    |     78 |     77 |    87 |
| 孙权      |     73 |     78 |    70 |
| 宋公明    |     65 |     30 |    75 |
+-----------+--------+--------+-------+
7 rows in set (0.00 sec)

查询同学及总分,由高到低

MariaDB [db1]> select name, yuwen + yingyu + shuxue 总分 from exam_result  order by yuwen + yingyu +shuxue desc;
+-----------+--------+
| name      | 总分   |
+-----------+--------+
| 猪悟能    |    276 |
| 孙悟空    |    242 |
| 曹孟德    |    233 |
| 唐三藏    |    221 |
| 孙权      |    221 |
| 刘玄德    |    185 |
| 宋公明    |    170 |
+-----------+--------+
查询姓孙的同学或者姓曹的同学数学成绩,结果按数学成绩由高到低显示
MariaDB [db1]> select name, shuxue from exam_result -> where name like '孙%' or name like '曹%'-> order by shuxue desc;
+-----------+--------+
| name      | shuxue |
+-----------+--------+
| 曹孟德    |     84 |
| 孙悟空    |     78 |
| 孙权      |     73 |
+-----------+--------+
3 rows in set (0.00 sec)

筛选分页结果:

语法

//起始下标为 0 -//从 0 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n; //从 s 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n; //从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET n;
建议:对未知表进行查询时,最好加一条 LIMIT 1,避免因为表中数据过大,查询全表数据导致数据库卡死
id 进行分页,每页 3 条记录,分别显示 第 123

eg:

//第1页
MariaDB [db1]> select id, name, shuxue, yingyu, yuwen from exam_result-> order by id limit 3 offset 0;
+----+-----------+--------+--------+-------+
| id | name      | shuxue | yingyu | yuwen |
+----+-----------+--------+--------+-------+
|  1 | 唐三藏    |     98 |     56 |    67 |
|  2 | 孙悟空    |     78 |     77 |    87 |
|  3 | 猪悟能    |     98 |     90 |    88 |
+----+-----------+--------+--------+-------+
3 rows in set (0.00 sec)//第2页
MariaDB [db1]> select id, name, shuxue, yingyu, yuwen from exam_result order by id limit 3 offset 3;
+----+-----------+--------+--------+-------+
| id | name      | shuxue | yingyu | yuwen |
+----+-----------+--------+--------+-------+
|  4 | 曹孟德    |     84 |     67 |    82 |
|  5 | 刘玄德    |     85 |     45 |    55 |
|  6 | 孙权      |     73 |     78 |    70 |
+----+-----------+--------+--------+-------+
3 rows in set (0.00 sec)//第3页
MariaDB [db1]> select id, name, shuxue, yingyu, yuwen from exam_result order by id limit 3 offset 6;
+----+-----------+--------+--------+-------+
| id | name      | shuxue | yingyu | yuwen |
+----+-----------+--------+--------+-------+
|  7 | 宋公明    |     65 |     30 |    75 |
+----+-----------+--------+--------+-------+
1 row in set (0.00 sec)

三:update

语法:

UPDATE table_name SET column = expr [, column = expr ...] [WHERE ...] [ORDER BY ...] [LIMIT ...]

eg:

将孙悟空同学的数学成绩变更为80分

//原始数据
MariaDB [db1]> select name, shuxue from exam_result where name = '孙悟空';
+-----------+--------+
| name      | shuxue |
+-----------+--------+
| 孙悟空    |     78 |
+-----------+--------+
1 row in set (0.00 sec)//更新
MariaDB [db1]> update exam_result set shuxue = 80 where name = '孙悟空';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0//更新后
MariaDB [db1]> select name, shuxue from exam_result where name = '孙悟空';
+-----------+--------+
| name      | shuxue |
+-----------+--------+
| 孙悟空    |     80 |
+-----------+--------+
1 row in set (0.00 sec)

将曹孟德同学的数学成绩变更为60分,语文成绩变更为70分

//一次操作多列

//原始数据
MariaDB [db1]> select name, shuxue, yuwen from exam_result where name = '曹孟德';
+-----------+--------+-------+
| name      | shuxue | yuwen |
+-----------+--------+-------+
| 曹孟德    |     84 |    82 |
+-----------+--------+-------+
1 row in set (0.00 sec)//更新
MariaDB [db1]> update exam_result set shuxue = 60, yuwen = 70 where name = '曹孟德';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0//更新后
MariaDB [db1]> select name, shuxue, yuwen from exam_result where name = '曹孟德';
+-----------+--------+-------+
| name      | shuxue | yuwen |
+-----------+--------+-------+
| 曹孟德    |     60 |    70 |
+-----------+--------+-------+
1 row in set (0.00 sec)

将总成绩倒数前三的三位同学的数学成绩加上30分

//倒数前三的同学
MariaDB [db1]> select name, shuxue + yuwen + yingyu 总分 from exam_result -> order by 总分  limit 3;
+-----------+--------+
| name      | 总分   |
+-----------+--------+
| 宋公明    |    170 |
| 刘玄德    |    185 |
| 曹孟德    |    197 |
+-----------+--------+
3 rows in set (0.00 sec)//他们的数学成绩
MariaDB [db1]> SELECT name, shuxue, yuwen + shuxue + yingyu 总分 FROM exam_result WHERE name IN ('宋公明', '刘玄德', '曹孟德');
+-----------+--------+--------+
| name      | shuxue | 总分   |
+-----------+--------+--------+
| 曹孟德    |     60 |    197 |
| 刘玄德    |     85 |    185 |
| 宋公明    |     65 |    170 |
+-----------+--------+--------+
3 rows in set (0.00 sec)//更新
MariaDB [db1]> UPDATE exam_result SET shuxue = shuxue + 30 ORDER BY yuwen + shuxue + yingyu LIMIT 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0//他们的数学成绩
MariaDB [db1]> SELECT name, shuxue, yuwen + shuxue + yingyu 总分 FROM exam_result WHERE name IN ('宋公明', '刘玄德', '曹孟德');
+-----------+--------+--------+
| name      | shuxue | 总分   |
+-----------+--------+--------+
| 曹孟德    |     90 |    227 |
| 刘玄德    |    115 |    215 |
| 宋公明    |     95 |    200 |
+-----------+--------+--------+
3 rows in set (0.00 sec)//总成绩的倒数前三
MariaDB [db1]> select name, shuxue, yuwen + yingyu + shuxue 总分 from exam_result             order by 总分 limit 3;
+-----------+--------+--------+
| name      | shuxue | 总分   |
+-----------+--------+--------+
| 宋公明    |     95 |    200 |
| 刘玄德    |    115 |    215 |
| 唐三藏    |     98 |    221 |
+-----------+--------+--------+
3 rows in set (0.00 sec)

将所有同学的语文成绩更新为原来的2倍

MariaDB [db1]> select * from exam_result;
+----+-----------+-------+--------+--------+
| id | name      | yuwen | shuxue | yingyu |
+----+-----------+-------+--------+--------+
|  1 | 唐三藏    |    67 |     98 |     56 |
|  2 | 孙悟空    |    87 |     80 |     77 |
|  3 | 猪悟能    |    88 |     98 |     90 |
|  4 | 曹孟德    |    70 |     90 |     67 |
|  5 | 刘玄德    |    55 |    115 |     45 |
|  6 | 孙权      |    70 |     73 |     78 |
|  7 | 宋公明    |    75 |     95 |     30 |
+----+-----------+-------+--------+--------+
7 rows in set (0.00 sec)MariaDB [db1]> update exam_result set yuwen = yuwen * 2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7  Changed: 7  Warnings: 0MariaDB [db1]> select * from exam_result;
+----+-----------+-------+--------+--------+
| id | name      | yuwen | shuxue | yingyu |
+----+-----------+-------+--------+--------+
|  1 | 唐三藏    |   134 |     98 |     56 |
|  2 | 孙悟空    |   174 |     80 |     77 |
|  3 | 猪悟能    |   176 |     98 |     90 |
|  4 | 曹孟德    |   140 |     90 |     67 |
|  5 | 刘玄德    |   110 |    115 |     45 |
|  6 | 孙权      |   140 |     73 |     78 |
|  7 | 宋公明    |   150 |     95 |     30 |
+----+-----------+-------+--------+--------+

四:delete

语法:

DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]

eg

删除孙悟空同学的考试成绩

MariaDB [db1]> select * from exam_result;
+----+-----------+-------+--------+--------+
| id | name      | yuwen | shuxue | yingyu |
+----+-----------+-------+--------+--------+
|  1 | 唐三藏    |   134 |     98 |     56 |
|  2 | 孙悟空    |   174 |     80 |     77 |
|  3 | 猪悟能    |   176 |     98 |     90 |
|  4 | 曹孟德    |   140 |     90 |     67 |
|  5 | 刘玄德    |   110 |    115 |     45 |
|  6 | 孙权      |   140 |     73 |     78 |
|  7 | 宋公明    |   150 |     95 |     30 |
+----+-----------+-------+--------+--------+
7 rows in set (0.00 sec)MariaDB [db1]> delete from exam_result where name = '孙悟空';
Query OK, 1 row affected (0.00 sec)MariaDB [db1]> select * from exam_result;
+----+-----------+-------+--------+--------+
| id | name      | yuwen | shuxue | yingyu |
+----+-----------+-------+--------+--------+
|  1 | 唐三藏    |   134 |     98 |     56 |
|  3 | 猪悟能    |   176 |     98 |     90 |
|  4 | 曹孟德    |   140 |     90 |     67 |
|  5 | 刘玄德    |   110 |    115 |     45 |
|  6 | 孙权      |   140 |     73 |     78 |
|  7 | 宋公明    |   150 |     95 |     30 |
+----+-----------+-------+--------+--------+
6 rows in set (0.00 sec)

删除整张表

//创建删除表
MariaDB [db1]> create table for_delete(-> id int primary key auto_increment,-> name varchar(20)-> );
Query OK, 0 rows affected (0.01 sec)//插入数据
MariaDB [db1]> insert into for_delete (name) values ('a'), ('b'), ('c');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0//查看表数据
MariaDB [db1]> select * from for_delete;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
+----+------+
3 rows in set (0.00 sec)//删除整张表
MariaDB [db1]> delete from for_delete;
Query OK, 3 rows affected (0.00 sec)//查看删除结果
MariaDB [db1]> select * from for_delete;
Empty set (0.00 sec)//再往表里插入数据
MariaDB [db1]> insert into for_delete (name) values ('d');
Query OK, 1 row affected (0.00 sec)//查看表结构,会发现id的自增长是在原值上增长
MariaDB [db1]> select * from for_delete;
+----+------+
| id | name |
+----+------+
|  4 | d    |
+----+------+
1 row in set (0.00 sec)//查看表结构。发现会有auto_increment=n项
MariaDB [db1]> show create table for_delete;
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                                      |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| for_delete | CREATE TABLE `for_delete` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(20) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

截断表

语法:

TRUNCATE [TABLE] table_name
注意:这个操作慎用
1. 只能对整表操作,不能像 DELETE 一样针对部分数据操作;
2. 实际上 MySQL 不对数据操作,所以比 DELETE 更快
3. 会重置 AUTO_INCREMENT 项
//准备测试表
MariaDB [db1]> insert into for_truncate (name) values ('a'), ('b'), ('c');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0//查看数据
MariaDB [db1]> select * from for_truncate;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
+----+------+
3 rows in set (0.00 sec)//截断
MariaDB [db1]> truncate for_truncate;
Query OK, 0 rows affected (0.01 sec)//查看截断结果
MariaDB [db1]> select * from for_truncate;
Empty set (0.00 sec)//插入数据
MariaDB [db1]> insert into for_truncate (name) values ('d');
Query OK, 1 row affected (0.00 sec)//查看数据,会发现从1开始
MariaDB [db1]> select * from for_truncate;
+----+------+
| id | name |
+----+------+
|  1 | d    |
+----+------+
1 row in set (0.00 sec)//auto_increment更新了
MariaDB [db1]> show create table for_truncate;
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table        | Create Table                                                                                                                                                                        |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| for_truncate | CREATE TABLE `for_truncate` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(20) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)MariaDB [db1]>

插入查询结果

语法

INSERT INTO table_name [(column [, column ...])] SELECT ...

eg:

//创建原始数据表
CREATE TABLE duplicate_table (id int, name varchar(20));
Query OK, 0 rows affected (0.01 sec)//插入测试数据
INSERT INTO duplicate_table VALUES
(100, 'aaa'),
(100, 'aaa'),
(200, 'bbb'),
(200, 'bbb'),
(200, 'bbb'),
(300, 'ccc');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0//创建一个和duplicate_table结构一样的空表
MariaDB [db1]> create table no_duplicate_table like duplicate_table;
Query OK, 0 rows affected (0.01 sec)// 将 duplicate_table 的去重数据插入到 no_duplicate_table
MariaDB [db1]> insert into no_duplicate_table select distinct * from duplicate_table;
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0//通过重命名表,实现原子的去重操作
MariaDB [db1]> rename table duplicate_table to old_duplicate_table,-> no_duplicate_table to duplicate_table;
Query OK, 0 rows affected (0.02 sec)//查看最终结果
MariaDB [db1]> select * from duplicate_table;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+
3 rows in set (0.00 sec)

mysql表的基础操作: Create,,Retrieve,Update,Delete(大量示例)相关推荐

  1. 【MySQL】MySQL表的CRUD操作(基础)

    MySQL表的CRUD操作 表数据操作 添加数据(Create) 多行全列插入 指定列添加 查询数据(Retrieve) 全列查询 指定列查询 表达式查询 使用别名查询 去重(Distinct) 排序 ...

  2. Linux下Mysql数据库的基础操作

    Linux下Mysql数据库的基础操作 一.Mysql数据介绍 二.数据库相关术语介绍 1.数据库相关名词 2.相关术语介绍 三.Mysql数据库的管理 1.创建数据库用户 2.查询用户状态 3.修改 ...

  3. MySQL数据库的基础操作

    文章目录 一.数据库的基础操作 二.数据表的基础操作 1.数据表的创建 2.数据表数据的插入 2.数据表的查询 3.数据表的更新 4.数据表的数据删除 我使用的是MySQL数据库专用软件MySQL5. ...

  4. 初识MySQL数据库(MySQL数据库的基础操作)

    初识MySQL数据库 前言 1.查询所有数据库 2.创建数据库 2.1指令1: 2.2指令2: 3.3指令3: 3.删除数据库 3.1指令1: 3.2指令2: 在这里插入图片描述 4.切换数据库 总结 ...

  5. mysql表级别的操作_MySql 库/表级操作 及 数据类型 - 纪宇

    数据库分类 关系型数据库(SQL):存储方式固定,安全 非关系型数据库(NoSQL):存储方式比较灵活,存储数据的效率比较高,不太安全 MySQL是一种关系型数据库管理系统(采用关系模型来组织管理数据 ...

  6. mysql的入门基础操作

    1.数据库的简介 1.1 什么是数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle  是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IBM公 ...

  7. mysql军刀_mysql基础操作

    登陆mysql命令行 mysql -h192.168.1.110 -uroot -p 查看所有数据库 show databases; 使用mysql数据库 use mysql; 如果数据库mysql存 ...

  8. 数据库基础笔记(MySQL)3 —— 基础操作

    表操作 表的创建 ( create ) create table 表名 ( 字段1 数据类型,字段2 数据类型 - ) ; 例:create table table_1 ( id int , name ...

  9. MySQL 视图的基础操作

    1.为什么使用视图:      为了提高复杂SQL语句的复用性和表操作的安全性(例如:工资字段不想展示给所有能查看该查询结果的人),MySQL提供了视图特性.所谓视图,本质上是一种虚拟表,其内容与真实 ...

最新文章

  1. [转] React Hot Loader 3 beta 升级指南
  2. swift和python语法区别_Swift 基本语法
  3. 修改altium designer原理图右下角信息
  4. 基于.NET实现数据挖掘--朴素贝叶斯算法
  5. 提速 30%!腾讯TQUIC 网络传输协议
  6. python能做页面加载动画吗_HTML+CSS实现页面加载(loading)动画效果
  7. CPU的实模式和保护模式
  8. @my_decorator
  9. Linux环境下编译运行大型C语言项目
  10. pr视频剪辑中工具栏功能详解
  11. 邮箱格式怎么写?电子邮箱格式怎么写?邮件地址怎么写?
  12. oracle 执行计划中出现 merge join cartesian
  13. 区块链毕设开题技术路线
  14. Python对Excel的常规操作 之 读取带密码的文件,解除Sheet密码
  15. DB2数据库如何修改字段名称
  16. 如何在Node.js应用程序中使用RethinkDB
  17. audio的自动播放
  18. 最详细的Cydia使用教程------完全版。新补充Cydia1.1.1离线安装(升级)方法。
  19. 《程序员的自我修养》解析第一章
  20. 介绍深度学习的一篇better文章

热门文章

  1. 设置笔记本电脑插入USB鼠标时,自动禁用触摸板
  2. 中国拳手徐灿将战世界拳王:有信心把金腰带带回祖国
  3. 线性调频信号MATLAB仿真
  4. 给excel设置格式
  5. 历时三年“鸽王”Filecoin主网上线,分布式存储市场将迎来最强劲敌?
  6. mybatis 一对一 ,一对多,多对多的实现
  7. python清华大学出版社答案_清华大学出版社的这本Python入门书,出版8年 仍经久不衰...
  8. Zedboard(一)开发环境Vivado
  9. npm install报错的一天
  10. 前端框架vue3的node安装及项目构建的4种方法