PostgreSQL:默认约束名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4107915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostgreSQL: default constraint names
提问by Ian Mackinnon
When creating a table in PostgreSQL, default constraint names will assigned if not provided:
在 PostgreSQL 中创建表时,如果未提供,将分配默认约束名称:
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
添加约束似乎名称是强制性的:
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?
是否有一种简单的方法可以将约束添加到现有表中,其名称是在表创建期间添加时会收到的名称?
如果不是,是否应该完全避免默认名称以防止不一致?
采纳答案by a_horse_with_no_name
回答by Frank Heikens
The standard names for indexes in PostgreSQL are:
PostgreSQL 中索引的标准名称是:
{tablename}_{columnname(s)}_{suffix}
{tablename}_{columnname(s)}_{suffix}
where the suffix is one of the following:
其中后缀是以下之一:
pkey
for a Primary Key constraintkey
for a Unique constraintexcl
for an Exclusion constraintidx
for any other kind of indexfkey
for a Foreign keycheck
for a Check constraint
pkey
对于主键约束key
对于唯一约束excl
对于排除约束idx
对于任何其他类型的索引fkey
对于外键check
对于 Check 约束
Standard suffix for sequences is
序列的标准后缀是
seq
for all sequences
seq
对于所有序列
Proof of your UNIQUE-constraint:
证明您的 UNIQUE 约束:
NOTICE: CREATE TABLE / UNIQUE will create implicit index "example_a_b_key" for table "example"
注意:CREATE TABLE / UNIQUE 将为表“example”创建隐式索引“example_a_b_key”