MYSQL视图

本文详细介绍了数据库中视图的创建、查看、修改、查询、更新及删除操作,包括使用WITH CHECK OPTION约束、ALTER VIEW修改视图定义、通过INSERT、UPDATE、DELETE操作视图数据以及DROP VIEW删除视图的方法。

视图的创建

create view stud as select stuid,stuname,stuschool from stu;
select * from stud;
create or replace view stu_view as select * from stu where stubirth>='1998-01-01' with check option;
create view stu_view as select * from stu where stubirth>='1997-01-01' with check option;
create view course_sc as select courseid ,avg(score) as avg_sc from score group by courseid;
create or replace view stu_sc as select stu.stuid,stuname,courseid,score from score join stu on stu.stuid=score.stuid order by score desc;

视图结构的查看与修改

1.查看视图结构

show create view stu_sc;

2.修改视图结构

alter view stu_view as select stuid,stuname,stusex,stubirth from stu where stubirth>='1998-01-01' with check option;
alter view stud as select stuid,stuname,stusex as '性别',stuschool from stu;

3.视图的查询与更新

select stusex,count(*) as number from stu_view group by stusex;
select * from course_sc where courseid regexp '^E';
select stuname,courseid,score from stu_sc where score>=85;

4.视图数据的更新
(1).数据的插入

insert into stu_view values('20160511020','方琼','女','1998-02-20');
insert into stu_view values('20160511021','刘一','男','1664-03-23');
insert into stu values('20160211070','李明','男','1998-01-01','计算机学院');

(2).数据的修改

update stu_view set stuid='20160511022' where stuname='方琼';
update course_sc set courseid='E2201290' where courseid = 'E2201240';

(3).数据的删除

delete from stu_sc where stuname='王小强';
delete from stu_view where stuname='方琼';

视图的删除

drop view abc;
drop view if exists abc;
drop view stu_sc,stu_view;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值