Database Key Concept
There are a number database keys. Among them, two types database keys are important – Primary key and foreign key. In this page, you will learn about those two database key concept.
[WpProQuiz 1]
Primary Key:
The primary key uniquely identifies each item/row in the table.
- No two rows in a table can have the same values in the primary key field.
- The primary key field cannot be null.
Fig-1: The ‘Employee Id’ field uniquely identifies each employee(row).
Fig-2: The StudentId field uniquely identifies each student.
Fig-3: More than one vendor supply the same product (609). The ProductID field is not enough to uniquely identify a row in this table because ProductID ‘609’ appeared twice in the table. The combination of ProductID and VendorID together uniquely identify each row. So, {ProductID, VendorID} is the primary key.
Foreign Key:
An attribute of a table is a foreign key if it is the primary key of another table.
Fig-4: Foreign key Example 1
In Fig-4, Employee_ID field is present in both the Employee table and the Certified table. As the Employee_ID in the Certified table is the primary key of Employee table, Employee_ID in the Certified table is a foreign key. Also Skill_ID field is present in both the Skill table and the Certified table. As the Skill_ID in the Certified table is the primary key of Skill table, Skill_ID in the Certified table is a foreign key.
Fig-5: Foreign key Example 2
In Fig-5,
- REGION_CODE field is present in both the REGION table and the STORE table. As the REGION_CODE in the STORE table is the primary key of REGION table, REGION_CODE in the STORE table is a foreign key.
- STORE_CODE field is present in both the STORE table and the EMPLOYEE table. As the STORE_CODE in the EMPLOYEE table is the primary key of STORE table, STORE_CODE in the EMPLOYEE table is a foreign key.
- JOB_CODE field is present in both the JOB table and the EMPLOYEE table. As the JOB_CODE in the EMPLOYEE table is the primary key of JOB table, JOB_CODE in the EMPLOYEE table is a foreign key.
[WpProQuiz 1]