Foreign Key in SAP ABAP
What is Foreign Key?
Below are the two requirements for creating a foreign key relationship:
What is a Primary Key?
A primary key is a field that uniquely identifies the rows in a table by one or more columns. The primary key field cannot have a NULL value or duplicate values. In order to establish the relationship between two tables or to link the tables, the primary key of the first table (referenced table) will be added to another table (dependent table), and it will become the foreign key for the second table.
Concept of Foreign key Relationship:
The foreign key relationship is about relating or connecting two different tables in order to avoid redundancy and perform table validations in ABAP.
We can understand it by an example. Suppose there are two tables; Table1(foreign key table or dependent table), Table2(Check Table or referenced table). The tables are given below:
All the records are successfully saved in Table1. And Below, we will save records in Table2.
As we can see, it has two records that are successfully saved. But if we try to store another record in this table that is not present in Table1, such as 5 Dept3 Assist. Manager
Then due to the foreign key relationship, we are not allowed to add this record in Table2.
Table validation in ABAP:
Table validation is a technique by which we can restrict the invalid entries in our table. There are two ways to perform the table validation in ABAP:
Note: Here, we will understand the Field level validation with the help of the Check table concept.
Check Table Concept(Field Level Validation)
In the Check table concept, we consider two tables; Check table and Foreign key table.
The check table is the table that stores the master data.
The other table, which is linked with the check table to validate its own field data, is called a foreign key table. The value table is developed at the domain level and primarily used for data integrity and consistency.
In the below diagram, we can understand the check table concept:
As we can see in the above diagram, there are two tables, T1(Foreign key table) and T2(Check Table). The T2 table contains two primary key fields Field5 and field 6, and the T1 table includes the two foreign key fields, Field 2 and Field 4, with one primary key Field 1.
So, we can connect the two tables with the primary keys of T2(Field 5 and Field 6) to the Foreign key fields of T1(Field 2 and field 3).
Here one point we need to remember that the data type and length of all the linked fields should be the same, then only the foreign key relationship can be maintained.
How does the input check work?
It can be understood by the understanding of the below concept:
Triggering Foreign Keys
When we enter a value in the foreign key table, immediately, the foreign key gets triggered. Triggering of the foreign key is initiated using the SELECT statement, which checks for the matching rows in the check table. If there is no matching occurred in the table, it displays a standard message which indicates that the entered value is invalid. Apart from the SELECT statement, the foreign key can be triggered using a function key, a button, or a menu item.
Below are some other concepts related to the Foreign key:
Compound Foreign Keys:
A foreign key consisting of two or more fields is called a compound foreign key. To apply checks for compound foreign keys, the system compares two fields in the foreign key table against the two fields in the check table.
The field that defines the compound foreign key is called a check field, and it must be empty, else the compound foreign key will not get triggered.
Generic and Constant Foreign Keys
As we know, to create a foreign key, all the primary keys of the check table must be included in a foreign key relationship. But in some cases, we just want to perform validation against selected fields only, and other fields can be excluded, so for such cases, we can use the generic foreign keys.
We can also assign a constant value to the key field of the check table, which will allow us to check the values against a constant value of the check table.
Semantic Attributes of Foreign Keys
The foreign key relationship between two tables can be specified more precisely by defining the Cardinality and Type of foreign key fields. Although providing this information is optional and not used in the value check. It is mostly used for documentation purposes.
The cardinality of a Foreign Key
The cardinality of the foreign key is used to specify the allowed rows in the foreign key table for the corresponding value in the check table. It is defined while creating the foreign key relationship. It can be expressed as X: Y relationship, where X is used for the check table, and Y is for the foreign key table.
The possible values for X can be:
The values for Y can be:
Example: If the cardinality is given as 1:N, it means there is exactly one record that exists for each record of the foreign key table.
Foreign Key Field Type
Foreign key field type describes the meaning of the foreign key field in the foreign key table. Below are the different types of foreign key field:
Creating Foreign keys in ABAP
Below are the steps to create the foreign key relationships in ABAP database tables:
Customizing Error message in the foreign key check
If the check table validation or foreign key check gets failed, a standard error message is generated. This error message can be replaced with a customized error message. It can be done by using the SE91 SAP transaction code.
For Videos Join Our Youtube Channel: Join Now
Help Others, Please Share
Learn Latest Tutorials
B.Tech / MCA