联合查询
基本语句包括select 语句1 , union【union选项】 , select语句2。。。。
union选项 : all保留所有不管重复 distinct 去重 默认的
1 . select * from my_class
union --- 默认去重
select * from my_class
2. select * from my_class
union all --- 不去重
select * from my_class
3. select id , c_name,room from my_class
union all ---不去重
select name,number,id from my_student
select * from my_student order by sex asc,height desc
--需求:男生升序,女生降序(年龄) (select* from my_student where sex=' 男 ' order by age asc limit 999999) union (select * from my_student where sex=' 女 ' order by age desc limit 999999)
子查询(sub query)
1 按位置分类
from 子查询,
where子查询,
exists子查询 。 select * from my_student where exists(select * from my_class) ---是否成立
select * from my_student where exists(select * from my_class where id = 3)
select * from my_student where exist (select * from my_class where id = 2)
2 按结果分类
[1]标量 子查询(一行一列),
select * from my_class select * from my_student
select * from my_student where c_id=? select id from my_class where c_name = 'Python1910' ---id一定只有一个值(一行一列)
[2]列 子查询(一列多行),
select * from my_class select * from my_student where c_id in(select id from my_class)
{
1 。 =any等价于in --其中一个就可以 select * from my_student where c_id = any(select id from my_class) ---所有结果除了NULL除外
2。 any等价于some ---二者是一样的select * from my_student where c_id = some(select id from my_class) ---所有结果除了NULL除外
3 。 == all为全部 select * from my_student where c_id = all(select id from my_class) ---NULL除外
}
---查询年龄最大且身高最高 select * from my_student where age = (select max(age) from my_student) and height = (select max(age) from my_student)
[3]行 子查询(多列一行或多行多列),select * from my_student where --(age ,height)称为行元素 (age,height) = (select max (age),max(height) from my_student)
select * from my_student order by age desc,height desclimit 1 --可能查询结果不是预想的
[4]表 子查询(多行多列)
---插入学生 insert into my_student value(null,'bc200007','小红','23',''186)
--查找每个班身高最高的学生 select * from (select * from my_student order by height desc limit 999999 ) as student group by c_id -----每个班选出一个学生
视图(view)
定义:是一种有结构,但是没有结果的虚拟表
创建视图
基本语句 create view 视图名字 as select 语句;
创建单表视图 基表只有一个
创建多表视图 基表来源至少两个
--视图: 单表+ 多表
create view my_v1 as select * from my_student
create view my_v2 as select * from my_class
create view my_v3 as select * from my_student as s left join my_class as c on s.c_id -----错误, id重复
-----多表视图
create view my_v3 as select s.*,c.c_name,c.room from my_student as s join my_class as c on s.c_id
外链接 不符合条件不放入
内链接 符合条件放入
--查看视图创建语句 show create view my_v3\G
查看视图
show tables[like] / desc 视图名/ show create table 视图名
show create view my_v3\G --查看视图创建语句
show views 不可以v查看所有视图
show * from my_v1
show * from my_v2
show * from my_v3
修改视图
alter view 视图名字 as 新的select语句
alter view my_ v1 as select id,name,age,sex,height,c_id from my_student
删除视图
drop view 视图名字
create view my_v4 as select * from my_student ---删除视图v4 drop table my_v4 --删除失败(不能删表) drop view my_v4 - -- 删除成功
新增数据
{ 1 }多表视图不能新增数据
---多表视图不能插入数据 insert into my_v3 value(null,'bc20200008','张山','男',15,180,3,'Python1910','A204') -----插入失败
--将学生表的学号字段设置成不允许为空 alter tabel my_student modify number char(10) not null unique
{ 2 } 可以向单表视图插入数据,但是视图中包含的字段必须有基表中所不能为空,或没默认值的字段
---单表视图插入数据 : 视图不包含所有不允许为空的字段 insert into my_v1 values(null,'张山','男',15,180,3)
--单表视图插入数据 insert into my_v2 values(2,'Python1903','A204')
{ 3 } 视图是可以向基表插入数据的
删除数据
多表视图不能删除数据
单表视图可以删除数据
更新数据
更新限制 with check option
---视图 age 字段限制更新
create view my_v4 as
select * from my_student where age>30 with cheak option
--表示视图的数据来源都是年龄大于30岁 是由where age 》30 的
--将视图可以查到数据改成年龄小与30 update my_v4 set age = 29 where id = 5
---可以修改数据源让视图可以查到 可以该 但是无效果
uodate my_v4 set age = 32 where id=3