PostgreSQL: is it possible to provide custom name for PRIMARY KEY or UNIQUE?

天大地大妈咪最大 提交于 2019-12-08 16:37:59

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8674562/postgresql-is-it-possible-to-provide-custom-name-for-primary-key-or-unique

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!