SQL Server Unique Constraint
Whats is Unique Constraint in SQL | How to create Unique Constraint in SQL | Unique Constraint with Example in SQL Server
A UNIQUE constraint ensures that all values in a column are unique. This provides uniqueness for the column(s) and helps identify each row uniquely. Unlike primary key, there can be multiple unique constraints defined per table. The code syntax for UNIQUE is similar to that of PRIMARY KEY and can be used interchangeably.
CREATE TABLE Employee ( /* Create table with a single field as unique */
ID INT NOT NULL UNIQUE
Name VARCHAR(255)
);
CREATE TABLE Employees ( /* Create table with multiple fields as unique */
ID INT NOT NULL
LastName VARCHAR(255)
FirstName VARCHAR(255) NOT NULL
CONSTRAINT PK_Student
UNIQUE (ID, FirstName)
);
ALTER TABLE Employees /* Set a column as unique */
ADD UNIQUE (ID);
ALTER TABLE Employees /* Set multiple columns as unique */
ADD CONSTRAINT PK_Employee /* Naming a unique constraint */
UNIQUE (ID, FirstName);
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.