[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 770
  • Last Modified:

Foreign keys and constraints in PostgreSQL

Is it recommended to use foreign keys in PostgreSQL.
Does it slow down the database ?

Thanks
0
yudazdk
Asked:
yudazdk
  • 2
2 Solutions
 
Arthur_WoodCommented:
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
 
earth man2Commented:
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
 
Arthur_WoodCommented:
Gald to be of assistance

AW
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now