6、 综合练习
设有如下 4 个基本表(表结构与表内容是假设的),如表 2-4、表 2-5、表 2-6、表 2-7 所示,请先创建数据库(stcgl)及根据表内容创建表结构,并添加表记录,实践以下各题 的 SQL 命令操作:
表 2-4 学生表(Student)表结构
字段名 |
数据类型 |
长度 |
是否允许为空值 |
说明 |
SNO |
字符型(char) |
6 |
× |
学号,主键 |
SNAME |
字符型(varchar) |
20 |
× |
姓名 |
SEX |
字符型(char) |
2 |
|
性别,默认”男“ |
SBIRTH |
日期型(datetime) |
|
√ |
年龄 |
CLASS |
字符型(varchar) |
4 |
√ |
班级名 |
表 2-5 成绩表(SC)表结构
字段名 |
数据类型 |
长度 |
是否允许为空值 |
说明 |
SNO |
字符型(char) |
6 |
× |
学号,主键,外键 |
CNO |
字符型(varchar) |
4 |
× |
课程号,主键,外键 |
GRADE |
精确值型(decimal) |
decimal(4,1) |
√ |
成绩,0~100之间 |
表 2-6 教师表(Teacher)表结构
字段名 |
数据类型 |
长度 |
是否允许为空值 |
说明 |
TNO |
字符型(char) |
3 |
× |
教师编号,主键 |
TNAME |
字符型(varchar) |
20 |
× |
教师姓名 |
SEX |
字符型(char) |
2 |
√ |
性别,默认”男“ |
TBIRTH |
日期型(datetime) |
|
√ |
年龄 |
PROF |
字符型(char) |
6 |
√ |
班级名 |
DEPT |
字符型(varchar) |
20 |
√ |
班级名 |
表 2-7 课程表(Course)表结构
字段名 |
数据类型 |
长度 |
是否允许为空值 |
说明 |
CNO |
字符型(char) |
4 |
× |
课程编号,主键 |
CNAME |
字符型(varchar) |
20 |
× |
课程名 |
TNO |
字符型(char) |
3 |
√ |
教师编号 |
学生表:Student 表中现有记录为:
SNO |
SNAME |
SEX |
SBIRTH |
CLASS |
980101 |
李华 |
男 |
1995-10-1 |
9801 |
980102 |
张军 |
男 |
1994-8-8 |
9801 |
980103 |
王红 |
女 |
1995-5-12 |
9801 |
980301 |
黄华 |
女 |
1996-12-23 |
9803 |
980302 |
大卫 |
男 |
1997-6-9 |
9803 |
980303 |
赵峰 |
男 |
1993-7-5 |
9803 |
980304 |
孙娟 |
女 |
1992-4-17 |
9803 |
成绩表:SC 表中现有记录为:
SNO |
CNO |
GRADE |
980101 |
8104 |
67 |
980101 |
8105 |
86 |
980102 |
8244 |
96 |
980102 |
8245 |
76 |
980103 |
8104 |
86 |
980103 |
8105 |
56 |
980301 |
8244 |
76 |
980302 |
8245 |
96 |
980302 |
8104 |
45 |
980302 |
8105 |
85 |
980303 |
8244 |
76 |
980303 |
8245 |
79 |
980304 |
8104 |
86 |
980304 |
8105 |
95 |
教师表:Teacher 表中现有记录为:
TNO |
TNAME |
SEX |
TBIRTH |
PROF |
DEPT |
801 |
李新 |
男 |
1973-10-4 |
副教授 |
计算机系 |
802 |
钱军 |
男 |
1965-8-6 |
教授 |
计算机系 |
803 |
王立 |
女 |
1980-4-19 |
副教授 |
食品系 |
804 |
李丹 |
女 |
1985-11-23 |
讲师 |
食品系 |
课程表:Course 表中现有记录为:
CNO |
CNAME |
TNO |
8104 |
计算机导论 |
801 |
8105 |
C 语言 |
802 |
8244 |
数据库系统 |
803 |
8245 |
数据结构 |
804 |
use Stcgl01
create table Student(
SNO char(6) not null primary key,
SNAME varchar(20) not null,
SEX char(2) default('男'),
SBIRTH datetime,
CLASS char(4),
)
create table SC (
SNO char(6) not null,
CNO char(4) not null,
GRADE decimal(4,1),
)
alter table SC add constraint pk_name primary key(SNO,CNO)
select *
from SC
create table Teacher(
TNO char(3) not null primary key,
TNAME varchar(20) not null,
SEX char(2) default('男'),
TBIRTH datetime,
PROF char(6),
DEPT varchar(20),
)
select *
from Teacher
create table Course(
CNO char(4) not null primary key,
CNAME varchar(20) not null,
TNO char(3),
)
select *
from Course
insert into Student values ('980101','李华','男','1995-10-1','9801')
insert into Student values ('980102','张军','男','1994-8-8','9801')
insert into Student values ('980103','王红','女','1995-5-12','9801')
insert into Student values ('980301','黄华','女','1999-12-23','9803')
insert into Student values ('980302','大卫','男','1997-6-9','9803')
insert into Student values ('980303','赵峰','男','1993-7-5','9803')
insert into Student values ('980304','孙娟','女','1992-4-17','9803')
update Student set Student.SBIRTH = '1995-10-1' where Student.SNO = '980101'
update Student set Student.SBIRTH = '1994-8-8' where Student.SNO = '980102'
update Student set Student.SBIRTH = '1995-5-12' where Student.SNO = '980103'
update Student set Student.SBIRTH = '1999-12-23' where Student.SNO = '980301'
update Student set Student.SBIRTH = '1997-6-9' where Student.SNO = '980302'
update Student set Student.SBIRTH = '1993-7-5' where Student.SNO = '980303'
update Student set Student.SBIRTH = '1992-4-17' where Student.SNO = '980304'
select SNO,SNAME,SEX,SBIRTH,CLASS
from Student
insert into SC values ('980101','8104',67)
insert into SC values ('980101','8105',86)
insert into SC values ('980102','8244',96)
insert into SC values ('980102','8245',76)
insert into SC values ('980103','8104',86)
insert into SC values ('980103','8105',56)
insert into SC values ('980301','8244',76)
insert into SC values ('980302','8245',96)
insert into SC values ('980302','8104',45)
insert into SC values ('980302','8105',85)
insert into SC values ('980303','8244',76)
insert into SC values ('980303','8245',79)
insert into SC values ('980304','8104',86)
insert into SC values ('980304','8105',95)
select *
from SC
insert into Teacher values('801','李新','男','1973-10-4','副教授','计算机系')
insert into Teacher values('802','钱军','男','1965-8-6','教授','计算机系')
insert into Teacher values('803','王立','女','1980-4-19','副教授','食品系')
insert into Teacher values('804','李丹','女','1985-11-23','讲师','食品系')
insert into Teacher values('805','张三','男','1990-10-20','教授','土木系')
update Teacher set Teacher.TBIRTH = '1973-10-4' where Teacher.TNO = '801'
update Teacher set Teacher.TBIRTH = '1965-8-6' where Teacher.TNO = '802'
update Teacher set Teacher.TBIRTH = '1980-4-19' where Teacher.TNO = '803'
update Teacher set Teacher.TBIRTH = '1985-11-23' where Teacher.TNO = '804'
select *
from Teacher
insert into Course values('8104','计算机导论','801')
insert into Course values('8105','C语言','802')
insert into Course values('8244','数据库系统','803')
insert into Course values('8245','数据结构','804')
select *
from Course