Liquibase Change Types addDefaultValue

Database provides a concept of default value, i.e. if no value is provided for the column in the insert record query, database will automatically populate it with the default value of the column. Liquibase supports default value with addDefaultValue Change Type to add a default value parameter to alter the existing definition for the specified column in a table.

Use generic addDefaultValue Change Type, or specific defaultValue defaultValueNumeric defaultValueBoolean or defaultValueDate Change Type

Liquibase allows the addition of default value at creation time or with another ChangeSet. let's see both

Default Value at Creation Time

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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 http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
	<changeSet author="shriksha (generated)" id="1693057141122-1">
		<createTable catalogName="appledb" tableName="apple">
			<column autoIncrement="true" name="apple_id" type="BIGINT">
				<constraints nullable="false" primaryKey="true" />
			</column>
			<column defaultValue="shrikant" name="apple_name" type="VARCHAR(255)" />
			<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
		</createTable>
	</changeSet>
	<changeSet author="shriksha (generated)" id="1693057141122-2">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="shrikant" />
			<column name="current_date_time" valueDate="2023-08-26T19:08:36" />
		</insert>
	</changeSet>
</databaseChangeLog>

liquibase.properties (update changeLog File file format with SQL, XML, YAML, JSON as per changelog.mysql.)

changeLogFile=changelog.mysql.xml
liquibase.command.url:jdbc:mysql://localhost:3306/appleDb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
liquibase.command.username : root
liquibase.command.password : rootroot

Run command liquibase update

default value in the table definition can be verified by running the query

desc appleDb.apple;

Default Value as Change Set

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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 http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
	<changeSet author="wesome" id="1692599548266-1">
		<createTable catalogName="appledb" tableName="apple">
			<column name="apple_id" type="BIGINT" />
			<column name="apple_name" type="varchar(20)" />
			<column name="available" type="boolean" />
			<column name="price" type="float" />
			<column name="current_date_time" type="datetime" valueDate="now()" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1692599548266-2">
		<addDefaultValue catalogName="appledb" columnDataType="BIGINT" columnName="apple_id" defaultValue="1" schemaName="appledb" tableName="apple" />
	</changeSet>
	<changeSet author="wesome" id="1692599548266-3">
		<addDefaultValue catalogName="appledb" columnDataType="float" columnName="price" defaultValueNumeric="10.10" schemaName="appledb" tableName="apple" />
	</changeSet>
	<changeSet author="wesome" id="1692599548266-4">
		<addDefaultValue catalogName="appledb" columnDataType="varchar(20)" columnName="apple_name" defaultValue="Fuji" schemaName="appledb" tableName="apple" />
	</changeSet>
	<changeSet author="wesome" id="1692599548266-5">
		<addDefaultValue catalogName="appledb" columnDataType="boolean" columnName="available" defaultValueBoolean="true" schemaName="appledb" tableName="apple" />
	</changeSet>
	<changeSet author="wesome" id="1692599548266-6">
		<addDefaultValue catalogName="appledb" columnDataType="datetime" columnName="current_date_time" defaultValueComputed="now()" schemaName="appledb" tableName="apple" />
	</changeSet>
</databaseChangeLog>

liquibase.properties (update changeLog File file format with SQL, XML, YAML, JSON as per changelog.mysql.)

changeLogFile=changelog.mysql.xml
liquibase.command.url:jdbc:mysql://localhost:3306/appleDb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
liquibase.command.username : root
liquibase.command.password : rootroot

Run command liquibase update

default value in the table definition can be verified by running the query

desc appleDb.apple;

follow us on