

- POSTGRESQL ALTER TABLE ADD CONSTRAINT UNIQUE HOW TO
- POSTGRESQL ALTER TABLE ADD CONSTRAINT UNIQUE UPDATE

POSTGRESQL ALTER TABLE ADD CONSTRAINT UNIQUE UPDATE
REFERENCES customers(customer_id) Code language: SQL (Structured Query Language) ( sql )īecause the foreign key constraint does not have the ON DELETE and ON UPDATE action, they default to NO ACTION. The following foreign key constraint fk_customer in the contacts table defines the customer_id as the foreign key: CONSTRAINT fk_customer The customer_id column in the contacts table is the foreign key column that references the primary key column with the same name in the customers table. In this example, the customers table is the parent table and the contacts table is the child table.Įach customer has zero or many contacts and each contact belongs to zero or one customer. ) Code language: SQL (Structured Query Language) ( sql ) The following statements create the customers and contacts tables: DROP TABLE IF EXISTS customers Ĭustomer_id INT GENERATED ALWAYS AS IDENTITY,Ĭontact_id INT GENERATED ALWAYS AS IDENTITY, PostgreSQL foreign key constraint examples PostgreSQL supports the following actions: Since the primary key is rarely updated, the ON UPDATE action is not often used in practice. The delete and update actions determine the behaviors when the primary key in the parent table is deleted and updated. Finally, specify the delete and update actions in the ON DELETE and ON UPDATE clauses.Third, specify the parent table and parent key columns referenced by the foreign key columns in the REFERENCES clause.Second, specify one or more foreign key columns in parentheses after the FOREIGN KEY keywords.If you omit it, PostgreSQL will assign an auto-generated name. First, specify the name for the foreign key constraint after the CONSTRAINT keyword.REFERENCES parent_table( parent_key_columns) The following illustrates a foreign key constraint syntax: FOREIGN KEY( fk_columns) The foreign key constraint helps maintain the referential integrity of data between the child and parent tables.Ī foreign key constraint indicates that values in a column or a group of columns in the child table equal the values in a column or a group of columns of the parent table. In PostgreSQL, you define a foreign key using the foreign key constraint. And the table referenced by the foreign key is called the referenced table or parent table.Ī table can have multiple foreign keys depending on its relationships with other tables. The table that contains the foreign key is called the referencing table or child table. Introduction to PostgreSQL Foreign Key ConstraintĪ foreign key is a column or a group of columns in a table that reference the primary key of another table.
POSTGRESQL ALTER TABLE ADD CONSTRAINT UNIQUE HOW TO
If the column fails either of those checks, the alter table command will fail.Summary: in this tutorial, you will learn about PostgreSQL foreign key and how to add foreign keys to tables using foreign key constraints. It is important to note that we can only make an existing column a primary key if that column has no null values and no duplicates. We can inspect our table again and see that in addition to the uniqueness constraint we already had, the employee number now has a not null constraint, and an index. alter table users add primary key (employee_number) We then specify our action, which is "add primary key", and we pass the name of the column we are making our new primary key. To make an existing column the primary key, we can use the "alter table" command, passing the table we're dealing with, which for us is "users". \d usersĪs you can see we currently have one constraint, which is asserting that the employee number is unique. We can also inspect the table to see the current columns and constraints. We can select from this table and see that we already have data: select * from users In this example, we're going to be dealing with a dummy database that has a table called users, with the columns name, and employee number.
