Liquibase® version 3.8.5 is now available! Get it for free.
2018 XML Announcement
News All Previous Posts >>

Subscribe for email updates

- and/or -

Change: ‘addNotNullConstraint’

Adds a not-null constraint to an existing table. If a defaultNullValue attribute is passed, all null values for the column will be updated to the passed value before the constraint is applied.

Available Attributes

NameDescription
catalogNameName of the catalog@ v3.0E.g. 'cat'
columnDataTypeCurrent data type of the columnE.g. 'int'Required for: informix, mariadb, mssql, mysql
columnNameName of the column to add the constraint toE.g. 'id'
constraintNameCreated constraint name (if database supports names for NOT NULL constraints)E.g. 'const_name'
defaultNullValueValue to set all currently null values to. If not set, change will fail if null values existE.g. 'A String'
schemaNameName of the schemaE.g. 'public'
tableNameAdds a not-null constraint to an existing table. If a defaultNullValue attribute is passed, all null values for the column will be updated to the passed value before the constraint is applied.E.g. 'person'
validatebooleanThis is true if the not null constraint has 'ENABLE VALIDATE' set, or false if the not null constrain has 'ENABLE NOVALIDATE' set.
<changeSet author="liquibase-docs" id="addNotNullConstraint-example">
    <addNotNullConstraint catalogName="cat"
            columnDataType="int"
            columnName="id"
            constraintName="const_name"
            defaultNullValue="A String"
            schemaName="public"
            tableName="person"
            validate="true"/>
</changeSet>
changeSet:
  id: addNotNullConstraint-example
  author: liquibase-docs
  changes:
  - addNotNullConstraint:
      catalogName: cat
      columnDataType: int
      columnName: id
      constraintName: const_name
      defaultNullValue: A String
      schemaName: public
      tableName: person
      validate: true
{
  "changeSet": {
    "id": "addNotNullConstraint-example",
    "author": "liquibase-docs",
    "changes": [
      {
        "addNotNullConstraint": {
          "catalogName": "cat",
          "columnDataType": "int",
          "columnName": "id",
          "constraintName": "const_name",
          "defaultNullValue": "A String",
          "schemaName": "public",
          "tableName": "person",
          "validate": true
        }
      }]
    
  }
}

SQL Generated From Above Sample (MySQL)

UPDATE cat.person SET id = 'A String' WHERE id IS NULL;

ALTER TABLE cat.person MODIFY id INT NOT NULL;

Database Support

DatabaseNotesAuto Rollback
DB2/LUWSupportedYes
DB2/zNot SupportedYes
DerbySupportedYes
FirebirdNot SupportedYes
H2SupportedYes
HyperSQLSupportedYes
INGRESSupportedYes
InformixSupportedYes
MariaDBSupportedYes
MySQLSupportedYes
OracleSupportedYes
PostgreSQLSupportedYes
SQL ServerSupportedYes
SQLiteNot SupportedYes
SybaseSupportedYes
Sybase AnywhereSupportedYes