📁 SQL Templates - Database
Description
This folder holds standard templates for SQL Server database objects. They follow the naming conventions and standards the ecosystem defines.
Contents
| File | Description |
|---|---|
PLANTILLA_STORED_PROCEDURE.sql | Full template for stored procedures |
PLANTILLA_TABLA.sql | Table template with audit columns |
PLANTILLA_TABLA_CATALOGO.sql | Template for catalogue/lookup tables |
PLANTILLA_VISTA.sql | Standard view template |
PLANTILLA_FUNCION.sql | Function templates (scalar and table-valued) |
PLANTILLA_MIGRACION.sql | Migration script with rollback |
CONVENCION_SCHEMAS.sql | Recommended schema structure |
The file names stay as they are: the skills point at those exact paths.
How to use them
- Copy the template you need
- Rename it after the object you are creating
- Replace the
{...}placeholders with real values - Delete the sections that do not apply
- Review it before running it in production
Naming Conventions
Database objects
| Object | Convention | Example |
|---|---|---|
| Table | PascalCase, singular | Student, ScholarshipApplication |
| Catalogue table | PascalCase, singular | DocumentType, ApplicationStatus |
| Column | PascalCase | BirthDate, DocumentNumber |
| Stored procedure | usp_{Entity}_{Action} | usp_Student_GetById |
| View | vw_{Description} | vw_ActiveStudents |
| Scalar function | fn_{Description} | fn_CalculateAge |
| Table-valued function | fnt_{Description} | fnt_ScholarshipsByStudent |
| Trigger | tr_{Table}_{Event} | tr_Student_AfterInsert |
| Index | IX_{Table}_{Columns} | IX_Student_Email |
| Unique index | UX_{Table}_{Columns} | UX_Student_DocumentNumber |
| Primary key | PK_{Table} | PK_Student |
| Foreign key | FK_{ChildTable}_{ParentTable} | FK_ScholarshipApplication_Student |
| Check | CK_{Table}_{Column} | CK_Student_Age |
| Default | DF_{Table}_{Column} | DF_Student_RegisteredAt |
| Schema | lowercase | academico, financiero |
The verbs and the object names are in English; the schema names are the organization's own and are not translated. Which verbs are allowed is set by
database.naming.allowedVerbs.
Stored procedure prefixes
| Prefix | Used for | Example |
|---|---|---|
usp_{Entity}_Insert | Insert | usp_Student_Insert |
usp_{Entity}_Update | Update | usp_Student_Update |
usp_{Entity}_Delete | Hard delete | usp_Student_Delete |
usp_{Entity}_SoftDelete | Soft delete | usp_Student_SoftDelete |
usp_{Entity}_GetById | Get by ID | usp_Student_GetById |
usp_{Entity}_GetAll | List with paging | usp_Student_GetAll |
usp_{Entity}_Search | Advanced search | usp_Student_Search |
usp_{Entity}_Exists | Check existence | usp_Student_Exists |
usp_Rpt_{Name} | Reports | usp_Rpt_EnrollmentsByDegree |
Audit columns (MANDATORY)
Every table must include these columns:
-- Mandatory
Active BIT NOT NULL DEFAULT 1,
CreatedAt DATETIME2(3) NOT NULL DEFAULT GETUTCDATE(),
CreatedBy NVARCHAR(100) NOT NULL DEFAULT SYSTEM_USER,
ModifiedAt DATETIME2(3) NULL,
ModifiedBy NVARCHAR(100) NULL
Recommended data types
| Used for | Type | Notes |
|---|---|---|
| Identifiers | INT IDENTITY | BIGINT above 2 billion |
| Text | NVARCHAR(n) | Always Unicode |
| Long text | NVARCHAR(MAX) | Do not use TEXT (deprecated) |
| Timestamps | DATETIME2(3) | Do not use DATETIME |
| Date only | DATE | No time part |
| Money | DECIMAL(18,2) | Do not use MONEY |
| Booleans | BIT | 0/1 |
Environments
| Environment | Version |
|---|---|
| Main production | SQL Server 2017+ |
| New applications | SQL Server 2019/2022 |
| Cloud | Azure SQL |
Reference
- Full rule:
.claude/skills/database/SKILL.md - SQL Server documentation: https://docs.microsoft.com/sql
Fuente: ovillo/_patron/05_Plantillas_SQL/README_EN.md