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 > theory

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-24-04, 01:44
pollux0 pollux0 is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
theory

just a theory question.

Say I had a login screen which takes username and password. but i have two tables, customers and employees (both can log in)

I know its easy to check both tables for the correct combo...but:

How would I (using fk) make sure the same username was not in both tables? Is this the best design or is it better to have a table called "person" which stores everybody. It would have an extra column to store wheather the person was an employee or a customer?

thanks
Reply With Quote
  #2 (permalink)  
Old 01-24-04, 02:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
"How would I (using fk) make sure the same username was not in both tables?"

you can't

"Is this the best design or is it better to have a table called "person" which stores everybody. It would have an extra column to store wheather the person was an employee or a customer?"

that is better

it is called supertype/subtype
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-24-04, 12:02
pollux0 pollux0 is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
Would the pk of the supertype table person bee the pk + fk of the subtype table?

What would be the correct naming convention for this setup?

person
person_employee
person_customer


Which brings me to my other main "theory" question..In your expereince what is the best naming convention for table and column names?

id or ID or PersonID or person_id

I know these are trivial questions but I would rather get it right the first time...
Reply With Quote
  #4 (permalink)  
Old 01-24-04, 12:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
"Would the pk of the supertype table person bee the pk + fk of the subtype table?"

yes, that's right

there is no "correct" naming convention

the best naming convention, in my opinion, is

- lean
- short
- evocative

lean means don't throw crap into the name, like tblPeople for the people table

short should be obvious, you will be typing the name, and the fewer keystrokes the better

evocative to give an indication of what's in the table/column, so BackorderAmount instead of BOAmount (shorter but misleading) or -- $deity help us -- Amount (meaningless)

it should also go without saying that you should not name a column with a reserved word like Date, but i see this all the time

there is a bit of controversy surrounding the naming of primary keys

if table1 has a pk called ID, and table2 is related to table1 with a foreign key, what do you call the foreign key?

most people (myself included) would call it table1ID, but this prevents you from using the USING keyword in joins, and the NATURAL join altogether

in order to use USING or a NATURAL join, you would have to go back to table1 and name the primary key table1ID, which i find a bit redundant
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-24-04, 13:03
pollux0 pollux0 is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
so a column can be a pk and a fk at the same time

examle:

person:
personid | name
5 | jon

employee:
personId | officeID
5 | 10

employee.personId is a pk and a fk, if not how do you stop a double entry:

employee:
personId | officeID
5 | 10
5 | 10
Reply With Quote
  #6 (permalink)  
Old 01-24-04, 13:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, personID in the employee table is both pk in its own table and fk to the people table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 01-24-04, 23:30
pollux0 pollux0 is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
Talking

In your experiences, is it normal to have a supertype/subtype relationship where employees and customers can be stored in one supertype table. I am just trying to think of any problems that can arise in the future where it might need to be seperated.

It seems that a database designer can "over-think" a database design....
Reply With Quote
  #8 (permalink)  
Old 01-25-04, 06:40
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
normal?

i dunno

it depends on so many things
__________________
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