mysql数据库实验答案
- 格式:doc
- 大小:227.50 KB
- 文档页数:50
实验一创建、修改数据库和表结构
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);
Create Database Student default character set utf8 default COLLATE utf8_bin;
Use Student;
Create Table Student(
SNo c har(20) primary key,
SName char(20) ,
SSex char(4) default '男',
SAge int
) ENGINE=InnoDB;
Create Table Course(
CNo c har(20) primary key,
CName char(20) NOT NULL,
CRedit f loat
) ENGINE=InnoDB;
Create Table SC(
SNo c har(20) NOT NULL,
CNo c har(20) NOT NULL,
Grade float,
Primary Key(SNo, CNo),
Foreign Key(SNo) References Student(SNo) On Delete Cascade,
Foreign Key(CNo) References Course(CNo)
)ENGINE=InnoD B;
Create Table T(
TNo c har(20) Primary Key,
TName char(20) NOT NULL,
TSex char(4) default '男',
birthday DateTime,
dept char(20),
title char(20),
address char(20)
)ENGINE=InnoDB;
Create Table Salary(
TNo c har(20) NOT NULL,
jbgz float,
zwgz float,
hj float,
Foreign Key(TNo) References T(TNo) On Delete Cascade
)ENGINE=InnoDB;
2、用alter修改基本表
(1)在已存在的学生表student中增加一个sdept(系)的新的属性列;
alter table Student add Dept char(20);
(2)将学生表student中sname属性列的数据类型修改为变长字符串varchar(10)。
alter able Student modify colum sname varchar(10)
3、建立一个临时表,然后将其删除
Create Table temp (
ANo c har(20) NOT NULL,B float, C char(10) )
Drop table temp
实验二建立与删除索引
1、用create index在学生表student的学号sno上建立聚簇索引。
Create Clustered Index SNo_Index On Student(SNo);
2、在学生表student中,为姓名sname建立非聚簇索引。
Create Index SName_Index On Student(SName);
3、在课程表的课程号Cno上建立唯一索引。
Create Unique Index CNo_Index On Course(CNo);
4、在选课表的学号sno、成绩Grade上建立复合索引,要求学号为升序,学号相同时成绩为
降序。
Create Index SCNo_Index On SC(SNo ASC, Grade DESC);
5、用drop删除学生表student的索引。
Drop Index ;
6、增加学生表student中姓名唯一约束。
Alter Table Student Add Unique(SName);
7、增加学生表student中性别‘男’、‘女’唯一约束。
Alter Table Student Add Constraint:SSex check(SSex = '男' or SSex = '女');
8、增加学生表student中年龄18~25岁约束。
Alter Table Student Add Constraint:SAge check(SAge >= 18 And SAge <= 25);
9、增加选课表SC中学号sno的外码约束。
Alter Table SC Add Foreign Key(SNo) references Student(SNo);
-
实验三数据的插入、更新及删除操作
1、用insert输入数据。