You are on page 1of 2

This is a lab exercise to create two or more tables in a database that obey a certain restricted set of

rules.
To enforce the property that duplicate rows are forbidden, each relation must have its own primary
key. A primary key is one or more columns of a table where values are used to uniquely identify each
of the rows in a table.
In the above example, the DeptNo field in the Department table is a foreign key. It is linked to the
DeptNo field in the Employee table which is a primary key. For any given record, the value of
Department.DeptNo will match the value of Employee.DeptNo.

A foreign key is a field that is linked to another table‘s primary key field in a relationship between
two tables.

Employee table

EmpNo Fname Job Mgr Hiredate Salary commission


Deptno
7369 Smith clerk 7902 17-Dec-80 800 20
7499 Allen Salesman 7688 20-Feb-81 1600 800 30
7521 Ward Salesman 7698 22-Feb-81 1250 300 30
7566 John Manager 7839 02-Apr-81 2975 20
7654 Martin Salesman 7698 28-Sep-81 1250 30
7698 Blake Manager 7839 01-May-81 2850 30
7782 Clarke Manager 7839 29-Nov-81 3000 20
7788 Scott Analyst 7566 09-Nov-81 5000 40
7839 King President 17-Nov-81 5100 10
7844 Turner Salesman 7688 08-Sep-81 1500 0 30
7876 Adams Clerk 7788 23-Sep-81 1100 20
7900 James Clerk 7698 03-Dec-81 950 0 30
7902 Ford Analyst 7566 02-Dec-81 3000 20
7934 Miller Clerk 7782 23-Jan-82 1300 10

Departments

Deptno Dname Location


10 Accounting London
20 Research Bristol
30 Sales London
40 Operations Birmingham Example of a one-to-many
relationship. The DeptNo in the
Department table is the foreign key.
Employee Department

EmpNo DeptNo
DeptNo Dname
Location
In relational database management systems, a relationship defines a relationship between two
or more tables. That is, the data in one table is related to the data in the other. One table
contains the primary key and the other table contains the foreign key.

When we establish a relationship between the tables, we link the foreign key with the primary
key. From that point on, any value in the foreign key field should match a value from the
primary key field in the other table.

So foreign keys are an essential part of relational database systems. Without foreign keys, we
wouldn’t be able to match data in one table with data from another. At least, we wouldn’t be
able to match it with full confidence that the results are correct. This is because the value of the
primary field is a unique identifier.

Therefore, the value of the foreign key field is the unique identifier for the record in the other
table. So we know that the foreign key value refers to only that single record.

You might also like