Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL Server - Get Started
  • Install SQL Server
  • SQL Server Management Studio
  • SQL Server - Windows Authentication
  • SQL Server - Authentication
  • SQL Server - Create New User
  • SQL Server - GRANT/REVOKE Permissions to User
  • SQL Server - Data Types
  • SQL Server - Naming Conventions
  • SQL Server - CREATE Database
  • SQL Server - CREATE Table
  • Add Columns
  • Identity Column
  • Rename Column, Table
  • Drop Columns
  • SQL Server - Schema
  • SQL Server - Tables Relations
  • SQL Server - Primary Keys
  • Modify/Delete Primary Keys
  • SQL Server - Foreign Keys
  • Modify/Delete Foreign Keys
  • SQL Server - Check Constraints
  • SQL Server - Unique Constraints
  • SQL Server - Views
  • Modify/Delete Views
  • SQL Server - Functions
  • SQL Server - Stored Procedures
  • Stored Procedure Parameters
  • SQL Server - Indexes
  • Non-clustered Indexes
  • Modify/Delete Indexes
  • SQL Server - Triggers
  • DDL Triggers
  • LOGON Triggers
  • Enable/Disable Triggers
  • Modify/Delete Triggers
  • SQL Server - Sequence
  • SQL Server - Synonyms
  • SQL Server - IF ELSE Statement
  • SQL Server - Loops
  • SQL Server - Insert Data
  • SQL Server - Update Data
  • SQL Server - Delete Data
  • SQL Server - Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL Server - Inner Join
  • Left Join
  • Right Join
  • Full Join
  • Self Join
  • Dynamic SQL
  • Built-in Functions
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

SQL Server: Unique Key Constraints

The Unique Constraint ensures the uniqueness of all values and no duplicate values are entered in a column of a table.

The unique constraints can be created using CREATE or ALTER TABLE T-SQL. Add unique constraints after columns definitions in the CREATE TABLE statement.

Syntax:
CONSTRAINT <constraint_name> UNIQE(<column_name>)

The following CREATE TABLE statement defines a unique key constraint on the PhoneNo column of the Employee table.

Example: Add Unique Constraint in Existing Table
CREATE TABLE Employee(EmployeeID int,
    FirstName nvarchar(50) NOT NULL,  
    LastName nvarchar(50) NOT NULL, 
    EMail nvarchar(50),
    PhoneNo varchar(15),
    ADD CONSTRAINT UNQ_Emp_Phone Unique(PhoneNo))

The following ALTER TABLE statement adds a unique constraint on the PhoneNo column of the existing Employee table.

Example: Add Unique Constraint in Existing Table
ALTER TABLE Employee   
ADD CONSTRAINT UNQ_Emp_Phone Unique(PhoneNo)

Now, if you try to insert or update a record in the Employee table where Phone is not within the specified range, then it will raise the following error.

Violation of UNIQUE KEY constraint 'UNQ_Emp_Phone'. Cannot insert duplicate key in object 'dbo.Employee'. The duplicate key value is (123.123.1834). The statement has been terminated.

Important Points:

  • Both Unique constraint and Primary key constraint enforce uniqueness. It is recommended to use Unique constraint instead of Primary key constraint whenever you want to enforce uniqueness in a column.
  • Unlike Primary key constraint, Unique constraints allow only one NULL value.
  • A unique index is automatically created when a unique key constraint is created.
  • SQL Server raises an error whenever a duplicate value is inserted or updated.
  • When a Unique index is added to an existing column in a table, the database engine first checks for the uniqueness of the specified column data. If a duplicate entry is found, then the engine returns an error and does not add the constraint.
  • A Unique key in a table can be referenced by a Foreign Key from another table.

Create Unique Constraint Using SSMS

Step 1: Open SSMS, login to a database. In the Object Explorer, expand the table folder and right-click on a table where you want to add a unique constraint and select Design to open it in a table designer.

SQL Server Unique Key Constraint

Now, right-click on the table designer and select Indexes/Keys,as shown below.

SQL Server Unique Key Constraint

Step 2: In the "Indexes/Keys" dialog box, select PhoneNo column in the Columns property.

Next, select Unique Key type to apply the unique key constraint on the PhoneNo column.

Add Unique Key Constraint

Step 3: give the name of a constraint in the name property.

Add Unique Constraint Expression

Click Close and Save the table.

Thus, you can create an unique key constraint on a column in a table.

Delete Unique Key Constraint

Use the DROP CONSTRAINT statement in the ALTER TABLE statement to delete existing unique constraints.

Syntax:
ALTER TABLE <table-name>
DROP CONSTRAINT <constraint_name>;

The following deletes a unique key constraint UNQ_Emp_Phone in the Employee table.

Example: Delete Unique Constraint
ALTER TABLE Employee   
DROP CONSTRAINT UNQ_Emp_Phone;

To delete constraints using SQL Server Management Studio, expand the table having the constraint to be deleted.

Now, expand Keys folder and right-click on a constraint to be deleted and click Delete.

Delete Unique Key Constraints

This will open "Delete Object" dialog box, as shown below.

Delete Unique Key Constraint

In the "Delete Object" dialog box, click Ok to delete a constraint.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.