Solved

Foreign keys and constraints in PostgreSQL

Posted on 2007-12-05
3
745 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
Comment Utility
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
Comment Utility
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
Comment Utility
Gald to be of assistance

AW
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now