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
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.

Question by:RHADMIN
  • 2
  • 2
LVL 37

Accepted Solution

momi_sabag earned 250 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])

Assisted Solution

orcic earned 250 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
LVL 37

Expert Comment

ID: 22621925
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

Expert Comment

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.")

Author Comment

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,

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Procedure syntax 5 38
Powershell v3 - SQLCMD 3 26
Dynamic SQL select query 4 36
SSRS: Why is Visual Studio stripping these properties? 2 19
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

809 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