Skip to content

Instantly share code, notes, and snippets.

@jbaranski
Created August 1, 2020 14:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbaranski/53ff907b1af32404c88f37922dd57912 to your computer and use it in GitHub Desktop.
Save jbaranski/53ff907b1af32404c88f37922dd57912 to your computer and use it in GitHub Desktop.
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>
@piffzero
Copy link

Hi.
Maybe I'm late, but why is the first way preferred?
Thanks

@jbaranski
Copy link
Author

Hey,

Preferred just meant my personal preference. Do whatever makes the most sense for you, both work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment