20220418
DQL
样例表
create table if not exists tb_users(
id bigint primary key auto_increment,
username varchar(20) not null,
password varchar(24) not null,
birth date,
sex boolean default 1,
score numeric(8,2)
)engine = innodb default charset utf8;
语法:
select [all/distinct] <目标表达式>,<目标表达式>,...
from 表名称或者视图名称,表名称或者视图名称,... -- 在MySQL中允许查询语句中没有from
where 条件语句
group by 分组条件 [having 条件表达式]
order by 排序条件 [asc/desc]
limit ...
1.查看表中的所有数据select * from tb_users
,这里的星号*是个通配符,标识获取所有列的值
在查询语句中使用*代表获取表中的所有列,如果只获取其中的一部分列,例如只查询用户的名称和口令,可以写成
select username,password from tb_users
select *
,在阿里的开发规范中强制规则,不允许使用星号,要求指明需要查询的列名称例如
select id,username,password,birth,sex,score from tb_users
使用星号会增加查询分析器的解析成本
如果使用MyBatis之类的持久层框架,增删字段容易导致操作匹配不一致问题
查村无用的字段会增加网络消耗,尤其是针对text/blob之类的大对象字段
使用星号可能会失去优化器中覆盖缩影策略优化
使用星号的优势
在一定程度上减少了编码量
不需要知道表中的字段名称,可以避免书写具体字段名称的繁琐和笔误
增加新字段时,不需要修改select代码,在一定程度上提高了代码的复用率
2.带条件查询。
例如查询表中所有年龄大于18岁的所有用户信息select * from tb_users where year(now()) - year(birth) > 18
例如查看表中id值大于等于5的所有用户信息select * from tb_users where id >= 5;
-
注意等值判断使用=,不是==。例如查看编号位16的用户信息
select * from tb_users where id=16;
等于=,不等于!= ,大于>,小于<,大于等于>=、小于等于<=
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2022-04-18 17:01:26 |
+---------------------+
1 row in set (0.00 sec)
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| 2022 |
+-------------+
1 row in set (0.00 sec)
3.非空或者控制判断,注意不能使用=null或者不等于!= null的写法,应该使用is null或者is not null
例如查询性别未知的用户信息select * from tb_users where sex is null;
4.字符串比较
例如用户登录select * from tb_users where username='yan1' and password='123456';
模糊查询 关键字like/not like 通配符_和%
-
通配符_表示一个任意字符
-
通配符%表示任意个任意字符
例如查询所有姓张的用户信息select * from tb_users where username like '张%';
通配符_和 % 可以是在字符串的任意位置
-
y_表示包含两个字符长的字符串,必须y开头
-
y%表示已y开头,后面跟任意个字符的字符串
5.选择表中的若干元组
元组就是事物特征的组合,可以用于描述一个具体的事物
关系就是一个带有表头的二维表格,表中的每行数据就是一个元组,元组的集合就是关系,每个列就是一个属性。再二维表中元组也可以称为行,属性为列
字段:每个事物的一个特征,也可以称为属性或者列
表:记录属性的组合,表示同一类事物的组合
主键:能够唯一标识一行数据的列组合。一个表只能有一个主键,但是主键可以是由多个列构成
域:属性的取值范围
关系模式:就是关系的描述,表示方法【关系名(属性列表,....)】,例如学生(学号,姓名,性别,年龄)
关系的约束:实体完整性、参照完整性、用户自定义完整性
写法:select * from tb_users where (id,name) > (1,'aaa');
select * from tb_users where id > 1 and name > 'aaa';
在使用条件计算时可以使用and与,or或,not非运算
练习题:
学生表,其中包括学号、姓名、性别、年龄和系别
create table if not exists tb_student(
sno bigint primary key auto_increment,
sname varchar(10) not null comment '姓名',
sex boolean default 1 comment '性别',
age tinyint default 18 comment '年龄',
depte varchar(20) default '计算机系' comment '系别'
)engine=innodb default charset utf8;
插入测试数据
-- 这里由于粗心,列名称本来时dept,现在写成了 depte
alter table tb_student change depte dept varchar(20) default '计算机系' comment '系别';
insert into tb_student(sname,age) values('赵小胖',20),('张毅',18),('东方',19);
update tb_student set sex=0 where sno=3;
update tb_student set dept='数学计算机系';
-- 多插入的学生信息【赵双井】
insert into tb_student(sname,age) values('赵双井',20);
-- 需要删除姓名为【赵双井】
delete from tb_student where sname='赵双井';
1:查询全体学生的学号和姓名
select sno,sname from tb_student;
默认显示时列名称就是显示的名称,实际上允许进行修改
select sno as 学号, sname 姓名 from tb_student;
2:查询全体学生的姓名、学号、所在系
select sname,sno,dept from tb_student;
表中任意两行交换位置不影响表达的数据,表中任意两列交换位置不影响表达的信息。具体的业务顺序的概念来自于编号,而不是实际存放位置
3:查询全体学生的详细记录和对应的年龄
select * from tb_student;
-
在具体应用中一般不会直接存储年龄,而是记录生日,通过year(now())-year(birth) as age
4:查询年龄大于20的学生
select * from tb_student where age>20;
比较运算符>大于 <小于 >=大于等于 <=小于等于 =等于 !=不等于
5:查询年龄小于20的所有女生信息
select * from tb_student where age<20 and sex=0;
-
允许使用and、or连接多个条件
6:获取计算机系或者姓张的学生信息
select * from tb_student where dept='计算机' or sname like '张%';
7:查询不是女生的所有学生信息
select * from tb_student where not (sex=0);
-
not是非运算
导入导出操作
将文本数据传入到数据库 文本文件的格式为【字段之间的数据采用tab键分割,一行对应一行数据】
3 张三 1898-4-23 4 李四 2012-3-6
创建对应的表,表结构应该对应文本文件中的数据
create table tb_users(
id int primary key,
name varchar(20) not null,
birth date,
)engine = innodb default charset utf8;
导入语句,由于是在sql中直接进行读取硬盘文件,会涉及安全性配置问题
load date infile "d:/users.txt" into table tb_users(id,name,birth);