SQL Coding Standards Guide
SQL Coding Standards Guide
The SQL coding standards emphasize creating indexes on columns involved in WHERE, JOIN, and ORDER BY clauses to optimize query performance. However, the standards caution against using functions on indexed columns within predicates, as this can negatively impact performance by preventing the use of index seeks .
The standards emphasize granting the least privilege by assigning necessary rights through roles, and they advocate using parameterized queries to prevent SQL injection attacks. Parameterized queries ensure that user inputs are treated as data rather than executable code, effectively mitigating a common attack vector .
Header comments are placed at the top of each script or object to describe its purpose, author, and creation date, providing essential context for understanding the script's role. Inline comments are used sparingly for non-obvious logic to clarify complex parts of the code. These practices enhance code readability and maintainability by making the logic and structure clear to developers .
Consistent naming conventions are crucial to ensure clarity and facilitate ease of understanding the purpose and type of database objects. For instance, tables are named as plural nouns (e.g., 'customers'), columns should represent attributes clearly using formats like 'entity_attribute' (e.g., 'customer_id'), primary keys follow the format 'pk_table' (e.g., 'pk_customers'), and indexes are named 'idx_table_col1_col2' (e.g., 'idx_orders_customer_date').
The guidelines recommend wrapping multi-step DML operations in transactions using BEGIN TRANSACTION and COMMIT to ensure atomicity. This means that either all changes are applied or none, which is crucial for maintaining data consistency and integrity in case of errors or system failures during complex updates .
The document recommends using ALTER scripts for schema changes and tracking them in version control systems. This practice is important because it ensures that changes are systematically documented, reducing the risk of errors, and allows for rolling back changes when needed, which is crucial for maintaining the integrity and consistency of the database .
'Soft deletes' refer to the practice of marking records as deleted by updating a flag (e.g., setting 'is_deleted' to 1) rather than physically deleting the records from the database. This approach allows for data recovery and auditing, avoids data loss, and can help maintain referential integrity and historical records .
Using SELECT * is discouraged because it can lead to fetching unnecessary data, which impacts performance and readability, especially when database schemas change. The standards recommend explicitly listing required columns to provide clarity about what data is being retrieved and to optimize resource usage .
The document prescribes the use of uppercase for SQL keywords, lowercase_snake_case for identifiers, and a 2-space indentation per level, with line breaks after each clause and blank lines between major blocks. These formatting rules help improve readability and maintainability of the code by providing a structured and consistent appearance, facilitating smooth navigation and comprehension of complex SQL scripts .
TRY...CATCH blocks in stored procedures help manage and handle exceptions robustly by providing mechanisms to catch errors and respond with appropriate actions. This supports better error handling and ensures that the application can cope with unexpected conditions without crashing, enabling logging or user notifications .