Solved

How do I setup relationship for these 2 tables?

Posted on 2014-11-09
7
105 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
Comment Utility
>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
Comment Utility
>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
Comment Utility
>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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
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 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the grade, good luck with your exercise / whatever this was.  Feel free to ask more.  -Jim
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now