addForeignKeyConstraint

Adds a foreign key constraint to an existing column

Available attributes

Name Description Required for Supports
baseColumnNames Name of column(s) to place the foreign key constraint on. Comma-separate if multiple all all
baseTableCatalogName Catalog name of the base table all
baseTableName Name of the table containing the column to constraint all all
baseTableSchemaName Schema name of the base table all
constraintName Name of the new foreign key constraint all all
deferrable Is the foreign key deferrable oracle, postgresql, sqlite
initiallyDeferred Is the foreign key initially deferred oracle, postgresql, sqlite
onDelete ON DELETE functionality. Possible values: 'CASCADE', 'SET NULL', 'SET DEFAULT', 'RESTRICT', 'NO ACTION' 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
referencedColumnNames Column(s) the foreign key points to. Comma-separate if multiple all all
referencedTableCatalogName Schema name of the referenced table all
referencedTableName Name of the table the foreign key points to all all
referencedTableSchemaName Schema name of the referenced table all
referencesUniqueColumn all
validate Shall be true if the foreign key should 'ENABLE VALIDATE' set, or false if the foreign key should 'ENABLE NOVALIDATE' set. all

XML example

<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>

YAML example

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

JSON example

{  "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 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;

Database support

Database Notes Auto rollback
DB2/LUW Supported Yes
DB2/z Supported Yes
Derby Supported Yes
Firebird 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
SQL Server Supported Yes
SQLite Not Supported Yes
Sybase Supported Yes
Sybase Anywhere Supported Yes