6实验六-数据库完整性

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验报告书

课程名《数据库原理与应用A 》题目:数据库完整性

班级:

学号:

姓名:

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)

)

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

SQLQuery6(例13.6,在test数据库中创建table8表,其中c2指定默认值为10,c3指定默认值为当前日期):

use test

go

create table table8

( c1 int,

c2 int default 2*5,

c3 datetime default getdate()

)

go

use test

insert table8(c1)values(1)

select*from table8

go

SQLQuery7(例13.7,在test数据库中创建table9表,并设置默认值,插入4个记录,输出所有行): use test

go

create table table9

( c1 smallint,

c2 smallint default 10*2,

c3 char(10),

c4 char(10)default'xyz',

)

go

create default con5 as'China'

go

exec sp_bindefault con5,'table13.c3'

go

insert into table9(c1)values(1)

insert into table9(c1,c2)values(2,50)

insert into table9(c1,c3)values(3,'Wuhan')

insert into table9(c1,c3,c4)values(4,'Beijing','Good')

select*from table9

go

SQLQuery8(例13.8,创建名为rule1的规则,限定输入的值在0-10之间):

use test

go

create rule rule1 as @c1 between 0 and 10

go

SQLQuery9(例13.9,创建名为rule2的规则,限定输入到该规则所绑定的列中的实际值只能是该规则列中的值):

相关文档
最新文档