SQL操作复习题及答案

更新时间:2023-11-12 11:54:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

- 1 -

1 数据库操作

1.创建数据库:

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

参考答案:

create database Test ON primary ( ) LOG ON ( ) GO

name = test_log,

filename = 'd:\\test\\test_log.ldf', size = 1MB, maxsize = 5MB, filegrowth = 10% name = test_data,

filename = 'd:\\test\\test_data.mdf', size = 5MB,

maxsize = unlimited, filegrowth = 1MB

2.查看数据库属性:

操作1.2:使用T-SQL语句查看数据库test属性 参考答案:

EXEC sp_helpdb test

3.删除数据库:

操作1.3:使用T-SQL语句删除数据库test 参考答案:

drop database Test

2 表操作

1.创建表:

操作2.1:创建学生表:

表名:student 长度 说明:学生基本信息表 空值 列约束 说明 属性列 数据类型 - 2 -

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 9 8 2 20 2 Not Null Not Null Null Null Null Null Null Null Null PK 学生学号 学生姓名 学生性别 出生日期 入学成绩 入学日期 学生来源 所在系编号 学生职务 参考答案:

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:创建选课表:

- 3 -

表名:slt_couse 属性列 cs_id st_id score sltdate 数据类型 nVarChar nVarChar int datetime 长度 4 9 说明:选课表 空值 Not Null Not Null Null Null 列约束 FK FK 说明 课程编号 学生编号 课程成绩 选课日期 参考答案: USE test GO

CREATE TABLE 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

2.修改表结构:

(1)向表中添加列:

操作2.5:为“dept”表添加“dp_count”列(数据类型为nvarchar,长度为3,允许为空) 参考答案:

ALTER TABLE dept ADD dp_count nvarchar(3) NULL (2)修改列数据类型:

操作2.6:修改“dept”表的“dp_count”列数据类型为int 参考答案:

- 4 -

ALTER TABLE dept ALTER COLUMN dp_count int NULL (3)删除表中指定列:

操作2.7:删除“dept”表的“dp_count”列 参考答案:

ALTER TABLE dept DROP COLUMN dp_count

3.删除表

操作2.8:删除“dept”表 参考答案:

DROP TABLE student

4.向表中输入数据记录

操作2.9:分别向“student”表、“couse”表、“slt_couse”表、“dept”表中输入数据记录

3 数据完整性

1.空值约束( NULL )

操作3.1:将student表中的st_sex列属性更改为NOT NULL 参考答案:

ALTER TABLE student ALTER COLUME st_nm nVarChar(8) NOT NULL

2.默认值约束( DEFAULT )

操作3.2:将student表中的st_from列默认值设置为“陕西省” 参考答案:

ALTER TABLE student ADD DEFAULT '陕西省' FOR st_from

3.默认值对象

操作3.3:创建默认值对象df_today为当前日期,并将其绑定到slt_couse表中的sltdate列,然后取消绑定,最后删除默认值对象df_today。

参考答案:

CREATE DEFAULT df_today AS Getdate( ) GO

EXEC sp_bindefault df_today, 'slt_couse.sltdate' GO

EXEC sp_unbindefault 'slt_couse.sltdate' GO

DROP DEFAULT df_today GO

4.检查约束( CHECK )

操作3.4:将slt_couse表中的score列的检查约束设置为>=0且<=100 参考答案:

ALTER TABLE slt_couse ADD CHECK (score>=0 AND score<=100)

- 5 -

5.规则约束对象

操作3.5:创建规则约束对象rl_sex,用于检查性别的取值仅限于“男”和“女”,并将其绑定到student表中的st_sex列,然后取消绑定,最后删除规则约束对象rl_sex。

参考答案:

CREATE RULE rl_sex AS @chksex ’男’ OR @chksex=’女’ 或

CREATE RULE rl_sex AS @chksex IN (’男’, ’女’) GO

EXEC sp_bindrule rl_sex, 'student.st_sex' GO

EXEC sp_unbindrule 'student.st_sex' GO

DROP RULE rl_sex GO

6.主键

操作3.6:将dept表中的dp_id列设置为主键 参考答案:

ALTER TABLE dept ADD PRIMARY KEY (dp_id)

7.唯一性约束( UNIQUE )

操作3.7:将dept表中的dp_nm列设置为唯一性约束 参考答案:

ALTER TABLE dept ADD UNIQUE (dp_nm)

8.标识列

操作3.8:向slt_couse表中添加标识列id,第1行默认值为1,相邻两个标识列间的增量为1 参考答案:

ALTER TABLE slt_couse ADD id INT IDENTITY(1,1) NOT NULL

9.外键( FOREIGN KEY )

操作3.9:被参照表为dept,参照表为student 参考答案:

ALTER TABLE student

ADD FOREIGN KEY (st_dpid) REFERENCES dept(dp_id)

4 数据更新

1.表中插入数据

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

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

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

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

本文来源:https://www.bwwdw.com/article/kzlv.html

Top