数据库实验——SQL语句查询

数据库实验课的SQL语句查询,是《数据库系统概论(第5版)》的第三章的全部例题

DBMS是SQL Server

create table Student
(Sno char(9) primary key,
Sname char(20) unique,  --Sname取唯一值
Ssex char(2),
Sage smallint,
Sdept char(20)
);
create table Course
(Cno char(4) primary key,
Cname char(40) not null,  --Cname不能取空
Cpno char(4),
Ccreait smallint,
--foreign key(Cpno)references Course(Cno)
/*表级完整性约束,Cpno是外码,被参照表是Course,被参照列是Cno*/
);


create table SC
(Sno char(9),
Cno char(4),
Grade smallint,
primary key(Sno,Cno),
--foreign key(Sno)references Student(Sno),
--foreign key(Cno)references Course(Cno)
); 


insert into Course values('1','数据库','5',4);
insert into Course values('2','数学',null,2);
insert into Course values('3','信息系统','1',4);
insert into Course values('4','操作系统','6',3);
insert into Course values('5','数据结构','7',4);
insert into Course values('6','数据处理',null,2);
insert into Course values('7','PASCAL语言','6',4);

select * from SC;
insert into SC values('201215121','1',92);
insert into SC values('201215121','2',85);
insert into SC values('201215121','3',88);
insert into SC values('201215122','2',90);
insert into SC values('201215122','3',80);


insert into Student values('201215121','李勇','男',20,'CS');
insert into Student values('201215122','刘晨','女',19,'CS');
insert into Student values('201215123','王敏','女',18,'MA');
insert into Student values('201215125','张立','男',19,'IS');


--alter table pra add S_entrance DATE;
alter table Student add S_entrance DATE;
alter table Student alter column Sage int;
alter table Course add unique(Cname);

create table SCtest
(Sno char(9),
Cno char(4),
Grade smallint,
primary key(Sno,Cno),
--foreign key(Sno)references Student(Sno),
--foreign key(Cno)references Course(Cno)
); 
insert into SCtest values('201215121','1',92);
insert into SCtest values('201215121','2',85);
insert into SCtest values('201215121','3',88);
insert into SCtest values('201215122','2',90);
insert into SCtest values('201215122','3',80);
select * from SCtest;
drop table SCtest;

select * from SC
select * from Course
select * from Student
select * from pra

create unique index Stusno on Student(Sno);
create unique index Coucno on Course(Cno);
create unique index SCno on SC(Sno ASC,Cno DESC);

--alter index SCno rename to SCSno;
exec sp_rename 'SC.SCno','SCSno';

create unique index Stusname on Student(Sno);
drop index Stusname on Student;


select * from Student;

select Sno,Sname
from Student;

select Sname,Sno,Sdept
from Student;

select Sname,2014-Sage
from Student;

select Sname,'Year of Birth:'as'Year of Birth:',2014-Sage as '2014-Sage',lower(Sdept) as'lower(Sdept)'
from Student;

select Sname,'Year of Birth:'as'BIRTH',2014-Sage as 'BIRTHDAY',lower(Sdept) as'DEPARTMENT'
from Student;

select Sno
from SC;

select distinct Sno
from SC;

select Sname 
from Student
where Sdept='CS';

select Sname,Sage
from Student
where Sage<20;

select distinct Sno 
from SC
where Grade>80;

select Sname,Sdept,Sage
from Student
where Sage between 20 and 23;

select Sname,Sdept,Sage
from Student
where Sage not between 20 and 23;

select Sname,Ssex
from Student
where Sdept in('CS','MA','IS');

select Sname,Ssex
from Student
where Sdept not in('MA','IS');

select *
from Student
where Sno like '201215121';

select Sname,Sno,Ssex
from Student
where Sname like '刘%';

insert into Student values('201215130','欧阳锋','男',21,'EE',null);
insert into Student values('201215131','欧豪','男',21,'EE',null);
insert into Student values('201215126','欧阳娜娜','女',22,'MBA',null);
insert into Student values('201215132','赵阳恬','女',20,'CE',null);

select Sname
from Student
where Sname like'欧阳_';

select * 
from Student
where Sname like'_阳%';

select * 
from Student
where Sname not like '刘%';

insert into Course values(8,'DB_Design',3,4);

select Cno,Ccreait
from Course
where Cname like 'DB\_Design' escape'\';

select * 
from Course
where Cname like'DB\_%i__'escape'\';


insert into SC values('201215123',4,null);
insert into SC values('201215124',0,null);


select * from SC
where Grade is  null;

select * 
from Student
where Sdept='CS' and Sage<20;


select * from Student
where Sdept='CS' or Sdept='MA' or Sdept='IS';

select Sno,Grade
from SC
where Cno=3
order by Grade DESC;

select *
from Student
order by Sdept,Sage DESC;


select count(*)
from Student;

select count(distinct Sno)
from SC;


select avg(Grade)
from SC
where Cno=3;

select max(Grade)
from SC
where Cno=1;

exec sp_rename 'Course.Ccreait','Credit';
exec sp_rename 'Course.Credit','Ccredit';

select sum(Ccredit)
from SC,Course
where Sno='201215121' and SC.Cno=Course.Cno;

select * from SC;
select Cno,count(Sno) as'COUNT(Sno)'
from SC
group by Cno;

select Sno
from SC
group by SC.Sno
having count(*)>=2;

select Sno,avg(Grade)
from SC
group by Sno
having avg(Grade)>=70;


select Student.*,SC.*
from Student,SC
where Student.Sno=SC.Sno;

select Student.Sno,Ssex,Sage,Sdept,Cno,Grade
from Student,SC
where Student.Sno=SC.Sno;

select Student.Sno,Sname
from SC,Student
where Student.Sno=SC.Sno and Cno=2 and Grade>90;

select * from Course;
select first.Cno,second.Cno
from Course as first,Course as second
where first.cpno=second.cno;

select Student.Sno,Ssex,Sage,Sdept,Cno,Grade
from Student left outer join SC on(Student.Sno=SC.Sno);
select Student.Sno,Ssex,Sage,Sdept,Cno,Grade
from Student right outer join SC on(Student.Sno=SC.Sno);

select Student.Sno,Sname,Cname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno;


select Sno,Sname,Sdept
from Student
where Sdept in
(
	select Sdept 
	from Student
	where Sname='刘晨'
);

select S1.Sno,S1.Sname,S1.Sdept
from Student as S1,Student as S2
where S1.Sdept=S2.Sdept and S2.Sname='刘晨';

select Sno,Sname
from Student
where Sno in
(
select Sno
from SC
where Cno in
(
	select Cno
	from Course
	where Cname='操作系统'
)
);

select Student.Sno,Sname
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Course.Cname='操作系统';

select Sno,Sname,Sdept
from Student
where Sdept=
(
	select Sdept
	from Student
	where Sname='刘晨'
);

select Sno,Cno
from SC as x
where Grade>=(
	select avg(Grade)
	from SC as y
	where x.Sno=y.Sno
);

select Sname,Sage
from Student
where Sage=Avg_sc.avg_grade

insert into Student
values('201215128','陈冬','男',18,'IS',null);

delete from Student
where Sno='201215126';


insert into Student
values('201215126','张成民','男',18,'CS',null);

insert into SC(Sno,Cno)
values('201215128','1');

create table Dept_age
(
	Sdept char(15),
	Avg_age smallint
);

insert into Dept_age
select Sdept,AVG(Sage)
from Student
group by Sdept;

update Student
set Sage=22
where Sno='201215121';

update Student
set Sage=Sage+1;

select * from SC;
select * from Student;
update SC
set Grade=0
where Sno in(
	select Sno
	from Student
	where Sdept='CS'
);

delete
from Student
where Sno='201215128';

delete from pra;

insert into SC
values('201215126','1',null);

update Student
set Sdept=null
where Sno='201215132';

select * from Student
where Sname is null or Ssex is null or Sage is null or Sdept is null;

select * from SC
where Grade<90
union
select * from SC
where Grade is null;

select * from SC
where Grade<90 or Grade is null;

create view IS_Student
as
select Sno,Sname,Sage
from Student
where Sdept='IS';

create view IS_Student1
as
select Sno,Sname,Sage
from Student
where Sdept='IS'
with check option;

create view IS_S1(Sno,Sname,Grade)
as
select Student.Sno,Sname,Grade
from Student,SC
where Sdept='IS' and Student.Sno=SC.Sno and SC.Cno='1';

create view IS_S2
as
select Sno,Sname,Grade
from IS_S1
where Grade>=90;

create view BT_S(Sno,Sname,Sbirth)
as
select Sno,Sname,2014-Sage
from Student;

create view S_G(Sno,Gavg)
as 
select Sno,AVG(Grade)
from SC
group by Sno;

create view F_Student(F_sno,name,sex,age,dept,entrance)
as
select *
from Student
where Ssex='女';

drop view BT_S;
drop view IS_S1;

select Sno,Sage
from IS_Student
where Sage<=20;

select IS_Student.Sno,Sname
from IS_Student,SC
where IS_Student.Sno=SC.Sno and SC.Cno='1';

select * 
from S_G
where Gavg>60;

select Sno,avg(Grade)
from SC
group by Sno
having avg(Grade)>60;

select * from IS_Student;

update IS_Student
set Sname='刘辰'
where Sno='201215125';

insert into IS_Student
values('201215128','赵华',20);

select * from IS_Student;

 

你可能感兴趣的:(数据库,SQL,SQL,Server)