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

COMMIT

Learn how the COMMIT statement permanently saves changes made during a SQL Server transaction.

What is COMMIT?

COMMIT is a SQL Server transaction command used to permanently save all changes made during the current transaction.

Once a transaction is committed, the changes become permanent in the database and normally cannot be undone using ROLLBACK.

Why Do We Use COMMIT?

  • To permanently save transaction changes.
  • To confirm that all required operations completed successfully.
  • To make the transaction changes permanent.
  • To complete a transaction successfully.

COMMIT Syntax

BEGIN TRANSACTION;

-- SQL statements

COMMIT;

Simple COMMIT Example

Suppose we want to insert a new student into the Students table.

BEGIN TRANSACTION;

INSERT INTO Students
(
    StudentId,
    StudentName,
    City,
    Age
)
VALUES
(
    201,
    'Rahul',
    'Pune',
    22
);

COMMIT;

Here, the INSERT operation is performed inside the transaction. When COMMIT executes, the new student record is permanently saved.

UPDATE with COMMIT

COMMIT can also be used after an UPDATE operation.

BEGIN TRANSACTION;

UPDATE Students
SET City = 'Mumbai'
WHERE StudentId = 201;

COMMIT;

The student's city is updated and the change is permanently saved after COMMIT.

DELETE with COMMIT

BEGIN TRANSACTION;

DELETE FROM Students
WHERE StudentId = 201;

COMMIT;

The DELETE operation is performed inside the transaction and becomes permanent after COMMIT.

COMMIT with Multiple Operations

A transaction can contain multiple SQL statements. COMMIT saves all of them together.

BEGIN TRANSACTION;

INSERT INTO Students
(
    StudentId,
    StudentName,
    City,
    Age
)
VALUES
(
    202,
    'Priya',
    'Pune',
    21
);

UPDATE Students
SET Age = 22
WHERE StudentId = 202;

COMMIT;

Both operations are part of the same transaction. When COMMIT executes, the changes are saved.

COMMIT vs ROLLBACK

COMMIT ROLLBACK
Permanently saves changes. Undoes uncommitted changes.
Completes the transaction successfully. Cancels/undoes the transaction changes.
Used when the operation is successful. Used when an error or unwanted result occurs.

Real-World Example

Consider an online shopping application.

When a customer places an order, several operations may need to happen:

  1. Create the order.
  2. Add order items.
  3. Reduce product stock.
  4. Save payment information.

If all operations are successful, the application can COMMIT the transaction.

BEGIN TRANSACTION;

-- Create Order

-- Add Order Items

-- Update Stock

-- Save Payment

COMMIT;

Important Points

  • COMMIT permanently saves transaction changes.
  • COMMIT is normally used after successful transaction operations.
  • COMMIT completes the current transaction.
  • After COMMIT, the transaction changes are no longer available for normal ROLLBACK.
  • Multiple SQL statements can be committed together as one transaction.

Summary

COMMIT is used to permanently save changes made during a SQL Server transaction.

A transaction should generally be committed when all required operations have completed successfully.