有人可以澄清没有唯一约束(Oracle)的唯一索引的目的是什么?例如,
create table test22(id int, id1 int, tmp varchar(20));
create unique index idx_test22 on test22(id);
insert into test22(id, id1, tmp) values (1, 2, 'aaa'); // ok
insert into test22(id, id1, tmp) values (1, 2, 'aaa'); // fails, ORA-00001: unique
// constraint (TEST.IDX_TEST22) violated
Run Code Online (Sandbox Code Playgroud)
到目前为止看起来有一个约束.但
create table test33(id int not null primary key,
test22_id int not null,
foreign key(test22_id) references test22(id) );
Run Code Online (Sandbox Code Playgroud)
也失败了"ORA-02270: no matching unique or primary key for this column-list"
.我完全被这种行为搞糊涂了.有没有约束?
有很多文章解释了为什么可以在没有唯一索引的情况下获得唯一约束; 这很清楚,也很有道理.但是,我不理解没有约束的唯一索引的原因.