SQL

View a Query Plan Without Running the Query

Viewing execution plans is useful for troubleshooting queries and stored procedures. But sometimes the query or stored procedure takes a long time to run, or you cannot run it yourself because it modifies data.

You can view these plans by pulling them from the plan cache. SQL Server provides a way to do this:

UPDATE 7/18/2016 – When I first wrote this, I thought that this would show the actual execution plans. I have since learned that this shows the estimated execution plans. Hopefully some readers may still find it useful.
(more…)

First Normal Form (1NF)

Definition

Each row contains one value, of the appropriate type, for each column, and there are no duplicate rows.

This is a simple rewording of the formal definition:

Let relation r have attributes A1, …, An, of types T1, …, Tn, respectively. Then r is in first normal form (1NF) if and only if, for all tuples t appearing in r, the value of attribute Ai in t is of type Ti (i= 1, …, n).”
Date, C.J. Database Design & Relational Theory. Sebastopol: O,Reilly Media, Inc., 2012. Print. (Emphasis theirs)

(more…)

How to Prevent Overlapping Date Ranges in a Table

Have you ever seen a table where each row had a ‘start date’ and an ‘end date’? I’m sure most of us have seen one or created one.

These tables tend to pose a key problem (no pun intended) if the following business logic applies: “If two or more rows represent different date ranges for a single item, the ranges cannot overlap.” In other words, the date range must be unique from the start date until the end date for any particular item represented by the table. (more…)