March 29, 2013 at 12:01 pm
One of my databases has a table for Project Dates. Some of the milestone dates can occur multiple times such as 'Project Review'. Others can only occur once, 'Project Handover'. Is there a way to create a check constraint whereby the dates that should only exist once, can be limited to existing only once while the others can exist multiple times?
I do have an external table that manages the various dates and assigns a key to each date.
Table: DateCategories
Id (Primary Key)
Description
[Other Fields]
Table: Project Dates
Id (Primary Key)
DateCategoryId (Foreign Key)
Milestone Date
March 29, 2013 at 12:06 pm
yes, you can make a check constraint that uses a user defined function.
something like this is untested, but kind of gives you an idea of how it would work
create function dbo.LimitedToOne (@GroupId int)
returns int
as
begin
return (select count(ANumber) from dbo.TestTable where GroupId=@GroupId)
end
GO
alter table dbo.TestTable add constraint chkgrp check (dbo.LimitedToOne(GroupId) <= 1)
Lowell
March 29, 2013 at 1:13 pm
Hmmm...
So in the DateCategory table, add a column that defines if the specific date category (Project Review, Weekly Check-In, Project Handover, etc.) is 'limited' (?) and then have the function look up the value, check the records and then grant its blessing.
Thanks.
March 29, 2013 at 3:31 pm
It is possible and it works. I did this at a previous employer where I separated information into two separate tables. The idea was to allow for future expansion where a one to many relationship would be permitted, but currently was not. I wanted the tables in place during initial design rather than having to create the new table later when he new functionality would be implemented.
Doing the initial work upfront was to make it easier, though it never became a requirement. In fact, this particular system actually went away in favor of a third party solution.
March 29, 2013 at 3:35 pm
Lynn Pettis (3/29/2013)
It is possible and it works. I did this at a previous employer where I separated information into two separate tables. The idea was to allow for future expansion where a one to many relationship would be permitted, but currently was not. I wanted the tables in place during initial design rather than having to create the new table later when he new functionality would be implemented.Doing the initial work upfront was to make it easier, though it never became a requirement. In fact, this particular system actually went away in favor of a third party solution.
Oh Dear Lord! Someone thinking ahead and considering the what-if possibility as opposed to just operating within the hard and fast defined requirements of the current scope of the project because he understands the cost/benefit of going a little bit further than scope in order to lay a more flexible groundwork for the future.
March 29, 2013 at 3:46 pm
david.holley (3/29/2013)
Lynn Pettis (3/29/2013)
It is possible and it works. I did this at a previous employer where I separated information into two separate tables. The idea was to allow for future expansion where a one to many relationship would be permitted, but currently was not. I wanted the tables in place during initial design rather than having to create the new table later when he new functionality would be implemented.Doing the initial work upfront was to make it easier, though it never became a requirement. In fact, this particular system actually went away in favor of a third party solution.
Oh Dear Lord! Someone thinking ahead and considering the what-if possibility as opposed to just operating within the hard and fast defined requirements of the current scope of the project because
shehe understands the cost/benefit of going a little bit further than scope in order to lay a more flexible groundwork for the future.
Note the gender change above.
April 1, 2013 at 6:21 am
I once created a function that predicted the gender of a person by doing a lookup based on their name.
April 2, 2013 at 8:23 am
david.holley (4/1/2013)
I once created a function that predicted the gender of a person by doing a lookup based on their name.
How did you handle the Lynns, Ashleys, Taylors, and all the other names that have commonly been given to both boys and girls? Did you just arbitrarily classify those names as male or female and accept that they would result in erroneous predictions for some portion of the sample?
😀
Jason Wolfkill
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply