Rules to Better SQL Databases - Developers

Here are some of the typical things that all SQL Server DBAs and Database developers should know. These rules are above and beyond the most basic textbook recommendations of:

  • Ensuring your databases are Normalized and in 3rd Normal Form
  • Making sure you have primary keys, foreign keys and simple indexes to improve performance
  • Making sure you Back up regularly
  • Basic Naming conventions (see some of our object naming conventions)
  • Minimizing result set sizes and data over the wire

View Database Coding Standard and Guideline.

Want to develop your SQL Server Database with SSW? Check SSW's Databases consulting page.

  1. Imagine an e-commerce company called Northwind Traders, where thousands of products, multiple sellers, and millions of customers converge. In such a company, the importance of a well-designed database cannot be overstated.

    Consider the scenario where a customer places an order only to encounter delays and frustration due to data inconsistencies that wrongly indicate an out-of-stock product. To mitigate such chaos, developers rely on relational database design principles, including normalization and entity-relationship diagrams (ERDs).

    These concepts provide the foundation for a well-structured database with improved data integrity and reduced redundancy, ensuring smooth operations and customer satisfaction.

  2. Database Normalization is a systematic approach to designing databases that reduces redundancy and avoids potential anomalies during data update, deletion, or insertion. This process involves organizing the columns (attributes) and tables (relations) of a database to ensure their dependencies are properly enforced by database integrity constraints.

  3. Relational databases are complicated, and understanding the entire architecture of a database can be difficult when expressed solely in words. That's where Entity Relationship Diagrams (ERDs) come in. ERDs are a way of visualizing the relationships between different entities and their cardinality.

  4. NULLs complicate your life. To avoid having to constantly differentiate between empty strings and NULLs, you should avoid storing NULLS if you can. Why? Well, what is wrong with this?

  5. NULLs create difficulty in the middle-tier because you need to add further handling. So avoid them where you can, eg. For a Discount field, make the default 0 and don't allow NULLs.

  6. Text in character columns (char, varchar, text, nchar, varchar, text) can start with spaces or empty lines which is usually data entry error.

    The best way to avoid this issue is to handle whitespace in the middle-tier before it reaches the database.

  7. When users are deleting a lot of records as part of normal operations - they can and do make mistakes. Instead of the painful process of having to go to a backup to get these records, why not simply flag the records as IsDeleted?

  8. SQL Server dates can range from the year 1900 up to the year 9999. However, certain date data in your database just wouldn't make any sense in the context of your business. For example, if your company started trading in 2015 you should not have any dates in your database before 2015 (unless you are tracking start dates of your clients, but this is an exception). An invoice date of 2013 wouldn't make sense at all.

    There are two methods to avoid this:

  9. Any DateTime fields must be converted to universal time from the application to the stored procedures when storing data into the database.

    We can simplify dealing with datetime conversions by using a date and time API such as Noda TIme.

per page
1 - 10 of 65 items
We open source.Loving SSW Rules? Star us on GitHub. Star