The Concept Of Foreign Key

Explain the concept of foreign key.

Foreign key is nothing but simply we can say, "a field in one table related to field in another table".  See the below example:-

Consider, there are two tables like emp_personal and emp_official.

In emp_personal table , the fields are empid, empname, dob for example. In this table, empid is the primary key field.

In emp_official table, the fields are empid, dept, designation for example. In this also, empid is the Primary key or may not be a Primary key field.

Now we relate these two tables using the foreign key relationship. Foreign key relationship is used to avoid the redundancy mainly.

Now, emp_personal is the Check table (Because it holds the empid field, which has the primary key...)

emp_official table is the Foreign key table (Because it holds the empid field, which may or may not be a Primary key).

Now, the foreign key relationship is given. (click the KEY Like icon in ABAP Dict - Database Table : Emp Official )

Tables are ready to enter the records...for example create some 5 records in the emp_personal table . 

Table : emp_personal :   Primary Key - empid

empid       empname        empdob             Status 
---------     --------------    ------------          ---------------------
   1            AAA               1.1.1990           Record saved successfully
   2            BBB                2.2.1990           Record saved successfully
   3            CCC                3.3.1990           Record saved successfully
   4            DDD               4.4.1990           Record saved successfully
   5            EEE                5.5.1990            Record saved successfully

Records are saved in the table : emp_personal.

Now, we are trying to create records in emp_official table.

Table : emp_official :   Primary Key or not a Primary Key - empid

empid        dept                designation        Status 
---------     --------------    ------------          ---------------------
   1            Dept1              S.E                    Record saved successfully
   2            Dept2              S.SE                  Record saved successfully

Now, if you try to create the record which is not in the table emp_personal, which has the empid = 6. But it'll not allow us to create the record 6, because of the foreign key relationship between these two tables....

   6            Dept6               P.M                   Record doesn't exist......

Now, the foreign key relationship helps us to avoid the duplicate entry.
Foreign key relationship is possible between the tables having at least one same type of field.  Also, the Technical characteristics of the fields should be same. This is done in the value table.

This is the concept of Foreign key.

ABAP Tips

Read Also
Compound, Constant and Adapted Foreign Keys

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Main Index
SAP ERP Modules, Basis, ABAP and Other IMG Stuff

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.