回顾
mysql -uroot -p123456显示数据库show databases;create database java2204use java2204create table person(id int,name varchar(32));表里插入数据insert into peroson values(1,"朱志伟");删除数据delete from person where id = 1;修改数据update person ser name = "博儿" where id = 1;alter 修改
一、MySQL查询
在开发中,查询比增删改都重要
网站页面显示就是查询,所以查询偏多,查询将是一个重点
1.查询【重点】别名
select语句用于从表中查询选取的数据
结果被储存在一个结果表中,称为结果集。
语法格式:select 列明 from 表名; 查询指定字段的数据
或者:select * from 表明; 查询所有字段的数据
#select 列明 from 表名;select id,name from person;#按照字段进行查询的时候,还可以对字段起别名#select 字段1 as "别名1",字段2 as "别名2" from 表明;select id as "编号",name as "名字" from person;
where子句
运算符
如需有条件的从表中选取数据,可将where子句添加到select语句
语法格式:
select 列明 from 表明 where 列名 运算符 值;运算符:= > >= < <= !=(<>) &&(and) ||(or)
案例
#查询年龄等于24岁的;select * from person where age =24;#查询年龄大于30岁的select * from person where age > 30;#查询年龄不等于50的select * from person where age != 50;#查询年龄等于23岁的且性别等于0的select * from person where age = 23 && grnder = 0;select * from person where age = 23 and grnder = 0;#查询年龄等于23岁的或者年龄等于50的select * from person where age = 23 || age = 50;select * from person where age = 23 or age = 50;
排序order by
order by 只能放在where后面
#按照薪资排序,默认是升序 asc省略了没写select * from person order by salary#降序select * from person order by salary desc;#年龄小于40岁的,然后降序排列select * from person where id < 40 order by age desc;#先按照薪资升序排列,如果薪资一样,在按年龄降序排列select * from person order by salary asc,age desc;
限制limit
分页的时候需要使用,限制输出 只能放在where后面
语法格式
limit 数字1limit 数字1,数字2
案例
#输出打印3条数据select * from person limit 3;#略过3个数据(从第四个开始),展示三条数据select * from person limit 3,3;#查找年龄小于40的,打印出来三个select * from person where age < 40 limit 3;
分页
limit在做分页的时候有规律 偏移量当前页数 pageNo = 1 pageSize = 3limit (pageNo-1)*pageSize,pageSize;
2.SQL一些内置函数(看官方手册)
#SQL的内置函数#一般不建议使用,#语法格式:select sql函数 from person;#获取年龄这一列的最大值 maxselect max(age) from person;#平均值 avgselect avg(salary) from person;#求和 sumselect sum(salary) from person;#统计数据库中有多少条数据 count(*) null不算数据select count(*) from person;select count(age) from person;#获取当前时间 nowselect now() from person;
SQL语句的嵌套
#找出年龄最大的这条数据 方式1 如果最大年龄的不是一个人 只打印一个,有漏洞select * from person order by age desc limit 1;#找出年龄最大的这条数据 方式2#一个sql语句的结果当成另一个sql语句的条件select * from person where age = (select max(age) from person);
模糊查询like
大量查询的时候使用。
关键字 like
语法格式
select * from 表明 where 字段 like 模糊的语法格式
模糊的语法格式:
%或者_
案例
select * from person where name like "朱_";#打印朱航select * from person where name like "朱__";#打印朱志伟#一个下划线是一个占位select * from person where name like "_小_";#用%%,只要包含朱的select * from person where name like "%朱%";#最后一个字是天的,前面随意select * from person where name like "%天";#第一个字是朱的,后面随意select * from person where name like "朱%";
分组统计 group by
group by
#按照性别进行分组统计#注意事项 select和from中间必须写要分组统计的字段(SQL内置函数得到的结果)select gender from person group by gender;+--------+| grnder |+--------+| NULL || 1 |+--------+#统计出 null有几个 1有几个。select gender,count(*) from person group by gender;+--------+----------+| grnder | count(*) |+--------+----------+| NULL | 3 || 1 | 7 |+--------+----------+#按照性别进行分组,统计个数大于6的性别组#用关键字having#错误形式如下:select gender,count(*) from person where count(*) > 6 group by gender; #错误的#注意事项:where在分组之前执行,分组以后的条件在进行操作用havingselect gender,count(*) from person group by gender having count(*) > 6;+--------+----------+| grnder | count(*) |+--------+----------+| 1 | 7 |+--------+----------+#找出年龄大于20的信息,然后按照性别分组,统计结果个数大于3的数据select gender,count(*) from person where age > 20 group by gender having count(*) > 3;+--------+----------+| grnder | count(*) |+--------+----------+| 1 | 5 |+--------+----------+
需要自己练习的:
distinct 去重
#去除重复的idselect distinct id from person;
between 取介于两个值之间范围的值
select * from person where age between 20 and 25;
二、数据的约束问题
在创建表的时候,咱们一定要对字段进行设置约束,这样咱们在插入数据的时候,才是比较规范的。
所以数据约束是创建表的时候都i字段进行约束的。
一个完整的数据表必须加约束
2.1默认值 default
default
#用户在插入数据的时候,如果没有给当前字段赋值,会给一个默认值create table person1(-> id int,-> name varchar(25),-> country char(20) default "PRC"); #默认值PRC
2.2非空 not null
如果给字段设置了非空,在添加数据的时候,就必须给当前字段赋值
create table person2(-> id int not null, #必须进行赋值-> name varchar(32));
2.3唯一 unique
设置字段的值是唯一的,如果添加数据的时候数据重复的话会报错
空不算重复,可以为空
unique
create table person3(-> id int,-> name varchar(25) unique); #唯一字段#案例mysql> insert into person3 values(1,"朱志伟");Query OK, 1 row affected (0.40 sec)mysql> insert into person3 values(2,"朱志伟");ERROR 1062 (23000): Duplicate entry '朱志伟' for key 'name'
2.4主键【重点】primary key
主键是唯一和非空的组合。
在设计数据库的时候,一定要有一个字段是主键,而且主键的字段一般和业务逻辑无关,比如name和age不能设置为主键,一般id作为主键
primary key
create table person4(-> id int primary key,-> name varchar(32));
2.5自增长auto_increment
在插入数据的时候,有些字段是可以时间自增长的,自己增加1
auto_increment 一般开发中,主键设置为自增长
加入了自增长的,就不用给赋值了,默认从1开始,每加一个数据 自增1
在删除最后一条数据的时候,然后在插入一条时,这条数据的还是在原来基础上增加1的
如果设置了主键值,下一条自动从设置的主键值开始自增
create table person5(-> id int primary key auto_increment,-> name varchar(32));
完整的表创建方式
text 不能设置默认值
create table person6(-> id int primary key auto_increment,-> name varchar(32) not null,-> age int not null,-> info text);
三、外键的约束
一个表中的某一个字段是另一个表中的主键
案例
#创建一个员工表 id 员工名 部门 时间mysql> create table employee(-> id int primary key auto_increment,-> empName varchar(32) not null,-> deptName varchar(32) not null,-> regTime timestamp default current_timestamp-> );Query OK, 0 rows affected (0.64 sec)mysql> insert into employee(empName,deptName) values("谷小天","董事长");Query OK, 1 row affected (0.45 sec)mysql> insert into employee(empName,deptName) values("朱航","大股东");Query OK, 1 row affected (0.05 sec)mysql> insert into employee(empName,deptName) values("朱志伟","打工仔");Query OK, 1 row affected (0.13 sec)mysql> insert into employee(empName,deptName) values("张桂园","技术部");Query OK, 1 row affected (0.20 sec)mysql> insert into employee(empName,deptName) values("王梦哲","技术部");Query OK, 1 row affected (0.04 sec)mysql> insert into employee(empName,deptName) values("刘子贤","大神");Query OK, 1 row affected (0.13 sec)mysql> select * from employee;+----+-----------+-----------+---------------------+| id | empName | deptName | regTime |+----+-----------+-----------+---------------------+| 1 | 谷小天 | 董事长 | 2022-04-24 15:36:12 || 2 | 朱航 | 大股东 | 2022-04-24 15:36:23 || 3 | 朱志伟 | 打工仔 | 2022-04-24 15:36:32 || 4 | 张桂园 | 技术部 | 2022-04-24 15:37:09 || 5 | 王梦哲 | 技术部 | 2022-04-24 15:37:20 || 6 | 刘子贤 | 大神 | 2022-04-24 15:37:35 |+----+-----------+-----------+---------------------+6 rows in set (0.00 sec)
部门数据会出现冗余,
所以要建两个表,并且设计外键的约束
完善后的案例
#部门表create table dept(-> id int primary key auto_increment,-> deptName varchar(32) not null);#数据表create table employee(-> id int primary key auto_increment,-> empName varchar(32) not null,-> deptId int not null,-> regTime timestamp default current_timestamp,#constraint 约束的意思#fk_emp_dept 外键的名字可以随便起,一般fk_一张表名_另一张表名#foreign key(deptId) 外键 本表中的一个字段,去关联另个表的字段#references dept(id) 关联的另一个表中的字段-> constraint fk_emp_dept foreign key(deptId) references dept(id)-> );#插入数据insert into employee(empName,deptId) values("谷小天",1);#如果插入一个部门id中没有的数据 会报错 没有部门8insert into employee(empName,deptId) values("朱志伟",8);ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`java2204`.`employee`, CONSTRAINT `fk_emp_dept` FOREIGN KEY (`deptId`) REFERENCES `dept` (`id`))
问题1
关联以后,增加数据和删除数据容易报错
总结:
如果两张表添加了外键约束,这两张表就有关系了,添加数据:先添加主表【部门表】,在添加从表【员工表】删除数据:先删除从表【员工表】,在删除主表【部门表】修改数据:先修改从表【员工表】,在修改主表【部门表】
级联操作
on delete cascade
on update cascade
可以直接对主表和从表的内容进行修改,没有限制
mysql> create table employee(-> id int primary key auto_increment,-> empName varchar(32) not null,-> deptId int not null,-> regTime timestamp default current_timestamp,-> constraint fk_emp_dept foreign key(deptId) references dept(id)-> on delete cascade-> on update cascade-> );Query OK, 0 rows affected (0.32 sec)mysql> insert into employee(empName,deptId) values("谷小天",1);Query OK, 1 row affected (0.04 sec)mysql> insert into employee(empName,deptId) values("朱航",2);Query OK, 1 row affected (0.08 sec)mysql> insert into employee(empName,deptId) values("刘子贤",4);Query OK, 1 row affected (0.06 sec)mysql> insert into employee(empName,deptId) values("朱志伟",5);Query OK, 1 row affected (0.04 sec)mysql> select * from employee;+----+-----------+--------+---------------------+| id | empName | deptId | regTime |+----+-----------+--------+---------------------+| 1 | 谷小天 | 1 | 2022-04-24 16:25:14 || 2 | 朱航 | 2 | 2022-04-24 16:25:16 || 3 | 刘子贤 | 4 | 2022-04-24 16:25:18 || 4 | 朱志伟 | 5 | 2022-04-24 16:25:20 |+----+-----------+--------+---------------------+4 rows in set (0.00 sec)mysql> delete from dept where id = 4;Query OK, 1 row affected (0.13 sec)mysql> select * from employee;+----+-----------+--------+---------------------+| id | empName | deptId | regTime |+----+-----------+--------+---------------------+| 1 | 谷小天 | 1 | 2022-04-24 16:25:14 || 2 | 朱航 | 2 | 2022-04-24 16:25:16 || 4 | 朱志伟 | 5 | 2022-04-24 16:25:20 |+----+-----------+--------+---------------------+3 rows in set (0.00 sec)
重点 级联操作
#部门表 主表create table dept(-> id int primary key auto_increment,-> deptName varchar(32) not null);#员工表 从表mysql> create table employee(-> id int primary key auto_increment,-> empName varchar(32) not null,-> deptId int not null,-> regTime timestamp default current_timestamp,-> constraint fk_emp_dept foreign key(deptId) references dept(id)-> on delete cascade-> on update cascade-> );#修改主表和从表内容不受限制,主表内容变化,从表也会跟随变化
