如何在datetime的日期部分创建唯一约束? [英] How to create a unique constraint just on the date part of a datetime?

查看:256
本文介绍了如何在datetime的日期部分创建唯一约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自己使用一个非常简单的博客引擎(因为我遇到的每个博客引擎太复杂)。我希望能够通过其URL来唯一地标识每个帖子,这些URL就像 / 2009/03/05 / my-blog-post-slug 。要在数据层中完成,我想在(Date,Slug)上创建一个复合的唯一约束,其中 Date 只是组合日期的日期部分(忽略时间)。我自己有几个想法(像另一个列,可能是计算,只能保留日期部分),但是我来到了解,以了解解决这个问题的最佳做法是什么。



我怀疑SQL Server版本在这里是重要的,但是对于记录,我在2008 Express(我感谢更便携的解决方案)。



表架构:

 创建表条目(
标识符int不为空标识,
CompositionDate datetime not null默认值为getdate(),
Slug varchar(128)not null default'',
标题nvarchar(max)not null default'',
ShortBody nvarchar(max)not null default'',
Body nvarchar (max)not null default'',
FeedbackState tinyint not null默认值0,
约束pk_Entries主键(标识符),

约束uk_Entries unique(Date,Slug)问题题目



选择解决方案:



我瘦了k marc的解决方案更合适,考虑到这个问题是关于2008年。但是,我将使用整数方法(但不是使用 INSERT s,因为它不能确保数据完整性;我会使用一个预先计算的整数列),因为我认为使用客户端的整数(在查询中)更容易。



谢谢你们。

 创建表条目(
标识符int不为空标识,
CompositionDate smalldatetime不为null默认getdate(),
CompositionDateStamp as cast(year(CompositionDate)* 10000 + month(CompositionDate)* 100 + day(CompositionDate)as int)persisted,
Slug varchar(128)not null default'',
Title nvarchar(max)not null default'',
ShortBody nvarchar(max)not null default'',
Body nvarchar(max)not null default'',
FeedbackState tinyint not null default 0 ,
约束pk_Entries主键(标识符),
约束uk_Entries唯一(CompositionDateStamp,Slug)

go


解决方案

嗯,在SQL Server 2008中,有一个新的数据类型叫做 DATE - 您可以使用该列并创建一个索引。



您当然可以将一个类型为DATE的计算列添加到表中,只需填写DATETIME列的日期部分到该计算列中,使其成为PERSISTED,并对其进行索引。应该工作很好!



这样的东西:

  ALTER TABLE dbo.Entries 
添加日期作为CAST(CompositionDate AS DATE)PERSISTED

CREATE UNIQUE INDEX UX_Entries ON条目(DateOnly,Slug)

Marc


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 is something like /2009/03/05/my-blog-post-slug. To accomplish it in the data tier, I want to create a compound unique constraint on (Date, Slug) where Date is only the date part (ignoring the time of day) of the composition date. I have a few ideas myself (like another column, probably calculated, to hold only the date part) but I came to SO to know what's the best practice to solve this problem.

I doubt SQL Server version matters here, but for the records, I'm on 2008 Express (I appreciate a more portable solution).

Table schema:

create table Entries (
    Identifier int not null identity,
    CompositionDate datetime not null default getdate(),
    Slug varchar(128) not null default '',
    Title nvarchar(max) not null default '',
    ShortBody nvarchar(max) not null default '',
    Body nvarchar(max) not null default '',
    FeedbackState tinyint not null default 0,
    constraint pk_Entries primary key(Identifier),

    constraint uk_Entries unique (Date, Slug) -- the subject of the question
)

Selected Solution:

I think marc's solution is more appropriate, considering this question is about 2008. However, I'll go with the integer method (but not with INSERTs, as it does not ensure the integrity of data; I'll use a precomputed integer column) since I think it's easier to work with the integer thing from the client (in the query).

Thank you guys.

create table Entries (
    Identifier int not null identity,
    CompositionDate smalldatetime not null default getdate(),
    CompositionDateStamp as cast(year(CompositionDate) * 10000 + month(CompositionDate) * 100 + day(CompositionDate) as int) persisted,
    Slug varchar(128) not null default '',
    Title nvarchar(max) not null default '',
    ShortBody nvarchar(max) not null default '',
    Body nvarchar(max) not null default '',
    FeedbackState tinyint not null default 0,
    constraint pk_Entries primary key(Identifier),
    constraint uk_Entries unique (CompositionDateStamp, Slug)
)
go

解决方案

Well, in SQL Server 2008, there's a new datatype called "DATE" - you could use that column and create an index on that.

You could of course also add a computed column of type "DATE" to your table and just fill the date portion of the DATETIME column into that computed column, make it PERSISTED, and index it. Should work just fine!

Something like that:

ALTER TABLE dbo.Entries
   ADD DateOnly as CAST(CompositionDate AS DATE) PERSISTED

CREATE UNIQUE INDEX UX_Entries ON Entries(DateOnly, Slug)

Marc

这篇关于如何在datetime的日期部分创建唯一约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆