Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Complicated Constraint - Possible or not?

 
   Database Help (Home) -> Programming RSS
Next:  downloading a zip archive from another server  
Author Message
Mat Bailie

External


Since: Oct 02, 2008
Posts: 1



(Msg. 1) Posted: Thu Oct 02, 2008 5:38 am
Post subject: Complicated Constraint - Possible or not?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Imagine the following overly simple table...

CREATE TABLE entity_valid_dates (
id INT IDENTITY(1,1) NOT NULL,
enity_id INT NOT NULL,
valid_from SMALLDATETIME NOT NULL,
valid_until SMALLDATETIME NOT NULL
)

(Entity_id is a foreign key to a table not mentioned here)

This holds dates between which a particulate entity "is valid". (When to include this entities data, and implicitly, when not to.)

What I want to do is ensure that "overlapping" dates can't be entered.


Valid Table Contents:
1, 1, '2008 Sep 01', '2008 Sep 10'
2, 1, '2008 Sep 20', '2008 Sep 28'
3, 2, '2008 Sep 06', '2008 Sep 24'
4, 2, '2008 Sep 26', '2008 Sep 28'

(No dates for the same "entity_id" overlap)



Invalid Talbe Contents:
1, 1, '2008 Sep 01', '2008 Sep 10'
2, 1, '2008 Sep 20', '2008 Sep 28'
3, 2, '2008 Sep 06', '2008 Sep 26'
4, 2, '2008 Sep 24', '2008 Sep 28'

(For entity two, the two dates overlap on the 24th and 25th, assuming the end_date is 'exlusive' rather than 'inclusive')



As far as I can tell there is no constraint that can do this. (Check the inserted/updated start/end date against all other start/end dates for the same entity_id in the table)

If there is, I'd love to know how Smile


Alternatively, is there an alternative design that would work? The best I can think of is to have one record per date:


CREATE TABLE entity_valid_dates (
id INT IDENTITY(1,1) NOT NULL,
enity_id INT NOT NULL,
valid_date SMALLDATETIME NOT NULL
)

Then you get a unique row for each and every date that is valid for that Entity.


This doesn't work though when (as expected) the time of day becomes a factor, and is messy when the from an to dates are "now onwards, forever" (Represented by "Now until '2070 Jan 01'" at present, as NULLs made other code much slower and messier)


Love to know what other people think Smile


Cheers,
Mat.

 >> Stay informed about: Complicated Constraint - Possible or not? 
Back to top
Login to vote
"Roy Harvey

External


Since: Jan 12, 2008
Posts: 593



(Msg. 2) Posted: Thu Oct 02, 2008 8:47 am
Post subject: Re: Complicated Constraint - Possible or not? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 02 Oct 2008 05:38:35 -0700, Mat Bailie wrote:

>Imagine the following overly simple table...
>
>CREATE TABLE entity_valid_dates (
> id INT IDENTITY(1,1) NOT NULL,
> enity_id INT NOT NULL,
> valid_from SMALLDATETIME NOT NULL,
> valid_until SMALLDATETIME NOT NULL
> )
>
>(Entity_id is a foreign key to a table not mentioned here)
>
>This holds dates between which a particulate entity "is valid". (When to include this entities data, and implicitly, when not to.)
>
>What I want to do is ensure that "overlapping" dates can't be entered.

Write a trigger to enforce it. Use a SELECT against the "inserted"
virtual table with a WHERE EXISTS test that looks for overlaps.
ROLLBACK if there is an overlap.

Roy Harvey
Beacon Falls, CT

 >> Stay informed about: Complicated Constraint - Possible or not? 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 645



(Msg. 3) Posted: Thu Oct 02, 2008 11:00 am
Post subject: Re: Complicated Constraint - Possible or not? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Here is a good example by Alex Kuznetsov on implementing this with a
foreign key constraint:
http://sqlblogcasts.com/blogs/alex_kuznetsov/archive/2008/02/07/using-...oreign-

Another solution by Tony Rogerson using UDF in check constraint:
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2008/02/07/using-a-...-in-a-c

--
Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Complicated Constraint - Possible or not? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
complicated update. - Hi all, Need SP to: a. update values in one table based on values in another. in order to do that, I need to get the last entries in a history table called "GA_Acct_Reset_History" I've got this code straightened out: (thanks to you Forumites...

Complicated Query ( at least I think so ) - Here is the table: idtrans idrma phmdel part qty bagqty btini btdte 211 1117 SAN2300Pink M 5 27 JP 2007-01-24 233 1117 SAN2300Pink M 3 1 JP 2007-01-26 280 1122 SAN2300Pink M 5 90 JP 2007-01-29...

A complicated Query - I've 3 tables : 1- Orders (Order_ID , Seller_ID , .....) 2- Sellers (Seller_ID, Name , ........) 3- Order_Pricing (Order_ID , Total_Price , ......... ) & The desired Query is : List top 5 Orders per Seller (the top total_price) Thanks

Very Complicated Query - Hi All , I have a table named Table1 consists of 3 columns : 1-UserID 2-ProductID 3-NumOfTimesSold Ex : the record : ( 12 --- 5 --- 240 ) indicates that the user(12) has sold 240 piece of the product no. 5 //PrimaryKey is the composition of Column1...

Complicated Insert question... - The 'custom_carts' table has these fields: id file_name description thumb_name name position When I do an insert into this table, I want the position field to automatically be the next number higher than the highest position number. Example: ..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]