How to create a unique constraint just on the date part of a datetime?

前端 未结 4 1990
夕颜
夕颜 2021-01-02 06:13

I\'m writing a very simple blog engine for own use (since every blog engine I encountered is too complex). I want to be able to uniquely identify each post by its URL which

4条回答
  •  死守一世寂寞
    2021-01-02 06:51

    Over the years we've had a variety of problems with Computed Columns in SQL Server, so we've stopped using them.

    You could use a VIEW with a column for date-only - and put a unique index on that VIEW's column.

    (A possibly useful side-effect is that you can have the VIEW exclude some rows - so you could implement things like "DateColumn must be unique, but exclude WHERE DateColumn IS NULL)

    The existing CompositionDate column could be split into two fields - CompositionDate and CompositionTime - and a Retrieve View that joins them back together if you need that - which would then allow a native index on the Date-only column

    (This can be implemented in SQL 2005 and earlier using DateTime - although slightly extravagant for just the Date or Time, and not both)

    And lastly you could have an INSERT / UPDATE trigger that enforced that no other record existed with a duplicate CompositionDate (date part only)

提交回复
热议问题