6实验六-数据库完整性 下载本文

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

实验报告书

课程名《 数据库原理与应用A 》

题 目: 数据库完整性 班 级: 学 号: 姓 名:

评语: 成绩: 指导教师: 批阅时间: 年 月 日

《 数据库原理与应用A 》实验报告 - 1 -

1、实验内容或题目

数据库完整性

2、实验目的与要求

(1)掌握sql server 2005 数据库完整性的机理,理解并掌握约束。 (2)进行实验操作。

3、实验步骤与源程序

⑴ 实验步骤

? 掌握Sql server 2005基本语句的使用,主要是PRIMARY KEY约束、FOREIGN KEY约束、UNIQUE

约束、CHECK约束、NOT NULL。

? 练习Sql server 2005基本语句的使用。 ? 完成实验报告。 ⑵ 编写源代码

SQLQuery1(例13.1,在test数据库中创建department表,其中指定dno为主键):

use test go

create table department ( dno int primary key, dname char(20), ) go

SQLQuery2(例13.2,使用FOREIGN KEY子句进行相关操作):

use test go

create table worker ( no int primary key, name char(8), sex char(2), dno int

foreign key references department(dno) on delete no action, address char(30) )

《 数据库原理与应用A 》实验报告 - 2 -

go

SQLQuery3 (例13.3,在test数据库中创建table5表,其中指定c1列不能包含重复的值):

use test go

create table table5 ( c1 int unique, c2 int ) go

insert table5 values(1,100) go

/*插入下一行,则会出现错误消息*/ insert table5 values(1,200)

SQLQuery4(例13.4,在test数据库中创建table6表,其中使用CHECK约束限定f2列为0-100分):

use test go

create table table6 ( f1 int,

f2 int not null check(f2>=0 and f2<=100) ) go

/*插入下一行,则会出现错误消息*/ insert table6 values(1,120)

SQLQuery5(例13.5,在test数据库中创建table7表,其主键为c1和c2,将其中插入两个记录,输出这些记录):

use test go

create table table7 ( c1 int, c2 int, c3 char(5), c4 char(10),

constraint c1 primary key(c1,c2) ) go use test

insert table7 values(1,2,'ABC1','XYZ1') insert table7 values(1,2,'ABC2','XYZ2') go

select * from table7 go