I need to explain this by example:
Is there a best practice or preference for specifying a DateTime and BIT in a database table?
In my database I have a Widget table. I need to know if a widget is "Closed" and it's "Closed Date" Business rules say that if a widget is closed, it must have a closed date. If a widget is not closed, it should not have a "Closed Date".
To design this, I could do the following:
(Example 1):
CREATE TABLE [Widget]
(
[WidgetID] INT IDENTITY(1,1)
,[ClosedDate] DATETIME NULL
)
or (Example 2):
CREATE TABLE [Widget]
(
[WidgetID] INT IDENTITY(1,1)
,[IsClosed] BIT NOT NULL CONSTRAINT [DF_Widget_IsClosed] DEFAULT (0)
,[ClosedDate] DATETIME NULL
)
I think that Example 1 is cleaner because it is one less column to have to worry about. But, whenever I need to evaluate whether a Widget is Closed, I would need an extra step to figure out if the ClosedDate column IS NOT NULL.
Example 2 creates extra overhead because now I have to keep both the IsClosed and ClosedDate values in sync.
Is there a best practice when designing something like this? Would querying the table be more performant for Example 2? Is there any reason why I should choose one design over the other?
Note: I would be accessing this value through an ORM tool as well as Stored Procedures.