《《数据库原理及应用》实验指导.doc》由会员分享,可在线阅读,更多相关《《数据库原理及应用》实验指导.doc(12页珍藏版)》请在金锄头文库上搜索。
1、 理学院 信科082班 陈先国 3080942214实验1 创建数据库与数据表下面写出实现如下操作的SQL语句: (1) 创建供应系统“GYXT”数据库。 CREATE DATABASE GYXTON(NAME=GYXT_data,FILENAME=D:GYXT.mdf,SIZE=10,MAXSIZE=50,FILEGROWTH=5)LOG ON(NAME=GYXT_Log,FILENAME=D:GXTYData.ldf,SIZE=5,MAXSIZE=25,FILEGROWTH=5);(2) 建立供应商表S。 CREATE TABLE S( SNO char(5) not null uniqu
2、e, SNAME char(20) not null unique, CITY char(20);INSERT INTO S VALUES(S1,精益,天津);INSERT INTO S VALUES(S2,万胜,北京);INSERT INTO S VALUES(S3,东方,北京);INSERT INTO S VALUES(S4,丰泰隆,上海);INSERT INTO S VALUES(S5,康健,南京);(3) 建立零件表P。CREATE TABLE P( PNO char(2) not null PRIMARY KEY(PNO), PNAME char(20),COLOR char(20)
3、,WEIGHT smallint);INSERT INTO P VALUES(P1,螺母,红,12);INSERT INTO P VALUES(P2,螺栓,绿,17);INSERT INTO P VALUES(P3,螺丝刀,蓝,14);INSERT INTO P VALUES(P4,螺丝刀,红,14);INSERT INTO P VALUES(P5,凸轮,蓝,40);INSERT INTO P VALUES(P6,齿轮,红,30); (4) 建立工程项目表J。CREATE TABLE J( JNO char(5) not null unique, JNAME char(20) not null
4、 unique, CITY char(20);INSERT INTO J VALUES(J1,三建,北京);INSERT INTO J VALUES(J2,一汽,长春);INSERT INTO J VALUES(J3,弹簧厂,天津);INSERT INTO J VALUES(J4,造船厂,天津);INSERT INTO J VALUES(J5,机车厂,唐山);INSERT INTO J VALUES(J6,无线电厂,常州);INSERT INTO J VALUES(J7,半导体厂,南京);(5) 建立供应情况表SPJ。CREATE TABLE SPJ( SNO char(5), PNO cha
5、r(5), JNO char(5), QTY int);INSERT INTO SPJ VALUES(S1,P1,J1,200);INSERT INTO SPJ VALUES(S1,P1,J3,100);INSERT INTO SPJ VALUES(S1,P1,J4,700);INSERT INTO SPJ VALUES(S1,P2,J2,100);INSERT INTO SPJ VALUES(S2,P3,J1,400);INSERT INTO SPJ VALUES(S2,P3,J2,200);INSERT INTO SPJ VALUES(S2,P3,J4,500);INSERT INTO S
6、PJ VALUES(S2,P3,J5,400);INSERT INTO SPJ VALUES(S2,P5,J1,400);INSERT INTO SPJ VALUES(S2,P5,J2,100);INSERT INTO SPJ VALUES(S3,P1,J1,200);INSERT INTO SPJ VALUES(S3,P3,J1,200);INSERT INTO SPJ VALUES(S4,P5,J1,100);INSERT INTO SPJ VALUES(S4,P6,J3,300);INSERT INTO SPJ VALUES(S4,P6,J4,200);INSERT INTO SPJ V
7、ALUES(S5,P2,J4,100);INSERT INTO SPJ VALUES(S5,P3,J1,200);INSERT INTO SPJ VALUES(S5,P6,J2,200);INSERT INTO SPJ VALUES(S5,P6,J4,500);实验2 简单查询和连接查询 首先,写出下列操作的SQL语句,然后进入到SQL Server查询分析器中调试结果,并将结果抄写下来。1在教学管理JXGL数据库中进行如下操作:(1) 求数学系学生的学号和姓名。SELECT Sno,SnameFROM STUDENTWHERE Sdept=MA调试结果: Sno Snanme 1 95003
8、 王敏(2) 求选修了课程的学生学号。SELECT Sno FROM STUDENTWHERE Sno not in(select Sno from SC )调试结果: Sno 1 95003 2 95004(3) 求选修了数学课的学生学号和成绩,并要求对查询结果按成绩降序排列,如果成绩相同则按学号升序排列。SELECT STUDENT.Sno,Gradefrom STUDENT,SC,COURSEWHERE STUDENT.Sno=SC.Sno and COURSE.Cno=SC.Cno and Cname like (数学 )order by Grade desc,STUDENT.Sno
9、调试结果: Sno Grade1 95002 902 95001 85(4) 求选修数学课其且成绩在80-90之间的学生学号和成绩,并将成绩乘以系数0.8输出。SELECT DISTINCT Sno,0.8*Grade AS Grade FROM SCWHERE SC.Cno=2 AND SC.Grade BETWEEN 80 AND 90调试结果: Sno Grade1 95001 68.02 95002 72.0(5) 求数学系或计算机系姓刘的学生的信息。SELECT * FROM STUDENTWHERE (Sdept=MA OR Sdept=IS)AND Sname LIKE刘%调试结
10、果: Sno Sname Ssex Sage Sdept1 95002 刘晨 F 19 IS(6) 求缺少了成绩的学生的学号和课程号。SELECT Sno,Cno FROM SCWHERE SC.Grade IS NULL调试结果: Sno Cno (7) 查询每个学生的情况以及他(她)所选修的课程。SELECT STUDENT.Sno,Sname,Ssex,Sage,Sdept,Cname FROM STUDENT,COURSE,SCWHERE SC.Sno=STUDENT.Sno AND SC.Cno=COURSE.Cno调试结果: Sno Sname Ssex Sage Sdet Cna
11、me1 95001 李勇 M 20 CS 数据库2 95001 李勇 M 20 CS 数学3 95001 李勇 M 20 CS 信息系统4 95002 刘晨 F 19 IS 数学5 95002 刘晨 F 19 IS 信息系统(8) 求学生的学号、姓名、选修的课程名及成绩。SELECT STUDENT.Sno,Sname,Cname,Grade FROM STUDENT,COURSE,SCWHERE SC.Sno=STUDENT.Sno AND SC.Cno=COURSE.Cno调试结果: Sno Sname Cname Grade1 95001 李勇 数据库 922 95001 李勇 数学 8
12、53 95001 李勇 信息系统 884 95002 刘晨 数学 905 95002 刘晨 信息系统 80(9) 求选修数学课且成绩为90分以上的学生学号、姓名、及成绩。SELECT STUDENT.Sno,Sname,Grade FROM STUDENT,COURSE,SCWHERE SC.Sno=STUDENT.Sno AND SC.Cno=COURSE.Cno AND COURSE.Cname=数学 AND SC.Grade=90调试结果: Sno Sname Grade1 95002 刘晨 90(10)查询每一门课的间接先行课(即先行课的先行课)。SELECT T1.CNO, T1.C
13、PNO,T2.CPNO FROM COURSE T1,COURSE T2 WHERE T1.CPNO=T2.CNO调试结果: CNO CPNO CPNO1 1 5 7 2 3 1 53 4 64 5 7 65 7 62在供应系统GYXT数据库中进行如下操作:(1) 求供应工程J1零件的供应商号SNO。SELECT DISTINCT SNO FROM SPJWHERE SPJ.JNO=J1调试结果: SNO1 S12 S23 S34 S45 S5(2) 求供应工程J1零件P1的供应商号SNO。SELECT DISTINCT SNO FROM SPJWHERE SPJ.JNO=J1 AND SPJ.PNO=P1调试结果