SQL-Server2012综合练习题1 - 参考答案 下载本文

内容发布更新时间 : 2024/5/2 12:42:11星期一 下面是文章的全部内容请认真阅读。

数据库综合练习题

SQL Server数据库操作

1.创建数据库:

操作1.1:创建一个test数据库,其主数据文件逻辑名test_data,物理文件名test_data.mdf,初始大小10MB,最大尺寸为无限大,增长速度1MB;数据库日志文件逻辑名称为test_log,物理文件名为test_log.ldf,初始大小为1MB,最大尺寸为5MB,增长速度为10%。

2.创建表:

操作2.1:创建学生表:

表名:student 长度 9 8 2 20 2 说明:学生基本信息表 空值 Not Null Not Null Null Null Null Null Null Null Null 列约束 PK 说明 学生学号 学生姓名 学生性别 出生日期 入学成绩 入学日期 学生来源 所在系编号 学生职务 属性列 st_id st_nm st_sex st_birth st_score st_date st_from st_dpid st_mnt 数据类型 nVarChar nVarChar nVarChar datetime int datetime nChar nVarChar tinyint 参考答案:

USE test GO

CREATE TABLE student (

st_id nVarChar(9) primary key NOT NULL, st_nm nVarChar(8) NOT NULL, st_sex nVarChar(2) NULL, st_birth datetime NULL, st_score int NULL, st_date datetime NULL, st_ from nVarChar(20) NULL , st_dpid nVarChar(2) NULL , st_ mnt tinyint NULL ) GO 操作2.2:创建课程信息表:

表名:couse 长度 4 20 说明:课程信息表 空值 Not Null Not Null Null Null 列约束 PK 说明 课程编号 课程名称 课程学时 课程学分 属性列 cs_id cs_nm cs_tm cs_sc 数据类型 nVarChar nVarChar int int 数据库综合练习题

参考答案:

USE test GO

CREATE TABLE couse (

cs_id nVarChar(4) primary key NOT NULL, cs_nm nVarChar(20) NOT NULL, cs_tm int NULL , cs_sc int NULL ) GO 操作2.3:创建选课表:

表名:slt_couse 长度 4 9 说明:选课表 空值 Not Null Not Null Null Null 列约束 FK FK 说明 课程编号 学生编号 课程成绩 选课日期 属性列 cs_id st_id score sltdate 参考答案:

USE test GO

数据类型 nVarChar nVarChar int datetime CREATE TABLE slt_couse (

cs_id nVarChar(4) NOT NULL, st_id nVarChar(9) NOT NULL, score int NULL , sltdate datetime ) GO

操作2.4:创建院系信息表:

表名:dept NULL

长度 2 20 8 12 说明:院系信息表 空值 Not Null Not Null Null Null 列约束 说明 系编号 院系名称 院系主任 联系电话 属性列 dp_id dp_nm dp_drt dt_tel 数据类型 nVarChar nVarChar nVarChar nVarChar 参考答案:

USE test GO

CREATE TABLE dept (

dp_id nVarChar(2) NOT NULL, dp_nm nVarChar(20) NOT NULL, dp_drt nVarChar(8) NULL, dp_tel nVarChar(12) NULL

数据库综合练习题

) GO

3.表中插入数据

操作3.1:向dept表插入一条记录,系号11,系名自动控制系,系主任为李其余,电话81234567

INSERT INTO deptVALUES('11', '自动控制系', '李其余','81234567')

操作3.2:向student表插入一条记录,学号070201001,姓名为王小五,性别为男,出生日期为1990年9月9日,系号为11,其余字段为NULL或默认值

INSERT INTO student(st_id, st_nm, st_sex, st_birth, st_dpid)

VALUES ('070201001', '王小五', '男', '1990.9.9', '11' )

操作3.3:向couse表插入一条记录,课程号1234,课程名为操作系统,其余字段为NULL或默认值

INSERT INTO couse(cs_id, cs_nm)VALUES ('1234', '操作系统')

操作3.4:向slt_couse表插入一条记录,课程号1234,学名070201001,其余字段为NULL或默认值

INSERT INTO slt_couse(cs_id, st_id)VALUES ('1234', '070201001')

4.修改表中数据

操作4.1:修改student表记录,将王小五的入学成绩改为88

UPDATE student SET st_score=88WHERE st_nm='王小五' 操作4.2:修改couse表记录,将所有记录的学分改为4,学时改为64

UPDATE couse SET cs_tm=64, cs_sc=4

操作4.3:修改slt_couse表记录,将课程号为1234,学名为070201001的记录的成绩改为77

UPDATE slt_couse SET score=77 WHERE cs_id='1234' AND st_id='070201001'

5.删除表中数据

操作5.1:删除slt_couse表记录,将课程号为1234,学名为070201001的记录删除

DELETE FROM slt_couse WHERE cs_id='1234' AND st_id='070201001' 操作5.2:删除couse表记录,将课程号为1234的记录删除

DELETE FROM couse WHERE cs_id='1234'

6.简单查询

(1)查询表中所有的列 操作6.1:查询所有系的信息

SELECT * FROM dept

(2)查询表中指定列的信息

操作6.2:查询所有的课程号与课程名称

SELECT cs_id, cs_nm FROM couse

(3)在查询列表中使用列表达式

操作6.3:在查询student表时使用列表达式:入学成绩+400

SELECT st_id,st_nm,st_score,st_score+400 AS new_score FROM student

(4)重新命名查询结果

操作6.4:使用AS关键字为dept表中属性指定列名:系号、系名、系主任、联系电话

SELECT dp_id AS 系号, dp_nm AS 系名,dp_drt AS 系主任, dp_tel AS联系电话 FROM dept 操作6.5:使用\号为couse表中属性指定列名:课程号、课程名、学时(=cs_sc*16)、学分