SlideShare uma empresa Scribd logo
1 de 15
The Developer

vs

The Relational Database
About me
MVP:
SQL Server

Gogula
Aryalingam

MCSE:
Business
Intelligence

6
Author:
Chapter 57

Technical Architect

2011
#DevDaySL

@gogula
Agenda
Database Design

Do it right:
Defensive Programming

Indexing considerations

Best Practices
Documenting

#DevDaySL

@gogula

Utility objects
What’s wrong with this picture

#DevDaySL

@gogula
Here’s what…
Keys/Uniqueness Constraints
Normalization

Data Types

Naming Convention

#DevDaySL

@gogula
A better design

#DevDaySL

@gogula
Database design
Code
is
Agile

#DevDaySL

Data
base
is
brittle
@gogula
Programming defensively
1. Define & understand assumptions

2. Modularize into fully testable and fully tested code

3. Test as many use cases as possible
4. Reuse code when feasible*

#DevDaySL

@gogula
Indexing for performance
A clustered index on a heap (table) is a good idea
Unique, narrow, static field

Leave small tables as is

Non-clustered indexes on fields used in
JOINs and WHERE predicates
Do not index every field

Index based on usage

Consider included indexes

Consider filtered indexes

#DevDaySL

@gogula
Use utility objects
The Numbers table, for set based:
String sorting

Extraction of distinct characters

Generate time ranges or time slices

Even something trivial like
Find IDENTITY gaps
generating an IP range

#DevDaySL

@gogula
Document your code
Document
through
code
Document
the Why
apart from
the What

#DevDaySL

@gogula
Do we have time for a demo?

Defensive
database
programming

#DevDaySL

@gogula
Questions?

During
the
break
#DevDaySL

@gogula
THANK YOU

#DevDaySL

@gogula
Contact
MVP:
SQL Server

@gogula

Gogula Aryalingam
Technical Architect

#DevDaySL

dbantics.wordpress.com
www.sqlserveruniverse.com

gogulaa@gmail
.com

@gogula

lk.linkedin.com/i
n/gogula

Mais conteúdo relacionado

Semelhante a The Developer vs. The Relational Database: How to do it right

Which Questions We Should Have
Which Questions We Should HaveWhich Questions We Should Have
Which Questions We Should HaveOracle Korea
 
Building scalable application with sql server
Building scalable application with sql serverBuilding scalable application with sql server
Building scalable application with sql serverChris Adkin
 
Top 10 tips for Oracle performance (Updated April 2015)
Top 10 tips for Oracle performance (Updated April 2015)Top 10 tips for Oracle performance (Updated April 2015)
Top 10 tips for Oracle performance (Updated April 2015)Guy Harrison
 
DBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptxDBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptxHong Ong
 
How to Survive as a Data Architect in a Polyglot Database World
How to Survive as a Data Architect in a Polyglot Database WorldHow to Survive as a Data Architect in a Polyglot Database World
How to Survive as a Data Architect in a Polyglot Database WorldKaren Lopez
 
Use Performance Insights To Enhance MongoDB Performance - (Manosh Malai - Myd...
Use Performance Insights To Enhance MongoDB Performance - (Manosh Malai - Myd...Use Performance Insights To Enhance MongoDB Performance - (Manosh Malai - Myd...
Use Performance Insights To Enhance MongoDB Performance - (Manosh Malai - Myd...Mydbops
 
NoSQLDatabases
NoSQLDatabasesNoSQLDatabases
NoSQLDatabasesAdi Challa
 
Model Driven Architectures
Model Driven ArchitecturesModel Driven Architectures
Model Driven ArchitecturesLalit Kale
 
Agile Data Warehouse Modeling: Introduction to Data Vault Data Modeling
Agile Data Warehouse Modeling: Introduction to Data Vault Data ModelingAgile Data Warehouse Modeling: Introduction to Data Vault Data Modeling
Agile Data Warehouse Modeling: Introduction to Data Vault Data ModelingKent Graziano
 
No sql bigdata and postgresql
No sql bigdata and postgresqlNo sql bigdata and postgresql
No sql bigdata and postgresqlZaid Shabbir
 
Chapter 5 design of keyvalue databses from nosql for mere mortals
Chapter 5 design of keyvalue databses from nosql for mere mortalsChapter 5 design of keyvalue databses from nosql for mere mortals
Chapter 5 design of keyvalue databses from nosql for mere mortalsnehabsairam
 
Taming Large Databases
Taming Large DatabasesTaming Large Databases
Taming Large DatabasesNeo4j
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBeroza Paul
 
Indexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdfIndexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdfMalak Abu Hammad
 
White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...Jeff Jacobs
 
Developer Productivity Engineering with Gradle
Developer Productivity Engineering with GradleDeveloper Productivity Engineering with Gradle
Developer Productivity Engineering with GradleAll Things Open
 
ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons LearnedITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons LearnedJoão Pedro Martins
 

Semelhante a The Developer vs. The Relational Database: How to do it right (20)

Which Questions We Should Have
Which Questions We Should HaveWhich Questions We Should Have
Which Questions We Should Have
 
Building scalable application with sql server
Building scalable application with sql serverBuilding scalable application with sql server
Building scalable application with sql server
 
Top 10 tips for Oracle performance (Updated April 2015)
Top 10 tips for Oracle performance (Updated April 2015)Top 10 tips for Oracle performance (Updated April 2015)
Top 10 tips for Oracle performance (Updated April 2015)
 
DBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptxDBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptx
 
Flexible Design
Flexible DesignFlexible Design
Flexible Design
 
How to Survive as a Data Architect in a Polyglot Database World
How to Survive as a Data Architect in a Polyglot Database WorldHow to Survive as a Data Architect in a Polyglot Database World
How to Survive as a Data Architect in a Polyglot Database World
 
Use Performance Insights To Enhance MongoDB Performance - (Manosh Malai - Myd...
Use Performance Insights To Enhance MongoDB Performance - (Manosh Malai - Myd...Use Performance Insights To Enhance MongoDB Performance - (Manosh Malai - Myd...
Use Performance Insights To Enhance MongoDB Performance - (Manosh Malai - Myd...
 
NoSQLDatabases
NoSQLDatabasesNoSQLDatabases
NoSQLDatabases
 
Model Driven Architectures
Model Driven ArchitecturesModel Driven Architectures
Model Driven Architectures
 
Agile Data Warehouse Modeling: Introduction to Data Vault Data Modeling
Agile Data Warehouse Modeling: Introduction to Data Vault Data ModelingAgile Data Warehouse Modeling: Introduction to Data Vault Data Modeling
Agile Data Warehouse Modeling: Introduction to Data Vault Data Modeling
 
No sql bigdata and postgresql
No sql bigdata and postgresqlNo sql bigdata and postgresql
No sql bigdata and postgresql
 
Chapter 5 design of keyvalue databses from nosql for mere mortals
Chapter 5 design of keyvalue databses from nosql for mere mortalsChapter 5 design of keyvalue databses from nosql for mere mortals
Chapter 5 design of keyvalue databses from nosql for mere mortals
 
Taming Large Databases
Taming Large DatabasesTaming Large Databases
Taming Large Databases
 
Mongo DB
Mongo DBMongo DB
Mongo DB
 
mongo db EMERSON EDUARDO RODRIGUES
mongo db EMERSON EDUARDO RODRIGUESmongo db EMERSON EDUARDO RODRIGUES
mongo db EMERSON EDUARDO RODRIGUES
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design pattern
 
Indexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdfIndexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdf
 
White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...
 
Developer Productivity Engineering with Gradle
Developer Productivity Engineering with GradleDeveloper Productivity Engineering with Gradle
Developer Productivity Engineering with Gradle
 
ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons LearnedITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
 

Último

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 

Último (20)

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 

The Developer vs. The Relational Database: How to do it right

Notas do Editor

  1. The motive for this presentation are developers and the attitude most of them have towards databases. No, this is not a presentation about venting. This is about tips and standards that application and front-end developers need to know when working with databases. This presentation is limited to only relational databases, and limited to content that I could present within 30 minutes.In my career I have seen and worked with databases that have been atrociously designed and programmed on. For instance, a table that contains a field that by all means is supposed to be a primary key. It is also named as such with a PK_ prefix and all, and it contains a lot of records, most of which have NULL for this field. I attribute this to developers who treat databases just as a dump for data. In the subsequent slides, we’ll take a look at certain issues that are common to databases and see how they can be addressed.
  2. The presentation would take a look at a few things that developers could do right, and a few things that you could apply a best practices on.
  3. Take a look at this picture. It depicts a table with data, and it’s schema definition. Try to identify as many things that are wrong here.
  4. Here’s what I could garner from it. Take a closer look a the table definition and the sample set of data from the same table.The lack of constraintsImproperly implemented uniquenessInconsistent naming conventionsNon-Normalized table designCarelessly chosen data typesshow that the table is not well designed.NORMALIZATIONWhen you record data in a database you need to split the data up by entities. You don’t display eggs, cabbages, batteries and disinfectant cans in the same crate as carrots in the supermarket, do you? The same goes for the different entities. Each table is to record details of just one entity (or a single relationship among entities)KEYS and RECORD UNIQUENESSEach table should have a primary key (First Normal Form governs this). A primary key uniquely identifies a record or object.In each scenario or case study or problem domain that you map to a database system, there will be something that uniquely identifies each instance of each entity. This is called the natural key.A natural key is a field that uniquely identifies an instance of an entity (i.e. a record in a table) naturally, such as a Student Code or Customer Code that is used by the school or business to track a student or customer if there was no RDBMS database. In some scenarios it could be the NIC number or Social Security Number.There are rare situations when you cannot have a natural key. In such situations it is alright for you to have only the surrogate key.In all other cases it is strongly recommended that you include a natural key.NAMING CONVENTIONSNaming conventions are very important, regardless of how many developers think otherwise. Using a consistent and standard naming convention automatically documents your database (and objects) to a good level.Here are some tips:Try not to use prefixes (and suffixes) on object names. Rather than “tblCustomer” as a table name; “Customer” would make an ideal name. When all tables are named consistently as this users and developers will automatically know that the object is a table.Try not to use white spaces and underscores in object and column names. A good choice would be to start each word in an object name with a capital letter followed by simple characters: “Customer” , “GetProduct”, “CalculateLateCharges” etc.CONSTRAINTS Constraints are very important too. They help with restricting values that can be stored. This is important such that invalid and wrong data from being stored in the table. A lot of developers make the mistake of letting only the application perform all validations and data restrictions. This of course will not stop data from not being validated when data is stored through another pathway, such as an ad-hoc bulk insert from an Excel file. DATA TYPESData type considerations are important such it helps you with space savings. Saving on 2 bytes from three columns on a table will eventually help you save around 57MB when you go upto 10,000,000 records. How about 10 tables of that nature and so forth… You would be saving a lot. Try to use the optimum data type for each field. A char(10) field for phone number has lesser overhead than varchar(10). A field that stores English language names would save on space if it’s VARCHAR instead of NVARCHAR. Do not forget the 8060 rule when creating tables: (http://msdn.microsoft.com/en-us/library/ms186981(v=sql.105).aspx).
  5. The tblCustomer table can be re-created like this. Normalized and all.Yes, it looks complicated. But as you read through it for 10 minutes you would notice that it is granular and easy to understand. Each entity is in its own structure.Exercise: Try to see if you can come up with an alternative design for the CustomerAddress table or any improvements for it.
  6. The code that you write, say stored procedures, functions and triggers, can be iteratively made better, such as when you follow an agile approach. However, unlike your application code, a database design is harder to implement in an iterative way, especially if the design needs to be broken and rebuilt. Sure, adding to the schema would be a way forward, but breaking and building: nah-ah!Try to make sure that your database design is robust and well thought out when designing (in agile or traditional means) – You don’t want to break the data do you?See what Brent Ozar has to say, when he says “code is agile, and databases are brittle” in http://www.brentozar.com/archive/2013/10/what-developers-need-to-know-about-sql-server/
  7. SQL Code (or any code for that matter) can always break. We just think that they won’t because they pass a few test cases (and they are not even proper test cases, but checks that you run a few times after developing your piece of code). But when that unanticipated parameter is passed or when the code is run on a server with different settings, that’s when your code and break apart.Hence, it is important that your code is written defensively so that these undesirable effects are minimized.When you write code you make assumptions. It is important that these assumptions are explicitly defined and then understood.explicitly list the assumptions that have been madeensure that the these assumptions always holdsystematically remove assumptions that are not essential, or are Incorrect.Make your code modularized as possible, and testable as a unit, and fully testedWhen testing ensure that the code is tested for every type of use case possible, no matter how improbable they are.Reuse the modularized code whenever possible. But make sure chunks out of your code modules are not re-used (since these are not testes as modules and certainly could fail)For more information and to better write resilient T-SQL code, check out this e-book: http://www.red-gate.com/community/books/defensive-database-programming
  8. And finally, documentingDocumenting of code is very important, in that not just others after, you but yourself after 5 months know what the code actually does.More often than not SQL code is written without proper refactoring of the text. This is in part due to the IDEs unlike what you find IDEs like Visual Studio do not provide much support for beautifying the code.Refactored code in itself a first step in documenting your code. For instance one would easily be able to read through a multi-level sub-query call or a loop when it is refactored.Commenting what each piece of code does wonders for the reader in understanding what your code does, and commenting why your code is written would make it drive in the idea of your code much deeper.Sure it takes more time documenting your code, but just like everything else it is worth it in the long run.