Datetime as Primary Key?

  • Every so often, I see Datetime data type columns being used as part of a clustered Primary Key. What are the advantages and disadvantages of this?

    I personally try to avoid using datetime as part of any primary key, and only use INT columns whenever possible. Occasionally a varchar() column, but rarely even that.

    Any comments on this would be greatly appreciated.

  • We use datetime when the majority of the queries run against the table will be based on a range of dates. It is the clustered index for that table and 90% of the queries are efficient.

    Patrick

    Quand on parle du loup, on en voit la queue

  • I can see it as part of a PK if you do a lot of time range queries, but definetely not as the only column. The risk of rejected insert because of duplicate keys seems to high. Don't forget that the datetime value is only accurate up to 3/1000 of a second.

    [font="Verdana"]Markus Bohse[/font]

  • Markus -

    You are right - in a heavily transacted database this would not be a good idea.

    We discussed this in design mode and the table is not in that category.

    We are also using smalldatetime which is accurate to the minute.

    Patrick

    Quand on parle du loup, on en voit la queue

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply