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
Solved

Foreign keys and constraints in PostgreSQL

Posted on 2007-12-05
3
753 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Read about achieving the basic levels of HRIS security in the workplace.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 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