If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Unique over several tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-21-10, 09:57
ichibann ichibann is offline
Registered User
 
Join Date: Jul 2010
Posts: 1
Unique over several tables

I've got tables:

Lessons:
id int (PK)
dayNumber int
lessonNumber int
classTeacherSubjectId int (FK)

ClassTeacherSubject:
id int (PK)
classId int (FK)
teacherSubjectId int (FK)

TeacherSubject
id int (PK)
teacherId int (FK)
subjectId int (FK)

I want to create constraint or whatever works so for each record in [Lessons] any combination of dayNumber, lessonNumber and classId is UNIQUE as well as any combination of dayNumber, lessonNumber and teacherId, so no class or teacher has two different lessons at the samy time.
How to do that?
Reply With Quote
  #2 (permalink)  
Old 07-21-10, 10:06
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
You can't do this with your current physical design.
You can't decompose surrogate keys back in to natural keys so you will have to use natural keys in your relationships.
Either that, or redesign your logical model, probably by dispensing with TeacherSubject and ClassTeacherSubject.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #3 (permalink)  
Old 07-21-10, 10:17
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
I'm going to move this to DBConcepts & Design - it better fits that.
I also have a stinking cold and a woolly head - I'm much less sure about what I posted.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #4 (permalink)  
Old 07-21-10, 11:00
futurity futurity is offline
Registered User
 
Join Date: May 2008
Posts: 270
Code:
create table class (
    teacher_id int,
    subject_id int, 
    foreign key (teacher_id, subject_id) references teacher_subject,  

    room_id references classroom,
    time_slot_id reference time_slot,

    unique(teacher_id, time_slot_id),
    unique(room_id, time_slot_id)
)
Leave the subject out of your constraint. Whether or not different subjects are being taught, neither teachers nor rooms can be double-booked.
Reply With Quote
Reply

Tags
unique

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On