数据库实验答

上传人:橙** 文档编号:333351842 上传时间:2022-09-02 格式:PDF 页数:11 大小:98.38KB
返回 下载 相关 举报
数据库实验答_第1页
第1页 / 共11页
数据库实验答_第2页
第2页 / 共11页
数据库实验答_第3页
第3页 / 共11页
数据库实验答_第4页
第4页 / 共11页
数据库实验答_第5页
第5页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《数据库实验答》由会员分享,可在线阅读,更多相关《数据库实验答(11页珍藏版)》请在金锄头文库上搜索。

1、1 实验一创建、修改数据库和表结构1、用 create建立教学数据库的五个基本表:(1)学生表(学号,姓名,性别,年龄),student(Sno,sname,ssex,sage);(2)课程表(课程号,课程名,学分),Course(Cno,Cname,credit);(3)选课表(学号,课程号,成绩),SC(Sno,Cno,grade);(4)教师表(教师号,姓名,性别,出生年月,系部,职称,地址),T(Tno,Tname,ssex,birthday,dept,title,address);(5)工资表(教师号,基本工资,职务工资,合计),Salary(Tno,jbgz,zwgz,hj);Cr

2、eate Database Student Use Student Create Table Student(SNo char(20)primary key,SName char(20),SSex char(4)default 男,SAge int);在 MySQL 中,每个表指明数据库引擎,字符集比较好,用下列语句!Create Table Student(SNo char(20)primary key,SName char(20),SSex char(4)default 男,SAge int)ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_b

3、in;Create Table Course(CNo char(20)primary key,CName char(20)NOT NULL,CRedit float,);Create Table SC(SNo char(20)NOT NULL,CNo char(20)NOT NULL,Grade float,名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 11 页 -2 Primary Key(SNo,CNo),Foreign Key(SNo)References Student(SNo)On Delete Cascade,Foreign Key(CNo)References C

4、ourse(CNo);Create Table T(TNo char(20)Primary Key,TName char(20)NOT NULL,TSex char(4)default 男,birthday DateTime,dept char(20),title char(20),address char(20);Create Table Salary(TNo char(20)NOT NULL,jbgz float,zwgz float,hj float,Foreign Key(TNo)References T(TNo)On Delete Cascade);2、用 alter 修改基本表(1

5、)在已存在的学生表student 中增加一个sdept(系)的新的属性列;alter table Student add Dept char(20);(2)将学生表student中 sname 属性列的数据类型修改为变长字符串varchar(10)。alter able Student alter colum sname varchar(10)3、建立一个临时表,然后将其删除Create Table temp(ANo char(20)NOT NULL,B float,C char(10)Drop tabte temp 实验二建立与删除索引1、用 create index 在学生表student

6、 的学号 sno上建立聚簇索引。Create Clustered Index SNo_Index On Student(SNo);2、在学生表student 中,为姓名sname建立非聚簇索引。名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 11 页 -3 Create Index SName_Index On Student(SName);3、在课程表的课程号Cno 上建立唯一索引。Create Unique Index CNo_Index On Course(CNo);4、在选课表的学号sno、成绩 Grade 上建立复合索引,要求学号为升序,学号相同时成绩为降序。Creat

7、e Index SCNo_Index On SC(SNo ASC,Grade DESC);5、用 drop 删除学生表student 的索引。Drop Index Student.SNo_Index;6、增加学生表student中姓名唯一约束。Alter Table Student Add Unique(SName);7、增加学生表student中性别男、女唯一约束。Alter Table Student Add Constraint:SSex check(SSex=男 or SSex=女);8、增加学生表student中年龄 1825 岁约束。Alter Table Student Add

8、Constraint:SAge check(SAge=18 And SAge=25);9、增加选课表SC 中学号 sno的外码约束。Alter Table SC Add Foreign Key(SNo)references Student(SNo);-实验三数据的插入、更新及删除操作1、用 insert 输入数据。学生表 student 的数据991201 张三22 男计算机系991202 李四21 男信息系991101 王五23 男数学系991102 陈六19 男计算机系991103 吴七24 女数学系000101 刘八22 女信息系Insert Into Student Values(99

9、1201,张三,男,22,计算机科学与技术系);Insert Into Student Values(991202,李四,男,21,信息科学系);名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 11 页 -4 Insert Into Student Values(991101,王五,男,23,数理系);Insert Into Student Values(991102,陈六,男,19,计算机科学与技术系);Insert Into Student Values(991103,吴七,女,24,数理系);Insert Into Student Values(000101,刘八,女,22

10、,信息科学系);课程表 course的数据1 数学5 2 数据结构4 3 程序设计2 4 数据库原理3 5 操作系统3 Insert Into Course Values(1,数学,5);Insert Into Course Values(2,数据结构,4);Insert Into Course Values(3,程序设计,2);Insert Into Course Values(4,数据库原理,3);Insert Into Course Values(5,操作系统,3);选课表 SC 的数据991201 1 90 991201 5 80 991201 3 85 991201 4 90 991

11、102 1 85 991102 2 98 000101 2 91 Insert Into SC Values(991201,1,90);Insert Into SC Values(991201,5,80);Insert Into SC Values(991201,3,85);Insert Into SC Values(991201,4,90);Insert Into SC Values(991102,1,85);Insert Into SC Values(991102,2,98);Insert Into SC Values(000101,2,91);基本表 T 的数据0001 张三男1968-

12、10 信息副教授湘潭0002 李四女1956-11 信息教授长沙1001 王五男1973-07 计算机讲师湘潭1008 陈六男1970-08 计算机副教授北京Insert Into T Values(0001,张三,男,1968-10-10,信息科学系,副教授,湘潭);Insert Into T Values(0002,李四,女,1956-11-10,信息科学系,教授,长沙);Insert Into T Values(1001,王五,男,1973-07-20,计算机科学与技术系,讲师,湘潭);名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 11 页 -5 Insert Into

13、T Values(1008,陈六,男,1970-08-20,计算机科学与技术系,副教授,北京);基本表 Salary 的数据0001 1000 300 1300 0002 1500 500 2000 1001 800 200 1000 Insert Into Salary Values(0001,1000,300,1300);Insert Into Salary Values(0002,1500,500,2000);Insert Into Salary Values(1001,800,200,1000);*/2、用 delete 删除数据记录(1)删除教师表T 中教师号为0001 的元组。(2

14、)删除教师表T 中的全部数据。update t set birthday=1961-10-04 where Tno=0001 Delete From T;3、用update 更新数据记录(1)把 0001 号教师的基本工资加100。(2)把所有教师的基本工资都加100。Update Salary Set jbgz=jbgz+100 Where TNo=0001 Update Salary Set jbgz=jbgz+100 实验四数据的查询1、简单查询,用select检索(1)查询所有学生的基本情况。select*from student;(2)查询教师每月应交纳的个人所得税。select h

15、j*0.005 as monthshui from Salary;(3)查询张三与李四两位同学的基本情况。select*from student where sname=张三 or sname=李四;(4)查询 9911 班学生的基本信息(规定学生学号的前四位是班级号)。select*from student where sno like 9911%;(5)查询所有年龄在20 岁以下的学生姓名及其年龄。名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 11 页 -6 select sname,sage from student where sage2;2、多表查询,用select检

16、索(1)查询教师的收入情况,包括教师号、姓名及月总收入。select T.Tno,Tname,hj/不能写成select Tno,因为 Tno 不明确from T,Salary where T.Tno=Salary.Tno;(2)查询每个学生的学号、姓名、选修课程及成绩。select student.sno,sname,cno,grade from student,sc where student.sno=sc.sno;(3)查询每一门课的间接先修课。select CA.cno AS 课程号,CB.PreCourse AS 间接先修课号from course CA,course CB where CA.PreCourse=CB.cno and CB.PreCourse is not null;(4)查询有相同地址的两位教师的信息。select*from T Tx where Tx.address in(select address from T Ty where Tx.TnameTy.Tname);select*from T Tx,T Ty where Tx.address=Ty.Ad

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 初中教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号