1---2name: sql-development3description: T-SQL, stored procedures, and MS SQL Server DBA practices. Use when writing SQL queries, designing schemas, tuning SQL Server performance, managing backups, configuring security, or using SQL Server 2025+ features.4license: Complete terms in LICENSE.txt5---67# SQL Development89Comprehensive SQL development guidelines combining SQL coding standards, stored procedure generation, and MS SQL Server DBA best practices.1011## Skill Paths1213- Workspace skills: `.github/skills/`14- Global skills: `C:/Users/LOQ/.agents/skills/`1516## Activation Conditions1718- Writing SQL queries and stored procedures19- Designing database schemas and table structures20- Working with MS SQL Server as a DBA21- Performance tuning and query optimization22- Database backup, restore, and security configuration23- SQL Server 2025+ feature adoption and migration2425---2627## Part 1: Database Schema Design2829### Table Naming30- All table names in singular form31- All column names in singular form3233### Required Columns34- All tables must have a primary key column named `id`35- All tables must have `created_at` for creation timestamp36- All tables must have `updated_at` for last update timestamp3738### Constraints39- All tables must have a primary key constraint40- All foreign key constraints must have a name41- All foreign key constraints defined inline42- All foreign keys must have `ON DELETE CASCADE`43- All foreign keys must have `ON UPDATE CASCADE`44- All foreign keys must reference the primary key of the parent table4546---4748## Part 2: SQL Coding Style4950### Formatting51- Uppercase for SQL keywords (`SELECT`, `FROM`, `WHERE`)52- Consistent indentation for nested queries53- Comments to explain complex logic54- Break long queries into multiple lines55- Organize clauses: `SELECT`, `FROM`, `JOIN`, `WHERE`, `GROUP BY`, `HAVING`, `ORDER BY`5657### Query Structure58- Use explicit column names, never `SELECT *`59- Qualify column names with table alias when using multiple tables60- Prefer JOINs over subqueries when possible61- Include `LIMIT`/`TOP` clauses to restrict result sets62- Use appropriate indexing for frequently queried columns63- Avoid functions on indexed columns in `WHERE` clauses6465---6667## Part 3: Stored Procedure Standards6869### Naming Conventions70- Prefix with `usp_`71- Use PascalCase: `usp_GetCustomerOrders`72- Include plural noun for multiple records: `usp_GetProducts`73- Include singular noun for single record: `usp_GetProduct`7475### Parameter Handling76- Prefix parameters with `@`77- Use camelCase: `@customerId`78- Provide default values for optional parameters79- Validate parameter values before use80- Document parameters with comments81- Required parameters first, optional later8283### Structure84- Include header comment block with description, parameters, return values85- Return standardized error codes/messages86- Return result sets with consistent column order87- Use `OUTPUT` parameters for returning status information88- Prefix temporary tables with `tmp_`89- Include `SET NOCOUNT ON` for data-modifying procedures9091---9293## Part 4: Security Best Practices9495### Query Security96- Parameterize all queries to prevent SQL injection97- Use prepared statements for dynamic SQL98- Avoid embedding credentials in SQL scripts99- Proper error handling without exposing system details100- Avoid dynamic SQL in stored procedures101102### Transaction Management103- Explicitly begin and commit transactions104- Use appropriate isolation levels105- Avoid long-running transactions that lock tables106- Use batch processing for large data operations107108---109110## Part 5: MS SQL Server DBA111112### Tooling113- Install and enable `ms-mssql.mssql` VS Code extension for full database management114- Use official Microsoft documentation for reference and troubleshooting115116### DBA Responsibilities117- Database creation and configuration118- Backup and restore strategies119- Performance tuning and index optimization120- Security management and auditing121- Upgrades and compatibility planning (SQL Server 2025+)122123### Best Practices124- Focus on tool-based database inspection over codebase analysis125- Highlight deprecated/discontinued features in SQL Server 2025+126- Encourage secure, auditable, performance-oriented solutions127- Reference official docs for troubleshooting128- Warn about deprecated features and suggest alternatives129130---131132## Troubleshooting133134| Issue | Solution |135|-------|----------|136| Slow queries | Check execution plan, add indexes, optimize JOINs |137| Deadlocks | Reduce transaction scope, consistent lock ordering |138| Missing data | Verify CASCADE rules, check transaction isolation |139| Permission errors | Review GRANT/REVOKE statements, check role membership |140| Connection issues | Verify firewall rules, connection strings, SQL auth settings |141142---143144## References & Resources145146### Documentation147- [T-SQL Patterns](./references/tsql-patterns.md) — MERGE, CTEs, PIVOT, JSON operations, window functions, and error handling148- [Performance Tuning](./references/performance-tuning.md) — Execution plans, index tuning, Query Store, and anti-patterns149150### Scripts151- [Stored Procedure Template](./scripts/stored-proc-template.sql) — Production-ready SP template with TRY/CATCH, pagination, and dynamic sorting152153### Examples154- [Schema Design Example](./examples/schema-design-example.md) — Recipe Management System with 10 tables, stored procedures, and migrations155156---157158## Related Skills159160| Skill | Relationship |161|-------|-------------|162| [nestjs](../nestjs/SKILL.md) | TypeORM integration with NestJS |163| [php-development](../php-development/SKILL.md) | PDO/MySQL database access from PHP |164| [mongodb-mongoose](../mongodb-mongoose/SKILL.md) | Alternative NoSQL database approach |165| [powerbi-modeling](../powerbi-modeling/SKILL.md) | SQL sources for Power BI semantic models |