PostgreSQL:默认约束名称 [英] PostgreSQL: default constraint names

查看:179
本文介绍了PostgreSQL:默认约束名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PostgreSQL中创建表时,如果未提供,将分配默认约束名称:

  CREATE TABLE示例ba整数,
b整数,
UNIQUE(a,b)
);

但是使用 ALTER TABLE 添加约束看起来一个名字是强制性的:

  ALTER TABLE示例ADD CONSTRAINT my_explicit_constraint_name UNIQUE(a,b); 

这导致我处理的项目出现一些命名不一致的问题,并提示以下问题: / p>


  1. 有一个简单的方法来添加一个约束到一个现有的表,


  2. 如果没有,是否应避免使用默认名称以防止不一致?



解决方案

手册< a>

$>

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:

  1. 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?

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆