PostgreSQL:默认约束名称 [英] PostgreSQL: default constraint names
问题描述
在PostgreSQL中创建表时,如果未提供,将分配默认约束名称:
CREATE TABLE示例ba整数,
b整数,
UNIQUE(a,b)
);
但是使用 ALTER TABLE
添加约束看起来一个名字是强制性的:
ALTER TABLE示例ADD CONSTRAINT my_explicit_constraint_name UNIQUE(a,b);
这导致我处理的项目出现一些命名不一致的问题,并提示以下问题: / p>
-
有一个简单的方法来添加一个约束到一个现有的表,
-
如果没有,是否应避免使用默认名称以防止不一致?
p>所以你可以简单地运行:
ALTER TABLE示例ADD UNIQUE(a,b);
When creating a table in PostgreSQL, default constraint names will assigned if not provided:
CREATE TABLE example (
a integer,
b integer,
UNIQUE (a, b)
);
But using ALTER TABLE
to add a constraint it seems a name is mandatory:
ALTER TABLE example ADD CONSTRAINT my_explicit_constraint_name UNIQUE (a, b);
This has caused some naming inconsistencies on projects I've worked on, and prompts the following questions:
Is there a simple way to add a constraint to an extant table with the name it would have received if added during table creation?
If not, should default names be avoided altogether to prevent inconsistencies?
The manual is pretty clear about this ("tableconstraint: This form adds a new constraint to a table using the same syntax as CREATE TABLE.")
So you can simply run:
ALTER TABLE example ADD UNIQUE (a, b);
这篇关于PostgreSQL:默认约束名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!