Liquibase® version 3.8.1 is now available! Get it for free.
2018 XML Announcement
News Liquibase 3.8.1 Released
06 Nov 2019

- and/or -

All Previous Posts >>

Change: ‘addPrimaryKey’

Adds creates a primary key out of an existing column or set of columns.

Available Attributes

NameDescriptionRequired ForSupportsSince
catalogNameName of the catalogall3.0
clusteredall
columnNamesName of the column(s) to create the primary key on. Comma separated if multipleallall
constraintNameName of primary key constraintall
forIndexCatalogNameall
forIndexNameoracle, db2, db2z
forIndexSchemaNameall
schemaNameName of the schemaall
tableNameName of the table to create the primary key onallall
tablespaceall
validateThis is true if the primary key has 'ENABLE VALIDATE' set, or false if the primary key has 'ENABLE NOVALIDATE' set.all
<changeSet author="liquibase-docs"
        id="addPrimaryKey-example"
        objectQuotingStrategy="LEGACY">
    <addPrimaryKey catalogName="cat"
            clustered="true"
            columnNames="id, name"
            constraintName="pk_person"
            forIndexName="A String"
            schemaName="public"
            tableName="person"
            tablespace="A String"
            validate="true"/>
</changeSet>
changeSet:
  id: addPrimaryKey-example
  author: liquibase-docs
  objectQuotingStrategy: LEGACY
  changes:
  - addPrimaryKey:
      catalogName: cat
      clustered: true
      columnNames: id, name
      constraintName: pk_person
      forIndexName: A String
      schemaName: public
      tableName: person
      tablespace: A String
      validate: true
{
  "changeSet": {
    "id": "addPrimaryKey-example",
    "author": "liquibase-docs",
    "objectQuotingStrategy": "LEGACY",
    "changes": [
      {
        "addPrimaryKey": {
          "catalogName": "cat",
          "clustered": true,
          "columnNames": "id, name",
          "constraintName": "pk_person",
          "forIndexName": "A String",
          "schemaName": "public",
          "tableName": "person",
          "tablespace": "A String",
          "validate": true
        }
      }]
    
  }
}

SQL Generated From Above Sample (MySQL)

ALTER TABLE cat.person ADD PRIMARY KEY (id,
 name);

Database Support

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