If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Need help with key design

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-27-07, 11:43
tom.fox tom.fox is offline
Registered User
 
Join Date: Aug 2007
Posts: 12
Need help with key design

Hi guys...

....I have a very simple question, which unfortunately seems hard to answer for me

I have two tables. Salesperson with its attributes 'ID' and 'Name'. And Invoice with several other attributes (Invoice# and another attribute are compound key). Each salesperson's ID is related to an invoice#.

How do i have to design the structure, that, if an instance gets deleted it will not delete the salesperson. But also i do not want an instance to be deleted if a salesperson gets deleted (if he gets fired or whatever).

because lets say, an instance exists in table "invoice" related to salesperson that does not exist anymore in table "salesperson" and a select query is made.
an error would occur, right?

hope it makes sense, english is not my mother tongue

thank you
Reply With Quote
  #2 (permalink)  
Old 08-27-07, 12:22
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Well, its a bad application design if you are deleting salesmen and leaving orphaned invoice records. So my first advice would be to not do this.
But if you insist, then just do not enforce the foreign key relationship between the two tables.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #3 (permalink)  
Old 08-27-07, 14:14
tom.fox tom.fox is offline
Registered User
 
Join Date: Aug 2007
Posts: 12
i kind of figured it might be that way, to do some statistics far back regarding sales etc., even if the corresponding salesperson is not working at this company anymore.........

so all i need to do is not defining a foreign key?
Reply With Quote
  #4 (permalink)  
Old 08-27-07, 15:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the way to do this is to leave the salesperson in the salespersons table forever, but with a status indicator of "fired" or "quit" or "died writing his monthly status report" or whatever

that way, everything stays properly linked up (as regards foreign keys)

if you do wish to delete a salesperson who still has invoices, one of the ON DELETE options is SET NULL

this allows you to keep the deleted salesperson's invoices in the invoices table, but now you no longer know whose invoices they were (which makes sense since that salesperson doesn't exist anymore)

regardless of which way you decide to go, under no circumstances will a select query get an error if something has been deleted that once was there before

the select query may not show you a clear picture of the truth, but it won't give an error

if you're worried about the salepersons table having inactive salespersons in it, you could simply create a view for active salespersons
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On