数据库实验:索引和完整性语言

一、实验内容
创建唯一索引
1、在department表的departName字段上创建唯一性索引
创建复合索引
2、在employee表的name和address两个字段上创建一个复合索引
创建表的时候创建索引
3、创建与department表相同结构的表department1,并将departName 设为主键departmentID上建立一个索引
创建HASH索引
4、创建一个包含(employeeID, name, education)等字段的临时员工表 (tmpEmployee) ,并在该表的员工编号字段上创建一个HASH
创建表时定义实体完整性
5、创建一个表employee1,只含employeeID、name、gender和 education列。以name为主键作为列name的完整性约束, employeeID为替代键作为表的完整性约束
创建表后定义实体完整性
6、定义不含主键的表employee2,包含上述属性列,然后定义实体完整性,以employeeID为主码
7、设计记录增加到employee1表和employee2表,验证实体完整性是否起作用
创建表时定义参照完整性
8、创建一个表salary1,要求所有salary1表上出现的employeeID都要 出现在salary表中,利用完整性约束实现,要求当删除或修改salary 表上的employeeID列时,salary1表中的值也随之变化
9、创建完salary1表后,初始化该表的数据与salary表相同,删除 salary表中的一行数据,再查看salary1表的内容,看看会发生 什么情况
思考与练习
1、使用CREATE INDEX语句能创建主键吗?添加主键与添加普通索引有 什么区别?
2、如果删除基本表的一个列或者多个列,该列上的索引会受怎样的影响?

二、实验过程
1、use dbem;
create unique index depart_ind
on department(departName);

2、create index na_ind
on employee(name,address);

3、CREATE TABLE department1 (
departName CHAR(20) NOT NULL PRIMARY KEY, /部门名称/
departmentID CHAR(3) , /部门编号/
comment VARCHAR(100), /备注/
index dn_ind(departmentID)
);

4、CREATE TABLE tmpemployee (
employeeID CHAR(6) NOT NULL PRIMARY KEY, /员工号/
name CHAR(10) NOT NULL, /*姓名 */
education CHAR(4) NOT NULL, /学历/
index tei_ind(employeeID) using hash
);

5、use dbem;
create table employee1
(
employeeID CHAR(6) NOT NULL , /员工号/
name CHAR(10) NOT NULL, /*姓名 */
education CHAR(4) NOT NULL, /学历/
gender TINYINT(1) NOT NULL DEFAULT 1, /男1女0/
primary key(name),
unique (employeeID)
);

6、create table employee2
(
employeeID CHAR(6) NOT NULL , /员工号/
name CHAR(10) NOT NULL, /*姓名 */
education CHAR(4) NOT NULL, /学历/
gender TINYINT(1) NOT NULL DEFAULT 1 /男1女0/
);
alter table employee2
add primary key(employeeID);
7、insert into employee1
set employeeID=“000001”,
name=‘王林’,
education=‘大专’,
gender=1;
在这里插入图片描述
insert into employee1
set employeeID=“000002”,
name=‘王林’,
education=‘大专’,
gender=1;

在这里插入图片描述
insert into employee1
set employeeID=“000001”,
name=‘张三’,
education=‘大专’,
gender=1;
在这里插入图片描述
insert into employee2
set name=‘张三’,
education=‘大专’,
gender=1;
在这里插入图片描述
实体完整性起了作用

8、use dbem;
create table salary1
(
employeeID CHAR(6) REFERENCES employee(employeeID),
income REAL, /收入/
outcome REAL, /支出/
PRIMARY KEY(employeeID),
foreign key(employeeID)
references salary(employeeID)
on delete restrict
on update restrict
);

9、SET SQL_SAFE_UPDATES = 0;
delete from salary where employeeID=504209;
SELECT * FROM dbem.salary;
在这里插入图片描述
SELECT * FROM dbem.salary1;
在这里插入图片描述
三、思考题
思考题1:create index语句不能创建主键,会产生报错。添加主键会自动创建主键索引,普通索引需要自己手动添加和指定,表中效率最高的索引就是主键索引。

思考题2:索引是由数据库进行维护的,当我们对存在索引的数据库表进行增删改的操作时,如果涉及到索引列,数据库都会对索引表进行更新;由此可以推断出,当表中某个列被删除后,在该列上的索引也会被删除掉。

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值