数据库实验六

更新时间:2023-10-20 23:08:01 阅读量: 综合文库 文档下载

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

6 综合型实验项目索引和数据完整性

匹配课程代码及名称:070785,数据库应用设计

适用专业及本项目实验学时:计算机科学与技术专升本,6学时 一、实验目的及要求

(1)掌握索引的使用方法; (2)掌握索引的设计与实现方法。 (3)掌握数据完整性的类型; (4)掌握数据完整性的实现方法。

二、 实验内容

在已建好的YGGL数据库中,设计并创建索引,并进行索引的重建、删除操作。提交程序源代码(电子版,1周内)和实验报告(纸制、1周内)。 三、实验条件及设备要求

已安装SQL Server 2008数据库管理系统的实验机。 四、实验相关知识点

数据库实体完整性、索引。 五、实验实施步骤

(一)建立索引

1、对YGGL数据库的Employees表中的DepartmentID列建立索引。

createindexdepart_ind

onEmployees(DepartmentID)

2、在Employees表的Name列和Address列上建立复合索引。

create index ad_ind on Employees(Name,Address)

3、对Departments表上的DepartmentName列建立唯一非聚集索引。

go

create unique index dep_ind on Departments(DepartmentName)

go

(二)重建索引

1、重建表Employees中的所有索引。

alter index all on Employees rebuild

(三)删除索引。

1、使用DROP INDEX语句删除表Employees上的索引Depart_ind。

drop index Depart_ind on Employees

2、使用DROP INDEX一次删除Employees表上的多个索引。

go

drop index Departments.Dep_ind,Employees.Ad_ind

go

(四)数据完整性操作

1、创建一个表Employees5,只含EmployeesID,Name,Sex和Education列。将Name,设为主键,作为列Name的约束。对EmployeesID列进行UNIQUE约束,并作为表的约束。

create table Employees5( EmployeesID char(6) not null, Name char(10) not null primary key, Sex tinyint,

Education char(4),

constraint UK_id unique(EmployeesID) )

2、删除上题中创建的UNIQUE约束。

alter table Employees5

drop constraint UK_id

3、使用T-SQL命令创建一个新表,使用一个复合列作为主键,作为表的约束,并为其命名。

create table Employees7 (

EmployeeID char(6) not null, Name char(10) not null, Education char(4) not null, Birthday date not null, Sex bit not null default 1, WorkYear tinyint null, Address varchar(40) null, PhoneNumber char(12) null, DepartmentID char(3) not null,

primary key (EmployeeID,DepartmentID),

constraint ED_UK unique(EmployeeID,DepartmentID)

)

4、使用语句为表ALTER TABLEEmployees5添加一个新列Address,并为该列定义UNIQUE约束,并了解如何使用图形向导方式删除主键和UNIOQUE约束。

alter table Employees5

add Address varchar(40),

constraint AD_UK unique (Address)

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

Top