根据如下需求,定义视图
1、为了保证数据库表的安全性,开发人员在操作tb_user表时,只能看到的用户的基本手段,屏蔽手机号和邮箱两个字段
create view tb_user_view as select id,name,profession,age,gender,status,createtime from tb_user;
select * from tb_user_view;#验证
2、查询每个学生所选修的课程(三张表联查),这个功能在很多的业务中都有使用到,为了简化操作,定义一个视图
xxxxxxxxxx
select s.name as student_name,s.no as student_no,c.name as course_name from student as s,student_course as sc,course c where
s.id = sc.studentid and sc.courseid = c.id;#多表联查
create view tb_stu_course_view as select s.name as student_name,s.no as student_no,c.name as course_name from
student as s,student_course as sc,course c where s.id = sc.studentid and sc.courseid = c.id;#视图
#优点:后续不需要每次都写这条多表联查的SQL,我们只需要查看视图就可以了,如下
select * from tb_stu_course_view;#验证
视图的优点: 视图相当于封装查询语句,简化了操作,跟用函数一样