Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Constraint/Restriction question

Author  Topic 

thealley
Starting Member

5 Posts

Posted - 2008-02-07 : 13:53:33
Hello all,

First of all I would like to show my satisfaction at this great forum.

I'm quite a beginner at SQL and would like to ask a question.

Let's say I have a table of Students and a table of Subjects with their respective details. Now I want to create another table that references both the previous tables so that I would know what subjects the Student is attending. My problem is how to put a restriction in the new table on the number of subjects that each student can have (e.g. each student can only apply for 8 subjects, not more).

I've heard about triggers and other possible solutions, but it's too advanced for me at the moment, so I'd like to possible find a solution through the new table.

Roughly, I guess the new table (let's call it StudentAttends) will have the student id from the Students table and the subject id from the Subjects table. Now how should I add the above restriction?

Thanks for your help!

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-02-07 : 14:46:56
The "out of the box" CHECK CONSTRAINTS don't look at multiple rows then only look at column values within the row, so that option is out. You can build this restriction as well as any other business logic into whatever process populates the table. What is that process? A stored procedure?

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -