Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 30th, 2004, 02:33 PM
jp1234 jp1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 jp1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
primary and foreign keys

I am creating a database for a project, it currently has 4 tables. I have unique primary keys in each table, but did not use any foreign keys. Do I need to have foreign keys in my tables?

Reply With Quote
  #2  
Old January 30th, 2004, 03:53 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,246 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 17 h 3 m 28 sec
Reputation Power: 1055
only if you want the database to enforce relational integrity, e.g. row cannot be added to child table unless parent row exists, parent row cannot be deleted while child rows exist, etc.
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
  #3  
Old January 31st, 2004, 09:19 AM
jp1234 jp1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 jp1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Is it considered bad database design not to use them?

Reply With Quote
  #4  
Old January 31st, 2004, 09:28 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,246 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 17 h 3 m 28 sec
Reputation Power: 1055
no, i don't see why it would be

it is not considered bad form not to use something that you don't need

(yikes, three negatives in one sentence)

Reply With Quote
  #5  
Old February 1st, 2004, 10:27 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 12 m 27 sec
Reputation Power: 56
jp1234: Yes, Rudy is right; foreign keys are only needed if you need foreign keys (how's that for a tautology?). But, I must say it's impossible to give you good advice on a subject like this without knowing a thing about your database or your needs other than '4 tables with primary keys'. My point is, depending on what you are trying to accomplish, it might very well be bad form not to use foreign keys.

Granted, a 4-table database might be simple enough that no relationships between tables are needed. But, after studying basic principles of database design, you might find that you are opening yourself up to real troubles in the future, and that indeed your database should be normalized into several more tables in order to have true data integrity.

Pal's Linux RDBMS Library might be a good place to start reading about this some more. Look for articles and tutorials on "normalization". (Don't worry if you don't use Linux-- the principles are the same). Also, you might want to get a copy of "Database Design for Mere Mortals".
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #6  
Old February 2nd, 2004, 04:26 PM
jp1234 jp1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 jp1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It is for a project at school, we are using an oracle database. We are creating a general help desk for an is/it department. Here is what I came up with for a layout of the tables. Primary Keys are noted by the *.

User Table
------------
*user_email
user_id

ADMIN TABLE
--------------------
*USERNAME


FAQ Table
----------
FAQ_subject
FAQ_question
FAQ_answer
*FAQ_number

Online reports TABLE
---------------
*OL_CASENUM
OL_NAME
OL_USERNAME
OL_PASSWORD
0L_EMAIL
OL_STATE
OL_TELEPHONE
OL_COURSE
OL_COMPUTER (PC OR MAC)
OL_OS (OPERATING SYSTEM)
OL_BROWSER
OL_ISP
OL_DESCRIPTION
OL_ERROR
OL_STATUS
OL_RESOLUTION
OL_SOLVEDBY
OL_DATE

ONCAMPUS REPORTS TABLE
----------------------------------------
*OC_CASENUM
OC_NAME
OC_DEPARTMENT
OC_EMAIL
OC_ROOM
OC_PHONE
OC_PROB_TYPE
OC_DESCRIPTION
OC_ERROR
OC_STATUS
OC_RESOLUTION
OC_SOLVEDBY
OC_DATE

Reply With Quote
  #7  
Old February 9th, 2004, 01:29 PM
jp1234 jp1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 jp1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Anyone have any comments/suggestions based on the table I posted?

Last edited by jp1234 : February 9th, 2004 at 01:32 PM.

Reply With Quote
  #8  
Old February 10th, 2004, 03:24 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 12 m 27 sec
Reputation Power: 56
Well, the obvious question is: where do we see data that is related?

For example, I see a User table, and then in the Online Reports table, I see a reference to OL_Username. Will people in the OL table people also exist in the User table? If so, it is a good idea to name these columns the same, and to enforce that relationship with a foreign key constraint. (Naming related columns the same makes for easy use of the NATURAL JOIN)

Also, I notice that your User table has user_email as the primary key. If this is the case, then what is the point of having a user_id. (generally, you need 'id' columns when the other data in the table is not enough to guarantee uniqueness, or if the 'id' references some other table or database which generates these IDs)

Generally, wherever you have a column that refers to something that can exist in another table, you have a good candidate for a foreign key constraint of some sort.

I also notice that your ONCAMPUS Reports table has a place for name, dept. etc... If we are talking again about actual people, wouldn't it make sense to have one centralized table with information about People, and then the other tables simply reference that table with a foreign key?

And, I see that in every table except ADMIN and FAQ, you have an Email column. Maybe that should be your foreign key to the User table.

Honestly though, it's still hard to know exactly what you need here. I really don't see a cohesive database model here. For example, I see Online Reports, and Oncampus Reports, and I have no idea what the relationship between those is supposed to be. In Online Reports, you have all kinds of information that has nothing to do directly with a report, such as Name, Password, Email, etc... There should be a generalized table with user information, and thus the report table doesn't need to be littered with repeated data. It should just be about the report with a single foreign key referenced to the user data. Basic rule of thumb: avoid repetition.

As I said, study some material relating to basic relational database design, as in the links I gave you, looking especially in the Fundamentals - Database Design area. (I know it's called the Pal's Linux library, but the concepts are the same whether you are using Linux or not). Maybe check out www.datamodel.org, or see some graphical examples of database models at www.databaseanswers.com/data_models/ . Also, for an excellent book on getting the basics quickly, look up "Database Design for Mere Mortals" at your favorite online bookstore.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > primary and foreign keys


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT