Javatpoint Logo
Javatpoint Logo

Difference between UNIQUE and UNIQUE All


When we talk about uniqueness in SQL then we often use the UNIQUE constraint to ensure that the values in a column or combination of columns are unique in the table. However, UNIQUE ALL is a conceptualization that implies an emphasis on uniqueness in a combination of pillars.


The SQL UNIQUE statement makes sure that all values in a defined column or combination of columns are distinctive from all rows in the database. It means that no two rows can have an identical value in the column(s) specified by the UNIQUE constraint. Avoiding duplicates is important to ensure data integrity.


Assume you want to ensure that each product appears just once per order in the order_items database. You can apply a composite UNIQUE constraint on the mix of order_id and product_id.

In this example:

  • The mix of order_id and product_id must be unique.
  • It ensures that a specific product can only be listed once per order.

First Insertion: Successful.

Second Insertion: Fails due to the UNIQUE constraint violation.


If UNIQUE ALL were used in a SQL database then it would emphasize that the uniqueness constraint applies to the combination of columns, guaranteeing that the values across these columns are unique. It is comparable to the composite UNIQUE constraint in normal SQL.


Consider the following practical example: we want to ensure that each product appears only once per order in the order_items table. We will use a composite UNIQUE constraint on the order_id and product_id columns.

In this example:

  • The order_id and product_id must be unique.
  • It prevents the identical product from being listed more than once in the same order.

First insertion

This insertion is successful since the order_id and product_id are unique.

Second insertion

This insertion fails with an error because the order_id already exists.

Third insertion

This insertion fails with an error because the combination of order_id and product_id (1, 101) already exists.

Error message

When attempting a third insertion, the database will provide an error message indicating a violation of the unique constraint:


Purpose Ensures that values in a single column or a group of columns are distinct inside the table. Hypothetically, all permutations of values across defined columns must be distinct within a table.
Standardization All major database systems provide the standard SQL functionality. It is not part of mainstream SQL and is infrequently utilized in database systems.
Usage Typically used to impose uniqueness restrictions on particular columns or groups of columns. Only sometimes utilized due to its hypothetical nature and lack of support in the majority of database systems.
Syntax Constraint applied directly to columns. A hypothetical syntax that emphasizes composite uniqueness and is not supported by SQL syntax.
Error handling When trying to insert a duplicate value into the given column(s), an error message is returned. If supported, attempts to insert a duplicate combination of values across specified columns will result in an error.
Composite uniqueness Using a composite key, you can guarantee uniqueness across many columns. Hypothetically emphasizes composite uniqueness, however, this is unnecessary given the capabilities of UNIQUE.
Real-world implementation Widely implemented and utilized in database designs to ensure data integrity. Due to the existence of composite UNIQUE restrictions, this is not commonly applied.
Documentation SQL standards and database documentation are both well-documented. It is not documented since it is not a typical SQL feature.
Index creation Automatically generates indexes to enforce uniqueness restrictions. To ensure composite uniqueness, indexes would be created on column combinations.
Data integrity Ensures data integrity by avoiding duplicate entries in certain columns or column combinations. UNIQUE ALL would ensure data integrity by prohibiting duplicate value combinations if enabled.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA