SQL Server Primary Key Using Date Range -



SQL Server Primary Key Using Date Range -

i'm writing app allows people send sms messages, , if recognize word sent, something. keywords recognize (handle) alter based on date. example, church work needs lot of easter volunteers, 2 months before easter, want specify 'easter' keyword, , after easter, want disable it.

so primary key needs <keyword, date-range>. set <keyword, date-start, date-end>, want there pk conflict (or @ to the lowest degree sort of constraint conflict) if seek insert new record same keyword , date-start or date-end between row's date-start , date-end.

what's best course of study of action? sql server have capability built in? need create sort of custom type .net? do primary key or secondary check constraint?

i optimizing speed, in truth, app doesn't need it.

unless i'm missing of import believe can accomplish using acheckconstraint paired function:

-- test table create table keyword_ranges (keyword varchar(10), startdate date, enddate date); -- function checks if ranges overlap create function checkkeywordrange (@keyword varchar(10), @startdate date, @enddate date) returns int begin declare @retval int select @retval = count(*) keyword_ranges keyword = @keyword , (@startdate <= enddate) , (@enddate >= startdate) homecoming @retval end; go -- constraint calls function alter table keyword_ranges add together constraint chkkeywordrange check (dbo.checkkeywordrange(keyword, startdate, enddate) = 1); -- insert succeed insert keyword_ranges values ('holiday', '2014-01-01', '2014-01-05') -- insert conflict check , fail insert keyword_ranges values ('holiday', '2014-01-03', '2014-01-07')

sample sql fiddle

sql-server

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -