看这篇文章的老铁们,点下赞,点下关注。
感恩点赞,感恩关注
2020年2月16日下午。嗯继续宅在家。明个早上继续去当志愿者咯。
闲话少叙,进入正题
多表查询
笛卡尔积的形式
内连接的形式 查询两个表中符合条件的记录
select 字段名
from tbl_name1 INNER JOIN tbl_name2 on 连接条件
外连接的形式
左外连接
select 字段名
from tbl_name1 LEFT OUTER JOIN tbl_name2 on 连接条件
先去查询左边表的全部记录。再去右表中查询符合条件的记录,不符合的用null 代替
右外连接
select 字段名
from tbl_name1 RIGHT OUTER JOIN tbl_name2 on 连接条件
先去查询右边表的全部记录。再去左表中查询符合条件的记录,不符合的用null 代替
外键约束
只有InnoDB存储引擎支持外键
建表时指定外键 [constraint 外键名称] foreign key(字段名) references 主表(字段名)
子表的外键字段和主表的关键字段类型要相似
若是数值型则要求一致而且无符号也要一致
若是字符型,要求类型一致,长度可以不同
若外键字段没有创建索引,MySQL会自动帮我们创建索引
子表的外键关联的必须是父表的主键
外键约束的参照操作 CASCADE 从附表删除或更新,子表也跟着删除或更新级联的操作
set null 从附表删除或更新记录,并设置子表的外键列为null
no action |restrict 拒绝对附表做更新或者删除操作
动态指定外键
动态添加外键
alter table tbl_name add [constraint 外键名称] foreign key(外键字段)references 主表(主键字段);
动态添加外键之前表中的记录一定合法的记录,没有脏值,否则添加不成功
动态删除外键 alter table tbl_name drop foreign key fk_name;
特殊形式的查询
子查询 select tbl_name where col_name=(select col_name from tbl_name)
内层语句查询的结果可以作为外层语句查询的条件
由in引发的子查询
由比较运算符引出的子查询
由exists引发的子查询,子查询是真则执行前面的查询,否则不执行
any some all 用来代替比较运算符
any some all
> >= 最小值 最小值 最大值
< <= 最大值 最大值 最小值
= 任意值 任意值
!= <> 任意值
insert ... select ..
create...select...
create table tbl_name like tbl_name;
联合查询 union select 字段名称...from tbl_name1
union
select 字段名称...from tbl_name2
去重合并
union all select 字段名称...from tbl_name1
union all
select 字段名称...from tbl_name2
简单的合并
自身连接查询
无限极分类的实现形式
测试代码
--查询 emp id username age 部门名称 dep depname
select emp.id ,emp.username,emp.age,dep.depname
from emp,dep;
select emp.id ,emp.username,emp.age,dep.depname
from emp,dep
where emp.id=dep.id;
--测试内连接
select emp.id ,emp.username,emp.age,dep.depname
from emp inner join dep on emp.id=dep.id;
--测试左外连接
select emp.id ,emp.username,emp.age,dep.depname
from emp left outer join dep on emp.id=dep.id;
select emp.id ,emp.username,emp.age,dep.depname
from emp left join dep on emp.id=dep.id;
--测试右外连接
select emp.id ,emp.username,emp.age,dep.depname
from emp right outer join dep on emp.id=dep.id;
create table provinces(
id TINYINT UNSIGNED AUTO_INCREMENT key,
proName varchar(10) not null unique
);
insert provinces(proName) values('北京'),('上海'),('深圳');
create table user(
id TINYINT UNSIGNED AUTO_INCREMENT key,
username varchar(20) not null unique,
email varchar(50) not null default '[email protected]',
proId TINYINT UNSIGNED not null
);
insert user(username,proId) values('a',1),('b',1),('c',1),('d',2),('e',3),('f',1),('g',1);
create table cate(
id TINYINT UNSIGNED AUTO_INCREMENT key,
cateName varchar(50) not null unique,
cateDesc varchar(100) not null default ''
);
insert product(cateName) values('母婴'),('服装'),('电子');
create table products(
id Int unsigned auto_increment key,
productName varchar(50) not null unique,
price Float(8,2) not null default 0,
cateId tinyInt unsigned not null,
adminId tinyint unsigned not null
);
insert products(productName,price,cateId,adminId) values('iphone',999,3,1),
('adidas',399,2,2),('nike',888,2,2),('奶嘴',29,1,1);
--查询products id productname price---cate catename
select p.id,p.productname,p.price,c.catename
from products as p join cate as c
on p.cateid=c.id;
--查询products id,productname price
--cate catename
--user username email
select p.id,p.productname,p.price,c.catename,u.username,u.email
from products as p join cate as c on p.cateId=c.id
join user as u on u.id=p.adminId
where p.price<900;
create Table news_cate(
id TINYINT UNSIGNED AUTO_INCREMENT key,
cateName varchar(50) not null unique,
cateDesc varchar(100) not null default ''
);
insert news_cate(cateName) values('国内新闻'),('国际新闻'),('娱乐新闻'),('体育新闻');
create table news(
id int unsigned auto_increment key,
title varchar(100) not null unique,
content varchar(1000) not null,
cateId tinyint unsigned not null
);
insert news(title,content,cateId) values('a1','1111111',1),
('a2','22222',1),('a3','aaa3',4),('a4','aaaa4',2),('a5','aaaa5',3);
--查询news id title content
--news_cate cateName
select n.id,n.title,n.content ,nc.cateName
from news as n join news_cate as nc on n.cateId=nc.id;
delete from news_cate where id=2;
Insert news(title,content,cateId) values('a6','66666',123);
--添加外键
create Table news_cate(
id TINYINT UNSIGNED AUTO_INCREMENT key,
cateName varchar(50) not null unique,
cateDesc varchar(100) not null default ''
);
create table news(
id int unsigned auto_increment key,
title varchar(100) not null unique,
content varchar(1000) not null,
cateId tinyint unsigned not null,
foreign key(cateId) references news_cate(id)
);
--测试非法记录
Insert news(title,content,cateId) values('a6','66666',123);
--测试删除父表记录和删除父表
delete from news_cate where id=1;
update news_cate set id=90 where id=1;
insert news_cate(cateName) values('教育新闻');
--将教育新闻改为教育
Update news_cate set cateName= '教新闻' where id=5;
--添加外键名称
create Table news_cate(
id TINYINT UNSIGNED AUTO_INCREMENT key,
cateName varchar(50) not null unique,
cateDesc varchar(100) not null default ''
);
create table news(
id int unsigned auto_increment key,
title varchar(100) not null unique,
content varchar(1000) not null,
cateId tinyint unsigned not null,
constraint cateId_fk_newsCate foreign key(cateId) references news_cate(id)
);
--删除外键
alter table news
drop foreign key cateId_fk_newsCate;
alter table news
drop foreign key news_ibfk_1;
--添加外键
alter table news
add foreign key(cateId) references news_cate(id);
--指定级联操作
alter table news
add foreign key(cateId) references new_cate(id)
on delete cascade on update cascade;
--测试子查询
--测试由in引发的子查询
select *
from user
where proId in(select id from provinces);
select *
from user
where proId not in(select id from provinces);
-- 学员stu
CREATE TABLE stu(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL UNIQUE,
score TINYINT UNSIGNED NOT NULL
);
INSERT stu(username,score) VALUES('king',95),
('queen',75),
('zhangsan',69),
('lisi',78),
('wangwu',87),
('zhaoliu',88),
('tianqi',98),
('ceshi',99),
('tiancai',50);
-- 分数级别level
CREATE TABLE level(
id tinyint UNSIGNED AUTO_INCREMENT KEY,
score TINYINT UNSIGNED COMMENT '分数'
);
INSERT level(score) VALUES(90),(80),(70);
--查询出成绩优秀的学员
select id,username,score
from stu
where score>=(select score from level where id=1);
--查询没有评级的学员
select id,username,score
from stu
where score<=(select score from level where id=3);
--由exists引发的子查询
select * from user where exists(select id from provinces where id=9);
select * from user where exists(select id from provinces where id=2);
--带有any some all的子查询
select *
from stu
where score>=any(select score from level);
select *
from stu
where score>=some(select score from level);
select *
from stu
where score>=all(select score from level);
--创建一个user1表
create table user1(
id int unsigned auto_increment key,
username varchar(20)
)select id,username from user;
--将stu表中名字插入到user1
insert user1(username) select username from stu;
create table user2 like user1;
insert user2 set username=(select username from stu where id=2);
--去掉字段的重复值
select distinct(username)from user2;
--将user1和user2数据合并到一起
select * from user1
union
select * from user2;
-- 测试自身连接
CREATE TABLE cate(
id SMALLINT UNSIGNED AUTO_INCREMENT KEY,
cateName VARCHAR(100) NOT NULL UNIQUE,
pId SMALLINT UNSIGNED NOT NULL DEFAULT 0
);
INSERT cate(cateName,pId) VALUES('服装',0);
INSERT cate(cateName,pId) VALUES('数码',0);
INSERT cate(cateName,pId) VALUES('箱包',0);
INSERT cate(cateName,pId) VALUES('男装',1);
INSERT cate(cateName,pId) VALUES('女装',1);
INSERT cate(cateName,pId) VALUES('内衣',1);
INSERT cate(cateName,pId) VALUES('电视',2);
INSERT cate(cateName,pId) VALUES('冰箱',2);
INSERT cate(cateName,pId) VALUES('洗衣机',2);
INSERT cate(cateName,pId) VALUES('爱马仕',3);
INSERT cate(cateName,pId) VALUES('LV',3);
INSERT cate(cateName,pId) VALUES('GUCCI',3);
INSERT cate(cateName,pId) VALUES('夹克',4);
INSERT cate(cateName,pId) VALUES('衬衫',4);
INSERT cate(cateName,pId) VALUES('裤子',4);
INSERT cate(cateName,pId) VALUES('液晶电视',10);
INSERT cate(cateName,pId) VALUES('等离子电视',10);
INSERT cate(cateName,pId) VALUES('背投电视',10);
--查询所有的分类包括父分类
select s.id,s.cateName as sCateName,p.cateName as pCateName
from cate as s
left join cate as p
on s.pid=p.id;
--查询所有分类包括其子分类
select p.cateName as pCateName,s.id,s.cateName as sCateName
from cate as s
right join cate as p
on s.pid=p.id;
--查询子分类的数目
select p.cateName,count(s.cateName)
from cate as s
right join cate as p
on s.pid=p.id
group by p.cateName;
最后宣传下我个人的微信公众号,微信搜索:可及的小屋,有志向整副业,娱乐的程序员们,欢迎您的到来。谢谢。
100G程序员资料,自取哦!!