Thursday, July 11, 2013

What is in a Database Field Name?

I dislike Database design that use the table name as a prefix for all fields in the table. Example:CREATE TABLE MyTable(MyTableID int Primary Key,MyTableModified DATE,MyTableDescription Varchar(500),MyTableCreatedBy int...)

As opposed to:CREATE TABLE MyTable(Id int PrimaryKeyModified DATE,Description Varchar(500),CreatedBy int...)

FKs are fine but I would recommend being creative with Names. CreatedBy is a FK to the User table. But notice I didn't call it UserId. Why? What happens when you start tracking UpdatedBy?

Well what about recognizing tables in a query with say 10+ joins. Use great names for Aliases instead. You are using aliases in this case? :) I have seen people error on one of two sides. SQL Server 2008 + or newer people tend to lean towards verbose or even avoiding aliases due to intellisense. That's if they haven't done something to break SQL Server Management Studio intellisense. Or people tend to abbreviate table names. Which is fine if you are consistent with Alias names. The great thing about leaning on Aliasing heavier is it is less permanent than having to rename every field because you decide a table needs to renamed. Most of time you will not rename fields or tables. That is why naming fields and tables well is such a big deal.

1 comment:

  1. I've heard Eric Rohlfs refer to this as "Smurf" notation as in Papa Smurf, Brainy Smurf, etc. Whenever I see this, I just start whistling the smurf song.

    ReplyDelete