如何为删除时间的日期设置约束 [英] How Do I Set A Constraint For A Date Filed Removing Time

查看:72
本文介绍了如何为删除时间的日期设置约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子



这样的设计



date token_no

09/02/2015 12:50:00 PM 1

09/02/2015 12:51:00 PM 2



i希望通过结合日期和令牌来创建一个独特的约束。



即一个令牌没有约会



如何创建约束

i have a table

desigin like this

date token_no
09/02/2015 12:50:00 PM 1
09/02/2015 12:51:00 PM 2

i want to create a unique constraints by combaining the date and token no

ie one token no for date

how can create constraints

推荐答案

好吧,在SQL Server 2008中,有一个名为DATE的新数据类型 - 您可以使用该列并在其上创建索引。



您当然也可以在表中添加DATE类型的计算列,只需将DATETIME列的日期部分填入该计算列, PERSISTED,并将其编入索引。应该工作得很好!



类似的东西:



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)





参考页

http://stackoverflow.com/questions/620561/how-to-create-a-unique-constraint-just-on-the-date-part-of-a-datetime [ ^ ]


这篇关于如何为删除时间的日期设置约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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