Solved

How do I setup relationship for these 2 tables?

Posted on 2014-11-09
7
110 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 34

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Data Warehouse / Data Marts 4 41
Select Sum query with group by 8 46
Server 2012 r2 and SQL 2014 6 34
Create a Calendar table 29 43
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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

734 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