Solved

How do I setup relationship for these 2 tables?

Posted on 2014-11-09
7
109 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

830 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