Sqlite NULL and unique?

15,776

Solution 1

While the following addresses multiple null values, it does not address any "issues" associated with such a design, other than possible database/SQL portability - as such, it should probably not be considered an answer, and is left here merely for reference.


This is actually covered in the SQLite FAQ. It is a design choice - SQLite (unlike SQL Server) chose that multiple NULL values do not count towards uniqueness in an index.

The SQL standard requires that a UNIQUE constraint be enforced even if one or more of the columns in the constraint are NULL, but SQLite does not do this. Isn't that a bug?

Perhaps you are referring to the following statement from SQL92:

  • A unique constraint is satisfied if and only if no two rows in a table have the same non-null values in the unique columns.

That statement is ambiguous, having at least two possible interpretations:

  1. A unique constraint is satisfied if and only if no two rows in a table have the same values and have non-null values in the unique columns.

  2. A unique constraint is satisfied if and only if no two rows in a table have the same values in the subset of unique columns that are not null.

SQLite follows interpretation (1), as does PostgreSQL, MySQL, Oracle, and Firebird. It is true that Informix and Microsoft SQL Server use interpretation (2), however we the SQLite developers hold that interpretation (1) is the most natural reading of the requirement and we also want to maximize compatibility with other SQL database engines, and most other database engines also go with (1), so that is what SQLite does.

See a comparison of NULL handling.

Solution 2

If you want your unique index to throw an error when a two rows would be the same if you disregard NULL columns (and don't want to use triggers from Satyam's answer) you can do something like this

CREATE TABLE `test` (
    `Field1`    INTEGER,
    `Field2`    INTEGER
);
CREATE UNIQUE INDEX `ix` ON `test` (
    `Field1`,
    `Field2`
);
INSERT INTO `test`(`Field1`,`Field2`) VALUES (1,NULL);
INSERT INTO `test`(`Field1`,`Field2`) VALUES (1,NULL); -- This shouldn't be allowed

DROP INDEX IF EXISTS `ix`;
CREATE UNIQUE INDEX `ix2` ON `test` (
    `Field1`,
    ifnull(`Field2`, 0)  --use this instead
); --will fail

Solution 3

You can create 2 triggers on table to check if a row with column as null exists before any insert or update operation, if so raise exception.

CREATE TRIGGER UniqueColumnCheckNullInsert
        BEFORE INSERT
        ON 'Tablename'
        WHEN NEW.'column_name' IS NULL
        BEGIN
        SELECT CASE WHEN((
            SELECT 1
            FROM 'Tablename'
            WHERE 'column_name' IS NULL
            )
            NOTNULL) THEN RAISE(ABORT, "error row exists") END;
    END;

CREATE TRIGGER UniqueColumnCheckNullUpdate
        BEFORE UPDATE
        ON 'Tablename'
        WHEN NEW.'column_name' IS NULL
        BEGIN
        SELECT CASE WHEN((
            SELECT 1
            FROM 'Tablename'
            WHERE 'column_name' IS NULL
            )
            NOTNULL) THEN RAISE(ABORT, "error row exists") END;
    END;
Share:
15,776
katie
Author by

katie

Updated on June 01, 2022

Comments

  • katie
    katie almost 2 years

    I noticed that I can have NULL values in columns that have the UNIQUE constraint: UNIQUE(col)

    Would that generate any issues in certain situations?

  • Rei Miyasaka
    Rei Miyasaka almost 4 years
    I can't think of a situation in which (1) would be the useful interpretation.
  • Steve V.
    Steve V. over 3 years
    The ifnull() solution also causes inserts to fail for me on 'INSERT INTO test(Field1,Field2) VALUES (2,3);' and I'm not sure why, but it's not what I expected. I solved the problem in the application instead of in the database, but I wanted to put that here for anyone who came after me.
  • Johannes Egger
    Johannes Egger almost 3 years
    I have one: Users have a key card with a unique id, which means no two users can have the same key. But some users don't have a key card anymore (NULL). And they might have given their key card to another user.
  • user2864740
    user2864740 almost 3 years
    @JohannesEgger One of those the times when SQL Server's filtered indices (stackoverflow.com/a/15206061/2864740) becomes very useful for behavior (1) semantics.
  • user2864740
    user2864740 almost 3 years
    Another option to to use a filtered index (stackoverflow.com/a/15206061/2864740).
  • HH321
    HH321 almost 3 years
    you're right, here is a better link sqlite.org/partialindex.html, also see the note on when it can be used
  • Manngo
    Manngo over 2 years
    Nice trick. However, the code sample could be improved on by dropping the unnecessary back-ticks (or at least replacing them with the standard double quotes) and using the standard coalesce(Field2,0) instead of ifnull().