How do I ALTER a PostgreSQL table and make a column unique?

How do I ALTER a PostgreSQL table and make a column unique?

Asked on January 7, 2019 in PostgreSQL.
Add Comment


  • 4 Answer(s)

    i  am estimate it out in the PostgreSQL docs, the exact syntax is:

    
    ALTER TABLE the_table ADD CONSTRAINT constraint_name UNIQUE (thecolumn);
    
    

     

    Answered on January 7, 2019.
    Add Comment

    This command can automatically assign DB constraint using name

    
    ALTER TABLE foo ADD UNIQUE (thecolumn);
    
    

     

    Answered on January 7, 2019.
    Add Comment

    Below function also   can used to  produce a unique constraint of more than 1 column:

    
    ALTER TABLE the_table ADD CONSTRAINT constraint_name UNIQUE (column1, column2);
    
    
    Answered on January 7, 2019.
    Add Comment

    ALTER TABLE the_table ADD CONSTRAINT constraint_name UNIQUE (thecolumn);

    Answered on March 5, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.