addDefaultValue

The addDefaultValue Change Type adds a default value to the database definition for the specified column in a table.

Uses

You can typically use the addDefaultValue Change Type when you want to set the default value for the column definition.

Running the addDefaultValue Change Type

To add a default value to the database definition for a column, follow these steps:

  1. Add the addDefaultValue Change Type to your changeset with the needed attributes as it is shown in the examples. When using the addDefaultValue Change Type, you must set one of the following attributes: defaultValue, defaultValueNumeric, defaultValueBoolean, or defaultValueDate.
  2. Deploy your changeset by running the update command:
  3. liquibase update

Now, you should see the default value created.

Available attributes

Name 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)
informix all
columnName Name of the column for which to add a default value. all all
defaultValue

The default value for fields in the column. Either this property or one of the other defaultValue* properties are required.

all
defaultValueBoolean

The default value for a column of a boolean type.

all
defaultValueComputed

The default value that is returned from a function or procedure call of the same type as the column. Contains the function or column name to call. Differs from defaultValue by returning the value of the function or column you specify instead of the name of the function/column as a string. Can also perform operations on the returned value.

The attribute is not supported by MySQL and HyperSQL.
defaultValueConstraintName

Sets a unique name for default constraint used for a specific column. It works only along with any of the defaultValue* attributes listed.

The attribute is supported only by MSSQL.
defaultValueDate

The default date and time value for column. The value is specified in one of the following forms: YYYY-MM-DD, hh:mm:ss, or YYYY-MM-DDThh:mm:ss.

all
defaultValueNumeric

The default value for a column of a numeric type. For example: integer, bigint, bigdecimal, and others.

all
defaultValueSequenceNext

Sets value for a specified column by using the value of the existing sequence. With every new input, the next value of the sequence will be taken.

Not supported by Apache Derby, Firebird, MySQL, MariaDB, SQLite, HyperSQL, Ingress, and Sybase.
schemaName

Name of the schema

all
tableName

Name of the table containing the column

all 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="addDefaultValue-example">
        <addDefaultValue  catalogName="cat"
            columnDataType="varchar(50)"
            columnName="fileName"
            defaultValue="Something Else"
            defaultValueBoolean="true"
            defaultValueComputed="now"
            defaultValueConstraintName="A String"
            defaultValueDate="2008-02-12T12:34:03"
            defaultValueNumeric="439.2"
            defaultValueSequenceNext="seq_name"
            schemaName="public"
            tableName="file"/>
    </changeSet>

</databaseChangeLog>
databaseChangeLog:
-  changeSet:
    id:  addDefaultValue-example
    author:  liquibase-docs
    changes:
    -  addDefaultValue:
        catalogName:  cat
        columnDataType:  varchar(50)
        columnName:  fileName
        defaultValue:  Something Else
        defaultValueBoolean:  true
        defaultValueComputed:  now
        defaultValueConstraintName:  A String
        defaultValueDate:  2008-02-12T12:34:03
        defaultValueNumeric:  '439.2'
        defaultValueSequenceNext:  seq_name
        schemaName:  public
        tableName:  file
{
    "databaseChangeLog": [
        {
            "changeSet": {
                "id": "addDefaultValue-example",
                "author": "liquibase-docs",
                "changes": [
                    {
                        "addDefaultValue": {
                            "catalogName": "cat",
                            "columnDataType": "varchar(50)",
                            "columnName": "fileName",
                            "defaultValue": "Something Else",
                            "defaultValueBoolean": true,
                            "defaultValueComputed": "now",
                            "defaultValueConstraintName": "A String",
                            "defaultValueDate": "2008-02-12T12:34:03",
                            "defaultValueNumeric": "439.2",
                            "defaultValueSequenceNext": "seq_name",
                            "schemaName": "public",
                            "tableName": "file"
                        }
                    }
                ]
            }
        }
    ]
}
--liquibase formatted sql

--changeset liquibase-docs:addDefaultValue-example
ALTER  TABLE  cat.file  ALTER  fileName  SET  DEFAULT  'Something Else';

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 Not Supported No
SQL Server Supported Yes
SQLite Supported Yes
Sybase Supported Yes
Sybase Anywhere Supported Yes