Liquibase® version 3.8.5 is now available! Get it for free.
2018 XML Announcement
News All Previous Posts >>

Subscribe for email updates

- and/or -

Change: ‘addForeignKeyConstraint’

Adds a foreign key constraint to an existing column

Available Attributes

NameDescription
baseColumnNamesName of column(s) to place the foreign key constraint on. Comma-separate if multipleE.g. 'person_id'
baseTableCatalogNameCatalog name of the base table@ v3.0E.g. 'cat'
baseTableNameName of the table containing the column to constraintE.g. 'address'
baseTableSchemaNameSchema name of the base tableE.g. 'public'
constraintNameName of the new foreign key constraintE.g. 'fk_address_person'
deferrablebooleanIs the foreign key deferrableSupported by: oracle, postgresql, sqlite
initiallyDeferredbooleanIs the foreign key initially deferredSupported by: oracle, postgresql, sqlite
onDeleteON DELETE functionality. Possible values: 'CASCADE', 'SET NULL', 'SET DEFAULT', 'RESTRICT', 'NO ACTION'Supported by: asany, db2, db2z, derby, firebird, h2, hsqldb, informix, ingres, mariadb, mssql, mysql, oracle, postgresql@ v2.0E.g. 'CASCADE'
onUpdateON UPDATE functionality. Possible values: 'CASCADE', 'SET NULL', 'SET DEFAULT', 'RESTRICT', 'NO ACTION'E.g. 'RESTRICT'
referencedColumnNamesColumn(s) the foreign key points to. Comma-separate if multipleE.g. 'id'
referencedTableCatalogNameSchema name of the referenced table@ v3.0E.g. 'cat'
referencedTableNameName of the table the foreign key points toE.g. 'person'
referencedTableSchemaNameSchema name of the referenced table@ v3.0E.g. 'public'
referencesUniqueColumnboolean
validatebooleanShall be true if the foreign key should 'ENABLE VALIDATE' set, or false if the foreign key should 'ENABLE NOVALIDATE' set.@ v3.6Default: true
<changeSet author="liquibase-docs" id="addForeignKeyConstraint-example">
    <addForeignKeyConstraint baseColumnNames="person_id"
            baseTableCatalogName="cat"
            baseTableName="address"
            baseTableSchemaName="public"
            constraintName="fk_address_person"
            deferrable="true"
            initiallyDeferred="true"
            onDelete="CASCADE"
            onUpdate="RESTRICT"
            referencedColumnNames="id"
            referencedTableCatalogName="cat"
            referencedTableName="person"
            referencedTableSchemaName="public"
            validate="true"/>
</changeSet>
changeSet:
  id: addForeignKeyConstraint-example
  author: liquibase-docs
  changes:
  - addForeignKeyConstraint:
      baseColumnNames: person_id
      baseTableCatalogName: cat
      baseTableName: address
      baseTableSchemaName: public
      constraintName: fk_address_person
      deferrable: true
      initiallyDeferred: true
      onDelete: CASCADE
      onUpdate: RESTRICT
      referencedColumnNames: id
      referencedTableCatalogName: cat
      referencedTableName: person
      referencedTableSchemaName: public
      validate: true
{
  "changeSet": {
    "id": "addForeignKeyConstraint-example",
    "author": "liquibase-docs",
    "changes": [
      {
        "addForeignKeyConstraint": {
          "baseColumnNames": "person_id",
          "baseTableCatalogName": "cat",
          "baseTableName": "address",
          "baseTableSchemaName": "public",
          "constraintName": "fk_address_person",
          "deferrable": true,
          "initiallyDeferred": true,
          "onDelete": "CASCADE",
          "onUpdate": "RESTRICT",
          "referencedColumnNames": "id",
          "referencedTableCatalogName": "cat",
          "referencedTableName": "person",
          "referencedTableSchemaName": "public",
          "validate": true
        }
      }]
    
  }
}

SQL Generated From Above Sample (MySQL)

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;

Database Support

DatabaseNotesAuto Rollback
DB2/LUWSupportedYes
DB2/zSupportedYes
DerbySupportedYes
FirebirdSupportedYes
H2SupportedYes
HyperSQLSupportedYes
INGRESSupportedYes
InformixSupportedYes
MariaDBSupportedYes
MySQLSupportedYes
OracleSupportedYes
PostgreSQLSupportedYes
SQL ServerSupportedYes
SQLiteNot SupportedYes
SybaseSupportedYes
Sybase AnywhereSupportedYes