You are on page 1of 4

A primary key is a field or group of fields that uniquely identify a record in a table.

Primary key
fields cannot be NULL and cannot contain duplicate values.
If you want to link two tables, then primary key of one table will be added to another table where
primary key of first table will be become the foreign key of second table.
Consider the following two tables.
Department Table
Department_ID (Primary
Department_Name
Key)
01
Computers
02
Physics
03
Electronics
Employee Table
Employee_ID
(Primary Key)
001
002
003
004

Name
Jim
Jack
Robin
Raj

Place
New York
London
Sydney
Bangalore

If you want to link department table and employee table, then add the primary key of department
table i.e. Department_ID to employee table. Department_ID becomes the foreign key of
employee table and Department table becomes the check table.
Employee table after creating foreign key.
Employee_ID
(Primary Key)
001
002
003
004

Name
Jim
Jack
Robin
Raj

Place
New York
London
Sydney
Bangalore

Department_ID
(Foreign Key)
01
01
02
03

The main purpose of the foreign key is data validation. This will not allow entering a
Department_ID in the employee table that is not there in the department table. For example if
you try to create a record in employee table with Department_ID as 04, it will throw an error.

The purpose of the foreign key is to validate the data that is being entered into a table by
checking entries in a check table. Foreign keys are checked by the front end user interface only
and it is not checked if you issue a direct a SQL statement to update the database.
Follow the steps given below to create a foreign key in SAP table.
Step 1: Open the table in Data Dictionary (SE11) for which you want to create a foreign key.
Select the field for which you want to create the foreign key and press Foreign Keys button.

Step 2: In the popup window enter the check table name and press Generate proposal button.

Step 3: The system proposes the foreign key relation based on the domain. Check that the
foreign key relationship proposed by the system is correct and press copy.

Foreign key is created, now save and activate the table. To check the foreign key go to menu path
Utilities->Table Contents->Create Entries.

Try to create an entry in Zemployee table with Department ID that is not there in the
Department table. Enter the values and try to save the entry.

Observe the error message in the status bar. The user interface does the foreign key validation
before creating the entries.

You might also like