Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Liquibase Composite Primary Key

Here are two examples of how to define a composite primary key for some table using Liquibase.

  • Add the composite primary key up front during table construction (preferred).
  <changeSet id="1">
    <createTable tableName="some_table">
      <column name="id_1" type="varchar_ignorecase">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_SOME_TABLE"></constraints>
      </column>
      <column name="id_2" type="varchar_ignorecase">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_SOME_TABLE"></constraints>
      </column>
    </createTable>
  </changeSet>
  • Add the composite primary key after the table has been created.
<changeSet id="2">
    <addPrimaryKey columnNames="id_1, id_2"
                   constraintName="PK_SOME_TABLE"
                   tableName="some_table"
                   validate="true"/>
</changeSet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.