sql语句练习题及答案.doc
文本预览下载声明
一 在数据库 school 中建立student , sc, course 表。
学生表、课程表、选课表属于数据库 School ,其各自的数据结构如下:
学生 Student (Sno,Sname,Ssex,Sage,Sdept)
序号
列名
含义
数据类型
长度
1
Sno
学号
字符型(char)
6
2
Sname
姓名
字符型(varchar)
8
3
Ssex
性别
字符型(char)
2
4
Sage
年龄
整数 (smallint)
5
sdept
系科
字符型(varchar)
15
课程表 course(Cno,Cname,Cpno,Ccredit)
序号
列名
含义
数据类型
长度
1
Cno
课程号
字符型(char)
4
2
cname
课程名
字符型(varchar)
20
3
Cpno
先修课
字符型(char)
4
4
Ccredit
学分
短整数 (tinyint)
学生选课 SC(Sno,Cno,Grade)
序号
列名
含义
数据类型
长度
1
Sno
学号
字符型(char)
6
2
Cno
课程号
字符型(char)
4
3
Grade
成绩
小数(decimal)
12,2
二 设定主码
1 Student表的主码:sno
2 Course表的主码:cno
3 Sc表的主码:sno,cno
1写出使用 Create Table 语句创建表 student , sc, course 的SQL语句
create table student
(sno char(6),
sname varchar(8),
ssex char(2),
sage smallint,
sdept varchar(15),
primary key(sno));
Create table course(
Cno char(4) primary key,
Cname varchar(20),
Cpno char(4),
Ccredit tinyint)
create table sc
(sno char(6),
cno char(4),
grade decimal(12,2),
primary key(sno,cno));
c
2在student表中插入信息
学号
姓名
性别
年龄
系科
4001
赵茵
男
20
SX
4002
杨华
女
21
JSJ
insert into student
values( ’4001’,’赵茵’,’男’,20,’SX’)
insert into student
values( ’4002’,’杨华’,’女’,21,’JXJ’)
Delete
1 删除所有 JSJ 系的男生
delete from Student where Sdept=’JSJ’ and Ssex=’男’;
2 删除“数据库原理”的课的选课纪录
delete from SC where Cno in (select Cno fromCourse where Cname=’数据库原理’);
Update
1 修改 0001 学生的系科为: JSJ
Update student set sdept=’JSJ’ where sno=’0001’
2 把陈小明的年龄加1岁,性别改为女。
Update student set age=age+1,ssex=’女’ where sname=’陈小明’
Select 查询语句
一 单表
1查询年龄在19至21岁之间的女生的学号,姓名,年龄,按年龄从大到小排列。
select sno, sname, sage
from student
where ssex=’女’ and sage between 19 and 21
order by sage desc;
2查询姓名中第2个字为“明”字的学生学号、性别。
select sno, ssex
from student
where sname like ’_明% ’ ;
3查询 1001课程没有成绩的学生学号、课程号
select sno, cno
from sc
where grade is null and cno=’1001’ ;
4查询JSJ 、SX、WL 系的年龄大于25岁的学生学号,姓名,结果按系排列
select sno, sname
from student
where sdept in (’JSJ’,’SX’,’WL’) and sage25
显示全部