PostgreSQL: default constraint names

后端 未结 2 780
误落风尘
误落风尘 2020-12-12 09:01

When creating a table in PostgreSQL, default constraint names will assigned if not provided:

CREATE TABLE example (
    a integer,
    b integer,
    UNIQUE          


        
相关标签:
2条回答
  • 2020-12-12 09:06

    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);
    
    0 讨论(0)
  • 2020-12-12 09:08

    The standard names for indexes in PostgreSQL are:

    {tablename}_{columnname(s)}_{suffix}

    where the suffix is one of the following:

    • pkey for a Primary Key constraint
    • key for a Unique constraint
    • excl for an Exclusion constraint
    • idx for any other kind of index
    • fkey for a Foreign key
    • check for a Check constraint

    Standard suffix for sequences is

    • seq for all sequences

    Proof of your UNIQUE-constraint:

    NOTICE: CREATE TABLE / UNIQUE will create implicit index "example_a_b_key" for table "example"

    0 讨论(0)
提交回复
热议问题