addLookupTable

Creates a lookup table containing values stored in a column and creates a foreign key to the new table.

Run addLookupTable

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
constraintName Name of the constraint (FOREIGN KEY CONSTRAINT) to create between the existing table and the lookup table all
existingColumnName Name of the column containing the data to extract all all
existingTableCatalogName Name of the catalog of the existing table all
existingTableName Name of the table containing the data to extract all all
existingTableSchemaName Name of the schema of the existing table all
newColumnDataType

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)

For Snowflake, Liquibase also implements BINARY, TIME, and TIMESTAMP_NTZ. For Databricks, Liquibase also implements ARRAY<INT> and ARRAY<STRING>.

Note: To specify a complex Databricks type like ARRAY<STRING> in an XML changelog, you must specify the escape sequences &lt; and &gt; rather than < and >. See createTable.

For more information, see Liquibase Data Type Handling.

informix, mariadb, mssql, mysql all
newColumnName Name of the column in the new table to create all all
newTableCatalogName Name of the catalog of the table to create all
newTableName Name of the table to create all all
newTableSchemaName Name of the schema of the table to create all

Examples

--liquibase formatted sql

--changeset liquibase-docs:addLookupTable-example
CREATE  TABLE  cat.state  AS  SELECT  DISTINCT  state  AS  abbreviation  FROM  address  WHERE  state  IS  NOT  NULL;
ALTER  TABLE  public.state  MODIFY  abbreviation  CHAR(2)  NOT  NULL;  
ALTER  TABLE  public.state  ADD  PRIMARY  KEY  (abbreviation);  
ALTER  TABLE  address  ADD  CONSTRAINT  fk_address_state  FOREIGN  KEY  (state)  REFERENCES  public.state  (abbreviation);
databaseChangeLog:
-  changeSet:
    id:  addLookupTable-example
    author:  liquibase-docs
    changes:
    -  addLookupTable:
        constraintName:  fk_address_state
        existingColumnName:  state
        existingTableName:  address
        newColumnDataType:  char(2)
        newColumnName:  abbreviation
        newTableCatalogName:  cat
        newTableName:  state
        newTableSchemaName:  public
{
    "databaseChangeLog": [
        {
            "changeSet": {
                "id": "addLookupTable-example",
                "author": "liquibase-docs",
                "changes": [
                    {
                        "addLookupTable": {
                            "constraintName": "fk_address_state",
                            "existingColumnName": "state",
                            "existingTableName": "address",
                            "newColumnDataType": "char(2)",
                            "newColumnName": "abbreviation",
                            "newTableCatalogName": "cat",
                            "newTableName": "state",
                            "newTableSchemaName": "public"
                        }
                    }
                ]
            }
        }
    ]
}
<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="addLookupTable-example">
        <addLookupTable  constraintName="fk_address_state"
            existingColumnName="state"
            existingTableName="address"
            newColumnDataType="char(2)"
            newColumnName="abbreviation"
            newTableCatalogName="cat"
            newTableName="state"
            newTableSchemaName="public"/>
    </changeSet>

</databaseChangeLog>

Database support

Database Notes Auto Rollback
DB2/LUW Supported Yes
DB2/z Supported Yes
Derby Supported Yes
Firebird Not Supported No
Google BigQuery Supported Yes
H2 Supported Yes
HyperSQL Not Supported No
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