1.CRUD
- 注释:在sql中可以使用==“–空格+描述”==表示注释说明
- CRUD:增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词首字母的缩写
2.新增(Create)
首先创建一张表,我们在这张表上的基础上插入数据
创建表如下:
mysql> create table stu_demo(
-> id int,
-> sn int comment '学号',
-> name varchar(20) comment '姓名',
-> password varchar(50) comment '密码'
-> );
Query OK, 0 rows affected (0.09 sec)
mysql> desc stu_demo;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| sn | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
2.1单行数据+全列插入
语法:insert into 表名 values(插入的数据);
比如我们在刚刚创建的表上插入两行信息
mysql> insert into stu_demo values (1,20181101,'悟空',123456);
Query OK, 1 row affected (0.03 sec)
mysql> insert into stu_demo values (2,20201102,'八戒',234567);
Query OK, 1 row affected (0.00 sec)
可以查询到插入后的表格如下:
mysql> select * from stu_demo;
+------+----------+--------+----------+
| id | sn | name | password |
+------+----------+--------+----------+
| 1 | 20181101 | 悟空 | 123456 |
| 2 | 20201102 | 八戒 | 234567 |
+------+----------+--------+----------+
2 rows in set (0.00 sec)
2.2多行数据+指定列插入
语法:insert into 表名 (要插入的列) values
(),(),();
我们在刚刚的表上再插入两行信息
插入的情况如下:
mysql> insert into stu_demo (id,name) values
-> (1,'阿花'),
-> (2,'小豆包');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
插入后的表格是这样子的~~~
mysql> select * from stu_demo;
+------+----------+-----------+----------+
| id | sn | name | password |
+------+----------+-----------+----------+
| 1 | 20181101 | 悟空 | 123456 |
| 2 | 20201102 | 八戒 | 234567 |
| 1 | NULL | 阿花 | NULL |
| 2 | NULL | 小豆包 | NULL |
+------+----------+-----------+----------+
4 rows in set (0.00 sec)
跟咱们的预期一致。
3.查询(Retrieve)
首先为了完成咱们下面的演示,得创建一个学生成绩表如下:
mysql> create table exam_result (
-> id int,
-> name varchar(20),
-> chinese decimal(3,1),
-> math decimal(3,1),
-> englist decimal(3,1)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql> desc exam_result;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| chinese | decimal(3,1) | YES | | NULL | |
| math | decimal(3,1) | YES | | NULL | |
| englist | decimal(3,1) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
然后在里面插入一些数据,完善表的内容
mysql> insert into exam_result (id,name,chinese,math,englist)values
-> (1,'一一',78.5,63.7,96.5),
-> (2,'尔尔',58.5,66.7,78.9),
-> (3,'散散',76.5,96.7,55.5),
-> (4,'思思',63.5,87.7,77.0),
-> (5,'呜呜',87.5,49.7,56.7),
-> (6,'柳柳',98.5,76.7,89.7);
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
3.1全列查询
语法:select *from 表名
查询刚刚插入的表结果如下:
mysql> select *from exam_result;
+------+--------+---------+------+---------+
| id | name | chinese | math | englist |
+------+--------+---------+------+---------+
| 1 | 一一 | 78.5 | 63.7 | 96.5 |
| 2 | 尔尔 | 58.5 | 66.7 | 78.9 |
| 3 | 散散 | 76.5 | 96.7 | 55.5 |
| 4 | 思思 | 63.5 | 87.7 | 77.0 |
| 5 | 呜呜 | 87.5 | 49.7 | 56.7 |
| 6 | 柳柳 | 98.5 | 76.7 | 89.7 |
+------+--------+---------+------+---------+
6 rows in set (0.00 sec)
3.2指定列查询
语法:select 要查询的列名称 from 表名(指定列的顺序与定义的顺序无关)
查询结果如下:
mysql> select id,name,englist from exam_result;
+------+--------+---------+
| id | name | englist |
+------+--------+---------+
| 1 | 一一 | 96.5 |
| 2 | 尔尔 | 78.9 |
| 3 | 散散 | 55.5 |
| 4 | 思思 | 77.0 |
| 5 | 呜呜 | 56.7 |
| 6 | 柳柳 | 89.7 |
+------+--------+---------+
6 rows in set (0.00 sec)
mysql> select id,name,englist,math from exam_result;
+------+--------+---------+------+
| id | name | englist | math |
+------+--------+---------+------+
| 1 | 一一 | 96.5 | 63.7 |
| 2 | 尔尔 | 78.9 | 66.7 |
| 3 | 散散 | 55.5 | 96.7 |
| 4 | 思思 | 77.0 | 87.7 |
| 5 | 呜呜 | 56.7 | 49.7 |
| 6 | 柳柳 | 89.7 | 76.7 |
+------+--------+---------+------+
6 rows in set (0.00 sec)
mysql> select id,name,englist+math from exam_result;
+------+--------+--------------+
| id | name | englist+math |
+------+--------+--------------+
| 1 | 一一 | 160.2 |
| 2 | 尔尔 | 145.6 |
| 3 | 散散 | 152.2 |
| 4 | 思思 | 164.7 |
| 5 | 呜呜 | 106.4 |
| 6 | 柳柳 | 166.4 |
+------+--------+--------------+
6 rows in set (0.03 sec)
3.3查询字段为表达式
语法同上,但是查询几个字段相加时,如语文数学英语成绩的总和,可以直接使用+号
mysql> select id,name,englist from exam_result;
+------+--------+---------+
| id | name | englist |
+------+--------+---------+
| 1 | 一一 | 96.5 |
| 2 | 尔尔 | 78.9 |
| 3 | 散散 | 55.5 |
| 4 | 思思 | 77.0 |
| 5 | 呜呜 | 56.7 |
| 6 | 柳柳 | 89.7 |
+------+--------+---------+
6 rows in set (0.00 sec)
mysql> select id,name,englist,math from exam_result;
+------+--------+---------+------+
| id | name | englist | math |
+------+--------+---------+------+
| 1 | 一一 | 96.5 | 63.7 |
| 2 | 尔尔 | 78.9 | 66.7 |
| 3 | 散散 | 55.5 | 96.7 |
| 4 | 思思 | 77.0 | 87.7 |
| 5 | 呜呜 | 56.7 | 49.7 |
| 6 | 柳柳 | 89.7 | 76.7 |
+------+--------+---------+------+
6 rows in set (0.00 sec)
mysql> select id,name,englist+math from exam_result;
+------+--------+--------------+
| id | name | englist+math |
+------+--------+--------------+
| 1 | 一一 | 160.2 |
| 2 | 尔尔 | 145.6 |
| 3 | 散散 | 152.2 |
| 4 | 思思 | 164.7 |
| 5 | 呜呜 | 106.4 |
| 6 | 柳柳 | 166.4 |
+------+--------+--------------+
6 rows in set (0.03 sec)
3.4别名
语法:select 要查询的列 别名 from 表名
mysql> select id,name,englist+math+chinese 总分 from exam_result;
+------+--------+--------+
| id | name | 总分 |
+------+--------+--------+
| 1 | 一一 | 238.7 |
| 2 | 尔尔 | 204.1 |
| 3 | 散散 | 228.7 |
| 4 | 思思 | 228.2 |
| 5 | 呜呜 | 193.9 |
| 6 | 柳柳 | 264.9 |
+------+--------+--------+
6 rows in set (0.02 sec)
mysql> select id,name 名字,englist 英语 from exam_result;
+------+--------+--------+
| id | 名字 | 英语 |
+------+--------+--------+
| 1 | 一一 | 96.5 |
| 2 | 尔尔 | 78.9 |
| 3 | 散散 | 55.5 |
| 4 | 思思 | 77.0 |
| 5 | 呜呜 | 56.7 |
| 6 | 柳柳 | 89.7 |
+------+--------+--------+
6 rows in set (0.00 sec)
3.5去重:distinct
语法:select distinct 需要去重的列 from 表名
mysql> select *from exam_result;
+------+--------+---------+------+---------+
| id | name | chinese | math | englist |
+------+--------+---------+------+---------+
| 1 | 一一 | 78.5 | 63.7 | 96.5 |
| 2 | 尔尔 | 58.5 | 66.7 | 78.9 |
| 3 | 散散 | 76.5 | 96.7 | 55.5 |
| 4 | 思思 | 63.5 | 87.7 | 77.0 |
| 5 | 呜呜 | 87.5 | 49.7 | 56.7 |
| 6 | 柳柳 | 98.5 | 76.7 | 89.7 |
| 1 | 琪琪 | 23.0 | 45.0 | 77.0 |
+------+--------+---------+------+---------+
7 rows in set (0.00 sec)
mysql> select distinct englist from exam_result;
+---------+
| englist |
+---------+
| 96.5 |
| 78.9 |
| 55.5 |
| 77.0 |
| 56.7 |
| 89.7 |
+---------+
6 rows in set (0.03 sec)
3.6排序:order by
语法:
asc 为升序(从小到大)
desc为降序(从大到小)
默认其为升序
使用表达式及别名排序的情况如下:
mysql> select name,chinese+math+englist total from exam_result
-> order by total asc;
+--------+-------+
| name | total |
+--------+-------+
| 琪琪 | 145.0 |
| 呜呜 | 193.9 |
| 尔尔 | 204.1 |
| 思思 | 228.2 |
| 散散 | 228.7 |
| 一一 | 238.7 |
| 柳柳 | 264.9 |
+--------+-------+
7 rows in set (0.02 sec)
也可以给多个字段进行排序,排序优先级按照书写顺序
mysql> select name,math,chinese from exam_result
-> order by math desc, chinese;
+--------+------+---------+
| name | math | chinese |
+--------+------+---------+
| 散散 | 96.7 | 76.5 |
| 思思 | 87.7 | 63.5 |
| 柳柳 | 76.7 | 98.5 |
| 尔尔 | 66.7 | 58.5 |
| 一一 | 63.7 | 78.5 |
| 呜呜 | 49.7 | 87.5 |
| 琪琪 | 45.0 | 23.0 |
+--------+------+---------+
7 rows in set (0.00 sec)
3.7条件查询:where
运算符 | 说明 |
---|---|
is null | 是null |
is not null | 不是null |
in(option,……) | 如果是option中的任意一个,返回true(1) |
like | %表示任意多个任意字符;_表示任意一个字符 |
1.where条件可以使用表达式,但不能使用别名
2.and的优先级高于or
- 基本查询
mysql> select name,englist from exam_result where englist < 90;
+--------+---------+
| name | englist |
+--------+---------+
| 尔尔 | 78.9 |
| 散散 | 55.5 |
| 思思 | 77.0 |
| 呜呜 | 56.7 |
| 柳柳 | 89.7 |
| 琪琪 | 77.0 |
+--------+---------+
6 rows in set (0.02 sec)
- and与or查询
mysql> select * from exam_result where chinese > 80 and math < 80;
+------+--------+---------+------+---------+
| id | name | chinese | math | englist |
+------+--------+---------+------+---------+
| 5 | 呜呜 | 87.5 | 49.7 | 56.7 |
| 6 | 柳柳 | 98.5 | 76.7 | 89.7 |
+------+--------+---------+------+---------+
2 rows in set (0.00 sec)
- 范围查询
1.between and 查询
mysql> select name,chinese from exam_result where chinese between 80 and 90;
+--------+---------+
| name | chinese |
+--------+---------+
| 呜呜 | 87.5 |
+--------+---------+
1 row in set (0.02 sec)
2.in查询
mysql> select name,englist from exam_result where englist in(11,77,55,66);
+--------+---------+
| name | englist |
+--------+---------+
| 思思 | 77.0 |
| 琪琪 | 77.0 |
+--------+---------+
2 rows in set (0.00 sec)
- 模糊查询:like
select name from exam_result where name like ’孙%’ - null的查询:is [not] null
select name,qq_mail from student where qq_mail is not null;
3.8分页查询:limit
语法:
select …… from 表名 order by …… limit s,n;
从s开始,筛选n条结果
mysql> select id,name,math from exam_result order by id limit 3,3;
+------+--------+------+
| id | name | math |
+------+--------+------+
| 3 | 散散 | 96.7 |
| 4 | 思思 | 87.7 |
| 5 | 呜呜 | 49.7 |
+------+--------+------+
3 rows in set (0.02 sec)
4.修改(Update)
语法:
update 表名 set 要修改的内容 = ……[where ……] [order by……][limit……]
mysql> update exam_result set math = 80 where name ='一一';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from exam_result;
+------+--------+---------+------+---------+
| id | name | chinese | math | englist |
+------+--------+---------+------+---------+
| 1 | 一一 | 78.5 | 80.0 | 96.5 |
| 2 | 尔尔 | 58.5 | 66.7 | 78.9 |
| 3 | 散散 | 76.5 | 96.7 | 55.5 |
| 4 | 思思 | 63.5 | 87.7 | 77.0 |
| 5 | 呜呜 | 87.5 | 49.7 | 56.7 |
| 6 | 柳柳 | 98.5 | 76.7 | 89.7 |
| 1 | 琪琪 | 23.0 | 45.0 | 77.0 |
+------+--------+---------+------+---------+
7 rows in set (0.00 sec)
5.删除(Delete)
语法:
delete from 表名 [where……] [order by……][limit……]
mysql> delete from exam_result where englist = 77;
Query OK, 2 rows affected (0.01 sec)
mysql> select * from exam_result;
+------+--------+---------+------+---------+
| id | name | chinese | math | englist |
+------+--------+---------+------+---------+
| 1 | 一一 | 78.5 | 80.0 | 96.5 |
| 2 | 尔尔 | 58.5 | 66.7 | 78.9 |
| 3 | 散散 | 76.5 | 96.7 | 55.5 |
| 5 | 呜呜 | 87.5 | 49.7 | 56.7 |
| 6 | 柳柳 | 98.5 | 76.7 | 89.7 |
+------+--------+---------+------+---------+
5 rows in set (0.00 sec)
6.内容重点汇总
- 新增
单行插入:
insert into 表名 (字段1,……字段n)values (value1,……value n)
多行插入:
insert into 表(字段1,……,字段n)values
(……),
(……),
(……);
- 查询
全列查询:
select * from 表名指定列查询:
select 字段1,字段2…… from 表名
别名查询:
select 字段1 别名1,字段2 别名2 from 表名;
去重distinct:
select distinct 字段 from 表名
排序order by :
select * from 表名 order by 排序字段
条件查询where:
比较运算符、between……and……、in、is null 、like、and、or
- 修改
update 表名 set 字段1 = value1,字段2 = value2 …… where 条件
- 删除
delete from 表 where 条件