Change: ‘addForeignKeyConstraint’

Adds a foreign key constraint to an existing column

Available Attributes

NameDescriptionRequired ForSupportsSince
baseColumnNamesName of column(s) to place the foreign key constraint on. Comma-separate if multipleallall
baseTableCatalogNameall3.0
baseTableNameName of the table containing the column to constrainallall
baseTableSchemaNameall
constraintNameName of the new foreign key constraintallall
deferrableIs the foreign key deferrablepostgresql, oracle
initiallyDeferredIs the foreign key initially deferredpostgresql, oracle
onDeleteON DELETE functionality. Possible values: 'CASCADE', 'SET NULL', 'SET DEFAULT', 'RESTRICT', 'NO ACTION'all
onUpdateON UPDATE functionality. Possible values: 'CASCADE', 'SET NULL', 'SET DEFAULT', 'RESTRICT', 'NO ACTION'all
referencedColumnNamesColumn(s) the foreign key points to. Comma-separate if multipleallall
referencedTableCatalogNameall3.0
referencedTableNameName of the table the foreign key points toallall
referencedTableSchemaNameall
referencesUniqueColumnall
<changeSet author="liquibase-docs" id="addForeignKeyConstraint-example">
    <addForeignKeyConstraint baseColumnNames="person_id"
            baseTableName="address"
            constraintName="fk_address_person"
            deferrable="true"
            initiallyDeferred="true"
            onDelete="CASCADE"
            onUpdate="RESTRICT"
            referencedColumnNames="id"
            referencedTableName="person"/>
</changeSet>
changeSet:
  id: addForeignKeyConstraint-example
  author: liquibase-docs
  changes:
  - addForeignKeyConstraint:
      baseColumnNames: person_id
      baseTableName: address
      constraintName: fk_address_person
      deferrable: true
      initiallyDeferred: true
      onDelete: CASCADE
      onUpdate: RESTRICT
      referencedColumnNames: id
      referencedTableName: person
{
  "changeSet": {
    "id": "addForeignKeyConstraint-example",
    "author": "liquibase-docs",
    "changes": [
      {
        "addForeignKeyConstraint": {
          "baseColumnNames": "person_id",
          "baseTableName": "address",
          "constraintName": "fk_address_person",
          "deferrable": true,
          "initiallyDeferred": true,
          "onDelete": "CASCADE",
          "onUpdate": "RESTRICT",
          "referencedColumnNames": "id",
          "referencedTableName": "person"
        }
      }]
    
  }
}

SQL Generated From Above Sample (MySQL)

ALTER TABLE address ADD CONSTRAINT fk_address_person FOREIGN KEY (person_id) REFERENCES person (id) ON UPDATE RESTRICT ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED;

Database Support

DatabaseNotesAuto Rollback
DB2SupportedYes
DerbySupportedYes
FirebirdSupportedYes
H2SupportedYes
HyperSQLSupportedYes
InformixSupportedYes
MySQLSupportedYes
OracleSupportedYes
PostgreSQLSupportedYes
SQL ServerSupportedYes
SQLiteNot SupportedYes
SybaseSupportedYes
Sybase AnywhereSupportedYes

Missing, incomplete or wrong information? Please send a pull request