Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I setup relationship for these 2 tables?

Posted on 2014-11-09
7
Medium Priority
?
113 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
[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
  • 4
  • 2
7 Comments
 
LVL 66

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 66

Accepted Solution

by:
Jim Horn earned 2000 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 66

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 35

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 66

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

704 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