数据库语句总结

  • 格式:docx
  • 大小:57.47 KB
  • 文档页数:11

实验四

1、 建立数据库

create database TSJY

on

(

name=TSJY,

filename ='E:\TSJY.mdf',

size=10MB,

maxsize=100MB,

filegrowth=1MB

)

log on

(

name='TSJY_log',

filename='E:\TSJYT.ldf',

size=5MB,

maxsize=50MB,

filegrowth=1MB

);

2、 建表

use S_T

create table Student(

Sno char (11) not null primary key,

Sname char (8) not null,

Ssex char (2) not null,

Sage Tinyint not null,

Sdept char (10) not null

check (Ccredit >=1 and Ccredit <=6)(学分取1-6的整数)

check (Grade >=1 and Grade <=100)(成绩在0-100之间));

3、 将course表的cpno的长度改为3

alter table Course alter column cpno char (3)

4、给student表中的ssex列的增加默认约束,默认值为“男”

alter table Student add constraint df_Student_Ssex

default '男' for Ssex;

5、为course表增加一列teacher,char(8)

alter table Course add teacher char(8);

6、删除course表的teacher列

alter table Course drop column teacher;

7、插入数据

insert into S(SNO , SNAME , STATUS , CITY)

values ('S1','精益','20','天津'),

('S2','盛锡','10','北京'),

('S3','东方红','30','北京'),

('S4','丰泰盛','20','天津'),

('S5','为民','50','上海');

8、为S表设一个主键

alter table S add primary key (SNO);

9、为S表增加一个唯一索引s_sno

create unique index s_sno on S (SNO);

为SPJ表增加索引spj_sno_pno_jno,要求索引列sno用降序,pno、jno用升序

create index spj_sno_pno_jno on SPJ (SNO desc,pno,jno);

为HISD的Doctor表按医生ID(dID)升序建立唯一索引

create unique index Dindex on Doctor(dID)

为HISD的Doctor表按医生ID(dID)降序建立唯一索引

create unique index Dindex on Doctor(dID desc)

10、为SPJ表加外键约束(若建表时已经建立外键,可以先删除某个已有外键,重新建立

alter table SPJ add constraint FK_SNO foreign key (SNO) references

S(SNO);

alter table SPJ add constraint FK_PNO foreign key (PNO) references

P(PNO);

alter table SPJ add constraint FK_JNO foreign key (JNO) references

J(JNO);

11、为SPJ表增加列qty的检查约束,要求qty>0

alter table SPJ add constraint CK_SPJ_qty check(qty>0);

12、删除SPJ表的检查约束CK_SPJ_qty

alter table SPJ drop constraint CK_SPJ_qty

13、查询全体学生的学号与姓名

select Sno,Sname FROM Student

14、查询全体学生的基本信息

select * FROM Student

15、查询全体学生的姓名、出生年份及所在系

use s_t

SELECT Sname,year(getdate()) -sage birthyear,sdept FROM student

16、查询选修了课程的学生学号。(掌握distinct的用法)

SELECT distinct Sno FROM SC

17、查询年龄在18-20岁间的06级的学生姓名及学号

SELECT Sname,Sno FROM student WHERE Sage BETWEEN 18 AND 20 AND SNO

like '2006%'

18、查询信息系(IS)、数学系(MA)和计算机科学系(CS)学生的姓名和性别

SELECT Sname,Ssex FROM Student WHERE Sdept IN ( 'IS','MA','CS' );

19、查找所有姓李的学生的信息

SELECT * FROM student where Sname like '李%'

20、查找所有已选修但没有成绩的学生学号。

select Sno

from SC

where Grade is null

21、对所有已选课的学生按学号进行升序排列,同时要求每个学生按成绩降序排列。(掌握order by的用法)

select Sno,Grade

from SC

order by Sno,Grade desc

22、统计每门课程的选课人数。(掌握group by用法)

select ame,COUNT(o) as 人数

from course,SC

where o=o

group by Cname

23、统计重名的学生姓名及人数。

select Sname,COUNT(*) as 人数

from student

group by Sname

having COUNT(*)>1

24、统计男生与女生的人数。

select ssex,COUNT(sno) cnt

from student

group by ssex

25、查询“计算机系”年龄最大的学生的基本信息。

select *

from student a

where Sage=(select MAX(Sage)

from student b

where Sdept='计算机系')

实验五

26、打出所有供应商的姓名和所在城市。

use SPJ

select SNAME,CITY

from s

27、找出所有零件的名称及重量。

use SPJ

select PNAME,WEIGHT

from p

28、统计每个供应商供应的各种零件数量。

select PNAME,COUNT(PNAME) cnt

from P,SPJ

where P.PNO=SPJ.PNO

group by PNAME

29、求供应工程J1零件的供应商号码SNO。

use SPJ

select distinct sno

from spj

where jno='j1'

30、求供应工程J1零件P1的的供应商号码SNO。

use SPJ

select distinct sno

from spj

where jno='j1' and pno='p1'

实验六

31、求所在的城市为直辖市的供应商的名称。

select Sname from s where city in ('北京','天津','重庆','上海')

32、求供应红色零件的供应商号,零件号和数量。

select SNO,PNO,QTY FROM SPJ

where Pno IN

(SELECT pno FROM p

where Color='红')

33、没有使用天津供应商生产的红色零件的工程号JNO。

select distinct JNO from SPJ where JNO not in(

select JNO from SPJ,P,S where SPJ.PNO=P.PNO

and SPJ.SNO=S.SNO and P.COLOR='红' and S.CITY='天津')

34、求供应数量超过300的供应信息,包括供应商名,零件名,项目名和供应数量。

select SNAME,PNAME,JNAME,QTY from SPJ,S,P,J

where S.SNO=SPJ.SNO and P.PNO =SPJ.PNO

and J.JNO=SPJ.JNO and QTY>300

35、至少使用了供应商S1所供应的全部零件的工程号JNO。

select PNO from SPJ where SNO='S1';

select JNO from SPJ where (PNO='P1' and SNO='S1')

intersect select JNO from SPJ where (PNO='P2' and SNO='S1')

36、找出使用供应商S1所提供零件的工程号码。

select distinct JNO from SPJ where SNO='S1'

37、找出工程项目J2使用的各种零件的名称及其重量。

select PNAME,WEIGHT from P,SPJ

where SPJ.PNO=P.PNO and JNO='J2'

38、找出上海厂商供应的所有零件号码。

select PNO from S,SPJ where S.SNO=SPJ.SNO

and S.CITY='上海' group by PNO

39、找出使用上海产的零件的工程名称。

select JNO from S,SPJ

where S.SNO=SPJ.SNO and S.CITY='上海'

group by JNO

40、找出没有使用天津产的零件的工程号码。

select JNO from SPJ where SPJ.JNO not in (

select JNO from S,SPJ where S.SNO=SPJ.SNO and s.CITY='天津')

41、查询‘IS’系学生的学号、所选课程名称及该门课程的成绩。

select SC.Sno,Cname,Grade from SC,Course,student

where student.Sno=SC.Sno and o=o and Sdept='数学系'

42、查询‘CS’系成绩不及格的学生姓名。

select distinct Sname from Student,SC

where student.Sno=SC.Sno and Grade<60 and Sdept='计算机系'