SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Check Constraint Question - Situations Where a Child Record Should Be Limited to One Occurrance


Check Constraint Question - Situations Where a Child Record Should Be Limited to One Occurrance

Author
Message
david.holley
david.holley
SSC Veteran
SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)

Group: General Forum Members
Points: 266 Visits: 260
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
Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29523 Visits: 39986
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

david.holley
david.holley
SSC Veteran
SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)

Group: General Forum Members
Points: 266 Visits: 260
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.
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41424 Visits: 38567
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.

Cool
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)
david.holley
david.holley
SSC Veteran
SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)

Group: General Forum Members
Points: 266 Visits: 260
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.
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41424 Visits: 38567
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.

Cool
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)
david.holley
david.holley
SSC Veteran
SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)

Group: General Forum Members
Points: 266 Visits: 260
I once created a function that predicted the gender of a person by doing a lookup based on their name.
wolfkillj
wolfkillj
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1516 Visits: 2582
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?

:-D

Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search