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

Constraints Introduction

SQL Server constraints are rules applied to table columns to maintain data accuracy, consistency and integrity. Constraints help control what type of data can be inserted or stored in a table.

What are Constraints?

A constraint is a rule that is applied to a column or table to control the data that can be stored.

Constraints help ensure that invalid or unwanted data is not inserted into the database.

Simple Example

If every student must have a unique StudentId, we can use a PRIMARY KEY constraint to enforce that rule.

Why Do We Need Constraints?

Constraints help maintain the quality and integrity of data stored in database tables.

  • Prevent invalid data.
  • Maintain data integrity.
  • Prevent duplicate values where required.
  • Establish relationships between tables.
  • Apply rules to column values.
  • Provide consistency across the database.

Types of SQL Server Constraints

Constraint Purpose Example
PRIMARY KEY Uniquely identifies each record in a table. StudentId
FOREIGN KEY Creates a relationship between tables. CourseId
UNIQUE Prevents duplicate values in a column. Email
NOT NULL Prevents NULL values in a column. StudentName
DEFAULT Provides a default value when no value is supplied. Status
CHECK Restricts values according to a condition. Age >= 18

Constraint Structure

Constraints can be applied while creating a table or added later to an existing table.

SQL Server

                                                                Database
                                                                   ↓
                                                                 Table
                                                                   ↓
                                                                Columns
                                                                   ↓
                                                                Constraints
                                                                   ↓
                                                                Data Integrity

Constraints Example

The following example uses multiple constraints while creating a Students table.

SQL
CREATE TABLE Students
(
    StudentId INT PRIMARY KEY,

    StudentName VARCHAR(100) NOT NULL,

    Email VARCHAR(150) UNIQUE,

    Age INT CHECK (Age >= 18),

    Status VARCHAR(20) DEFAULT 'Active'
);

Example Table Structure

Column Data Type Constraint
StudentId INT PRIMARY KEY
StudentName VARCHAR(100) NOT NULL
Email VARCHAR(150) UNIQUE
Age INT CHECK
Status VARCHAR(20) DEFAULT

Constraints and Data Integrity

Data integrity means maintaining accurate and consistent data in the database.

SQL Server
Valid Data
    ↓
Constraints
    ↓
Database Rules
    ↓
Accurate & Consistent Data
Important Points
  • Constraints enforce rules on table data.
  • Different constraints solve different data integrity requirements.
  • Constraints can be defined during table creation.
  • Constraints can also be added to existing tables.

Summary

In this section, we learned what SQL Server constraints are, why they are required and the different types of constraints used to maintain data integrity.