table constraints

I have Table A like this

Id, Name, ClassName
1, Neil, English
1, Neil, French
2, Rob, Spanish
2,Rob, English

I want to ensure that Id is unque to the name meaning only Neil can have id 1  no one else. Any idea how to achieve that ?
LVL 8
koppchaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
Use a trigger...
create trigger tablea_trg_iu
on tablea
as begin
if exists (select * from tablea t, inserted i where t.Id = i.Id and t.Name != i.Name) rollback trigger with raiserror 20000 "id/name must be unique"
end

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jan FranekCommented:
Well, it's badly designed table. You should have three tables - one for students, second for classes and third for binding students to classes.

http://en.wikipedia.org/wiki/Database_normalization
0
wilcoxonCommented:
To expand on what Jan said, you could easily use two tables (in this case).

Using three tables:

Students: Id, Name (better named StudentId and StudentName)
Classes: ClassId, ClassName
Xref: Id, ClassId (StudentId, ClassId)

Using two tables:

Students: Id, Name (better named StudentId and StudentName
Classes: Id, ClassName (StudentId and ClassName)

The three-table version will use less database space and make querying based on a class easier.
0
knel1234Commented:
koppcha,

There are a lot of good points from wilcoxon and jan

If this is an academic excerise rather than a real world exercise.  You may simply look to do:

create unique index idx1 on tableA (Id, Name, ClassName)

Note:  You would still need to control the Id somehow (programmatically, identity, etc)

Also, I could interpret your question as follows.  I want to have a unique id (not necessarily sequential) for each student.  As a result, I could take jan and wilcoxon's idea of a "Students" table (containing StudentId and StudentName) and have the StudentId field be an "identity" column(or integer field that is controlled).

Bottom line:  What was the real purpose of your question?  Was it academic and the instructor simply wanted you to have a "unique" index or was it real world and you should incorporate jan and wicoxon thoughts (normalization) and some indexes, triggers, etc.

Either way, let us know.

cheers
knel
0
BenSladeCommented:
For the 3 table solution, a unique index on Name would have to be added to prevent 1,Neil and 3,Neil from simultaneously existing.

In the real world, it's just the other way around, you would have assume the occasional duplicate John Smith type name and instead create a unique index on the Students.Id field

You could/should also then add referential integrity constraints to make sure students and classes are defined before they're used in the Xref table.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Sybase Database

From novice to tech pro — start learning today.