What are Primary Key and Foreign Key
In general, a single database can hold multiple tables. Each table can be related using primary key > foreign key relationships. So, in this article, we will discuss are primary keys and foreign keys.
Table relationships:
Primary keys
It is a column or set of columns used to uniquely identify a row in a table. In the above image, the primary keys are:
Employee table: EmpID column is the primary key used to uniquely identify a row.
Department Table: DepartmentID column is the primary key that is used to identify a row uniquely.
Loan Table: The loanID column is the primary key used to uniquely identify a row.
Foreign Keys
It is a column or set of columns in which the values are based on primary key values of other tables. In the above image, foreign keys are:
Employee Table: The deptID column of the employee table is a foreign key, It refers to the primary key DeptID of the department table. It means it is possible to enter department id values in the employee table only when they are found in the Department table. We cannot enter a new department “D4” in the Employee table as “D4” is not available in the Department table.
Loan Table: EmpID column of the loan table is a foreign key. it refers to the primary key EmpID of the Employee table. It means only it is possible to enter employee id values in the loan table which are available in the Employee table. We cannot enter a new EmpID “E1008” in the loan table as “E1008” is not available in the Employee table.