Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

UNIQUE Constraint

The UNIQUE constraint is used to ensure that all values in a column are different. It prevents duplicate values from being stored in a column.

What is a UNIQUE Constraint?

A UNIQUE constraint ensures that no two rows contain the same value in the specified column.

It is commonly used for columns such as Email, Phone Number, Username or other values that should remain unique.

Simple Example

If Email has a UNIQUE constraint, two students cannot have the same email address.

UNIQUE Constraint Rules

  • Duplicate values are not allowed.
  • UNIQUE constraints maintain data uniqueness.
  • A table can have multiple UNIQUE constraints.
  • A UNIQUE constraint can be applied to one or more columns.
  • A UNIQUE constraint is different from a PRIMARY KEY.

Create UNIQUE Constraint

A UNIQUE constraint can be created while defining a table.

SQL
CREATE TABLE Students
(
    StudentId INT PRIMARY KEY,
    StudentName VARCHAR(100),
    Email VARCHAR(150) UNIQUE
);

Named UNIQUE Constraint

We can also provide a specific name to the UNIQUE constraint.

SQL
CREATE TABLE Students
(
    StudentId INT PRIMARY KEY,
    StudentName VARCHAR(100),
    Email VARCHAR(150),

    CONSTRAINT UQ_Students_Email
    UNIQUE (Email)
);

Insert Unique Values

Different email addresses can be inserted successfully.

SQL
INSERT INTO Students
VALUES (101, 'Rahul', 'rahul@gmail.com');

INSERT INTO Students
VALUES (102, 'Priya', 'priya@gmail.com');
Result: Both records are inserted because their Email values are different.

Duplicate Value Example

Suppose the email already exists in the table.

SQL
INSERT INTO Students
VALUES (103, 'Amit', 'rahul@gmail.com');
Error: The INSERT operation fails because the Email value already exists in the table.

Add UNIQUE Constraint to Existing Table

A UNIQUE constraint can be added to an existing table using ALTER TABLE.

SQL
ALTER TABLE Students
ADD CONSTRAINT UQ_Students_Email
UNIQUE (Email);

Multiple UNIQUE Constraints

A table can contain multiple UNIQUE constraints.

SQL
CREATE TABLE Users
(
    UserId INT PRIMARY KEY,

    Username VARCHAR(50),

    Email VARCHAR(150),

    CONSTRAINT UQ_Users_Username
    UNIQUE (Username),

    CONSTRAINT UQ_Users_Email
    UNIQUE (Email)
);

Example Output

StudentId StudentName Email
101 Rahul rahul@gmail.com
102 Priya priya@gmail.com

Every Email value is unique in the table.

UNIQUE Constraint Structure

SQL Server

                                                            Students Table
                                                                  |
                                                                  ↓
                                                             Email Column
                                                                  |
                                                                  ↓
                                                            UNIQUE Constraint
                                                                  |
                                                                  ↓
                                                            Duplicate Values
                                                                 ❌
                                                                  |
                                                                  ↓
                                                             Unique Values
                                                                 ✓

PRIMARY KEY vs UNIQUE

PRIMARY KEY UNIQUE
Uniquely identifies each row. Ensures values are unique.
Only one PRIMARY KEY per table. Multiple UNIQUE constraints can exist.
Does not allow NULL values. NULL handling is different from PRIMARY KEY.
Usually identifies the main record. Used for additional unique values.
Important Points
  • UNIQUE prevents duplicate values.
  • Multiple UNIQUE constraints can be created in a table.
  • UNIQUE constraints can be named explicitly.
  • UNIQUE constraints can be added using ALTER TABLE.

Summary

In this section, we learned how the UNIQUE constraint prevents duplicate values, how to create it during table creation and how to add it to an existing table.