Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 732
Maciej Ziarko Avatar asked Dec 29 '11 23:12

Maciej Ziarko


1 Answers

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.

like image 152
A.H. Avatar answered Oct 18 '22 20:10

A.H.