SQL Server BasicsWhat is SQL Server database?What is RDBMS?What is Normalization?Why we use Denormalization?What_is_SQL?What is PL/SQL?Difference between SQL and PL/SQLDatabase TableOne to One RelationshipOne to Many RelationshipMany to Many RelationshipMany to One RelationshipString Data TypesNumber Data TypesDate Data TypesOther Data TypesCreate DatabaseDrop DatabaseCreating and Managing Users in SQL ServerCreate TableAlter TableDrop TableConstraints in SQL serverPrimary KeyForeign KeyUnique KeyCandidate KeyComposite KeyDifference between primary key and candidate keyPrimary key and foreign key relationshipSurrogate KeyCascading Referential Integrity ConstraintsSelf Referential Integrity ConstraintsInsert into statementInsert multiple recordsUpdate statementDelete statementTruncate statementDifference between Delete and TruncateAlias in SQL ServerSelect statementSelect DistinctSelect TopSelect IntoNull Functions(ISNULL(),NULLIF(),COALESCE())Sub QueryIdentity ColumnSequence objectDifference between sequence and identity columnSQL Server ClausesWHERE ClauseOrder By ClauseTop N ClauseGroup By ClauseHaving ClauseDifference between Where and HavingSQL Server OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAny OperatorsAll OperatorsUnion OperatorsUnion All OperatorsDifference between Union and Union AllIntersect OperatorExcept OperatorDifference between Except and IntersectJoinsInner JoinLeft JoinRight JoinFull JoinSelf JoinCross JoinViewsWhat are views?Create views using SSMSIndexed ViewsComplex ViewsCheck Option in ViewCheck Encryption in ViewSchema Binding Option in ViewRead-only ViewsUpdatable ViewsAdvantages and disadvantages of viewsCreate multiple views on one tableCan we implement index on views?Can we Perform Insert, update, delete operation on views?Stored Procedure and FunctionsWhat are Stored Procedures?Why we use stored procedures?Passing parameters to Stored procedureUser-Defined FunctionsDifference between UDF and Stored procedurePre-Defined Functions@@Indentity and Scope_IndentityNULLIF, ISNULL and COALESCE

What is Unique Key in SQL Server?

A unique key in SQL Server is a constraint that ensures the uniqueness of values within one or more columns in a table. It guarantees that each value in the specified column(s) is unique and cannot be duplicated within the table. Let's explain with an example:

Suppose we have a table called "Employees" with the following columns: EmployeeID, FirstName, LastName, and Email. We want to ensure that each employee's email address is unique within the table. To achieve this, we can define a unique key constraint on the "Email" column.

Here's an example of creating a unique key constraint in SQL Server:


CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    Email VARCHAR(100) UNIQUE
);

In this example, the "Email" column is defined as a unique key using the UNIQUE keyword. This means that each value in the "Email" column must be unique, and duplicate values are not allowed.

Let's insert some sample data into the "Employees" table to see how the unique key constraint works:


INSERT INTO Employees (EmployeeID, FirstName, LastName, Email)
VALUES (1, 'John', 'Doe', 'john.doe@example.com');

-- This insert will succeed since the email is unique
INSERT INTO Employees (EmployeeID, FirstName, LastName, Email)
VALUES (2, 'Jane', 'Smith', 'jane.smith@example.com');

-- This insert will fail since the email 'john.doe@example.com' already exists
INSERT INTO Employees (EmployeeID, FirstName, LastName, Email)
VALUES (3, 'Mike', 'Johnson', 'john.doe@example.com');

In the above example, the first two inserts succeed because they have unique email addresses. However, the third insert fails because it violates the unique key constraint. It tries to insert an email address that already exists in the table.

By enforcing a unique key constraint on the "Email" column, we can ensure that each employee has a unique email address within the "Employees" table, maintaining data integrity and preventing duplicate entries.