外键默认的作用有两点:一个对父表,一个对子表(外键字段所在的表)
对子表约束:子表数据进行写操作(增和改)的时候,如果对应的外键字段在父表找不到对应的匹配,那么操作会失败(约束子表数据操作).
对父表约束:父表数据进行写操作(删和改:都必须涉及到主键本身),如果对应的主键在子表已经被数据引用,那么就不被允许操作.-- 插入数据:外键字段在父表中不存在insertinto my_foreign2 values(null,'张自忠',4);-- 没有4班级insertinto my_foreign2 values(null,'项羽',1);insertinto my_foreign2 values(null,'刘邦',2);insertinto my_foreign2 values(null,'韩信',2);-- 更新父表记录update my_class set id =4where id =1;-- 失败: id=1记录已经被学生引用update my_class set id =4where id =3;-- 可以: 没有引用
在联合查询中:orderby 不能直接使用,需要使用括号才行.-- 需求: 男生升序,女生降序(年龄)(order by 失效)(select*from my_student where sex ='男'orderby age asc)union(select*from my_student where sex ='女'orderby age desc);
若要orderby 成效:必须搭配limit:limit 使用限定的最大数即可.-- 需求: 男生升序,女生降序(年龄)(select*from my_student where sex ='男'orderby age asclimit9999999)union(select*from my_student where sex ='女'orderby age desclimit9999999);
需求:知道班级名字为PHP0710,想获取该班的所有学生.1.确定数据源:获取所有的学生.select*from my_student where c_id=?;2.获取班级id:可以通过班级名字确定.select id from my_class where c_name = ‘PHP0710’;-- id一定只有一个值(一行一列)-- 标量子查询select*from my_student where c_id =(select id from my_class where c_name ='PHP0710');select*from my_student having c_id =(select id from my_class where c_name ='PHP0710');
列子查询
需求:查询所有在读班级的学生(班级表中存在的班级)1.确定数据源:学生
select*from my_student where c_id in(?);2.确定有效班级的id: 所有班级id.select id from my_class;-- 列子查询select*from my_student where c_id in(select id from my_class);
列子查询返回的结果会比较: 一列多行, 需要使用in作为条件匹配: 其实在mysql中有还有几个类似的条件: all,some,any=any<=>in;-- 其中一个即可any<=>some;--any跟some是一样;=all;-- 为全部-- any,some,allselect*from my_student where c_id =any(select id from my_class);select*from my_student where c_id =some(select id from my_class);select*from my_student where c_id =all(select id from my_class);select*from my_student where c_id !=any(select id from my_class);-- 所有结果(null除外)select*from my_student where c_id !=some(select id from my_class);-- 所有结果(null除外)select*from my_student where c_id !=all(select id from my_class);-- 2(null除外)
行子查询
行子查询:返回结果可以是多行多列(一行多列);
需求:要求查询整个学生中,年龄最大且身高是最高的学生.1.确定数据源
select*from my_student where age = ? And height = ?;2.确定最大的年龄和最高的身高;selectmax(age),max(height)from my_student;-- 方法1select*from my_student where
age =(selectmax(age)from my_student)and
height =(selectmax(height)from my_student);-- 行子查询select*from my_student where-- (age,height)称之为行元素(age,height)=(selectmax(age),max(height)from my_student);
基本语法
createview 视图名字 asselect语句;-- select语句可以是普通查询;可以是连接查询;可以是联合查询;可以是子查询.
创建单表视图:基表只有一个.
创建多表视图:基表来源至少两个.-- 视图: 单表+多表createview my_v1 asselect*from my_student;createview my_v2 asselect*from my_class;-- 多表视图createview my_v3 asselect*from my_student as s leftjoin my_class c on s.c_id = c.id;-- id重复,创建失败.createview my_v3 asselect s.*,c.c_name,c.room from my_student as s
leftjoin my_class c
on s.c_id = c.id;
视图本身不可修改,但是视图的来源是可以修改的.
修改视图:修改视图本身的来源语句(select语句)
alter view 视图名字 as 新的select语句;
-- 修改视图
alter view my_v1 as
select id,name,age,sex,height,c_id from my_student;
1.多表视图不能删除数据
-- 多表视图删除数据
delete from my_v3 where id = 1;
2.单表视图可以删除数据
-- 单表视图删除数据
delete from my_v2 where id = 4;
更新数据
理论上不能单表视图还是多表示视图都可以更新数据.-- 多表视图更新数据update my_v3 set c_id =3where id =5;
更新限制: withcheckoption, 如果对视图在新增的时候,限定了某个字段有限制: 那么在对视图进行数据更新操作时,系统会进行验证: 要保证更新之后,数据依然可以被实体查询出来,否则不让更新.-- 视图: age字段限制更新createview my_v4 asselect*from my_student where age >30withcheckoption;-- 表示视图的数据来源都是年龄大于30岁:where age > 30决定-- with check option: 决定通过视图更新的时候,不能将已经得到的数据age > 30的改成小于30的-- 将视图可以查到的数据改成小于30update my_v4 set age =29where id =1;-- 可以修改数据让视图可以查到: 可以改,但是无效果update my_v4 set age =32where id =6;