
This way, it forms one-to-many relationships between the sportspeople and sports tables, which means that for one sport there could be multiple sportspeople. This foreign key refers to the primary key column sports_id in the sports table. Suppose, we have the sportspeople table that has a PostgreSQL foreign key column sports_id. Foreign key exampleįor better understanding, let us consider the following example. Constraints are the rules enforced on data columns in tables and used to prevent invalid data from being entered into the database. Technically, FOREIGN KEYs, alongside PRIMARY KEYs, CHECK and UNIQUE constraints, belong to the PostgreSQL database constraints. In other words, the foreign key field in one table refers to another table’s primary key field. The foreign key is often called the referencing key as it matches the primary key field from another table. And the table referenced by the foreign key is called the parent table (or the referenced table). The database table that contains a foreign key is called the child table (or the referencing table). Learn the basics of the PostgreSQL FOREIGN KEY constraint What is FOREIGN KEY in PostgreSQL?Ī foreign key (FK) represents one or more than one column used to establish and enforce a link between data in two database tables for controlling data stored in the foreign key table.
#ALTER TABLE ADD FOREIGN KEY POSTGRES HOW TO#
Here we will go into the basics of PostgreSQL Foreign Keys, demonstrate to you how to create, drop, and manage foreign keys on real-life examples, and introduce to you one of the most convenient Postgres GUI tools on the market. Both primary keys and foreign keys are the two types of constraints that are used to enforce data integrity in PostgreSQL tables.
#ALTER TABLE ADD FOREIGN KEY POSTGRES SERIES#
This is the second article in our series on PostgreSQL database constraints, and it comprises a follow-up to our recent PostgreSQL Primary Keys review. Why is a GUI tool a better way to manage keys in PostgreSQL?.Learn the basics of the PostgreSQL FOREIGN KEY constraint.Read on to find out what foreign keys in PostgreSQL are and how to use them. In fact, it is the FOREIGN KEY mechanism that provides the relationality of relational databases. Foreign keys help normalize data in databases and reduce redundancy.
