addForeignKeyConstraint

Adds a foreign key constraint to an existing column.

Run addForeignKeyConstraint

To run this Change Type, follow these steps:

  1. Add the Change Type to your changeset, as shown in the examples on this page.

  2. Specify any required attributes. Use the table on this page to see which ones your database requires.

  3. Deploy your changeset by running the update command:liquibase update

Available attributes

Name

Description

Required for

Supports

baseColumnNames

Name of the column(s) to place the foreign key constraint on. To specify multiple columns, separate names with commas.

all

all

baseTableCatalogName

Name of the catalog of the base table

all

baseTableName

Name of the table containing the column to constraint

all

all

baseTableSchemaName

Name of the schema of the base table

all

constraintName

Name of the constraint

all

all

deferrable

Defines whether constraints are deferrable

oracle, postgresql

initiallyDeferred

Defines whether constraints are initially deferred

oracle, postgresql

onDelete

ON DELETE functionality. Possible values: CASCADE, SET NULL, SET DEFAULT, RESTRICT, NO ACTION. Note that db2z only supports CASCADE, SET NULL, and RESTRICT

asany, db2, db2z, derby, firebird, h2, hsqldb, informix, ingres, mariadb, mssql, mysql, oracle, postgresql

onUpdate

ON UPDATE functionality. Possible values: CASCADE, SET NULL, SET DEFAULT, RESTRICT, NO ACTION

all except db2z

referencedColumnNames

Name of the column(s) the foreign key points to. To specify multiple columns, separate names with commas.

all

all

referencedTableCatalogName

Name of the catalog of the referenced table

all

referencedTableName

Name of the table the foreign key points to

all

all

referencedTableSchemaName

Name of the schema of the referenced table

all

validate

Set to true if the constraint has ENABLE VALIDATE set, or false if the constraint has ENABLE NOVALIDATE set.

all

referencesUniqueColumn

[Deprecated] Boolean.

all

Database support

Database

Notes

Auto Rollback

DB2/LUW

Supported

Yes

DB2/z

Supported

Yes

Derby

Supported

Yes

Firebird

Supported

Yes

Google BigQuery

Supported

Yes

H2

Supported

Yes

HyperSQL

Supported

Yes

INGRES

Supported

Yes

Informix

Supported

Yes

MariaDB

Supported

Yes

MySQL

Supported

Yes

Oracle

Supported

Yes

PostgreSQL

Supported

Yes

Snowflake

Supported

Yes

SQL Server

Supported

Yes

SQLite

Not Supported

No

Sybase

Supported

Yes

Sybase Anywhere

Supported

Yes

addForeignKeyConstraint examples

--liquibase formatted sql
--changeset liquibase-docs:addForeignKeyConstraint-example
ALTER TABLE cat.address ADD CONSTRAINT fk_address_person FOREIGN KEY (person_id) REFERENCES cat.person (id) ON UPDATE RESTRICT ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED;