为什么 Oracle 不允许您使用相同的列同时创建主键和唯一约束?
Why Oracle does not permit you to create both a primary key and unique constraint with the same columns?
试图理解约束。
这个article说
Oracle does not permit you to create both a primary key and unique
constraint with the same columns.
后面有一个例子:
CREATE TABLE supplier
( supplier_id numeric(10) NOT NULL,
supplier_name varchar2(50) NOT NULL,
contact_name varchar2(50),
CONSTRAINT supplier_unique UNIQUE (supplier_id)
);
我是否理解正确,要使此查询正常工作,创建的 supplier_id
不能 是主键?
还有,为什么不允许呢?因为主键在定义上是唯一的,所以只对该列施加约束是没有意义的?
主键和唯一约束都创建了一个基础唯一索引。由于(大多数情况下)创建两次相同的索引没有意义,因此 Oracle 会检查并禁止这样做。
注意 关于上面的 "most of the time":有时在同一列上同时具有 B*Tree 索引和位图索引可能是有意义的。这可以通过基于函数的索引来完成,但这是相当 "advanced hack" 的,它的代价是使 Oracle 查询优化器对使用 "hacked" 有点疯狂。
Do I understand correctly that for this query to work the created supplier_id
must not be a primary key?
是的,在您的代码示例中,supplier_id
不能是主键,以便您可以在 supplier_id
上创建唯一约束。
试图理解约束。 这个article说
Oracle does not permit you to create both a primary key and unique constraint with the same columns.
后面有一个例子:
CREATE TABLE supplier
( supplier_id numeric(10) NOT NULL,
supplier_name varchar2(50) NOT NULL,
contact_name varchar2(50),
CONSTRAINT supplier_unique UNIQUE (supplier_id)
);
我是否理解正确,要使此查询正常工作,创建的 supplier_id
不能 是主键?
还有,为什么不允许呢?因为主键在定义上是唯一的,所以只对该列施加约束是没有意义的?
主键和唯一约束都创建了一个基础唯一索引。由于(大多数情况下)创建两次相同的索引没有意义,因此 Oracle 会检查并禁止这样做。
注意 关于上面的 "most of the time":有时在同一列上同时具有 B*Tree 索引和位图索引可能是有意义的。这可以通过基于函数的索引来完成,但这是相当 "advanced hack" 的,它的代价是使 Oracle 查询优化器对使用 "hacked" 有点疯狂。
Do I understand correctly that for this query to work the created
supplier_id
must not be a primary key?
是的,在您的代码示例中,supplier_id
不能是主键,以便您可以在 supplier_id
上创建唯一约束。