Instruction file imported from AIDDbot/ArchetypeAngularSPA (
.github/instructions/database.instructions.md). Copyright stays with the author.
Database Development
Generate script files with SQL statements for database schema definition, data manipulation, and queries.
Store them in a database folder in the project root with *.sql extension.
Those files could be run directly against the database, used in migration tools, called from repositories services, or serve as documentation and templates for developers.
Database schema generation
- all table names should be in plural form
- all column names should be in singular form
- all tables should have a primary key column named
id - all tables should have a column named
created_atto store the creation timestamp - all tables should have a column named
updated_atto store the last update timestamp
Database schema design
- all tables should have a primary key constraint
- all foreign key constraints should have a name
- all foreign key constraints should be defined inline
- all foreign key constraints should have
ON DELETE CASCADEoption - all foreign key constraints should have
ON UPDATE CASCADEoption - all foreign key constraints should reference the primary key of the parent table
SQL Coding Style
- use uppercase for SQL keywords (SELECT, FROM, WHERE)
- use consistent indentation for nested queries and conditions
- include comments to explain complex logic
- break long queries into multiple lines for readability
- organize clauses consistently (SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY)
SQL Query Structure
- use explicit column names in SELECT statements instead of SELECT *
- qualify column names with table name or alias when using multiple tables
- limit the use of subqueries when joins can be used instead
- include LIMIT/TOP clauses to restrict result sets
- use appropriate indexing for frequently queried columns
- avoid using functions on indexed columns in WHERE clauses
- avoid using stored procedures
SQL Security Best Practices
- parameterize all queries to prevent SQL injection
- use prepared statements when executing dynamic SQL
- avoid embedding credentials in SQL scripts
- implement proper error handling without exposing system details
- avoid using dynamic SQL within stored procedures
Transaction Management
- explicitly begin and commit transactions
- use appropriate isolation levels based on requirements
- avoid long-running transactions that lock tables
- use batch processing for large data operations
- include SET NOCOUNT ON for stored procedures that modify data