Solved

How do I setup relationship for these 2 tables?

Posted on 2014-11-09
7
108 Views
Last Modified: 2014-11-09
Hi,
I'm using sql express 2012 for an exercise.
If I have  a Auction table(PD AuctionID) and a Person table(PD PersonID) with the following condition.
The auction table would have many auctions.  A person can register to attend only one auction.

1.  I think I would add a PersonId(FK) column in Auction Table and then add a AuctionID in Person table(FK).
2.  In Design diagram, I dragged the AuctionID (PD) to the Person table's FK of AuctionId, that creates a Key and a infinite sign (On Person table side), which means one-to-many from Auction to Person.  So a Auction will have many person in it.
3.  How do I create a one-to-one relationship from person to Auction?  this is so that a person can only attend one Auction?
Thank you.
0
Comment
Question by:lapucca
  • 4
  • 2
7 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40431424
>The auction table would have many auctions.  A person can register to attend only one auction.
So this is a one-to-many:  Auctions can have many people, people can have only one auction.

>1.  I think I would add a PersonId(FK) column in Auction Table
correct

>and then add a AuctionID in Person table(FK).
Not correct, as Auction.Person already handles this relationship.

>3.  How do I create a one-to-one relationship from person to Auction?
Since we have a one-to-many this is not applicable.
0
 

Author Comment

by:lapucca
ID: 40431434
>and then add a AuctionID in Person table(FK).
Not correct, as Auction.Person already handles this relationship.

Then how can I find out which Auction did the Person register to?  I can only query from the auction table to find out? Lik
Select * from [dbo].[Auction] a inner join [dbo].[Person] b
on a.PersonId = b.PersonId
Where b.[FirstName] = 'Tom' and b.[Lastname] = 'Smith'

thank you.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40431448
>Then how can I find out which Auction did the Person register to?

SELECT a.AuctionId, a.AuctionName
FROM Auction a
   JOIN  Person p ON  a.PersonId = p.PersonId 
WHERE p.PersonId = {the PersonId you are searching for goes here}
-- or
WHERE p.PersonName = 'Spuds MacKenzie'

Open in new window

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 65

Expert Comment

by:Jim Horn
ID: 40431450
btw explain the logic behind 'A person can register to attend only one auction.', as my impression of auctions is that a person can attend as many auctions as they wish.
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40431482
For a semantically correct model you need a junction table Attendance. Depending on your requirements its:

CREATE TABLE Attendance
(
    AuctionID INT NOT NULL,
    PersonID INT NOT NULL,
    CONSTRAINT PK_Attendance PRIMARY KEY ( AuctionID ),
    CONSTRAINT FK_Attendance_AuctionID FOREIGN KEY ( AuctionID) REFERENCES Auction ( AuctionID ),
    CONSTRAINT FK_Attendance_PersonID FOREIGN KEY ( PersonD) REFERENCES Person ( PersonID ),
);

Open in new window


Whether you choose ( AuctionID ) or ( AuctionID, PersonID ) as primary key defines the cardinality of your relationship.
0
 

Author Closing Comment

by:lapucca
ID: 40431483
It's just an exercise for me.  It doesn't make real life/business sense for this database design.  I haven't worked with database for over 3 years and just getting my feet wet again.  I'm surprised that the join query I wrote actually works.   It's amazing how quickly I can forget stuff.  
Thank you for your patience and all your help.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40431498
Thanks for the grade, good luck with your exercise / whatever this was.  Feel free to ask more.  -Jim
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hyperlink data type in SQL 3 28
what are the unique tables in SQL master database 5 62
Tsql query 6 22
Connecting to multiple databases to create a Dashboard 5 26
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

810 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