Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 704171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:54:43+00:00 2026-05-14T03:54:43+00:00

Please imagine this small database… Diagram removed dead ImageShack link – volunteer database diagram

  • 0

Please imagine this small database…

Diagram

removed dead ImageShack link – volunteer database diagram

Tables

Volunteer     Event         Shift         EventVolunteer
=========     =====         =====         ==============
Id            Id            Id            EventId
Name          Name          EventId       VolunteerId
Email         Location      VolunteerId
Phone         Day           Description
Comment       Description   Start
                            End

Associations

Volunteers may sign up for multiple events.
Events may be staffed by multiple volunteers.

An event may have multiple shifts.
A shift belongs to only a single event.

A shift may be staffed by only a single volunteer.
A volunteer may staff multiple shifts.

Check Constraints

  1. Can I create a check constraint to
    enforce that no shift is staffed by
    a volunteer that’s not signed up for
    that shift’s event?

  2. Can I create a check constraint to
    enforce that two overlapping shifts
    are never staffed by the same
    volunteer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:54:43+00:00Added an answer on May 14, 2026 at 3:54 am

    The best place to enforce data integrity is the database. Rest assured that some developer, intentionally or not, will find a way to sneak inconsistent stuff into the database if you let them!

    Here’s an example with check constraints:

    CREATE FUNCTION dbo.SignupMismatches()
    RETURNS int
    AS BEGIN RETURN (
        SELECT count(*)
        FROM Shift s
        LEFT JOIN EventVolunteer ev
        ON ev.EventId = s.EventId
        AND ev.VolunteerId = s.VolunteerId
        WHERE ev.Id is null
    ) END
    go
    ALTER TABLE Shift ADD CONSTRAINT chkSignup CHECK (dbo.SignupMismatches() = 0);
    go
    CREATE FUNCTION dbo.OverlapMismatches()
    RETURNS int
    AS BEGIN RETURN (
        SELECT count(*)
        FROM Shift a
        JOIN Shift b
        ON a.id <> b.id
        AND a.Start < b.[End]
        AND a.[End] > b.Start
        AND a.VolunteerId = b.VolunteerId
    ) END
    go
    ALTER TABLE Shift ADD CONSTRAINT chkOverlap CHECK (dbo.OverlapMismatches() = 0);
    

    Here’s some tests for the new data integrity checks:

    insert into Volunteer (name) values ('Dubya')
    insert into Event (name) values ('Build Wall Around Texas')
    
    -- Dubya tries to build a wall, but Fails because he's not signed up
    insert into Shift (VolunteerID, EventID, Description, Start, [End]) 
        values (1, 1, 'Dunbya Builds Wall', '2010-01-01', '2010-01-02')
    
    -- Properly signed up?  Good
    insert into EventVolunteer (VolunteerID, EventID) 
        values (1, 1)
    insert into Shift (VolunteerID, EventID, Description, Start, [End]) 
        values (1, 1, 'Dunbya Builds Wall', '2010-01-01', '2010-01-03')
    
    -- Fails, you can't start the 2nd wall before you finished the 1st
    insert into Shift (VolunteerID, EventID, Description, Start, [End]) 
        values (1, 1, 'Dunbya Builds Second Wall', '2010-01-02', '2010-01-03')
    

    Here are the table definitions:

    set nocount on
    if OBJECT_ID('Shift') is not null
        drop table Shift
    if OBJECT_ID('EventVolunteer') is not null
        drop table EventVolunteer
    if OBJECT_ID('Volunteer') is not null
        drop table Volunteer
    if OBJECT_ID('Event') is not null
        drop table Event
    if OBJECT_ID('SignupMismatches') is not null
        drop function SignupMismatches
    if OBJECT_ID('OverlapMismatches') is not null
        drop function OverlapMismatches
    
    create table Volunteer (
        id int identity primary key
    ,   name varchar(50)
    )
    create table Event (
        Id int identity primary key
    ,   name varchar(50)
    )
    create table Shift (
        Id int identity primary key
    ,   VolunteerId int foreign key references Volunteer(id)
    ,   EventId int foreign key references Event(id)
    ,   Description varchar(250)
    ,   Start datetime
    ,   [End] datetime
    )
    create table EventVolunteer (
        Id int identity primary key
    ,   VolunteerId int foreign key references Volunteer(id)
    ,   EventId int foreign key references Event(id)
    ,   Location varchar(250)
    ,   [Day] datetime
    ,   Description varchar(250)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Please suggest some good resources to start writing Java Web services.
Please read the whole question. I'm not looking for an approach to managing multi-lingual
Please give me the direction of the best guidance on the Entity Framework.
Please bear with me, I'm just learning C++. I'm trying to write my header
Please explain to me why the very last echo statement is blank? I expect
Please excuse the vague title. If anyone has a suggestion, please let me know!
Please help! I'm really at my wits' end. My program is a little personal
Please provide tips for effectively using git with svn. What are your best practices?
Please, provide code examples in a language of your choice. Update : No constraints

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.