๐ง Day 10: Stored Procedures, Triggers, Normalization & More in SQL
๐ง Day 10: Stored Procedures, Triggers, Normalization & More in SQL
โ๏ธ What is a Stored Procedure?
A Stored Procedure is a set of SQL statements that are stored in the database and can be reused whenever needed.
It helps improve performance, reusability, and security.
๐ Definition:
A stored procedure is a precompiled collection of one or more SQL statements that can accept input parameters, return output parameters, or both.
๐งฉ Key Features
-
Can accept input and return output parameters
-
Allows code reuse
-
Reduces network traffic
-
Improves security and performance
๐งฑ Syntax
๐ Example 1: Simple Procedure
๐ผ Example 2: EmployeeDB โ Stored Procedure Demo
โ Stored Procedure 1 โ Get All Employees
โ Stored Procedure 2 โ Get Employees by Department
๐ง Normalization in SQL
Normalization is the process of organizing data to reduce redundancy and improve data integrity.
๐น Goals of Normalization
-
Remove duplicate data
-
Ensure data consistency
-
Improve database efficiency
๐งพ Types of Normal Forms
-
1NF (First Normal Form):
-
Eliminate repeating groups.
-
Ensure each cell holds a single value.
-
-
2NF (Second Normal Form):
-
Meet 1NF.
-
Remove partial dependencies.
-
-
3NF (Third Normal Form):
-
Meet 2NF.
-
Remove transitive dependencies.
-
-
BCNF (BoyceโCodd Normal Form)
-
Stronger version of 3NF.
-
-
4NF, 5NF, 6NF:
-
Handle more complex dependencies and relationships.
-
โก Triggers in SQL
A Trigger is a special kind of stored procedure that executes automatically when a specific event occurs in the database.
๐น Trigger Types
| Type | Description |
|---|---|
| DML Triggers | Fired automatically on INSERT, UPDATE, or DELETE. |
| DDL Triggers | Fired automatically on CREATE, ALTER, or DROP. |
| Logon Triggers | Fired automatically when a user logs into SQL Server. |
๐งฉ Example:
Automatically log updates made to a table.
๐งฑ Temporary Tables
Temporary tables are stored in TempDB and are deleted automatically when the session ends.
๐น Uses
-
Store intermediate data.
-
Simplify complex queries.
-
Improve performance during data processing.
๐งฉ 1. Local Temporary Table
-
Prefixed with
# -
Visible only to the current session
-
Dropped automatically when session ends
Syntax:
๐งฉ 2. Global Temporary Table
-
Prefixed with
## -
Accessible to all sessions
-
Dropped when all sessions close
Syntax:
โ๏ธ Indexes in SQL
Indexes are used to speed up data retrieval from a database table.
๐น Types of Indexes
-
Clustered Index โ Sorts and stores data rows in the table based on key values.
-
Non-Clustered Index โ Stores index separately from actual data.
๐งฉ Example:
๐ Cursor in SQL
A cursor is a database object that allows you to process each row individually.
๐น Types of Cursors
-
Implicit Cursor โ Managed automatically by SQL (e.g., in simple SELECT queries).
-
Explicit Cursor โ Declared and controlled manually by the user.
โ ๏ธ Use cursors only when set-based operations are not possible โ they are slower than standard SQL.