ABAP database Table foreign key relationship

Foreign keys are required to ensure data consistency through input checks by checking the data against input checks.

Foreign key is a Field in one Table that is connected to another table via foreign key relationship and the purpose is to validate the data being entered in one table ( Foreign key table ) with a valid set of values from another table ( Check table ).

Check field is the field in foreign key table whose value is to be checked.

Foreign key requirements

The field in the check table should be primary key. A foreign key is a combination of fields in a table called as the foreign key table, and this same set of fields has to be primary key in another table known as the check table. When foreign key relationship is created between foreign key table and the check table, the input value of foreign key field is checked against existing data of primary key field in check table.

The Domain names of the field in the Foreign Key table and for the field in the Check table should be same.

Steps to create foreign key relationship

Create foreign key relationship between employee table ( ZAMAMRMN_EMP ) and department table ( ZAMARMN_DEPT ) for the field DEPARTMENT.

Employee table
Department table
  • Open Employee table in change mode
  • Select DEPARTMENT field and choose Foreign keys button.
  • In the create foreign key dialog box, enter Short text, Check table name and click Create proposal.
  • Choose Copy
  • Save, check and activate the employee table

Test the foreign key

First, make sure that the department table has some entries.

Next, to test the foreign key relationship, create data for the employee table. In the employee table, from menu bar choose Utilities -> Table Contents -> Create entries.

Note that for the department field, the system gives proposal of input values. Try to enter some incorrect value and hit save button. The system will throw an error message ‘Entry does not exist in department table (check entry)’. Next, enter a valid department and save to create entry.