Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Liquibase, is there a difference between using a unique <createIndex> and using <column> with a unique constraint?

Liquibase has two ways to define a column as unique:

  1. When creating the table, using <constraints> on the column:

    <createTable tableName="my_table">
        <column name="my_column">
            <constraints unique="true"
                         uniqueConstraintName="my_table_my_column_uk">
        </column>
    </createTable>
    
  2. After creating the table, using <createIndex>:

    <createTable tableName="my_table">
        <column name="my_column"/>
    </createTable>
    <createIndex tableName="my_table" unique="true"
                 indexName="my_table_my_column_uk">
        <column name="my_column"/>
    </createIndex>
    

Is there any difference between these two approaches for single-column unique keys?

In my own observations with MySQL, there seems to be no difference. Both declarations (above) yield the same SHOW CREATE TABLE result:

...
UNIQUE_KEY `my_table_my_column_uk` (`my_column`)
...

However, does this hold true for all database implementations, or does <createIndex unique="true"> generate different schema output from <constraint unique="true"/> for different databases?

Background: I have a script that has built the liquibase changelog directly from my relational model in the code. The generation script created BOTH declarations if the model indicated the column was unique. I'm cleaning up the generated results and would like to remove one of the declarations, and want to know if that's appropriate.

like image 634
Rob Hruska Avatar asked May 13 '11 17:05

Rob Hruska


People also ask

What is the difference between unique key and unique constraints?

The difference between a UNIQUE constraint and a Primary Key is that per table you may only have one Primary Key but you may define more than one UNIQUE constraints. Primary Key constraints are not nullable. UNIQUE constraints may be nullable.

What is the difference between unique and non-unique indexes?

In addition to enforcing the uniqueness of data values, a unique index can also be used to improve data retrieval performance during query processing. Non-unique indexes are not used to enforce constraints on the tables with which they are associated.

What happens when you try to add unique constraint for a column which already has duplicate values?

If a UNIQUE constraint is added to a column that has duplicated values, the Database Engine returns an error and does not add the constraint. The Database Engine automatically creates a UNIQUE index to enforce the uniqueness requirement of the UNIQUE constraint.

What are the differences between a primary key and a unique index?

Primary key will not accept NULL values whereas Unique key can accept NULL values. A table can have only one primary key whereas there can be multiple unique key on a table. A Clustered index automatically created when a primary key is defined whereas Unique key generates the non-clustered index.


1 Answers

See PostgreSQL Documentation:

Note: The preferred way to add a unique constraint to a table is ALTER TABLE ... ADD CONSTRAINT. The use of indexes to enforce unique constraints could be considered an implementation detail that should not be accessed directly. One should, however, be aware that there's no need to manually create indexes on unique columns; doing so would just duplicate the automatically-created index.

So a unique constraint is a concept which is implemented (in PostgreSQL) with a unique index.

like image 52
Janning Avatar answered Sep 24 '22 00:09

Janning