PostgreSQL:是否可以为PRIMARY KEY或UNIQUE提供自定义名称? [英] PostgreSQL: is it possible to provide custom name for PRIMARY KEY or UNIQUE?

查看:803
本文介绍了PostgreSQL:是否可以为PRIMARY KEY或UNIQUE提供自定义名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写:

  CREATE TABLE accounts(

username varchar ,

我获得名为的主键:

  accounts_pkey 

可以分配自己的自定义名称,例如accounts_primary_key?



UNIQUE 的相同故事。



我在PostgreSQL文档中找不到它。



提前感谢。

解决方案

诀窍是 column_constraint 部分中的 CONSTRAINT > CREATE TABLE 。示例:

 > create table x(xx文本约束xxxx主键) ; 
注意:CREATE TABLE / PRIMARY KEY将为表x创建隐式索引xxxx
CREATE TABLE

这适用于所有类型的约束,包括 PRIMARY KEY UNIQUE 。 / p>

请参阅 CREATE TABLE 的文档a>。


When I write:

 CREATE TABLE accounts (

     username varchar(64) PRIMARY KEY,

I get primary key named:

accounts_pkey

Is it possible to assign my own custom name, for instance "accounts_primary_key"?

Same story about UNIQUE.

I couldn't find it in PostgreSQL documentation.

Thanks in advance.

解决方案

The trick is the CONSTRAINT part in the column_constraint section of CREATE TABLE. Example:

> create table x(xx text constraint xxxx primary key);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "xxxx" for table "x"
CREATE TABLE

This works for all kind of constraints, including PRIMARY KEY and UNIQUE.

See the docs of CREATE TABLE for details.

这篇关于PostgreSQL:是否可以为PRIMARY KEY或UNIQUE提供自定义名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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