It consists of the field called order_id. In this example, we've created a unique constraint on the existing order_details table called order_unique. ALTER TABLE order_detailsĪDD CONSTRAINT order_unique UNIQUE (order_id) Let's look at an example of how to add a unique constraint to an existing table in PostgreSQL using the ALTER TABLE statement. column_n The columns that make up the unique constraint. constraint_name The name of the unique constraint. This is the table that you wish to add a unique constraint to. column_n) table_name The name of the table to modify. The syntax for creating a unique constraint using an ALTER TABLE statement in PostgreSQL is: ALTER TABLE table_nameĪDD CONSTRAINT constraint_name UNIQUE (column1, column2. We could also create a unique constraint with more than one field as in the example below: CREATE TABLE order_detailsĬONSTRAINT order_date_unique UNIQUE (order_id, order_date)Ĭreate unique contraint - Using an ALTER TABLE statement It consists of only one field - the order_id field. In this example, we've created a unique constraint on the order_details table called order_unique. ( order_detail_id integer CONSTRAINT order_details_pk PRIMARY KEY,ĬONSTRAINT order_unique UNIQUE (order_id) Let's look at an example of how to create a unique constraint in PostgreSQL using the CREATE TABLE statement. uc_col_n The columns that make up the unique constraint. Now if you want to change an existing constraint, you will drop the constraint first and then follow it up with the add constraint statement. column1, column2 The columns that you wish to create in the table. ) table_name The name of the table that you wish to create. The syntax for creating a unique constraint using a CREATE TABLE statement in PostgreSQL is: CREATE TABLE table_nameĬONSTRAINT constraint_name UNIQUE (uc_col1, uc_col2. Create unique Contraint - Using a CREATE TABLE statement
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |