How to Create a Foreign Key in SAP ABAP

Are you aware of creating a foreign key in SAP ABAP? This SAP ABAP tutorial explains the role of the foreign key, what is required to create a foreign key and finally, we will see how to create a foreign key in SAP ABAP.

You can read this complete SAP ABAP article to learn How to Create Table in SAP ABAP [With Columns].

Role of Foreign Key in SAP ABAP

  • The main role of SAP ABAP foreign keys is to link or connect the data in the tables based on user requirements.
  • One field in Table 1 is a primary key and the same field in Table 2 will become a foreign key. For example, if a user wants to choose particular employee details with different tables with different records, the foreign key concept will be helpful.

What is Required to Create a Foreign Key

Creating a foreign key in the SAP ABAP requires two tables with the same ID. Generally, if table 1 has employee ID, name, and phone number then table 2 has also employee id(mandatory) the other fields we can give as per our wish.

It will be an easy and simple method to create a foreign key if both tables have been properly created and activated with technical settings and enhancement categories.

How to Create a Foreign Key in SAP ABAP

Make sure you have created and activated two tables to create a foreign key in SAP ABAP. Follow the below steps to create a foreign key concept in SAP ABAP.

Step-1:

  • Go to the first table by clicking the table name in se11 and click Change to start the foreign key concepts. Here my 1st table is ZCOMP_EMPLOYER.
1ST TABLE View in Foreign key

Step-2:

  • The table with the data elements and fields added will appear. Now simultaneously click the foreign key icon (Similar to the key) and EMID to create a foreign key.
Choosing foreign key in table 1

Step-3:

  • Click again on the foreign key icon, to fill the necessary details like short text as Foreign key declaration(since the naming short text is as per the user’s wish), in the check table we have to give our second table name then click Create Proposal options.
  • Create Proposal checks from which fields from this table match with the first table.
  • Once we click on that, the two table details will visible with the client field and employer ID.
  • Give the Foreign Key field type as key fields/candidates then give the cardinality as 1 and CN from the list of options.

Note: Cardinality means foreign key relationship related to the number of dependent records or reference records. n=1–> Only one record is assigned to check the table for all records in the table. CN denotes there can be any number of records for the table.

Foreign key concept in SAP ABAP

Step-4:

  • Once you click copy, come back to the table again. Make sure the table is activated if not, save, check, and activate. Now click on the graphical icon at the top of the page to see the output of the foreign key representation in the table. If the table is not activated, then this view can not be seen.
Graphical View in Foreign key concept

This is the way to create a foreign key concept in SAP ABAP.

You may also be interested in below tutorials:

Conclusion

This is the simple and standard method of creating a foreign key in SAP ABAP. It is mandatory to be two tables should be added with the proper fields, saved, and activated. Below are the main topics covered in this article.

  • Role of Foreign Key in SAP ABAP
  • What is required to create a foreign key
  • How to create a foreign key