
In PostgreSQL, you can add a foreign key to an existing table by using the ALTER TABLE statement. Incredible, right Add FOREIGN KEY to an existing table. Set the referencing column(s) to their default values. From the output, you can clearly see that the sportid value in the child table has been changed according to the change made in the parent table.

#Postgresql add foreign key to existing table update#
This is the default action.ĭelete any rows referencing the deleted row, or update the value of the referencing column to the new value of the referenced column, respectively. If the constraint is deferred, this error will be produced at constraint check time if there still exist any referencing rows. Produce an error indicating that the deletion or update would create a foreign key constraint violation. This is the same as NO ACTION except that the check is not deferrable. Produce an error indicating that the deletion or update would create a foreign key constraint violation. BEGIN - Alter field extra on user - (no-op) - Alter field id on user - ALTER TABLE 'appuseruser' ALTER COLUMN 'id' TYPE bigint USING 'id'::bigint ALTER SEQUENCE IF EXISTS 'appuseruseridseq' AS bigint ALTER TABLE 'appuserusergroups' ALTER COLUMN 'userid' TYPE bigint USING 'userid'::bigint ALTER TABLE 'appuseruseruse.

The On Delete and On Update drop-down list define the type of the actions to be taken. This form adds a new constraint to a table using the same syntax as CREATE TABLE, plus the option NOT VALID, which is currently only allowed for foreign key. To include field(s)/referenced field(s) to the key, just simply double-click the Fields/ Referenced Fields field or click to open the editor(s) for editing. Use the Referenced Schema and Referenced Table drop-down lists to select a foreign schema and table respectively.

Use the Name edit box to enter a name for the new key.
