0% found this document useful (0 votes)
7 views4 pages

SQL Coding Standards

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

SQL Coding Standards

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Coding Standards

Introduction
This document outlines the SQL coding standards to ensure consistency, readability,
maintainability, security, and performance across all database code. Follow these guidelines
when creating, modifying, and reviewing SQL scripts.

1. Naming Conventions
Consistent naming makes it easy to identify object types and their purposes. Use snake_case
(lowercase with underscores) throughout.

1.1 Tables
• Tables should be named as plural nouns.
Example:
CREATE TABLE customers (...);
CREATE TABLE sales_orders (...);

1.2 Columns
• Columns should clearly represent the attribute they store. Use {entity}_{attribute} or a
clear noun.
Examples:
customer_id -- foreign key to customers
order_date -- date of the order
total_amount -- numeric value of order total

1.3 Keys & Constraints


• Primary Key: pk_{table} (e.g., pk_customers)
• Foreign Key: fk_{child}_{parent} (e.g., fk_orders_customers)
• Unique: uq_{table}_{column} (e.g., uq_users_email)
• Check: chk_{table}_{condition} (e.g., chk_orders_total_positive)

1.4 Indexes
• Indexes should speed up queries on filtered or joined columns.
Pattern: idx_{table}_{col1}_{col2}
Example:
CREATE INDEX idx_orders_customer_date
ON sales_orders(customer_id, order_date);

1.5 Views, Procedures, Functions, Triggers


• Views: vw_{subject} (e.g., vw_monthly_revenue)
• Stored Procedures: sp_{action}_{entity} (e.g., sp_create_order)
• Functions: fn_{action}_{entity} (e.g., fn_calc_tax)
• Triggers: tr_{table}_{timing}_{event} (e.g., tr_orders_after_insert)

2. Formatting & Style


• Keywords: UPPERCASE
• Identifiers: lowercase_snake_case
• Indentation: 2 spaces per level
• Line breaks: one clause per line, blank line between major blocks

Example:

WITH recent_orders AS (
SELECT order_id, customer_id
FROM sales_orders
WHERE order_date >= '2025-07-01'
)

SELECT
r.order_id,
c.first_name || ' ' || c.last_name AS customer_name
FROM recent_orders r
JOIN customers c
ON r.customer_id = c.customer_id
WHERE
c.status = 'ACTIVE'
ORDER BY
r.order_id DESC;

3. Commenting
• Header Comments: At top of each script or object
Example:
/*
2025-07-17 vw_monthly_revenue
Author: Infer
Purpose: Total revenue per month.
*/
• Inline Comments: Only for non-obvious logic
Example:
-- Exclude internal test orders
WHERE order_type <> 'TEST'
4. Query Guidelines
• Avoid SELECT *; always list needed columns.
Example:
SELECT customer_id, first_name, last_name
FROM customers;

• Use ANSI JOIN syntax; put join conditions in ON, filters in WHERE.
Example:
SELECT o.order_id, c.customer_name
FROM sales_orders o
INNER JOIN customers c
ON o.customer_id = c.customer_id
WHERE o.order_date >= '2025-01-01';

• Aggregations: include non-aggregates in GROUP BY.


Example:
SELECT customer_id, COUNT(*) AS order_count, SUM(amount) AS total_spent
FROM sales_orders
GROUP BY customer_id;

5. DML Standards
• INSERT: list columns, use OUTPUT for IDs.
Example:
INSERT INTO customers (first_name, last_name, status)
OUTPUT INSERTED.customer_id
VALUES ('Alice', 'Smith', 'A');

• UPDATE: always include WHERE.


Example:
UPDATE sales_orders
SET status = 'COMPLETE'
WHERE order_date < '2024-01-01';

• DELETE: prefer soft deletes.


Example:
UPDATE customers
SET is_deleted = 1
WHERE customer_id = @id;

• Transactions: wrap multi-step DML.


Example:
BEGIN TRANSACTION;
-- multiple updates
COMMIT;
6. DDL Standards
• CREATE TABLE: explicit types, nullability, defaults, PK inline.
Example:
CREATE TABLE customers (
customer_id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
status CHAR(1) NOT NULL DEFAULT 'A'
);

• Migrations: use ALTER scripts, track in version control.

7. Procedures & Functions


• Naming: sp_… for procedures, fn_… for functions
• Structure: SET NOCOUNT ON; TRY…CATCH
Example:
CREATE PROCEDURE sp_get_orders
@customer_id INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
SELECT order_id, order_date, total_amount
FROM sales_orders
WHERE customer_id = @customer_id;
END TRY
BEGIN CATCH
THROW;
END CATCH;
END;

8. Performance & Security


• Indexing: on columns used in WHERE, JOIN, ORDER BY
• Avoid functions on indexed columns in predicates
• Least Privilege: grant only needed rights via roles
Example:
GRANT SELECT ON dbo.customers TO reporting_role;
• SQL Injection: always use parameterized queries

You might also like