16.4. The UNIQUE Predicate

The UNIQUE predicate is a test for the absence of duplicate rows in a subquery. The UNIQUE keyword is also used as a table or column. This predicate is used to define the constraint. The UNIQUE column constraint is implemented in many SQL implementations with a CREATE UNIQUE INDEX <indexname> ON <table>(<column list>) statement hidden under the covers. The syntax for this predicate is:

<unique predicate> ::= UNIQUE <table subquery>

If any two rows in the subquery are equal to each other, the predicate is FALSE. However, the definition in the standard is worded in the negative, so that NULLs get the benefit of the doubt. The query can be written as an EXISTS predicate that counts rows, thus:

EXISTS (SELECT <column list> ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.