Foreign Key


A foreign key is a term used in database management systems. It’s a field (or collection of fields) in a table, that is used to establish a link between the data in two tables. The foreign key in one table points to a primary key in another table, establishing a relationship between the two.


The phonetics of “Foreign Key” is: ˈfɔrɪn kiː.

Key Takeaways

  1. Defines Relationships: A foreign key in a database is used to establish a link or relationship between two tables. It acts as a cross-reference between tables as it references the primary key of another table, thus maintaining the consistency and integrity of the data.
  2. Data Integrity: Foreign keys enforce referential integrity in the database. This ensures that the data is consistent, correct, and reliable. It prevents actions that would destroy the links between tables and disrupts the integrity of the data.
  3. Imposes Rules: Foreign key constraints can be used to define the actions that the database should take when a user attempts to delete or update a primary key to which existing foreign keys point. The actions could be CASCADE, SET NULL, NO ACTION, or SET DEFAULT, etc., providing different ways of handling such situations.


A Foreign Key is a vital concept in database design that maintains the integrity and logical correlation among data. It’s essentially a column or a set of columns in a table that’s used to link it with the primary key of another table. The primary function of a foreign key is to enforce a link between the data in two tables, thus ensuring that the specific corresponding record exists. This feature plays a crucial role in preventing actions that would destroy links between tables and safeguards the consistency of the related data. It enables efficient data navigation and management, productive use of indexes, and implementation of constraints in the relational database, leading to more accurate and reliable databases.


The primary purpose of a Foreign Key in the realm of database technology relates to enforcing a form of referential integrity within the database. In essence, it provides a system to understand the relationships between tables, essentially linking them together. When a table has a foreign key, it is said to be the child table or referencing table, and the table it points (refers) to is called the parent table or referenced table. This connecting role of a foreign key is critical in creating a meaningful, connected, and logically structured set of datasets.In practice, Foreign Keys aid in preventing actions that would destabilize these aforementioned relationships. They ensure the consistency and correctness of data, thus preventing any incorrect entries from being saved in the table. For instance, when two tables are related, a foreign key in one table could reference the primary key in another. If an attempt to enter an unlisted or non-existing primary key value is made, the database transgression would be prevented by the foreign key constraint. So, foreign keys act as a checkpoint in maintaining the overall data integrity across multiple tables within a database.


1. Student-University Database: In the database of a university, there could be one table that includes information about all the students (student ID, name, major, etc.) and another table that includes all the offered courses (course ID, course name, professor). The ‘course ID’ in the student table will act as a foreign key, linking to the ‘course ID’ in the course table and displaying which course a student is enrolled in.2. Employee-Department Database: In a company’s database system, one table includes information about all the employees (employee ID, name, position etc.), and a separate table includes information about each department (department ID, department name, department location etc.). The ‘department ID’ in the employee table will act as the foreign key, connecting to the ‘department ID’ in the department table and showing which department an employee belongs to.3. Customer-Orders Database in E-commerce: In an e-commerce application, there may be a table listing customers (customer ID, name, email, address) and another table documenting all the orders placed (order ID, product, quantity, customer ID). Here, ‘customer ID’ is a foreign key in the orders table, linking to the ‘customer ID’ in the customer table. This association indicates which customer placed each order.

Frequently Asked Questions(FAQ)

**Q1: What is a Foreign Key in a database?**A1: A Foreign Key is a column or set of columns in a relational database table that provides a link between data in two tables. It acts as a cross-reference between tables as it references the primary key of another table, thereby enforcing certain constraints and links between data.**Q2: What is the purpose of a Foreign Key?**A2: The main purpose of a Foreign Key is to maintain the data integrity and consistency across tables in a database. It enforces referential integrity by ensuring that the relationship between two tables remains synchronized during updates and deletes.**Q3: Can a table have more than one Foreign Key?**A3: Yes, a table can have multiple Foreign Keys depending upon the relations with other tables. Each Foreign Key is linked to a specific Primary Key of another table.**Q4: Does a Foreign Key always need to connect to a Primary Key?**A4: Typically, a Foreign Key connects to a Primary Key of another table. However, it can also link to another Foreign Key or a unique key, as long as the referenced column(s) is unique and not null.**Q5: Can a Foreign Key have a null value?**A5: Yes, a Foreign Key can have null values. If a row in the child table doesn’t correspond to any row in the parent table, the Foreign Key field will be null.**Q6: What happens if you try to delete data which is in a Foreign Key that links to another table?**A6: This depends on the referential action specified during foreign key creation. If the action is ‘cascade delete’, the deletion of data in the parent table will lead to the automatic deletion of corresponding data in the child table. If ‘no action’ is specified, the database system will prevent deletion of data in the parent table. If set to ‘set null’, the deletion in the parent table will set the foreign key in the child table to null.**Q7: Is it necessary to have a Foreign Key in every table?**A7: No, it’s not necessary to have a Foreign Key in every table. Foreign Keys are used when tables have relationships with each other. If a table doesn’t have a relationship with another table, it won’t need a Foreign Key.**Q8: How is a Foreign Key different from a Primary Key?**A8: A Primary Key is a unique identifier for a row within a database table. Every table has one primary key. A Foreign Key, on the other hand, is a field (or collection of fields) in one table, that is used to match with the primary key in another table to establish a link between the two.

Related Finance Terms

  • Primary Key
  • Relational Database
  • Referential Integrity
  • Database Normalization
  • SQL (Structured Query Language)

Sources for More Information


About The Authors

The DevX Technology Glossary is reviewed by technology experts and writers from our community. Terms and definitions continue to go under updates to stay relevant and up-to-date. These experts help us maintain the almost 10,000+ technology terms on DevX. Our reviewers have a strong technical background in software development, engineering, and startup businesses. They are experts with real-world experience working in the tech industry and academia.

See our full expert review panel.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

Technology Glossary

Table of Contents

More Terms