SQL UNIQUE Constraint (2024)

SQL UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are different.

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has aUNIQUE constraint.

However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

SQL UNIQUE Constraint on CREATE TABLE

The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is created:

SQL Server / Oracle / MS Access:

CREATE TABLE Persons(
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);

MySQL:

CREATE TABLE Persons(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);

To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);

SQL UNIQUE Constraint on ALTER TABLE

To create a UNIQUE constraint on the "ID" column when the table is already created, use the following SQL:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ADD UNIQUE (ID);

To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);

DROP a UNIQUE Constraint

To drop a UNIQUE constraint, use the following SQL:

MySQL:

ALTER TABLE Persons
DROP INDEX UC_Person;

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT UC_Person;


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

SQL UNIQUE Constraint (2024)

FAQs

What does a unique constraint do read the answers carefully? ›

By using a UNIQUE constraint on a particular column, we ensure that this column cannot contain two of the same values. Let's see an example using the Person table with the columns Name, Age, and SSN. The SSN column implements the UNIQUE constraint, i.e. it cannot contain any duplicate values.

What is a violation of unique key constraint? ›

During the Load process, if a user receives an error with "unique constraint (...) violated" in it, that means the user submitted an insert transaction that is a duplicate of data already in the database.

Does unique constraint allow multiple NULL values? ›

You can insert NULL values into columns with the UNIQUE constraint because NULL is the absence of a value, so it is never equal to other NULL values and not considered a duplicate value. This means that it's possible to insert rows that appear to be duplicates if one of the values is NULL .

How many NULL values are in a unique key? ›

In the case of a primary key, we cannot save NULL values. In the case of a unique key, we can save a null value, however, only one NULL value is supported. It supports entity integrity. It supports unique data.

What do unique constraints make sure? ›

Unique constraints ensure that the data in a column or combination of columns is unique for each row. A table's primary key, for example, functions as an implicit unique constraint. Hence, the primary key constraint automatically has a unique constraint.

What is true about unique constraint? ›

The UNIQUE constraint ensures that all values in a column are different. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint.

How do you avoid violation of unique key constraint? ›

You should not create tables with unique constraints not including CompanyID. The companyID field is what's used to isolate snapshots from tenant's data, so it's required that it is a part of any unique index.

Can unique constraint accept duplicate values? ›

Constraints are rules that the SQL Server Database Engine enforces for you. For example, you can use UNIQUE constraints to make sure that no duplicate values are entered in specific columns that don't participate in a primary key.

Why only one NULL is allowed in unique key? ›

Logically, any key which is allowed to contain non duplicate (unique) values is a unique key, NULL is a permissible value in sql server , so it can have NULL for a single time just like any other value. If two cell values resembles null, then the column cell's values is not unique at all , therefore ...

What is a unique constraint if NOT NULL? ›

A NOT NULL constraint is a rule that prevents null values from being entered into one or more columns within a table. A unique constraint (also referred to as a unique key constraint) is a rule that forbids duplicate values in one or more columns within a table.

Which unique key will not accept NULL values? ›

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

What is the difference between a primary key constraint and a unique constraint? ›

The primary key does not store null values, whereas the unique key does. A table can only have one primary key, whereas it can have multiple unique keys. The primary key does not allow you to delete or modify the data. On the other hand, a unique key does.

What is the difference between a unique constraint and a primary key? ›

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 does a unique constraint do? ›

The UNIQUE constraint ensures that all values in a column are different. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint.

What do unique key constraints create? ›

Creating a unique constraint automatically creates a corresponding unique index. For information on unique constraints in Azure Synapse Analytics, see Primary key, foreign key, and unique key in Azure Synapse Analytics.

What is a unique key constraint in SQL? ›

The SQL UNIQUE KEY constraint ensures that all values in a column are unique. It prevents duplicate values from being stored in the table. It is usually used in conjunction with the PRIMARY KEY constraint to create a composite key. The UNIQUE KEY constraint is an alternative to the PRIMARY KEY constraint.

How can constraints be used to help define the problem your answer? ›

Constraints are logical conditions that a solution to an optimization problem must satisfy. They reflect real-world limits on production capacity, market demand, available funds, and so on. To define a constraint, you first compute the value of interest using the decision variables.

Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5919

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.