mysql数据库实验答案.docx
《mysql数据库实验答案.docx》由会员分享,可在线阅读,更多相关《mysql数据库实验答案.docx(20页珍藏版)》请在冰豆网上搜索。
mysql数据库实验答案
实验一创立、修改数据库和表构造
1、用create建立教学数据库的五个根本表:
(1)学生表〔学号,,性别,年龄〕,student((Sno,sname,ssex,sage);
〔2〕课程表〔课程号,课程名,学分〕,Course(o,ame,credit);
〔3〕选课表〔学号,课程号,成绩〕,SC(Sno,,o,grade);
(4)教师表〔教师号,,性别,出生年月,系部,职称,地址〕,
T(Tno,Tname,ssex,birthday,dept,title,address);
(5)工资表〔教师号,根本工资,职务工资,合计〕,Salary(Tno,gz,zwgz,hj);
CreateDatabaseStudentdefaultcharactersetutf8defaultCOLLATEutf8_bin;
UseStudent;
CreateTableStudent(
SNochar(20)primarykey,
SNamechar(20),
SSexchar(4)default'男',
SAgeint
)ENGINE=InnoDB;
CreateTableCourse(
ochar(20)primarykey,
amechar(20)NOTNULL,
CReditfloat
)ENGINE=InnoDB;
CreateTableSC(
SNochar(20)NOTNULL,
ochar(20)NOTNULL,
Gradefloat,
PrimaryKey(SNo,o),
ForeignKey(SNo)ReferencesStudent(SNo)OnDeleteCascade,
ForeignKey(o)ReferencesCourse(o)
)ENGINE=InnoDB;
CreateTableT(
TNochar(20)PrimaryKey,
TNamechar(20)NOTNULL,
TSexchar(4)default'男',
birthdayDateTime,
deptchar(20),
titlechar(20),
addresschar(20)
)ENGINE=InnoDB;
CreateTableSalary(
TNochar(20)NOTNULL,
gzfloat,
zwgzfloat,
hjfloat,
ForeignKey(TNo)ReferencesT(TNo)OnDeleteCascade
)ENGINE=InnoDB;
2、用alter修改根本表
〔1〕在已存在的学生表student中增加一个sdept〔系〕的新的属性列;
altertableStudentaddDeptchar(20);
〔2〕将学生表student中sname属性列的数据类型修改为变长字符串varchar(10)。
alterableStudentmodifycolumsnamevarchar(10)
3、建立一个临时表,然后将其删除
CreateTabletemp(
ANochar(20)NOTNULL,Bfloat,Cchar(10))
Droptabletemp
实验二建立与删除索引
1、用createindex在学生表student的学号sno上建立聚簇索引。
CreateClusteredIndexSNo_IndexOnStudent(SNo);
2、在学生表student中,为XXsname建立非聚簇索引。
CreateIndexSName_IndexOnStudent(SName);
3、在课程表的课程号o上建立唯一索引。
CreateUniqueIndexo_IndexOnCourse(o);
4、在选课表的学号sno、成绩Grade上建立复合索引,要求学号为升序,学号一样时成绩为
降序。
CreateIndexSo_IndexOnSC(SNoASC,GradeDESC);
5、用drop删除学生表student的索引。
DropIndexStudent.SNo_Index;
6、增加学生表student中XX唯一约束。
AlterTableStudentAddUnique(SName);
7、增加学生表student中性别‘男’、‘女’唯一约束。
AlterTableStudentAddConstraint:
SSexcheck(SSex='男'orSSex='女');
8、增加学生表student中年龄18~25岁约束。
AlterTableStudentAddConstraint:
SAgecheck(SAge>=18AndSAge<=25);
9、增加选课表SC中学号sno的外码约束。
AlterTableSCAddForeignKey(SNo)referencesStudent(SNo);
-
实验三数据的插入、更新及删除操作
1、用insert输入数据。
学生表student的数据
991201
X三
22
男
计算机系
991202
李四
21
男
信息系
991101
王五
23
男
数学系
991102
陈六
19
男
计算机系
991103
X七
24
女
数学系
000101
X八
22
女
信息系
InsertIntoStudentValues('991201','X三','男',22,'计算机科学与技术系');
InsertIntoStudentValues('991202','李四','男',21,'信息科学系');
InsertIntoStudentValues('991101','王五','男',23,'数理系');
InsertIntoStudentValues('991102','陈六','男',19,'计算机科学与技术系');
InsertIntoStudentValues('991103','X七','女',24,'数理系');
InsertIntoStudentValues('000101','X八','女',22,'信息科学系');
课程表course的数据
1
数学
5
2
数据构造
4
3
程序设计
2
4
数据库原理
3
5
操作系统
3
InsertIntoCourseValues('1','数学',5);
InsertIntoCourseValues('2','数据构造',4);
InsertIntoCourseValues('3','程序设计',2);
InsertIntoCourseValues('4','数据库原理',3);
InsertIntoCourseValues('5','操作系统',3);
选课表SC的数据
991201
1
90
991201
5
80
991201
3
85
991201
4
90
991102
1
85
991102
2
98
000101
2
91
InsertIntoSCValues('991201','1',90);
InsertIntoSCValues('991201','5',80);
InsertIntoSCValues('991201','3',85);
InsertIntoSCValues('991201','4',90);
InsertIntoSCValues('991102','1',85);
InsertIntoSCValues('991102','2',98);
InsertIntoSCValues('000101','2',91);
根本表T的数据
0001
X三
男
1968-10
信息
副教授
0002
李四
女
1956-11
信息
教授
1001
王五
男
1973-07
计算机
讲师
1008
陈六
男
1970-08
计算机
副教授
InsertIntoTValues('0001','X三','男','1968-10-10','信息科学系','副教授','');
InsertIntoTValues('0002','李四','女','1956-11-10','信息科学系','教授','');
InsertIntoTValues('1001','王五','男','1973-07-20','计算机科学与技术系','讲师','');
InsertIntoTValues('1008','陈六','男','1970-08-20','计算机科学与技术系','副教授','');
根本表Salary的数据
0001
1000
300
1300
0002
1500
500
2000
1001
800
200
1000
InsertIntoSalaryValues('0001',1000,300,1300);
InsertIntoSalaryValues('0002',1500,500,2000);
InsertIntoSalaryValues('1001',800,200,1000);*/
2、用delete删除数据记录
〔1〕删除教师表T中教师号为0001的元组。
〔2〕删除教师表T中的全部数据。
updatetsetbirthday='1961-10-04'whereTno='0001'
DeleteFromT;
3、用update更新数据记录
(1)把0001号教师的根本工资加100。
(2)把所有教师的根本工资都加100。
UpdateSalarySetgz=gz+100WhereTNo='0001'
UpdateSalarySetgz=gz+100
实验四数据的查询
1、简单查询,用select检索
〔1〕查询所有学生的根本情况。
select*fromstudent;
〔2〕查询教师每月应交纳的个人所得税。
selecthj*0.005asmonthshuifromSalary;
〔3〕查询X三与李四两位同学的根本情况。
select*fromstudentwheresname='X三'orsname='李四';
〔4〕查询9911班学生的根本信息〔规定学生学号的前四位是班级号〕。
select*fromstudentwheresnolike'9911%';
〔5〕查询所有年龄在20岁以下的学生XX及其年龄。
selectsname,sagefromstudentwheresage<20;
〔6〕查询选修了2门以上课程的学生