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: ‘addPrimaryKey’

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

Available Attributes

NameDescription
catalogNameName of the catalog@ v3.0E.g. 'cat'
clusteredboolean
columnNamesName of the column(s) to create the primary key on. Comma separated if multipleE.g. 'id, name'
constraintNameName of primary key constraintE.g. 'pk_person'
forIndexCatalogName
forIndexNameE.g. 'A String'Supported by: db2, db2z, oracle
forIndexSchemaName
schemaNameName of the schemaE.g. 'public'
tableNameName of the table to create the primary key onE.g. 'person'
tablespaceE.g. 'A String'
validatebooleanThis is true if the primary key has 'ENABLE VALIDATE' set, or false if the primary key has 'ENABLE NOVALIDATE' set.
<changeSet author="liquibase-docs" id="addPrimaryKey-example">
    <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
  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",
    "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
DB2/LUWSupportedYes
DB2/zSupportedYes
DerbySupportedYes
FirebirdSupportedYes
H2SupportedYes
HyperSQLSupportedYes
INGRESSupportedYes
InformixSupportedYes
MariaDBSupportedYes
MySQLSupportedYes
OracleSupportedYes
PostgreSQLSupportedYes
SQL ServerSupportedYes
SQLiteNot SupportedYes
SybaseSupportedYes
Sybase AnywhereSupportedYes