新浪博客

sql server 2005上机实操习题及答案

2013-11-14 13:48阅读:

实验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
(
name = test_data,
filename = 'd:\test\test_data.mdf',
size = 5MB,
maxsize = unlimited,
filegrowth = 1MB
)
LOG ON
(
name = test_log,
filename = 'd:\test\test_log.ldf',
size = 1MB,
maxsize = 5MB,
filegrowth = 10%
)
GO

2.查看数据库属性:

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

3.删除数据库:

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

实验2 表操作

1.创建表:

操作2.1:创建学生表:
表名:student

说明:学生基本信息表
属性列
数据类型
长度
空值
列约束
说明
st_id
nVarChar
9
Not Null
PK
学生学号
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
nChar
20
Null

学生来源
st_dpid
nVarChar
2
Null

所在系编号
st_mnt
tinyint

Null

学生职务

参考答案:
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

说明:课程信息表
属性列
数据类型
长度
空值
列约束
说明
cs_id
nVarChar
4
Not Null
PK
课程编号
cs_nm
nVarChar
20
Not Null

课程名称
cs_tm
int

Null

课程学时
cs_sc
int

Null

课程学分

参考答案:
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

说明:选课表
属性列
数据类型
长度
空值
列约束
说明
cs_id
nVarChar
4
Not Null
FK
课程编号
st_id
nVarChar
9
Not Null
FK
学生编号
score
int

Null

课程成绩
sltdate
datetime

Null

选课日期

参考答案:
USE test
GO
CREATE TABLE couse
(
cs_id nVarChar(4) NOT NULL ,
st_id nVarChar(9) NOT NULL ,
score int NULL ,
sltdate datetime NULL
)
GO
操作2.4:创建院系信息表:
表名:dept

说明:院系信息表
属性列
数据类型
长度
空值
列约束
说明
dp_id
nVarChar
2
Not Null

系编号
dp_nm
nVarChar
20
Not Null

院系名称
dp_drt
nVarChar
8
Null

院系主任
dt_tel
nVarChar
12
Null

联系电话

参考答案:
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
参考答案:
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.规则约束对象

操作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)

我的更多文章

下载客户端阅读体验更佳

APP专享