Solved

Foreign keys and constraints in PostgreSQL

Posted on 2007-12-05
3
754 Views
Last Modified: 2013-12-12
Is it recommended to use foreign keys in PostgreSQL.
Does it slow down the database ?

Thanks
0
Comment
Question by:yudazdk
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 400 total points
ID: 20410674
A foreign key serves a vital purpose in ANY database.  It assures you that when a 'child' record is added to a table, that record has a corresponding 'parent' record.

For instance, your application has and Orders table (holds CustomerID- another ForeignKey, and OrderDate, for instance)and an OrderDetail table (holds the ProductID, Quantity, and Unit Cost, for instance)

The Order table has a Primary key of OrderID, and the OrderDetail table has OrderID as a Foreign key.  Thus, whenever an new OrderDetail record is added, it MUST have a corresponding Order (since the OrderID is the foreign Key, thus an Odrder with that ID must be present in the Order Table) - You cannot have OrderDetails witout knowing what order those OrderDetail entries apply to.

It is NEVER a good design to leave out a foreign key, if it is relevant to your application.

AW

AW
0
 
LVL 22

Assisted Solution

by:earth man2
earth man2 earned 100 total points
ID: 20410806
Arthur is correct of course.  FKs will slow inserts, updates and delete operations because data integrity is enforced and (usally) this involves reading writing index operations..
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20413314
Gald to be of assistance

AW
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

679 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