数据库原理与应用实验3-1_第1页
数据库原理与应用实验3-1_第2页
数据库原理与应用实验3-1_第3页
数据库原理与应用实验3-1_第4页
数据库原理与应用实验3-1_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验实验 3 1 数据完整性管理数据完整性管理 一 实验目的 掌握域完整性的实现方法 掌握实体完整性的实现方法 掌握参照完整性的方法 二 实验内容 数据库的完整性设置 三 实验步骤 可视化 1 实体完整性 2 域完整性 3 参照完整性 命令方式 1 实体完整性 1 将 student 表的 sno 字段设为主键 alter table student add constraint pk sno primary key sno 2 添加一身份证号字段 设置其惟一性 注 操作前应删除表中的所有记录 Alter table student add id char 18 unique id 3 将 sc 表的 sno 和 cno 设置为主键 alter table sc add constraint PK SnoCno primary key sno cno 2 域完整性 4 将 ssex 字段设置为只能取 男 女 两值 alter table student add constraint CK Sex check ssex in 男 女 5 设置学号字段只能输入数字 alter table student add constraint CK Sno Format check sno like 0 9 0 9 0 9 0 9 0 9 6 设置身份证号的输入格式 alter table student add constraint CK ID Format check id like 0 9 0 9 0 9 0 9 0 9 0 9 1 2 0 9 0 9 0 9 0 1 0 9 0 3 0 9 0 9 0 9 0 9 OR id like 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 1 0 9 0 3 0 9 0 9 0 9 0 9 7 设置 18 位身份证号的第 7 位到第 10 位为合法的年份 1900 2050 alter table student add constraint CK ID Format2 check len id 18 and convert smallint substring id 7 4 1900 and convert smallint substring id 7 4 22 or ssex 女 and sage 20 3 参照完整性 9 将 student 表和 sc 表中的 sno 字段设为参照 alter table sc add constraint FP sno foreign key sno references student sno 完整性验证 1 实体完整性 在 student 表数据浏览可视化界面中输入学号相同的两条记录将会出 现错误或者在命令窗口输入下面两条命令也会出现错误提示 insert into student values 95003 张三 男 24 CSinsert into student values 95001 李四 女 21 CS下面的语句用来验证 sc 表中的实体完整性 insert into sc values 95002 10 65 insert into sc values 95002 10 90 2 域完整性 使用下面的语句验证 ssex 字段的域完整性 insert into student values 95009 张匀 大 20 CS3 参照完整性 使用下面的语句 验证 sc 表中的 sno 字段的域完整性 假设 student 表中没有学号为 95998 的学生记录 insert into sc values 98998 10 98 四 实验任务 1 建立课程的实体完整性 和课程号 cno 的参照完整性 2 对 HRM 数据库 练习建立三个表的主外键约束 唯一约束 取空值约束 用户自定 义 的约束 参考 HRM 数据库表定义图中说明 主键约束 Departments alter table departments add constraint FK DEPARTMENTID primary key dep artmentid Employee alter table employee add constraint FK EMPLOYEEID primary key employeeid 外键约束 Salary alter table salary add constraint FK EmployeeID foreign key EmployeeID referen ces Employee Employeeid Employee alter table employee add constraint FK EMPLOYEE DEPARTMENT foreign k ey DepartmentID references Departments DepartmentID 唯一约束 Departments alter table Departments add constraint UNIQUE Department UNIQUE Depar tmentID Employee alter table Employee add constraint UNIQUE Employee UNIQUE DepartmentI D Salary alter table Salary add constraint UNIQUE Salary UNIQUE EmployeeID 非空约束 Employee alter table Employee alter column EmployeeID char 6 not null alter table Employee alter column Name char 10 not null alter table Employee alter column Birthday Datetime not null alter table Employee alter column Sex Bit not null alter table Employee alter column DepartmentID char 3 not null 表 Departments alter table Departments alter column DepartmentID char 3 not null 表 Salary alter table Salary alter column EmployeeID char 6 not null alter table Salary alter column Income Float 8 not null alter table Salary alter column OutCome Float 8 not null 用户自定义约束 alter table Empl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论