Putting everybody's input together into one list.
Naming Standards
- Schemas are named by functional area (Products, Orders, Shipping)
- No Hungarian Notation: No type names in object names (no strFirstName)
- Do not use registered keywords for object names
- No spaces or any special characters in object names (Alphanumber + Underscore are the only things allowed)
- Name objects in a natural way (FirstName instead of NameFirst)
- Table name should match Primary Key Name and Description field (SalesType – SalesTypeId, SalesTypeDescription)
- Do not prefix with tbl_ or sp_
- Name code by object name (CustomerSearch, CustomerGetBalance)
- CamelCase database object names
- Column names should be singular
- Table names may be plural
- Give business names to all constraints (MustEnterFirstName)
Data Types
- Use same variable type across tables (Zip code – numeric in one table and varchar in another is not a good idea)
- Use nNVarChar for customer information (name, address(es)) etc. you never know when you may go multinational
In code
- Keywords always in UPPERCASE
- Never use implied joins (Comma syntax) - always use explicit INNER JOIN / OUTER JOIN
- One JOIN per line
- One WHERE clause per line
- No loops – replace with set based logic
- Use short forms of table names for aliases rather than A, B, C
- Avoid triggers unless there is no recourse
- Avoid cursors like the plague (read http://www.sqlservercentral.com/articles/T-SQL/66097/)
Documentation
- Create database diagrams
- Create a data dictionary
Normalization and Referential Integrity
- Use single column primary keys as much as possible. Use unique constraints where required.
- Referential integrity will be always enforced
- Avoid ON DELETE CASCADE
- OLTP must be at least 4NF
- Evaluate every one-to-many relationship as a potential many-to-many relationship
- Non user generated Primary Keys
- Build Insert based models instead of update based
- PK to FK must be same name (Employee.EmployeeId is the same field as EmployeeSalary.EmployeeId)
- Except when there is a double join (Person.PersonId joins to PersonRelation.PersonId_Parent and PersonRelation.PersonId_Child)
Maintenance : run periodic scripts to find
- Schema without table
- Orphaned records
- Tables without primary keys
- Tables without indexes
- Non-deterministic UDF
- Backup, Backup, Backup
Be good
- Be Consistent
- Fix errors now
- Read Joe Celko's SQL Programming Style (ISBN 978-0120887972)