?
Solved

How to add Foreign key constraint on (1:1) to (0:1) relationship in MS SQL 2005 using T-SQL/GUI?

Posted on 2008-10-02
5
Medium Priority
?
3,176 Views
Last Modified: 2011-10-19
Hi, Experts,
I am new to MS SQL 2005, attached is a part of my ERD:

I don't know how to add foreign key constraint between tables: Car(1,1) and SalesInvoice (0:1) using T-SQL/GUI, can anyone help?

Also, are there any differences at all with other similar types of foreign key constraints(like, (1:1)  to (1:*) or (1:1) to (0:*)) implementing on MS SQL using T-SQL/GUI.

Thanks,
-Rick
CarDealerShip.gif
0
Comment
Question by:RHADMIN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 1000 total points
ID: 22621869
foreign keys in sql server are always implemnted in the same way:
a child table key is pointing to a unique / primary key at the parent
which means, that every relationship is a 1:n where n>=0
add it like this:
alter table [Sales Invoices] add constraint MyConstraintName foreign key([car id]) references car([car id])
0
 
LVL 7

Assisted Solution

by:orcic
orcic earned 1000 total points
ID: 22621910
Actually, you can have 1:1 relation. You only have to make foreign key to be unique.

ALTER TABLE [Sales Invoices]
ADD CONSTRAINT SalesInvoicesUniqueCarID
UNIQUE NONCLUSTERED ([car id])
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22621925
no
what orcic wrote will add a unique constraint
not a foreign key

you can create a unique constraint on the car id column in your table and that will make it unique
but you can't achieve a 1:1 relationship using foreign keys alone
0
 
LVL 7

Expert Comment

by:orcic
ID: 22621958
of course you need to make foreign key first and then to make it unique (that's what I ment with "You only have to make foreign key to be unique.")
0
 
LVL 1

Author Comment

by:RHADMIN
ID: 22637092
Hi, momi_sabag & orcic:

Thanks for your answers, UNIQUE constraint is what I am looking for(which I originally thought it can be done on Foreign key constraint alone somehow).
Based on the question, another question: how to set a constraints on relations like: (1:1) to (1:5)  or  (1:1) to (1:100), which means how to set minimum & maximum number of relationship check?

Many thanks,
-Rick
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question