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

CHECK Constraint

The CHECK constraint is used to restrict the values that can be stored in a column. It allows data to be inserted only when a specified condition is satisfied.

What is a CHECK Constraint?

A CHECK constraint defines a condition that must be true when data is inserted or updated in a table.

If the condition is false, SQL Server rejects the INSERT or UPDATE operation.

Simple Example

If the Age column must contain a value greater than or equal to 18, a CHECK constraint can be used to enforce this rule.

Why Use CHECK Constraint?

  • Restricts invalid data.
  • Maintains data integrity.
  • Applies business rules at database level.
  • Prevents incorrect values from being inserted.
  • Validates data during INSERT and UPDATE operations.

Create CHECK Constraint

A CHECK constraint can be created while defining a table.

SQL
CREATE TABLE Students
(
    StudentId INT PRIMARY KEY,
    StudentName VARCHAR(100),
    Age INT CHECK (Age >= 18)
);

Insert Valid Data

Since the CHECK condition is Age >= 18, values greater than or equal to 18 are accepted.

SQL
INSERT INTO Students
VALUES (101, 'Rahul', 22);
Result: Record inserted successfully because Age 22 satisfies the CHECK condition.

Insert Invalid Data

A value that does not satisfy the CHECK condition will be rejected.

SQL
INSERT INTO Students
VALUES (102, 'Priya', 15);
Error: The record is rejected because Age 15 does not satisfy the condition Age >= 18.

Named CHECK Constraint

A CHECK constraint can be given a meaningful name.

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

    CONSTRAINT CK_Students_Age
    CHECK (Age >= 18)
);

Add CHECK Constraint to Existing Table

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

SQL
ALTER TABLE Students
ADD CONSTRAINT CK_Students_Age
CHECK (Age >= 18);

CHECK with Multiple Conditions

A CHECK constraint can contain more than one condition.

SQL
CREATE TABLE Employees
(
    EmployeeId INT PRIMARY KEY,
    EmployeeName VARCHAR(100),
    Age INT,
    Salary DECIMAL(10,2),

    CONSTRAINT CK_Employees_Age
    CHECK (Age >= 18),

    CONSTRAINT CK_Employees_Salary
    CHECK (Salary > 0)
);

CHECK with Range

CHECK can be used to restrict a value within a specific range.

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

    CONSTRAINT CK_Students_Marks
    CHECK (Marks BETWEEN 0 AND 100)
);

Example Output

StudentId StudentName Age Marks
101 Rahul 22 85
102 Priya 21 92

The records above satisfy the defined CHECK conditions.

CHECK Constraint Structure

SQL Server

                                                        Insert / Update Data
                                                              |
                                                              ↓
                                                        CHECK Condition
                                                              |
                                                          ┌───┴───┐
                                                          ↓       ↓
                                                       TRUE      FALSE
                                                        ↓          ↓
                                                     Accepted    Rejected
                                                        ↓          ↓
                                                      Table      Error

Remove CHECK Constraint

A named CHECK constraint can be removed using ALTER TABLE and DROP CONSTRAINT.

SQL
ALTER TABLE Students
DROP CONSTRAINT CK_Students_Age;
Important Points
  • CHECK validates values against a condition.
  • Invalid values are rejected.
  • Multiple CHECK constraints can exist in a table.
  • CHECK constraints can be named.
  • CHECK constraints can be added or removed using ALTER TABLE.

Summary

In this section, we learned how the CHECK constraint validates data, how to create and name CHECK constraints, how to add them to existing tables and how invalid values are prevented.