addAutoIncrement

Converts an existing column to be an auto-increment (a.k.a 'identity') column.

Run addAutoIncrement

To run this Change Type, follow these steps:

  1. Add the Change Type to your changeset, as shown in the examples on this page.
  2. Specify any required attributes. Use the table on this page to see which ones your database requires.
  3. Deploy your changeset by running the update command:
  4. liquibase update

Available attributes

Attribute Description Required for Supports
catalogName

Name of the catalog

-- all
columnDataType

Data type of the column.

To help make scripts database-independent, Liquibase automatically converts the following generic data types to the correct database implementation: BIGINT, BLOB, BOOLEAN, CHAR, CLOB, CURRENCY, DATE, DATETIME, DECIMAL, DOUBLE, FLOAT, INT, MEDIUMINT, NCHAR, NUMBER, NVARCHAR, SMALLINT, TIME, TIMESTAMP, TINYINT, UUID, VARCHAR, XML.

Also, specifying a java.sql.Types.* type is converted to the correct type as well. For example:

  • java.sql.Types.TIMESTAMP
  • java.sql.Types.VARCHAR(255)

For Snowflake, Liquibase also implements BINARY, TIME, and TIMESTAMP_NTZ. For Databricks, Liquibase also implements ARRAY<INT> and ARRAY<STRING>.

Note: To specify a complex Databricks type like ARRAY<STRING> in an XML changelog, you must specify the escape sequences &lt; and &gt; rather than < and >. See createTable.

For more information, see Liquibase Data Type Handling.

asany, h2, hsqldb, informix, ingres, mariadb, mysql, sybase, unsupported all
columnName

Name of the column

all all
defaultOnNull

When using generationType of BY DEFAULT then defaultOnNull=true allows the identity to be used if the identity column is referenced, but a value of NULL is specified.

-- oracle
generationType

Type of the generation in GENERATED %s AS IDENTITY. Default: "|".

-- all
incrementBy

Integer amount to increment by at each call

-- all
schemaName

Name of the schema

-- all
startWith

Initial integer value of the increment

-- all
tableName

Name of the table

all all

Examples

--liquibase formatted sql

--changeset liquibase-docs:addAutoIncrement-example
ALTER TABLE cat.person MODIFY id INT AUTO_INCREMENT;  
ALTER TABLE cat.person AUTO_INCREMENT=100;
databaseChangeLog:
- changeSet:
    id: addAutoIncrement-example
    author: liquibase-docs
    changes:
    - addAutoIncrement:
        catalogName: cat
        columnDataType: int
        columnName: id
        defaultOnNull: false
        generationType: ALWAYS
        incrementBy: 1
        schemaName: public
        startWith: 100
        tableName: person
{
    "databaseChangeLog": [
        {
            "changeSet": {
                "id": "addAutoIncrement-example",
                "author": "liquibase-docs",
                "changes": [
                    {
                        "addAutoIncrement": {
                            "catalogName": "cat",
                            "columnDataType": "int",
                            "columnName": "id",
                            "defaultOnNull": false,
                            "generationType": "ALWAYS",
                            "incrementBy": 1,
                            "schemaName": "public",
                            "startWith": 100,
                            "tableName": "person"
                        }
                    }
                ]
            }
        }
    ]
}
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xmlns:pro="http://www.liquibase.org/xml/ns/pro"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
        http://www.liquibase.org/xml/ns/pro
        http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">

    <changeSet author="liquibase-docs" id="addAutoIncrement-example">
        <addAutoIncrement catalogName="cat"
            columnDataType="int"
            columnName="id"
            defaultOnNull="false"
            generationType="ALWAYS"
            incrementBy="1"
            schemaName="public"
            startWith="100"
            tableName="person"/>
    </changeSet>

</databaseChangeLog>

Database support

Database Notes Auto Rollback
DB2/LUW Supported No
DB2/z Not Supported No
Derby Not Supported No
Firebird Not Supported No
Google BigQuery Supported No
H2 Supported No
HyperSQL Supported No
INGRES Supported No
Informix Supported No
MariaDB Supported No
MySQL Supported No
Oracle Not Supported No
PostgreSQL Supported No
Snowflake Not Supported No
SQL Server Not Supported No
SQLite Supported: If the column type is not INTEGER it is converted to INTEGER No
Sybase Supported No
Sybase Anywhere Supported No