Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Check Constraint Question - Situations Where a Child Record Should Be Limited to One Occurrance Expand / Collapse
Author
Message
Posted Friday, March 29, 2013 12:01 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 16, 2014 7:05 AM
Points: 118, Visits: 226
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
Post #1437040
Posted Friday, March 29, 2013 12:06 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:31 AM
Points: 12,962, Visits: 32,501
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1437043
Posted Friday, March 29, 2013 1:13 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 16, 2014 7:05 AM
Points: 118, Visits: 226
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.
Post #1437065
Posted Friday, March 29, 2013 3:31 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:02 PM
Points: 20,862, Visits: 32,893
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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1437109
Posted Friday, March 29, 2013 3:35 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 16, 2014 7:05 AM
Points: 118, Visits: 226
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.
Post #1437111
Posted Friday, March 29, 2013 3:46 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:02 PM
Points: 20,862, Visits: 32,893
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 she he 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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1437118
Posted Monday, April 1, 2013 6:21 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 16, 2014 7:05 AM
Points: 118, Visits: 226
I once created a function that predicted the gender of a person by doing a lookup based on their name.
Post #1437416
Posted Tuesday, April 2, 2013 8:23 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 12:50 PM
Points: 1,061, Visits: 2,580
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
Blog: SQLSouth
Twitter: @SQLSouth
Post #1437924
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse