addUniqueConstraint

Adds a unique constraint to an existing column or set of columns.

Run addUniqueConstraint

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

Name Description Required for Supports
catalogName

Name of the catalog

all
clustered

Boolean. Whether to create a clustered index

mssql
columnNames Name of the column(s) to create the unique constraint on. Comma separated if multiple all all
constraintName

Name of the constraint

all
deferrable

Defines whether constraints are deferrable

all
disabled

Boolean. Specifies whether the constraint is disabled.

all
forIndexCatalogName Name of the catalog of the index to associate with the constraint all
forIndexName Name of the index to associate with the constraint oracle
forIndexSchemaName Name of the schema of the index to associate with the constraint all
initiallyDeferred

Defines whether constraints are initially deferred

all
schemaName

Name of the schema

all
tableName Name of the table to create the unique constraint on all all
tablespace

Name of the tablespace to use for the index

all
validate

Set to true if the constraint has ENABLE VALIDATE set, or false if the constraint has ENABLE NOVALIDATE set.

all

Examples

<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="addUniqueConstraint-example">
        <addUniqueConstraint  catalogName="cat"
            clustered="true"
            columnNames="id, name"
            constraintName="const_name"
            deferrable="true"
            disabled="true"
            forIndexName="A String"
            initiallyDeferred="true"
            schemaName="public"
            tableName="person"
            tablespace="A String"
            validate="true"/>
    </changeSet>

</databaseChangeLog>
databaseChangeLog:
-  changeSet:
    id:  addUniqueConstraint-example
    author:  liquibase-docs
    changes:
    -  addUniqueConstraint:
        catalogName:  cat
        clustered:  true
        columnNames:  id, name
        constraintName:  const_name
        deferrable:  true
        disabled:  true
        forIndexName:  A String
        initiallyDeferred:  true
        schemaName:  public
        tableName:  person
        tablespace:  A String
        validate:  true
{
    "databaseChangeLog": [
        {
            "changeSet": {
                "id": "addUniqueConstraint-example",
                "author": "liquibase-docs",
                "changes": [
                    {
                        "addUniqueConstraint": {
                            "catalogName": "cat",
                            "clustered": true,
                            "columnNames": "id, name",
                            "constraintName": "const_name",
                            "deferrable": true,
                            "disabled": true,
                            "forIndexName": "A String",
                            "initiallyDeferred": true,
                            "schemaName": "public",
                            "tableName": "person",
                            "tablespace": "A String",
                            "validate": true
                        }
                    }
                ]
            }
        }
    ]
}
--liquibase formatted sql

--changeset liquibase-docs:addUniqueConstraint-example
ALTER  TABLE  cat.person  ADD  CONSTRAINT  const_name  UNIQUE  CLUSTERED  (id,  
 name)  USING  INDEX  `A String`;

Database support

Database Notes Auto Rollback
DB2/LUW Supported Yes
DB2/z Supported Yes
Derby Supported Yes
Firebird Supported Yes
H2 Supported Yes
HyperSQL Supported Yes
INGRES Supported Yes
Informix Supported Yes
MariaDB Supported Yes
MySQL Supported Yes
Oracle Supported Yes
PostgreSQL Supported Yes
Snowflake Supported Yes
SQL Server Supported Yes
SQLite Not Supported No
Sybase Supported Yes
Sybase Anywhere Supported Yes