sql复习题答案

发布 2022-09-02 20:14:28 阅读 9476

1、 创建一个数据库,数据库名称为自己名字的拼音。该数据库有1个10mb和一个20mb的数据文件和2个20mb的事务日志文件,数据文件分别位于d盘的data,e盘的 data文件夹下,两个数据文件的最大尺寸分别为无限大和100mb,增长速度分别为10%和1mb,事务日志文件都位于f盘的log目录下,最大尺寸均为50mb,文件增长为1mb

create database wyl

onname=shuju1,filename=’d:\data\

size=10,filegrowth=10%,maxsize=unlimited),filegroup shuju2

name=shuju2,filename=’e:\data\

size=20,filegrowth=1,maxsize=100),log on

name=rizhi1,filename=’f:\log\

size=20,filegrowth=1,maxsize=50),name=rizhi2,filename=’f:\log\

size=20,filegrowth=1,maxsize=50)

2、 为数据库名称为自己名字拼音的数据库添加一个文件组,其中包括两个数据文件,初识大小都为5mb,最大尺寸为500mb,增长速度为2mb

alter database wyl add filegroup data

goalter database wyl add fileg

name=shuju3,filename=’e:\data\

size=5,filegrowth=2,maxsize=500),name=shuju4,filename=’e:\data\

size=5,filegrowth=2,maxsize=500)

to filegroup data

3、 修改名称为自己名字拼音的数据库名称为newdb

alter database wyl modify name=newdb

4、 删除数据库名称为自己名字拼音的数据库的一个数据文件,该数据文件名为自己名字的拼音+data

alter database wyl remove file wyl_data

5、 创建“产品”数据库表。

产品(产品号 char(6),产品名称 char(20),**商号 char(6), ** decimal(10,2),库存量 int)其中主键为“产品号”,“**商号”的约束为【100001,200001】,库存量的默认值为0,每个字段都非空。

create table chanpin

cph char(6) primary key,cpname char(20) not null,gysh char(6) check(gysh>=100001 andgysh<=200001) not null,jiage decimal(10,2) not null,kcl int default(0) not null)

6、 创建成绩表(sno int ,cno int ,grade int)

其中 sno 和cno 为复合主键,sno为外键,参照 student(sno),cno 为外键,参照 course(cno),grade的范围在0到100之间。

create table cjb

sno int

foreign key references student(sno)

on delete cascade on update cascade,cno int

foreign key references course(cno)

on delete cascade on update cascade,grade int check(grade>=0 and grade<=100),primary key (sno,cno))

7、 为student表添加列 sbirthday, 该列为日期类型。

alter table student add sbirthday date

8、 为student 表删除列sbirthday

alter table student drop column sbirthday

9、 修改表student的名称为stu

sp_rename student,stu

10、 表student 共5列,为其中三列sno,sname,sage 插入数据分别为(1,‘mike’,20)

insert into student values(1,‘mike’,20)

11、 删除student表中姓张的学生信息。

delete from student where sname like ‘张%’

12、 修改成绩表sc(学号,课程号,成绩),将课程号为1的成绩都加上10

update sc set grade=grade+10 where cno=’1’

13、 表student(sno ,sname,sage)在sname 列上创建唯一索引,降序create unique index index_sname on student (sname desc)

14、 学生表student(学号,姓名,年龄,性别)

使用t_sql实现查询。

查询男生的人数、select count(*)from student where ssex=’ 男’

查询姓张的学生的年龄。

select sage from student where sname like’ 张%’

15、 学生表student(学号,姓名,年龄,性别)

成绩表 sc(学号,课程号,成绩)

课程表(课程号,课程名)

使用t_sql实现查询。

查询成绩大于90分的学生的姓名。

select sname from student where sno in

select sno from sc where grade >90)

查询女生的人数。

select count(*)from student where ssex=’ 女’

16、 学生表student(学号,姓名,年龄,性别)

成绩表 sc(学号,课程号,成绩)

课程表c(课程号,课程名)

使用t_sql实现查询。

选修数据库课程的学生的姓名。

select sname from student where sno in(

select sno from sc where cno=(

select cno from c where cname=’ 数据库’))

17、 成绩表 sc(学号,课程号,成绩),每个学生学习多门课程。

查询每个同学的平均成绩。

select **g (grade) from scgroup by sno;

查询平均成绩在60分以下的学生学号。

select sno,**g (grade) from sc group by sno h**ing **g (grade)<60

18、学生表student(学号,姓名,年龄,性别)

成绩表 sc(学号,课程号,成绩)

课程表(课程号,课程名)

使用嵌套查询实现:成绩大于90分的学生姓名。

select sname from student where sno in

select sno from sc where grade >90)

19、学生表student(学号,姓名,年龄,性别)

成绩表 sc(学号,课程号,成绩)

课程表(课程号,课程名)

使用嵌套实现:删除成绩小于60分的学生的信息。

delete from student where sno in

(select sno from sc where grade <60)

将数据库课程的分数加10分。

update sc set grade=grade+10 where cno=

select cno from c where cname= ’数据库’)

20、学生表student(学号,姓名,年龄,性别)

创建触发器,实现不能插入学号大于50的学生信息。

create trigger insert_stu

on student

after insert as if

(select sno from inserted)>50

beginprint’too much’

rollback

end21、成绩表 sc(学号,课程号,成绩)

创建触发器,实现成绩的修改不能超过5

create trigger update_sc

on sc

after update

asdeclare @a int select @a=grade from deleted,declare @n int select @n=grade from inserted

if abs (@a-@n)>5

beginprint ‘too much’

rollback

end22、学生表student(学号,姓名,年龄,性别)

创建存储过程,查询某个年龄的学生人数。

create proc sage

cc int

as declare @n int

select count(*)from student where sage =@cc

return @n

SQL理论复习题答案

1 d 2 b3 c 4 a5 d 6 d7 d 8 c9 c 分析 a选项 主键为单一属性,属于第二范式,本选项虽然存在学号 身份证号,身份证号 姓名,但不能说姓名传递依赖于学号,因为身份证号 学号,本选项由于任一非主属性都不传递依赖于关键字,所以属于第三范式。b选项,主键为单一属性,属于第二范式...

SQL总复习题 1答案

一 单选题。1.sql server提供的单行注释语句是使用 开始的一行内容。a.b.c.d.b2.不属于sql server系统全局变量的是 a.error b.connections c.fetch status d.records d3.在 连接 组中有两种连接认证方式,其中在 方式下,需要客...

SQL复习题

创建数据库 创建表 创建视图 创建触发器的 保存在考试结果文件夹下,文件名为。1 创建数据库。为 商品 系统 创建后台数据库 1 数据库名为spgy。2 主数据文件逻辑名称为spgy data,物理名称为spgy 存放在c盘根目录下,初始大小为3mb,文件增长不受限制,增长量为1mb。3 事务日志文...