数据库--视图操作

给出xxgl数据库以及学生表、课程表和选课表。进行以下查询:

Student(sno char(12),sname char(10),ssex char(2),sage tinyint,sdept nchar(20))

Course(cno char(3),cname nchar(20), Tname varchar(20),credit tinyint)

Sc(sno char(12),cno char(3),grade tinyint)

(1)建立计算机系学生的视图jsj_student,列出学号、姓名和年龄。

(2)建立选修了C3号课程的学生的视图s_sc_C3,列出学号、姓名、年龄和系别。

(3)定义一个查询学生学号、姓名、出生年份的视图s_birthday。

(4)按民族统计学生人数定义为一个视图s_p。

(5)在s_Age视图中找出年龄为20学生人数。

(6)将计算机系学生视图jsj_student中学号为S1的学生年龄改为19。

(7)创建查询每个学生的选课门数,要求列出学生的学号和选课门数的视图s_m。

(8)删除jsj_student视图。

(9)将所有学生的年龄增加1岁。

(10)删除学号为S4的学生的记录。

(11)删除所有学生的选课记录。

student表

sno

sname

ssex

sage

snat

sdept

S1

赵无言

18

汉族

计算机系

S2

蒋洪

19

回族

通信系

S3

汪艳

18

汉族

自动化

S4

张拟

18

汉族

通信系

S5

孙瑶

19

汉族

电子系

S6

张军军

20

回族

计算机系

course表

cno

cname

credit

001

C语言程序设计

2

002

高数

3

003

大学英语

2

004

计算机网络

3

005

数据库原理

2

sc表

sno

cno

grade

S1

001

80

S1

003

75

S2

002

54

S2

003

90

S3

002

70

S3

003

30

主要代码如下:

create database JXGL1
on 
(name=JXGL,
filename='D:\sql\JXGL1.mdf',//选择自己的文件保存位置
size=10MB,
maxsize=30MB,
filegrowth=5MB)
log on
(name=xxgl_log,
filename='D:\sql\JXGL1_log.ldf',
size=4MB,
maxsize=10MB,
filegrowth=2MB)
use JXGL1
go
create table S(sno char(12)primary key,
sname char(10),
sex char(2),
age tinyint,
sdept nchar(20)
)
create table C
(cno char(3)primary key,
   cname nchar(20),
   Tname varchar(20),
   credit tinyint
   )
create table  SC
(sno char(12)references S(sno),
cno char(3)references C(cno),
grade float,
primary key(sno,cno),
)
insert into S values
('S1','程晓晴','女',21,'CS'),
('S2','吴玉江','男',20,'CS'),
('S3','姜云','女',18,'CS'),
('S4','张峰','男',19,'CS'),
('S5','张丽丽','女',21,'MA'),
('S6','李文','女',25,'MA'),
('S7','李文远','女',19,'MA'),
('S8','张峰名','男',20,'IS'),
('S9','王大力','男',21,'IS'),
('S10','张姗姗','女',22,'IS')

insert into c values
('C1','C语言程序设计','殷老师',4),
('C2','计算机网络','王老师',4),
('C3','数据结构','詹老师',4),
('C4','数据库系统','詹老师',3),
('C5','Jave Web','支老师',3)

insert into SC values
('S1','C1',96),
('S1','C2',55),
('S1','C3',84),
('S1','C5',52),
('S2','C1',84),
('S2','C2',90),
('S2','C4',85),
('S3','C5',73),
('S3','C4',Null),
('S4','C1',50)

use JXGL1
go
create view jsj_student
as
select sno,sname,age
from S
where sdept='cs'
with check option
go

create view s_sc_C3(sno,sname,age,sdept)
as
select s.sno,sname,age,sdept
from s inner join SC
on s.sno=SC.sno 
where cno='c3'

create view s_birthday
as
select sno,sname,age
from S

create view s_p
as
select count(sno) as '人数'
from sc
group by cno

create view s_Age
as
select age
from s
where age=20

update jsj_student
set age=19
where sno='s1'

create view s_m
as
select sno,count(sno)as '人数'
from s
group by sno

alter table sc
drop constraint FK__SC__sno__286302EC
delete  jsj_student

update s
set age=age+1

delete from s
where sno='s4'

猜你喜欢

转载自blog.csdn.net/weixin_65089091/article/details/127759710