Showing posts with label reference. Show all posts
Showing posts with label reference. Show all posts

Wednesday, July 6, 2011

Database Primary Key Naming Conventions

Train couplingThis post specifically deals with the two naming conventions of a primary key for example “Id” or “<tablename>Id".

Using the convention including the tablename prepended to “Id” makes creating and reading SQL query joins more intuitively readable as in:

SELECT a.AuthorId
    , a.AuthorName
    , b.BookId
    , b.BookName 
FROM Author a
   INNER JOIN Book b 
   ON b.AuthorId = a.AuthorId

Versus the alternative using a standard key name, such as “Id" for all tables, as in the following:



SELECT a.Id AS AuthorId
    , a.AuthorName
    , b.Id AS BookId
    , b.BookName 
FROM Author a
   INNER JOIN Book b 
   ON b.AuthorId = a.Id

However it adds additional abstraction/mapping complexity when using a generic Repository pattern* that uses a standard primary key property name for the Repository base classes and interfaces.


*The design is possibly more correctly labeled as a DAO pattern depending on the specific implementation.


photo courtesy: amboo who? / CC BY-SA 2.0

Monday, October 26, 2009

Code-behind Number Format Reference

This post is to save me some time looking up the format reference for some of the things I find myself looking up every once in a while.

Formatting numbers for display like minutes and seconds to have the preceding zero when the number is less than ten (10) as you would see on a digital clock or stopwatch display.

example: [3:07:10]

   1: ' format integers to have preceding zero if 
   2: ' integer is less than 10 (as in a digital
   3: ' clock or stopwatch display)
   4: ' tsElapsed is a TimeSpan
   5: ' lblElapsedTime is a WPF Label
   6: lblElapsedTime.Content = _
   7:     String.Format("{0}:{1}:{2}", _
   8:     tsElapsed.Hours, _
   9:     tsElapsed.Minutes.ToString("0#"), _
  10:     tsElapsed.Seconds.ToString("0#"))