简体   繁体   English

唯一索引失败

[英]Unique Index Fail

I created a unique index for employee IDs to an existing table. 我为现有表的员工ID创建了唯一索引。 I did a test and entered an employee ID already saved in the database, and the database still saved it. 我进行了测试,然后输入了已经保存在数据库中的员工ID,数据库仍然保存了它。 When I do a search on that ID it lists both records. 当我在该ID上进行搜索时,它会列出两个记录。

Test the index by inserting a record with a duplicate employee ID 通过插入具有重复员工ID的记录来测试索引

CREATE INDEX empid_index ON staff (empId)

Expected an error message of a duplicated ID, but no error came up, the record was still saved. 预期出现错误消息,即ID重复,但未出现错误,该记录仍被保存。

Don't create unique indexes: add unique constraints to the table instead. 不要创建唯一索引:将唯一约束添加到表中。 For example: 例如:

create table staff (
  ...
  constraint unique_empid unique (empid),
  ...
);

Even though unique indexes will prevent you from inserting duplicate rows with that value, you will be missing other features of the constraint, such as exporting foreign keys, that you may need later on. 即使唯一索引将阻止您使用该值插入重复的行,但是您将缺少约束的其他功能,例如导出外键,以后可能需要使用这些功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM