视图创建与管理实验

(一)在job数据库中,有聘任人员信息表:Worklnfo表,其表结构如下表所示:
在这里插入图片描述
create table workinfo(
id int(4) not null unique primary key,
name varchar(20) not null,
sex varchar(4) not null,
age int(4),
address varchar(50),
tel varchar(20));
其中表中练习数据如下:
1.‘张明’,‘男’,19,‘北京市朝阳区’,‘1234567’
2.‘李广’,‘男’,21,‘北京市昌平区’,‘2345678’
3.‘王丹’,‘女’,18,‘湖南省永州市’,‘3456789’
4.‘赵一枚’,‘女’,24,‘浙江宁波市’,‘4567890’
insert into workinfo
(id,name,sex,age,address,tel)
values(1001,‘张明’,‘男’,19,‘北京市朝阳区’,‘1234567’),
(1002,‘李广’,‘男’,21,‘北京市昌平区’,‘2345678’),
(1003,‘王丹’,‘女’,18,‘湖南省永州市’,‘3456789’),
(1004,‘赵一枚’,‘女’,24,‘浙江宁波市’,‘4567890’);
按照下列要求进行操作:
1.创建视图info_view,显示年龄大于20岁的聘任人员id,name,sex,address信息。
CREATE VIEW info_view(id,name,sex,address)AS SELECT id,name,sex,address FROM workinfo WHERE age>20 WITH LOCAL CHECK OPTION;
在这里插入图片描述
2.查看视图info_view的基本结构和详细结构。
查看基本结构:
DESC info_view;
在这里插入图片描述
查看详细结构:
SHOW CREATE VIEW info_view;
在这里插入图片描述
3.查看视图info_view的所有记录。
SELECT * FROM info_view;
在这里插入图片描述
4.修改视图info_view,满足年龄小于20岁的聘任人员id,name,sex,address信息。
ALTER VIEW info_view(id,name,sex,address) AS SELECT id,name,sex,address FROM work_info WHERE age<20 WITH LOCAL CHECK OPTION;

5.更新视图,将id号为3的聘任员的性别,由“男“改为“女”。
UPDATE info_view SET sex=‘女’ WHERE id=3;

6.删除info_view视图。
DROP VIEW info_view;

三、设计性试验
在学生管理系统中,有学生信息表studentinfo表,其表结构如下:
在这里插入图片描述
create table studentinfo(Number int(4) not null UNIQUE primary key,Name varchar(20) not null,Major varchar(20),age int(4));
请完成如下操作:
1.使用CREATE VIEW语句来创建视图college_view,显示studentinfo表中的number,name,age,major,并将字段名显示为:student_num,student_name,student_age,department。
create view college_view(student_num,student_name,student_age,department) as select number,name,age,major from studentinfo;

2.执行SHOW CREATE VIEW语句来查看视图的详细结构。
show create view college_view;
在这里插入图片描述
3.更新视图。向视图中插入如下3条记录:
0901,‘张三’,20,‘外语’
0902,‘李四’,22,‘计算机’
0903,‘王五’,19,‘计算机’
insert into college_view() values
(0901,‘张三’,20,‘外语’),
(0902,‘李四’,22,‘计算机’),
(0903,‘王五’,19,‘计算机’);

在这里插入图片描述
3.修改视图,使视图中只显示专业为“计算机”的信息。
Create or replace view college_view(student_num, student_name, student_age,department) as select number,name,age, major from studentinfo where major=‘计算机’ with local check option;
在这里插入图片描述
4.删除视图。
drop view college_view ;
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_55726741/article/details/129248528