Three Rules for Database Work

Some developers love working with relational databases, and other developers can't stand to touch them. Either way - if your application uses a database, you have to treat the database with some respect. The database is as much a part of an application as the code and the models inside the software.

Here are three rules I've learned to live by over the years of working with relational databases.

1. Never use a shared database server for development work.

Fossil!The convenience of a shared database is tempting. All developers point their workstations to a single database server where they can test and make schema changes. The shared server functions as an authoritative source for the database schema, and schema changes appear immediately to all team members. The shared database also serves as a central repository for test data.

Like many conveniences in software development, a shared database is a tar pit waiting to fossilize a project. Developers overwrite each other's changes. The changes I make on the server break the code on your development machine. Remote development is slow and difficult.

Avoid using a shared database at all costs, as they ultimately waste time and help produce bugs.

2. Always Have a Single, Authoritative Source For Your Schema

Ideally, this single source will be your source control repository (see rule #3). Consider the following conversation:

Developer 1: It's time to push the app into testing. Do we copy the database from Jack's machine, or Jill's machine?

Developer 2: Ummmmmmmm, I don't remember which one is up to date.

Developer 1: We're screwed.

Everyone should know where the official schema resides, and have a frictionless experience in getting a fresh database setup. I should be able to walk up to a computer, get the latest from source control, build, and run a simple tool to setup the database (in many scenarios, the build process can even setup a database if none exists, so the process is one step shorter).

How you put your database into source control depends on your situation and preferences. Any decent O/R mapping tool should be able to create a database given the mappings you've defined in a project. You can also script out the database as a set of one or more files full of SQL DDL commands. I generally prefer to keep database views and programmatic features (including functions, triggers, and stored procedures) as separate files - but more on this in a later post.

There are plenty of tools to help. Leon Bambrick has a long list (albeit one year old list) of tools and articles that can help, while Jeff Atwood gushes over the virtues of Visual Studio for Database Professionals.

3. Always Version Your Database

There are many ways to version databases, but the common goal is to propagate changes from development, to test, and ultimately to production in a controlled and consistent manner. A second goal is to have the ability to recreate a database at any point in time. This second goal is particularly important if you are shipping software to clients. If someone finds a bug in build 20070612.1 of your application, you must be able to recreate the application as it appeared in that build - database and all.

In a future post, I'll describe an approach I've used for database versioning that has worked well for many years of commercial development.

In the meantime, if you are looking for more database rule, then Adam Cogan and SSW maintain an excellent list.

posted on Wednesday, January 30, 2008 9:15 PM by scott

Comments

Wednesday, January 30, 2008 9:12 PM by database

# Three Rules for Database Work

Bookmarked your post over at Blog Bookmarker.com!
Wednesday, January 30, 2008 9:25 PM by Christopher Steen

# Link Listing - January 30, 2008

Link Listing - January 30, 2008
Wednesday, January 30, 2008 9:25 PM by Christopher Steen

# Link Listing - January 30, 2008

ASP.NET Troubleshooting Expired ASP.NET Session State and Your Options [Via: Scott Hanselman ] Code...
Thursday, January 31, 2008 5:25 AM by rams

# re: Three Rules for Database Work

Thank you for reinforcing my beliefs.
Thursday, January 31, 2008 6:52 AM by Steve Campbell

# re: Three Rules for Database Work

Excellent observation! On my current project, we have a 10 year old application database that has evolved at an average rate of a couple of changes *per day* over the last year. I apply the same basic rules, and they have served me well.
Thursday, January 31, 2008 7:06 AM by Jason Haley

# Interesting Finds: January 31, 2008

Thursday, January 31, 2008 10:45 AM by Ian Hughes

# re: Three Rules for Database Work

I really look forward to your future post on database versioning. It seems like it should be no-brainer since all of your code is in an SCM system of some type or another, but I have not yet worked on a project with the db in source control.
Thursday, January 31, 2008 9:15 PM by K. Scott Allen

# Versioning Databases – The Baseline

Continuing from the last post (Three Rules for Database Work), I wanted to drill into some database versioning...
Thursday, January 31, 2008 9:17 PM by BusinessRx Reading List

# Versioning Databases – The Baseline

Continuing from the last post ( Three Rules for Database Work ), I wanted to drill into some database
Friday, February 01, 2008 9:22 PM by K. Scott Allen

# Versioning Databases – Change Scripts

After considering the three rules and creating a baseline, an entire team can work with a database whose...
Friday, February 01, 2008 9:32 PM by BusinessRx Reading List

# Versioning Databases – Change Scripts

After considering the three rules and creating a baseline , an entire team can work with a database whose
Saturday, February 02, 2008 2:34 AM by Sergei

# re: Three Rules for Database Work

Heh. In my last job (I left and am between jobs now) we implemented 2 & 3 but admittedly used production database for developer workstation.

The app we write is the one we sell and also the one our company uses, so if anything broke (it was very rare, I can hardly remember last occurrence) we saw it immediately in error logs.
I admit it's sortof irresponsible but it made you be Really Thoughtful and Careful when you were making schema changes... worked every time :)
Sunday, February 03, 2008 7:40 PM by K. Scott Allen

# Versioning Databases – Branching and Merging

Previous entries:
Three rules for Database Work
The Baseline
Change Scripts
Views, Stored Procedures...
Monday, February 04, 2008 6:04 AM by purrl.net |** urls that purr **|

# This is one of the web's most interesting stories on Mon 4th Feb 2008

These are the web's most talked about URLs on Mon 4th Feb 2008. The current winner is ..
Monday, February 04, 2008 10:12 AM by Community Blogs

# Versioning Databases – Branching and Merging

Previous entries: Three rules for Database Work The Baseline Change Scripts Views, Stored Procedures
Monday, February 04, 2008 10:12 AM by Community Blogs

# Versioning Databases – Change Scripts

After considering the three rules and creating a baseline , an entire team can work with a database whose
Monday, February 04, 2008 10:12 AM by Community Blogs

# Versioning Databases – The Baseline

Continuing from the last post ( Three Rules for Database Work ), I wanted to drill into some database
Monday, February 04, 2008 11:16 AM by Sava Chankov

# re: Three Rules for Database Work

These issues are pretty well addressed in Ruby on Rails.
Tuesday, February 05, 2008 5:09 AM by Nitin Badole

# Version Control for Database

We version our .net project assemblies, code files (in vss). Also we come across the various versions
Tuesday, February 05, 2008 7:10 AM by Tod1d's Thought Process

# Article of the Week: Three Rules for Database Work

Article: Three Rules for Database Work Author: K. Scott Allen Description: Short article on why database
Wednesday, February 06, 2008 10:35 AM by Scott

# re: Three Rules for Database Work

Please, please, please... hurry and elaborate on the tools you've used for #3. The gaps here are enormous - we have beautiful tools that help us do this in most coding environments but in database environments we are left wanting and are often stuck with an arcane, time-intesive, human process that leaves much to be desired and offers very little by way of consistancy or reproduceability. Its not just a question of code migration but structure and data migration as well.

I'm weary of half-solutions and manual processes.
Thursday, February 07, 2008 12:44 PM by Jeff Handley

# SQL Profiler - Being self-centered is a good thing

SQL Profiler - Being self-centered is a good thing
Tuesday, February 12, 2008 3:15 AM by Alex

# re: Three Rules for Database Work

We have been using shared sql server for a long time and didn't meet with problems you described. Just if there are errors a developer needs to get the latest code and that's all.
Also the rule 1 leads to other problems which perfectly described in a dialog in Rule 2.
Wednesday, February 13, 2008 8:57 AM by LavaBlast Software Blog

# I, for one, welcome our new revision control overlords!

I, for one, welcome our new revision control overlords!
Saturday, February 16, 2008 1:35 PM by Darryl L. Pierce

# re: Three Rules for Database Work

Excellent points. It's nice to see someone putting to words what ought to be a common practice. At my work, before we start a sprint, I take a snapshot (via mysqldump) of the schema as it stands before any changes are made. That gets checked into the development branch and is named "create_rhxuser_schema.sql" after our primary schema. Each branch has its own version of that file so we can always revert. Then, as changs are made, we capture those in another script, "update_rhxuser_schema.sql". So the convention has been to run the create script and then the update script at any point to verify updates before we go to production.
Monday, February 18, 2008 8:13 AM by Ron Piterman

# re: Three Rules for Database Work

for java developers there is liquibase, am wondering why its not already mentioned everyhwere, seems very promissing.
Tuesday, February 26, 2008 7:17 AM by asdling

# 数据库的持续集成和版本控制[转自INFOQ]

Allen在发布了关系型数据库开发的三个原则的经验总结文章之后,就开始写后续的系列文章了。这三个原则如下:一、不要在共享数据库服务器上进行开发工作就像软件开发中其它所谓便捷的方法一样,共享数据库的...
Tuesday, April 15, 2008 10:53 AM by .Net World

# Versioning Databases – Branching and Merging

Previous entries: Three rules for Database Work The Baseline Change Scripts Views, Stored Procedures
Tuesday, April 15, 2008 10:56 AM by .Net World

# Versioning Databases – Change Scripts

After considering the three rules and creating a baseline , an entire team can work with a database whose