The Primary key
is one of the most important constraints in a Database
, it is a single or a group of columns that can uniquely identify a row in a table. Primary keys
always have unique values and are indexed for faster searching and retrieval.
But sometimes it's required to drop the Primary key
from a table. Liquibase
provides a dropPrimaryKey
constraint to drop an existing primary key from a table.
primary key can be generated in 2 ways, at the time of table creation and with addPrimaryKey changeset.
Drop Primary Key Generated at Table Creation
<?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">
<constraints nullable="false" primaryKey="true" />
</column>
<column name="apple_name" type="VARCHAR(255)" />
<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
</createTable>
</changeSet>
<changeSet author="wesome" id="1692701200879-2">
<insert catalogName="appledb" tableName="apple">
<column name="apple_Id" value="1" />
<column name="apple_name" value="Macintosh" />
<column name="current_date_time" valueDate="now()" />
</insert>
</changeSet>
<changeSet author="wesome" id="1692599548266-3">
<dropPrimaryKey tableName="apple" schemaName="appleDb" dropIndex="true" />
</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
the primary key has been dropped and can be confirmed by the described table.
desc appleDb.apple;
Drop Primary Key Generated by Changeset
To drop a single column
Liquibase provides simple changeset
, that takes tableName
and columnName
<?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(255)" />
<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
</createTable>
</changeSet>
<changeSet author="wesome" id="1692701200879-2">
<addPrimaryKey tableName="apple" columnNames="apple_Id" constraintName="primary_key_constraint" />
</changeSet>
<changeSet author="wesome" id="1692701200879-3">
<insert catalogName="appledb" tableName="apple">
<column name="apple_Id" value="1" />
<column name="apple_name" value="Macintosh" />
<column name="current_date_time" valueDate="now()" />
</insert>
</changeSet>
<changeSet author="wesome" id="1692599548266-4">
<dropPrimaryKey tableName="apple" schemaName="appleDb" dropIndex="true" />
</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
The primary key has been dropped and can be confirmed by the described table.
desc appleDb.apple;