实验1 数据库的定义和维护
create table 图书 (
书号 int primary key ,
类型 nchar ( 10 ) ,
出版社 nchar ( 10 ) ,
作者 nchar ( 5 ) ,
书名 nchar ( 5 ) ,
定价 smallint
)
create table 读者 (
读者编号 int primary key ,
姓名 nchar ( 10 ) ,
单位 nchar ( 10 ) ,
性别 nchar ( 1 ) ,
电话 nchar ( 20 ) ,
constraint r1 check ( 性别 in ( '男' , '女' ) ) ,
)
create table 借阅 (
书号 int ,
读者编号 int ,
借书日期 date ,
还书日期 date ,
constraint r2 primary key ( 书号, 读者编号) ,
constraint r3 foreign key ( 书号) references 图书( 书号) ,
constraint r4 foreign key ( 读者编号) references 读者( 读者编号)
)
create table student (
sno char ( 10 ) primary key not null ,
sname char ( 8 ) ,
ssex char ( 2 ) not null default '男' ,
sage smallint ,
sdept char ( 30 ) ,
stel char ( 13 ) ,
constraint s2 check ( sname is not null ) ,
constraint s1 check ( sage >= 18 )
)
create index 学生姓名 on student( sname asc ) ;
alter table student drop constraint s2;
create table Course (
cno char ( 10 ) primary key not null ,
cname char ( 16 ) not null ,
ccredit smallint not null ,
cpno char ( 10 )
)
create index 课程编号 on Course( cno asc ) ;
create index 学分 on Course( ccredit desc ) ;
alter table Course add ctech char ( 10 ) ;
create table SC (
sno char ( 10 ) not null ,
cno char ( 10 ) not null ,
grade smallint ,
constraint sc1 primary key ( sno, cno) ,
constraint sc2 foreign key ( sno) references student( sno) ,
constraint sc3 foreign key ( cno) references Course( cno)
)
建表分割线,下面是操作
任务1 :
insert into 读者 values ( '2110' , '张零' , '数计' , '男' , '123450' ) ;
insert into 读者 values ( '2111' , '张一' , '数计' , '女' , '123451' ) ;
insert into 读者 values ( '2112' , '张二' , '数计' , '男' , '123452' ) ;
insert into 读者 values ( '2113' , '张三' , '数计' , '女' , '123453' ) ;
insert into 读者 values ( '2114' , '张四' , '数计' , '男' , '123454' ) ;
insert into 读者 values ( '2115' , '张五' , '数计' , '女' , '123455' ) ;
insert into 读者 values ( '2116' , '张六' , '数计' , '男' , '123456' ) ;
insert into 读者 values ( '2117' , '张七' , '管理' , '女' , '123457' ) ;
insert into 读者 values ( '2118' , '张八' , '数计' , '男' , '123458' ) ;
insert into 读者 values ( '2119' , '张九' , '数计' , '女' , '123459' ) ;
insert into 图书 values ( '12345' , '搞笑' , '虾皮' , '张哥' ,
'中国有哈哈' , '30' ) ;
insert into 图书 values ( '12344' , '计算机' , '机械工业' , '布莱恩特' ,
'csapp' , '110' ) ;
insert into 图书 values ( '12343' , '计算机' , '电子工业' , '王道' ,
'操作系统' , '32' ) ;
insert into 图书 values ( '12342' , '计算机' , '电子工业' , '王道' ,
'计网' , '80' ) ;
insert into 图书 values ( '12341' , '计算机' , '电子工业' , '王道' ,
'数据结构' , '90' ) ;
insert into 图书 values ( '12340' , '计算机' , '电子工业' , '王道' ,
'机组' , '110' ) ;
insert into 图书 values ( '12348' , '计算机' , '电子工业' , '张哥' ,
'中国哈哈1' , '220' ) ;
insert into 图书 values ( '12349' , '计算机' , '虾皮出版社' , '张哥' ,
'中国哈哈2' , '30' ) ;
insert into 图书 values ( '12347' , '计算机' , '虾皮出版社' , '张哥' ,
'中国哈哈3' , '30' ) ;
insert into 图书 values ( '12350' , '计算机' , '虾皮出版社' , '张哥' ,
'中国哈哈4' , '30' ) ;
insert into 借阅 values ( '12345' , '2110' , '2023-1-9' , '2023-3-23' ) ;
insert into 借阅 values ( '12344' , '2112' , '2023-1-10' , '2023-2-23' ) ;
insert into 借阅 values ( '12344' , '2113' , '2023-1-11' , '2023-1-23' ) ;
insert into 借阅 values ( '12345' , '2113' , '2023-1-12' , '2023-1-21' ) ;
insert into 借阅 values ( '12340' , '2111' , '2023-1-14' , '2023-3-9' ) ;
insert into 借阅 values ( '12342' , '2119' , '2023-1-17' , '2023-3-1' ) ;
insert into 借阅 values ( '12341' , '2110' , '2023-1-27' , '2023-3-4' ) ;
insert into 借阅 values ( '12343' , '2110' , '2023-1-3' , '2023-3-8' ) ;
insert into 借阅 values ( '12344' , '2111' , '2023-1-5' , '2023-2-21' ) ;
insert into 借阅 values ( '12342' , '2110' , '2023-1-7' , '2023-2-17' ) ;
update 借阅 set 还书日期 = '2023-4-19' where 读者编号 = '2111' ;
delete from 借阅 where 读者编号 = '2111' ;
任务2 :
insert into student
values ( '20021210' , '张零' , '男' , '20' , '数计' , '123450' ) ;
insert into student
values ( '20021211' , '张一' , '女' , '20' , '数计' , '123451' ) ;
insert into student
values ( '20021212' , '张二' , '男' , '20' , '管理' , '123452' ) ;
insert into student
values ( '20021213' , '张三' , '女' , '20' , '数计' , '123453' ) ;
insert into student
values ( '20021214' , '张四' , '男' , '20' , '数计' , '123454' ) ;
insert into student
values ( '20021215' , '张五' , '女' , '20' , '数计' , '123455' ) ;
insert into student
values ( '20021216' , '张六' , '男' , '20' , '数计' , '123456' ) ;
insert into student
values ( '20021217' , '张七' , '女' , '20' , '管理' , '123457' ) ;
insert into student
values ( '20021218' , '张八' , '男' , '20' , '数计' , '123458' ) ;
insert into student
values ( '20021219' , '张九' , '女' , '20' , '数计' , '123459' ) ;
insert into Course values ( '1' , '高数' , '5' , null , null ) ;
insert into Course values ( '2' , '线代' , '4' , null , null ) ;
insert into Course values ( '3' , '概率论' , '5' , null , null ) ;
insert into Course values ( '4' , 'C语言' , '5' , null , null ) ;
insert into Course values ( '5' , 'C++' , '5' , null , null ) ;
insert into Course values ( '6' , '编译原理' , '5' , null , null ) ;
insert into Course values ( '7' , '数据库' , '3' , '离散数学' , null ) ;
insert into Course values ( '8' , '离散数学' , '5' , null , null ) ;
insert into Course values ( '9' , '操作系统' , '5' , null , null ) ;
insert into Course values ( '10' , '计网' , '5' , null , null ) ;
insert into SC
values ( '20021210' , '1' , 80 ) ;
insert into SC
values ( '20021211' , '2' , 70 ) ;
insert into SC
values ( '20021212' , '3' , 75 ) ;
insert into SC
values ( '20021213' , '4' , 55 ) ;
insert into SC
values ( '20021214' , '5' , 98 ) ;
insert into SC
values ( '20021215' , '6' , 100 ) ;
insert into SC
values ( '20021216' , '7' , 60 ) ;
insert into SC
values ( '20021217' , '8' , 77 ) ;
insert into SC
values ( '20021218' , '9' , 54 ) ;
insert into SC
values ( '20021219' , '10' , 59 ) ;
use XSXK;
create table Grade (
son char ( 10 ) ,
cnt char ( 10 ) ,
avggrade char ( 10 )
)
insert into Grade
select sno, count ( * ) as cnt, avg ( grade) as avggrade
from SC group by sno
update SC set grade = grade + 5
where sno = ( select sno from student where sname = '张六' ) and cno = ( select cno f) ;
实验2 数据库的简单查询和连接查询
insert into student
values ( '2001124001' , '张飞' , '男' , 23 , '管理学院' , '1464312' ) ;
insert into student
values ( '2001124002' , '郭旭' , '男' , 89 , '管理学院' , '1145114' ) ;
insert into student
values ( '2001124004' , '周波' , '男' , 23 , '数计学院' , '4654654' ) ;
insert into student
values ( '2001124005' , '张柏芝' , '女' , 30 , '数计学院' , '154666' ) ;
insert into student
values ( '2001124006' , '风清扬' , '男' , 28 , '管理学院' , '14645463' ) ;
insert into student
values ( '2001124007' , '唐伯虎' , '男' , 26 , '材料学院' , '1454643' ) ;
insert into student
values ( '2001124008' , '于占鳌' , '男' , 25 , '电气学院' , '7842289' ) ;
insert into student
values ( '2001124009' , '九儿' , '女' , 19 , '电气学院' , '774512' ) ;
insert into student
values ( '2001124010' , '李茂贞' , '女' , 29 , '电气学院' , '6659873' ) ;
insert into student
values ( '2001124011' , '紫霞' , '女' , 19 , '材料学院' , '4654562' ) ;
INSERT INTO Course
VALUES ( '001' , 'java' , '4' , '003' , NULL )
, ( '002' , 'c语言' , '4' , '003' , NULL )
, ( '003' , '数据结构' , '4' , NULL , NULL )
, ( '004' , '数据库原理' , '3' , '002' , NULL )
, ( '005' , '计算机组成原理' , '2' , NULL , NULL )
, ( '006' , '操作系统' , '2' , '005' , NULL )
, ( '007' , '计算机网络原理' , '3' , NULL , NULL )
, ( '008' , '高等数学' , '5' , NULL , NULL )
, ( '009' , '大学英语一' , '2' , NULL , NULL )
, ( '010' , '大学物理三' , '3' , NULL , NULL )
;
insert into SC ( sno, grade, cno)
values
( '2001124001' , null , '001' ) ,
( '2001124001' , 82 , '002' ) ,
( '2001124001' , 78 , '003' ) ,
( '2001124001' , 96 , '004' ) ,
( '2001124001' , 76 , '005' ) ,
( '2001124001' , 39 , '006' ) ,
( '2001124001' , 88 , '007' ) ,
( '2001124001' , null , '008' ) ,
( '2001124001' , 82 , '009' ) ,
( '2001124001' , 69 , '010' ) ;
insert into SC ( sno, grade, cno)
values ( '2001124002' , 98 , '001' ) ,
( '2001124002' , 92 , '002' ) ,
( '2001124002' , 78 , '003' ) ,
( '2001124002' , 96 , '004' ) ,
( '2001124002' , null , '005' ) ,
( '2001124002' , 88 , '006' ) ,
( '2001124002' , 88 , '007' ) ,
( '2001124002' , 81 , '008' ) ,
( '2001124002' , 82 , '009' ) ,
( '2001124002' , 79 , '010' ) ;
insert into SC ( sno, grade, cno)
values ( '2001124005' , 66 , '001' ) ,
( '2001124005' , 72 , '002' ) ,
( '2001124005' , 88 , '003' ) ,
( '2001124005' , 76 , '004' ) ,
( '2001124005' , 76 , '005' ) ,
( '2001124005' , 68 , '006' ) ,
( '2001124005' , 68 , '007' ) ,
( '2001124005' , 86 , '008' ) ,
( '2001124005' , 80 , '009' ) ,
( '2001124005' , null , '010' ) ;
insert into SC ( sno, grade, cno)
values ( '2001124004' , 46 , '001' ) ,
( '2001124004' , null , '002' ) ,
( '2001124004' , null , '003' ) ,
( '2001124004' , 87 , '004' ) ,
( '2001124004' , null , '005' ) ,
( '2001124004' , 58 , '006' ) ,
( '2001124004' , 68 , '007' ) ,
( '2001124004' , 61 , '008' ) ,
( '2001124004' , 72 , '009' ) ,
( '2001124004' , 59 , '010' ) ;
insert into SC ( sno, grade, cno)
values ( '2001124006' , 49 , '001' ) ,
( '2001124006' , 52 , '002' ) ,
( '2001124006' , 88 , '003' ) ,
( '2001124006' , 87 , '004' ) ,
( '2001124006' , 66 , '005' ) ,
( '2001124006' , 88 , '006' ) ,
( '2001124006' , 68 , '007' ) ,
( '2001124006' , 61 , '008' ) ,
( '2001124006' , 72 , '009' ) ,
( '2001124006' , null , '010' ) ;
insert into SC ( sno, grade, cno)
values ( '2001124007' , 86 , '001' ) ,
( '2001124007' , 42 , '002' ) ,
( '2001124007' , 69 , '003' ) ,
( '2001124007' , 87 , '004' ) ,
( '2001124007' , 65 , '005' ) ,
( '2001124007' , null , '006' ) ,
( '2001124007' , 58 , '007' ) ,
( '2001124007' , 61 , '008' ) ,
( '2001124007' , 72 , '009' ) ,
( '2001124007' , 69 , '010' ) ;
insert into SC ( sno, grade, cno)
values ( '2001124008' , 87 , '001' ) ,
( '2001124008' , 72 , '002' ) ,
( '2001124008' , 68 , '003' ) ,
( '2001124008' , 87 , '004' ) ,
( '2001124008' , 96 , '005' ) ,
( '2001124008' , 58 , '006' ) ,
( '2001124008' , 68 , '007' ) ,
( '2001124008' , 61 , '008' ) ,
( '2001124008' , 72 , '009' ) ,
( '2001124008' , 59 , '010' ) ;
insert into SC ( sno, grade, cno)
values ( '2001124009' , 86 , '001' ) ,
( '2001124009' , 92 , '002' ) ,
( '2001124009' , 68 , '003' ) ,
( '2001124009' , 87 , '004' ) ,
( '2001124009' , 66 , '005' ) ,
( '2001124009' , 58 , '006' ) ,
( '2001124009' , 68 , '007' ) ,
( '2001124009' , 61 , '008' ) ,
( '2001124009' , 72 , '009' ) ,
( '2001124009' , 79 , '010' ) ;
insert into SC ( sno, grade, cno)
values ( '2001124010' , 96 , '001' ) ,
( '2001124010' , null , '002' ) ,
( '2001124010' , 88 , '003' ) ,
( '2001124010' , 87 , '004' ) ,
( '2001124010' , 86 , '005' ) ,
( '2001124010' , null , '006' ) ,
( '2001124010' , null , '007' ) ,
( '2001124010' , 79 , '008' ) ,
( '2001124010' , 72 , '009' ) ,
( '2001124010' , null , '010' ) ;
insert into SC ( sno, grade, cno)
values ( '2001124011' , 86 , '001' ) ,
( '2001124011' , 82 , '002' ) ,
( '2001124011' , 58 , '003' ) ,
( '2001124011' , 87 , '004' ) ,
( '2001124011' , 66 , '005' ) ,
( '2001124011' , 98 , '006' ) ,
( '2001124011' , 78 , '007' ) ,
( '2001124011' , 81 , '008' ) ,
( '2001124011' , null , '009' ) ,
( '2001124011' , 79 , '010' ) ;
use XSXK;
select sno as 学号, sname as 姓名
from student
where sdept = '数计学院' ;
select sno
from student
where ( select count ( cno)
from SC
where SC. sno = student. sno
) > 0 ;
select sno as 学生学号, grade as 成绩
from SC
where cno = '005'
order by grade desc , sno asc ;
select sno as 学生学号, grade * 0.8 as 成绩
from SC
where cno = '005' and grade >= 80 and grade <= 90 ;
select *
from student
where sdept in ( '管理学院' , '数计学院' ) and sname like '张%' ;
select sno as 学号, cno as 课程号
from SC
where grade is null ;
select SC. sno as 学号,
student. sname as 姓名,
Course. cname as 选修的课程,
SC. grade as 成绩
from SC
join student on SC. sno = student. sno
join course on SC. cno = Course. cno
select Course. *
from SC
join student on SC. sno = student. sno
join Course on SC. cno = Course. cno
where Course. ccredit > 2 and student. sdept = '数计学院'
select
student. * ,
Course. cno as 选课编号,
SC. grade as 成绩
from SC
join student on SC. sno = student. sno
join Course on SC. cno = Course. cno
select student. *
select
student. * ,
Course. cno as 选课编号,
SC. grade as 成绩
from student
join SC on SC. sno = student. sno
join Course on SC. cno = Course. cno;
select
SC. sno as 学号,
student. sname as 姓名,
SC. grade as 成绩
from SC
join student on student. sno = SC. sno
join Course on Course. cno = SC. cno
where
SC. cno = '002' and SC. grade >= 90
select a. cname as 课程, c. cname as 先行课
from Course a, Course b, Course c
where a. cpno = b. cno and b. cpno = c. cno
select sno from sc group by sno having count ( * ) >= 2
select distinct s. sno as 学号
from student s
join SC sc1 on sc1. sno = s. sno
join SC sc2 on sc2. sno = s. sno and sc2. cno != sc1. cno
select distinct sc1. sno as 学号
from SC sc1
join SC sc2 on sc2. sno = sc1. sno and sc2. cno != sc1. cno
实验3 数据库的组合查询和嵌套查询
select sno as 学生学号, sname as 姓名
from student
where sno in ( select sno from SC
where cno = ( select cno from Course where cname = '数据结构' )
)
select sno as 学生学号, sname as 学生姓名
from student
where sage > ( select sage from student where sname = '张飞' )
select distinct student. sno as 学生学号, SC. grade as 成绩
from student, SC
where grade < ( select grade from SC where sno =
( select sno from student where sname = '张飞' ) and cno = ( select cno from Course where cname = '数据结构' ) )
select student. *
from student
where sage < ( select min ( sage) from student where sdept = '数计学院' )
select student. sname as 学生姓名
from student
where sno in ( select sno from SC where cno =
( select cno from Course where cname = '数据结构' ) ) ;
select sname
from student
where not exists (
select *
from course
where not exists (
select *
from sc
where sc. sno = student. sno and sc. cno = course. cno
)
) ;
select sname as 学生姓名, sno as 学生学号
from student
where sno in ( select sno from SC where cno in (
select cno from SC where sno = '2001124001'
) )
select sno as 学号, sname as 学生姓名
from student
where not exists (
select *
from course
where not exists (
select *
from sc
where sc. sno = '2001124001' and sc. cno = course. cno
)
)
select sname as 学生姓名
from student
where sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据结构'
)
and sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据库原理'
)
)
)
select sname as 学生姓名
from student
where sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据结构'
)
or sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据库原理'
)
)
)
select sname as 学生姓名
from student
where sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据结构'
)
and sno in (
select sno
from sc
where cno in (
select cno
from course
where cname != '数据库原理'
)
)
)
select cno as 课程号
from sc x
where not exists (
select *
from student
where ssex = '女' and exists (
select *
from sc y
where x. cno = y. cno and x. sno = y. sno
)
)
select sname
from student
where sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据结构'
)
)
intersect
select sname
from student
where sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据库原理'
)
)
select sname
from student
where sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据结构'
)
)
union
select sname
from student
where sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据库原理'
)
)
select sname
from student
where sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据结构'
)
)
except
select sname
from student
where sno in (
select sno
from sc
where cno = (
select cno
from course
where cname = '数据库原理'
)
)
select count ( distinct sc. sno) as 学生人数
from sc
select sno as 学生学号, sum ( grade) as 总成绩
from sc
where grade >= 60 group by sno having count ( * ) > 4
select sdept as 学院, count ( * ) as 学院人数
from student
group by sdept
select sage as 年龄, count ( * ) as 人数
from student
group by sage
select count ( cno) as 选课数, avg ( grade) 平均成绩
from sc
group by sno
select course. * , cc. 选课人数 from course left join
( select
Course. cno,
COUNT ( sno) '选课人数'
from
course
left join sc
on course. cno = sc. cno
group by course. cno) as cc on course. cno = cc. cnos
第四章课前实验
use 图书管理;
create table 读者 (
读者卡号 char ( 50 ) primary key ,
姓名 char ( 50 ) ,
性别 char ( 50 ) ,
单位 char ( 50 ) ,
办卡日期 date ,
卡状态 char ( 50 ) ,
类别编号 int
)
create table 出版社 (
出版社名称 char ( 50 ) primary key ,
地址 char ( 50 )
)
create table 图书 (
图书编号 char ( 50 ) primary key ,
书名 char ( 50 ) ,
类别 char ( 50 ) ,
作者 char ( 50 ) ,
出版社名称 char ( 50 ) ,
出版日期 date ,
单价 float ,
库存数量 int
)
create table 借阅 (
读者卡号 char ( 50 ) ,
图书编号 char ( 50 ) primary key ( 读者卡号, 图书编号) ,
借书日期 date ,
还书日期 date
)
insert into 读者 values
( '2100001' , '李丽' , '女' , '数计学院' , '2021-03-10' , '正常' , '01' ) ,
( '2100002' , '赵健' , '男' , '数计学院' , '2021-03-10' , '正常' , '01' ) ,
( '2100003' , '张飞' , '男' , '数计学院' , '2020-09-10' , '正常' , '02' ) ,
( '2100004' , '赵亮' , '男' , '数计学院' , '2021-03-10' , '正常' , '03' ) ,
( '2100005' , '张晓' , '女' , '管理学院' , '2021-09-10' , '正常' , '03' ) ,
( '2200001' , '杨少华' , '男' , '管理学院' , '2021-09-10' , '正常' , '04' ) ,
( '2200002' , '王武' , '男' , '数计学院' , '2021-09-10' , '正常' , '02' )
insert into 出版社 values
( '电子工业出版社' , '北京' ) ,
( '高等教育出版社' , '北京' ) ,
( '机械工业出版社' , '北京' ) ,
( '科学出版社' , '北京' ) ,
( '南海出版社' , '海口' ) ,
( '清华大学出版社' , '北京' ) ,
( '人民邮电出版社' , '北京' ) ,
( '西南财经大学' , '成都' )
insert into 图书 values
( 'GL0001' , 'APP营销实战' , '管理' , '谭贤' , '人民邮电出版社' , '2021-03-01' , 58.0000 , 5 ) ,
( 'GL0002' , '产品经理的自我修炼' , '管理' , '张晨静' , '人民邮电出版社' , '2021-03-01' , 52.0000 , 5 ) ,
( 'GL0003' , '管理学原理' , '管理' , '赵玉田' , '科学出版社' , '2021-01-01' , 38.0000 , 5 ) ,
( 'JSJ001' , 'Python数据挖掘与机' , '计算机' , '魏伟一' , '清华大学出版社' , ' 2021-03-01' , 59.0000 , 5 ) ,
( 'JSJ002' , '机器学习' , '计算机' , '赵卫东' , '人民邮电出版社' , '2020-09-01' , 58.0000 , 5 ) ,
( 'JSJ003' , '数据库应用与开发教程' , '管理' , '谭贤' , '人民邮电出版社' , '2021-03-01' , 58.0000 , 5 )
insert into 借阅 values
( '2100001' , 'GL0003' , '2021-04-10' , '2021-07-10' ) ,
( '2100002' , 'JSJ001' , '2021-04-10' , '' ) ,
( '2100002' , 'JSJ003' , '2021-04-10' , '' ) ,
( '2100004' , 'GL0003' , '2021-05-10' , '2021-07-10' ) ,
( '2100004' , 'JSJ001' , '2021-05-10' , '2021-07-10' ) ,
( '2100001' , 'JSJ005' , '2021-05-10' , '2021-07-10' )
insert into 出版社 values
( '西安电子科技大学出版社' , '西安' ) ;
insert into 读者 values
( '2200003' , '陈晨' , '男' , '机械学院' , '2021-10-10' , '正常' , '02' ) ;
insert into 图书 values
( 'SK0001' , '请不要辜负这个时代' , '社科' , '周小平' ,
'南海出版社' , '2020-11-01' , '32.00' , '5' ) ;
delete from 读者 where 姓名 = '杨少华' ;
use 图书管理1 ;
create table 图书管理 (
读者卡号 char ( 50 ) ,
图书编号 char ( 50 ) , primary key ( 读者卡号, 图书编号) ,
姓名 char ( 50 ) ,
性别 char ( 50 ) ,
单位 char ( 50 ) ,
书名 char ( 50 ) ,
类别 char ( 50 ) ,
作者 char ( 50 ) ,
出版社名称 char ( 50 ) ,
地址 char ( 50 ) ,
出版日期 date ,
单价 float ,
库存数量 int ,
借书日期 date ,
还书日期 date
)
insert into 图书管理 values
( '2100001' , 'GL0001' , '张一年' , '男' , '数计学院' , '数据库' , '计算机' , '王佳和' , '人民邮电出版社' , '陕西' , '2021-04-10' , 38.00000 , 5 , '2021-05-10' , '2021-07-10' ) ,
( '2100002' , 'GL0002' , '张二月' , '男' , '数计学院' , '数据库' , '计算机' , '王佳和' , '人民邮电出版社' , '陕西' , '2021-04-10' , 38.00000 , 5 , '2021-05-10' , '2021-07-10' ) ,
( '2100003' , 'GL0003' , '张三天' , '男' , '数计学院' , '数据库' , '计算机' , '王佳和' , '人民邮电出版社' , '陕西' , '2021-04-10' , 38.00000 , 5 , '2021-05-10' , '2021-07-10' ) ,
( '2100004' , 'GL0004' , '张四日' , '男' , '数计学院' , '数据库' , '计算机' , '王佳和' , '人民邮电出版社' , '陕西' , '2021-04-10' , 38.00000 , 5 , '2021-05-10' , '2021-07-10' ) ,
( '2100005' , 'GL0005' , '张五秒' , '男' , '数计学院' , '数据库' , '计算机' , '王佳和' , '人民邮电出版社' , '陕西' , '2021-04-10' , 38.00000 , 5 , '2021-05-10' , '2021-07-10' ) ,
( '2100006' , 'GL0006' , '张六六' , '男' , '数计学院' , '数据库' , '计算机' , '王佳和' , '人民邮电出版社' , '陕西' , '2021-04-10' , 38.00000 , 5 , '2021-05-10' , '2021-07-10' ) ,
( '2100007' , 'GL0007' , '杨少华' , '男' , '数计学院' , '数据库' , '计算机' , '王佳和' , '人民邮电出版社' , '陕西' , '2021-04-10' , 38.00000 , 5 , '2021-05-10' , '2021-07-10' )
insert into 图书管理 ( 出版社名称, 地址) values
( '西安电子科技大学出版社' , '西安' ) ;
insert into 图书管理 values
( '2200003' , '陈晨' , '男' , '机械学院' , '2021-10-10' , '正常' , '02' ) ;
insert into 图书管理( 图书编号, 书名, 类别, 作者,
出版社名称, 出版日期, 单价, 库存数量) values
( 'SK0001' , '请不要辜负这个时代' , '社科' ,
'周小平' , '南海出版社' , '2020-11-01' , 32.00 , 5 ) ;
delete from 图书管理 where 姓名 = '杨少华' ;
实验5
create database 学生成绩管理系统
create table tdept(
tdept_no varchar ( 10 ) primary key ,
tdept_name varchar ( 10 ) not null
)
create table subject(
subject_no varchar ( 10 ) primary key ,
subject_name varchar ( 15 ) not null ,
tdept_no varchar ( 10 )
constraint FK_subject_tdept foreign key ( tdept_no) references tdept( tdept_no)
)
create table class(
class_no varchar ( 10 ) primary key ,
class_name varchar ( 15 ) not null ,
subject_no varchar ( 10 )
constraint FK_class_subject foreign key ( subject_no) references subject( subject_no)
)
create table student(
sno char ( 10 ) not null primary key ,
sname char ( 8 ) not null ,
ssex char ( 2 ) not null ,
sage smallint ,
class_no varchar ( 10 ) ,
constraint FK_student_class foreign key ( class_no) references class( class_no)
)
create table course(
cno char ( 10 ) not null primary key ,
cname char ( 16 ) not null ,
ccredit smallint not null ,
chours smallint not null ,
kind varchar ( 10 ) ,
semester varchar ( 20 ) not null
)
create table class_compulsory(
cno char ( 10 ) not null ,
class_no varchar ( 10 ) ,
start_date date not null ,
constraint fk3_cno_class_no primary key ( class_no, cno) ,
constraint fk3_class_compulsory foreign key ( class_no) references class( class_no) ,
constraint fk3_compulsory_class foreign key ( cno) references course( cno)
)
create table compulsory(
cno char ( 10 ) not null primary key
constraint FK_compulsory_course foreign key ( cno) references course( cno)
)
create table Selective(
cno char ( 10 ) not null primary key ,
start_time date not null ,
end_time date not null ,
constraint FK_Selective_course foreign key ( cno) references course( cno)
)
create table student_Selective(
sno char ( 10 ) not null ,
cno char ( 10 ) not null ,
grade smallint ,
point smallint ,
constraint fk1_c1_c2 primary key ( sno, cno) ,
constraint fk1_student foreign key ( sno) references student( sno) ,
constraint fk1_Selective foreign key ( cno) references course( cno)
)
create table student_compulsory(
sno char ( 10 ) not null ,
cno char ( 10 ) not null ,
grade smallint ,
point smallint ,
constraint fk2_c1_c2 primary key ( sno, cno) ,
constraint fk2_student foreign key ( sno) references student( sno) ,
constraint fk2_Selective foreign key ( cno) references course( cno)
)
go
create trigger student_Selective_delete
on student
for delete
as if ( select count ( * )
from student_Selective, deleted
where student_Selective. sno = deleted. sno) > 0
begin
delete student_Selective
from student_Selective inner join deleted
on student_Selective. sno = deleted. sno
end
go
create trigger student_compulsory_delete
on student
for delete
as if ( select count ( * )
from student_compulsory, deleted
where student_compulsory. sno = deleted. sno) > 0
begin
delete student_compulsory
from student_compulsory inner join deleted
on student_compulsory. sno = deleted. sno
end
go
create trigger course_delete
on course
for delete
as if ( select count ( * )
from Selective, deleted
where Selective. cno = deleted. cno) > 0
begin
delete Selective
from Selective inner join deleted
on Selective. cno = deleted. cno
delete student_Selective
from student_Selective inner join deleted
on student_Selective. cno = deleted. cno
end
go
create trigger course_delete1
on course
for delete
as if ( select count ( * )
from compulsory, deleted
where compulsory. cno = deleted. cno) > 0
begin
delete compulsory
from compulsory inner join deleted
on compulsory. cno = deleted. cno
delete student_compulsory
from student_compulsory inner join deleted
on student_compulsory. cno = deleted. cno
end
create unique index index1 on student ( sno)
create index index2 on class_compulsory ( start_date)
create index index3 on student_Selective ( cno)
create index index4 on student_compulsory ( cno)
go
create view 学生成绩
as
select s. sno, sname, class_name, c. cname, sc. grade, sc. point
from student s, class, course c, student_compulsory sc
where s. class_no = class. class_no
and s. sno = sc. sno
and sc. cno = c. cno
union
select s. sno, sname, class_name, c. cname, ss. grade, ss. point
from student s, class, course c, student_Selective ss
where s. class_no = class. class_no
and s. sno = ss. sno
and ss. cno = c. cno
go
insert into tdept ( tdept_no, tdept_name)
values ( 'T001' , '数计学院' ) ,
( 'T002' , '管理学院' ) ,
( 'T003' , '外语学院' ) ,
( 'T004' , '生工学院' ) ,
( 'T005' , '化工学院' )
insert into subject ( subject_no, subject_name, tdept_no)
values ( 'S001' , '计算机科学技术' , 'T001' ) ,
( 'S002' , '网络工程' , 'T001' ) ,
( 'S003' , '酒店管理' , 'T002' ) ,
( 'S004' , '工程管理' , 'T002' ) ,
( 'S005' , '英语' , 'T003' ) ,
( 'S006' , '日语' , 'T003' ) ,
( 'S007' , '生物科学' , 'T004' ) ,
( 'S008' , '食品工程' , 'T004' ) ,
( 'S009' , '化学科学' , 'T005' ) ,
( 'S010' , '应用化学' , 'T005' )
insert into class( class_no, class_name, subject_no)
values ( 'C001' , '计科01' , 'S001' ) ,
( 'C002' , '计科02' , 'S001' ) ,
( 'C003' , '网工01' , 'S002' ) ,
( 'C004' , '网工02' , 'S002' ) ,
( 'C005' , '酒管01' , 'S003' ) ,
( 'C006' , '酒管02' , 'S003' ) ,
( 'C007' , '工管01' , 'S004' ) ,
( 'C008' , '工管02' , 'S004' ) ,
( 'C009' , '英语01' , 'S005' ) ,
( 'C010' , '英语02' , 'S005' ) ,
( 'C011' , '日语01' , 'S006' ) ,
( 'C012' , '日语02' , 'S006' ) ,
( 'C013' , '生科01' , 'S007' ) ,
( 'C014' , '生科02' , 'S007' ) ,
( 'C015' , '食品01' , 'S008' ) ,
( 'C016' , '食品02' , 'S008' ) ,
( 'C017' , '化学01' , 'S009' ) ,
( 'C018' , '化学02' , 'S009' ) ,
( 'C019' , '应化01' , 'S010' ) ,
( 'C020' , '应化01' , 'S010' )
insert into student ( sno, sname, ssex, sage, class_no)
values ( '200001' , '张三' , '男' , 19 , 'C001' ) ,
( '200002' , '李四' , '男' , 21 , 'C002' ) ,
( '200003' , '王五' , '男' , 20 , 'C003' ) ,
( '200004' , '赵六' , '男' , 18 , 'C004' ) ,
( '200005' , '张一' , '女' , 22 , 'C005' ) ,
( '200006' , '张二' , '女' , 19 , 'C006' ) ,
( '200007' , '张四' , '男' , 18 , 'C007' ) ,
( '200008' , '张五' , '男' , 20 , 'C008' ) ,
( '200009' , '张六' , '男' , 21 , 'C009' ) ,
( '200010' , '张七' , '男' , 18 , 'C010' ) ,
( '200011' , '张八' , '男' , 18 , 'C011' ) ,
( '200012' , '张九' , '女' , 19 , 'C012' ) ,
( '200013' , '张十' , '男' , 20 , 'C013' ) ,
( '200014' , '张十一' , '男' , 18 , 'C014' ) ,
( '200015' , '张十二' , '女' , 21 , 'C015' ) ,
( '200016' , '张十三' , '男' , 20 , 'C016' ) ,
( '200017' , '张十四' , '男' , 17 , 'C017' ) ,
( '200018' , '张四五' , '女' , 18 , 'C018' ) ,
( '200019' , '张二三' , '女' , 18 , 'C019' ) ,
( '200020' , '杰哥' , '男' , 19 , 'C020' )
insert into Course( cno, cname, ccredit, chours, kind, semester)
values ( 'cn001' , '高等数学一' , 5 , 48 , '公共必修课' , '第一学期' ) ,
( 'cn002' , '高等数学二' , 5.5 , 48 , '公共必修课' , '第二学期' ) ,
( 'cn003' , '大学物理一' , 3 , 48 , '公共必修课' , '第二学期' ) ,
( 'cn004' , '大学物理二' , 3.5 , 48 , '公共必修课' , '第三学期' ) ,
( 'cn005' , '大学英语一' , 2.5 , 48 , '公共必修课' , '第一学期' ) ,
( 'cn006' , '大学英语二' , 2 , 48 , '公共必修课' , '第二学期' ) ,
( 'cn007' , '语言的魅力' , 2 , 32 , '公共选修课' , '第一学期' ) ,
( 'cn008' , '生物之谜' , 2 , 32 , '公共选修课' , '第二学期' ) ,
( 'cn009' , '电影鉴赏' , 2 , 32 , '公共选修课' , '第三学期' ) ,
( 'cn010' , '基因与人' , 2 , 32 , '公共选修课' , '第四学期' ) ,
( 'cn011' , '数学建模' , 2 , 32 , '公共选修课' , '第五学期' ) ,
( 'cn012' , '生物之谜' , 2 , 32 , '公共选修课' , '第六学期' ) ,
( 'cn013' , 'JAVA' , 3 , 40 , '专业必修课' , '第三学期' ) ,
( 'cn014' , '管理概要' , 3 , 40 , '专业必修课' , '第二学期' ) ,
( 'cn015' , '翻译概要' , 3 , 40 , '专业必修课' , '第三学期' ) ,
( 'cn016' , '生理基础' , 3 , 40 , '专业必修课' , '第四学期' ) ,
( 'cn017' , '工艺流程' , 3 , 40 , '专业必修课' , '第五学期' ) ,
( 'cn018' , '数据库原理' , 3 , 40 , '专业必修课' , '第四学期' ) ,
( 'cn019' , 'JAVAEE' , 4 , 24 , '专业选修课' , '第五学期' ) ,
( 'cn020' , '管理信息学' , 4 , 24 , '专业选修课' , '第六学期' ) ,
( 'cn021' , '商务英语' , 4 , 24 , '专业选修课' , '第五学期' ) ,
( 'cn022' , '生物化学' , 4 , 24 , '专业选修课' , '第六学期' ) ,
( 'cn023' , '无机化学' , 4 , 24 , '专业选修课' , '第五学期' ) ,
( 'cn024' , 'Python' , 4 , 24 , '专业选修课' , '第六学期' )
insert into compulsory( cno)
values ( 'cn001' ) ,
( 'cn002' ) ,
( 'cn003' ) ,
( 'cn004' ) ,
( 'cn005' ) ,
( 'cn006' ) ,
( 'cn013' ) ,
( 'cn014' ) ,
( 'cn015' ) ,
( 'cn016' ) ,
( 'cn017' ) ,
( 'cn018' )
insert into Selective( cno, start_time, end_time)
values ( 'cn007' , '2020-09-01' , '2021-02-01' ) ,
( 'cn008' , '2020-09-01' , '2021-02-01' ) ,
( 'cn009' , '2020-09-01' , '2021-02-01' ) ,
( 'cn010' , '2020-09-01' , '2021-02-01' ) ,
( 'cn011' , '2020-09-01' , '2021-02-01' ) ,
( 'cn012' , '2020-09-01' , '2021-02-01' ) ,
( 'cn019' , '2020-11-01' , '2021-01-01' ) ,
( 'cn020' , '2020-11-01' , '2021-01-01' ) ,
( 'cn021' , '2020-11-01' , '2021-01-01' ) ,
( 'cn022' , '2020-11-01' , '2021-01-01' ) ,
( 'cn023' , '2020-11-01' , '2021-01-01' ) ,
( 'cn024' , '2020-11-01' , '2021-01-01' )
insert into student_compulsory( sno, cno, grade, point )
values ( '200001' , 'cn001' , 85 , 3.5 ) ,
( '200002' , 'cn001' , 72 , 2 ) ,
( '200003' , 'cn001' , 66 , 1.5 ) ,
( '200004' , 'cn001' , 91 , 4 ) ,
( '200005' , 'cn001' , 82 , 3 ) ,
( '200006' , 'cn001' , 60 , 1 ) ,
( '200001' , 'cn002' , 85 , 3.5 ) ,
( '200002' , 'cn002' , 72 , 2 ) ,
( '200003' , 'cn002' , 66 , 1.5 ) ,
( '200004' , 'cn002' , 91 , 4 ) ,
( '200005' , 'cn002' , 82 , 3 ) ,
( '200006' , 'cn002' , 60 , 1 )
insert into student_Selective( sno, cno, grade, point )
values ( '200001' , 'cn007' , 85 , 3.5 ) ,
( '200001' , 'cn008' , 72 , 2 ) ,
( '200001' , 'cn009' , 66 , 1.5 ) ,
( '200001' , 'cn010' , 91 , 4 ) ,
( '200001' , 'cn011' , 82 , 3 ) ,
( '200001' , 'cn012' , 60 , 1 ) ,
( '200002' , 'cn007' , 85 , 3.5 ) ,
( '200002' , 'cn008' , 72 , 2 ) ,
( '200002' , 'cn009' , 66 , 1.5 ) ,
( '200002' , 'cn010' , 91 , 4 ) ,
( '200002' , 'cn011' , 82 , 3 ) ,
( '200002' , 'cn012' , 60 , 1 )
insert into class_compulsory( cno, class_no, start_date)
values ( 'cn001' , 'C001' , '2022-09-01' ) ,
( 'cn002' , 'C001' , '2022-09-01' ) ,
( 'cn003' , 'C001' , '2022-09-01' ) ,
( 'cn004' , 'C001' , '2022-09-01' ) ,
( 'cn005' , 'C001' , '2022-09-01' ) ,
( 'cn006' , 'C001' , '2022-09-01' ) ,
( 'cn001' , 'C002' , '2022-09-01' ) ,
( 'cn002' , 'C002' , '2022-09-01' ) ,
( 'cn003' , 'C002' , '2022-09-01' ) ,
( 'cn004' , 'C002' , '2022-09-01' ) ,
( 'cn005' , 'C002' , '2022-09-01' ) ,
( 'cn006' , 'C002' , '2022-09-01' )
select * from student where sno = '200007'
select * from student where sname = '杰哥'
select * from Course where cno = 'cn001'
select * from course where cname = '数据库原理'
select grade, point from student s, course c, student_Selective ss
where s. sno = ss. sno and c. cno = ss. cno and s. sno = '200001' and c. cno = 'cn004'
union
select grade, point from student s, course c, student_compulsory sc
where s. sno = sc. sno and c. cno = sc. cno and s. sno = '200004' and c. cno = 'cn001'
select *
from 学生成绩
where sno = '200001'
select sno, sname, grade, point
from 学生成绩 sg, course c
where sg. cname = c. cname and class_name = '计科02' and c. cno = 'cn001'
select sno, sum ( point ) '绩点总和' , avg ( point ) '平均学分绩点'
from 学生成绩
group by sno
insert into student values ( '200023' , '二毛' , '女' , 18 , 'C002' )
select * from student
insert into course values ( 'cn025' , '操作系统' , 5 , 48 , '公共必修课' , '第三学期' )
select * from course
insert into student_compulsory values ( '200001' , 'cn004' , 90 , 2 )
select * from student_compulsory
update student set sname = '大黑' , sage= 20 where sno = '200001'
select * from student
update course set cname = '唱跳rap' where cno = 'cn007'
select * from course
update student_compulsory set grade = '64' where sno = '200006' and cno = 'cn002'
select * from student_compulsory
delete from student_Selective where sno = '200001'
select * from student_Selective
delete from student_compulsory where sno = '200002' and cno = 'cn002'
select * from student_compulsory