Solved

Foreign keys and constraints in PostgreSQL

Posted on 2007-12-05
3
763 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to dynamically set the form action using jQuery.
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.

623 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