/*
学生(学号,姓名,性别,年龄,所在系);
课程(课程号,课程名,先行课),
选课(学号,课程号,成绩)。
用 Transact-SQL完成下列操作。
l)建立学生选课库。
2)建立学生、课程和选课表。
3)建立各表以主码为索引项的索引。
4)建立学生性别只能为“男”或“女”的规则,性别为“男”的缺省,并将它们绑定在学生表上。
5)建立在对选课表输入或更改数据时,必须服从参照完整性约束的INSERT和UPDATE触发器。
6)建立在删除学生记录时,同时也要把相应的选课记录删除的触发器。
7)查询各系及学生数,最后来出共有多少系和多少学生。
8)将学生表和选课表进行内连接、左外连接和右外连接。
9)列出学生学号、姓名及学习情况。学习情况用好、较好、一般或较差表示。当平均成绩大于85分时,学习情况为好;当平均成绩在70~85分之间,学习情况为较好;当平均成绩在60~70分之间,学习成绩为一般;平均成绩在60分以下的为学习情况较差。
*/
--Stu(Sno,Sname,Ssex,Sage,Sdpm);
--Course(Cno,Cname,Cprv);
--CoSelected(Sno,Cno,grade);
Create table Stu(Sno char(5) not null unique,
Sname char(8) unique,
Ssex char(2) check(Ssex in('男','女')),
Sage char(3),
primary key(Sno));
Create table Course(Cno char(5) not null unique,
Cname char(20) unique,
Cprv char(20),
primary key(Cno));
Create table CoSelected(Sno Char(5) not null,
Cno Char(5) not null,
--grade Char(3),
primary key(Sno,Cno),
foreign key(Sno) references Stu(Sno),
foreign key(Cno) references Course(Cno));
select * from stu
--alter table Stu add depm char(30);
--update stu set depm='计算机系' where Sno='00001';
--update stu set depm='计算机系' where Sno='00003';
--update stu set depm='计算机系' where Sno='00005';
--update stu set depm='计算机系' where Sno='00009';
--update stu set depm='计算机系' where Sno='00019';
--update stu set depm='法律系' where Sno='00002';
--update stu set depm='法律系' where Sno='00004';
--update stu set depm='艺术系' where Sno='00008';
--update stu set depm='艺术系' where Sno='00011';
--Insert into stu values('00001','张三','男','20');
--Insert into stu values('00002','李四','男','21');
--Insert into stu values('00003','王五','男','19');
--Insert into stu values('00004','赵六','男','23');
--Insert into stu values('00005','周七','男','20');
--Insert into stu values('00006','周八','男','25');
--update stu set Sname='王八' where Sno='00006';
--Insert into stu values('00007','豆豆','男','25');
--delete from stu where Sno='00007';
--Insert into stu values('00008','豆豆','女','25');
--Insert into stu values('00009','杰杰','男','23');
--Insert into stu values('00019','冰冰','女','22');
--update stu set Sno='00011' where Sno='00006';
--------------课程输入-------------------------
--select * from Course
/*
insert into Course values('C001','计算机导论','无');
insert into Course values('C002','高等数学','无');
insert into Course values('C003','离散数学','无');
insert into Course values('C004','C++程序设计','计算机导论');
insert into Course values('C005','操作系统','无');
insert into Course values('C006','数据结构','C++程序设计');
insert into Course values('C007','数据库系统','无');
insert into Course values('C008','软件工程','数据结构');
insert into Course values('C009','计算机网络','高等数学');
*/
--------------选课输入---------------------------
--select * from CoSelected
--alter table CoSelected add grade char(3);/*增加一列*/
/*
insert into CoSelected values('00001','C001','90');
insert into CoSelected values('00001','C002','98');
insert into CoSelected values('00001','C003','78');
insert into CoSelected values('00001','C004','99');
insert into CoSelected values('00002','C004','70');
insert into CoSelected values('00002','C005','80');
insert into CoSelected values('00002','C006','90');
insert into CoSelected values('00002','C007','86');
insert into CoSelected values('00003','C003','93');
insert into CoSelected values('00003','C005','92');
insert into CoSelected values('00003','C006','94');
insert into CoSelected values('00003','C007','98');
insert into CoSelected values('00004','C002','70');
insert into CoSelected values('00004','C004','90');
insert into CoSelected values('00004','C003','80');
insert into CoSelected values('00004','C007','68');
*/
/*
create unique index S_SNO on Stu(Sno);
create unique index C_CNO on Course(Cno);
create unique index SC_NO on CoSelected(Sno,Cno);
*/
/*
select count(*) from stu where depm='计算机系';--计算机系人数
select count(*) from stu where depm='法律系';
select count(*) from stu where depm='艺术系';
select count(*) from stu
*/
--select /*Sno,Sname,Ssex,Sage,depm,Cname*/ * from Stu S,CoSelected CS,Course C where S.Sno=CS.Sno and C.Cno=CS.Cno and S.Sno='00001'
--学号为00001学生的学号,名字,性别,年龄,系,课程名,分数,
select S.Sno as 学号,S.Sname as 名字,S.Ssex as 性别,S.Sage as 年龄,S.depm as 系,C.Cname as 课程名,CS.grade as 分数 from Stu S,CoSelected CS,Course C where S.Sno=CS.Sno and C.Cno=CS.Cno and S.Sno='00001'
--选择课程名为计算机导论学生的学号及名字
/*
select Sno,Sname from Stu where Sno in
(select Sno from CoSelected where Cno in
(select Cno from Course where Cname='计算机导论'))
*/
select S.Sno,S.Sname from Stu S,CoSelected CS,Course C where S.Sno=CS.Sno and CS.Cno=C.Cno and C.Cname='计算机导论';
-------------------------------------
----列出学生学号、姓名及学习情况。学习情况用好、较好、一般或较差表示。
--select S.Sno,S.Sname,S.Ssex,S.Sage,S.depm,C.Cname,CS.grade from Stu S,CoSelected CS,Course C where S.Sno=CS.Sno and C.Cno=CS.Cno and S.Sno='00001'
--Operand data type char is invalid for avg operator,表示字符为char的类型不能相加
/*
alter table CoSelected
alter column grade int;
*/
select t.Sno,t.Sname,t.Ssex,t.Sage,avg(t.grade)as AvgGrade from (select S.Sno,S.Sname,S.Ssex,S.Sage,CS.grade from
Stu S,CoSelected CS,Course C where S.Sno=CS.Sno and C.Cno=CS.Cno and S.Sno='00001') t group by t.Sno,t.Sname,t.Ssex,t.Sage
--选择课程号为C003的男生女生各多少个的表
select t1.Cno as 课程号,t1.Counts as 男,t2.Counts as 女 from
(select count(S.Ssex) as counts,CS.Cno from stu S,CoSelected CS where S.Sno=CS.Sno and S.Ssex='男'and CS.Cno='C003' group by CS.Cno,S.Ssex) as t1,
(select count(S.Ssex) as counts,CS.Cno from stu S,CoSelected CS where S.Sno=CS.Sno and S.Ssex='女'and CS.Cno='C003' group by CS.Cno,S.Ssex) as t2
where t1.Cno=t2.Cno