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

NOT NULL & DEFAULT

NOT NULL and DEFAULT are SQL Server constraints used to control the values stored in table columns. NOT NULL prevents NULL values, while DEFAULT automatically provides a value when no value is supplied.

What is NOT NULL?

The NOT NULL constraint ensures that a column must always contain a value. A NULL value cannot be inserted into a NOT NULL column.

Simple Example

If StudentName is defined as NOT NULL, every student record must contain a StudentName.

Creating a NOT NULL Column

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

NOT NULL Example Output

SQL
INSERT INTO Students
VALUES (101, NULL, 22);
Error: StudentName cannot contain NULL because the column is defined with the NOT NULL constraint.

What is DEFAULT?

The DEFAULT constraint automatically inserts a predefined value when a value is not provided for a column.

Simple Example

If City has a DEFAULT value of Pune, SQL Server will automatically insert Pune when City is not specified.

Creating a DEFAULT Constraint

SQL
CREATE TABLE Students
(
    StudentId INT PRIMARY KEY,
    StudentName VARCHAR(100) NOT NULL,
    City VARCHAR(50) DEFAULT 'Pune'
);

Using DEFAULT Value

When City is not specified, SQL Server automatically uses the default value.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName
)
VALUES
(
    101,
    'Rahul'
);
Output
StudentId StudentName City
101 Rahul Pune

Named DEFAULT Constraint

We can provide a specific name to a DEFAULT constraint.

SQL
CREATE TABLE Students
(
    StudentId INT PRIMARY KEY,
    StudentName VARCHAR(100) NOT NULL,
    City VARCHAR(50),

    CONSTRAINT DF_Students_City
    DEFAULT 'Pune' FOR City
);

Add DEFAULT to Existing Table

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

SQL
ALTER TABLE Students
ADD CONSTRAINT DF_Students_City
DEFAULT 'Pune' FOR City;

NOT NULL vs DEFAULT

NOT NULL DEFAULT
Prevents NULL values. Provides a default value.
Column must contain a value. Value is automatically supplied when omitted.
Used for mandatory data. Used for automatic default data.

NOT NULL and DEFAULT Together

NOT NULL and DEFAULT can be used together when a column must always contain a value and should receive an automatic value when one is not supplied.

SQL
CREATE TABLE Employees
(
    EmployeeId INT PRIMARY KEY,

    EmployeeName VARCHAR(100) NOT NULL,

    Department VARCHAR(50) DEFAULT 'IT'
);

NOT NULL & DEFAULT Structure

SQL Server
Column
  |
  ├── NOT NULL
  |      ↓
  |   NULL Not Allowed
  |
  └── DEFAULT
         ↓
   Automatic Value
         ↓
      When Value
      Is Not Provided
Important Points
  • NOT NULL prevents NULL values.
  • DEFAULT provides an automatic value when a value is not supplied.
  • NOT NULL is useful for mandatory columns.
  • NOT NULL and DEFAULT can be used together.

Summary

In this section, we learned about NOT NULL and DEFAULT constraints, how to create them, how to add them to an existing table and how they control values stored in SQL Server tables.