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

  • 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

  • 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!

  • 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.

  • 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 (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.

  • 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.

  • I once created a function that predicted the gender of a person by doing a lookup based on their name.

  • 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