You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ποΈ SQL Server Mastery - T-SQL Script Collection
A Comprehensive Guide from Basics to Advanced Database Operations
π Overview
This repository contains a comprehensive collection of T-SQL scripts for Microsoft SQL Server, designed as a practical learning resource and reference guide. From basic database creation to advanced stored procedures and functions, these scripts cover essential SQL Server concepts through hands-on, executable examples.
Perfect for database administrators, developers, and students, this collection provides ready-to-use code snippets that demonstrate real-world SQL Server operations and best practices.
π Complete Script Index
π Foundation & Schema Management
File
Topic
Description
01-Create.sql
Database Creation
Creating new databases and understanding options
03-Create_table.sql
Table Creation
Creating tables with various data types and constraints
02-DELETE_Drop.sql
Deletion Operations
Properly dropping databases, tables, and data
π Constraints & Data Integrity
File
Topic
Description
04-Defult_constrain.sql
Default Constraints
Setting default values for columns
05-Defult_constrain.sql
Advanced Defaults
Complex default constraint scenarios
06-Cascadingreferentialintegrityconstraint.sql
Referential Integrity
Implementing cascading updates/deletes
07-Check_constraint.sql
Check Constraints
Data validation using check constraints
π Identity & Special Values
File
Topic
Description
08-Retrive_identity_column_Valuessql.sql
Identity Values
Retrieving identity column values after insertion
09-Retrivint_Identity.sql
Identity Management
Working with IDENTITY columns and SCOPE_IDENTITY()
SQL Server Management Studio (SSMS) or Azure Data Studio
Basic understanding of database concepts
(Optional) AdventureWorks sample database for practice
How to Use These Scripts
Method 1: SSMS/Azure Data Studio
-- Open any .sql file in SSMS-- Execute the entire script or selected portions-- Modify parameters as needed for your environment
Method 2: Command Line (sqlcmd)
# Execute a script using sqlcmd
sqlcmd -S your_server -d your_database -i 01-Create.sql
Method 3: Practice Environment Setup
-- 1. Create a practice databaseCREATEDATABASESQLPractice;
-- 2. Switch to the new database
USE SQLPractice;
-- 3. Execute learning scripts in sequence-- Start with 01-Create.sql, then proceed numerically
π― Learning Path
Week 1: Fundamentals
Days 1-2: Database and table creation (01-03)
Days 3-4: Constraints and data integrity (04-07)
Days 5-7: Basic queries and SELECT statements (10-11)
Week 2: Intermediate Skills
Days 8-10: Joins and relationships (12-14)
Days 11-12: Advanced query techniques (15-17)
Days 13-14: Identity and special values (08-09)
Week 3: Advanced Topics
Days 15-17: Stored procedures (18-21)
Days 18-20: Functions (22-23, 26)
Days 21-22: Specialized functions (24-25)
π‘ Practical Examples
Creating a Table with Constraints
-- From 03-Create_table.sql and 07-Check_constraint.sqlCREATETABLEEmployees (
EmployeeID INTPRIMARY KEY IDENTITY(1,1),
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE,
Age INTCHECK (Age >=18),
HireDate DATE DEFAULT GETDATE(),
DepartmentID INTFOREIGN KEYREFERENCES Departments(DepartmentID)
);
Advanced Join Example
-- From 13-AdvancedORintelligentJoin.sqlSELECTe.FirstName,
e.LastName,
d.DepartmentName,
m.FirstNameAS ManagerFirstName,
m.LastNameAS ManagerLastName
FROM Employees e
LEFT JOIN Departments d ONe.DepartmentID=d.DepartmentIDLEFT JOIN Employees m ONe.ManagerID=m.EmployeeIDWHEREe.HireDate>'2020-01-01';
Stored Procedure with Output
-- From 19-Store_procedureWithOutputParameter.sql
CREATE PROCEDURE GetEmployeeCount
@DepartmentID INT,
@EmployeeCount INT OUTPUT
ASBEGINSELECT @EmployeeCount =COUNT(*)
FROM Employees
WHERE DepartmentID = @DepartmentID;
END;
π Repository Statistics
Metric
Value
Details
Total Scripts
26
Comprehensive coverage
Language
100% T-SQL
SQL Server Transact-SQL
Total Commits
27
Active development
Development Period
March 2022
Intensive learning phase
Lines of Code
1,000+ (estimated)
Substantial code base
License
MIT
Open source
π§ Best Practices Demonstrated
1. Code Organization
Clear, descriptive file names
Logical progression from simple to complex
Consistent formatting and commenting
Separation of concerns (DDL, DML, procedures)
2. Performance Considerations
Efficient query writing
Proper indexing strategies
Set-based operations over cursors
Parameterized queries for security
3. Security Practices
SQL injection prevention techniques
Principle of least privilege in permissions
Secure coding patterns
Error handling implementation
π€ How to Contribute
Ways to Contribute
Add More Examples: Additional use cases for each topic
Improve Documentation: Better comments and explanations
Add Performance Tips: Optimization techniques
Include New Features: SQL Server 2019/2022 features
Fix Issues: Bug reports and corrections
Contribution Guidelines
Fork the repository
Create a feature branch (git checkout -b feature/enhancement)
Commit changes (git commit -m 'Add: description of improvement')
Push to branch (git push origin feature/enhancement)
Open a Pull Request
π License
This project is licensed under the MIT License. See the LICENSE file for full details.