Show TOC

 Foreign KeysLocate this document in the navigation structure

Use

You use foreign keys to define relationships between tables in the ABAP Dictionary, create value checks for input fields and link several tables in a view or a lock object .

Features

Field Assignment in the Foreign Key

A foreign key links two tables T1 and T2 by assigning fields of table T1 to the primary key fields of table T2. In the following figure you can see the relationships between the fields of these two tables.

Table T1 is called the foreign key table (dependent table) and table T2 the check table (referenced table). The pair of fields for the two tables must have the same data type and length. One field of the foreign key table corresponds to each key field of the check table. This field is called the foreign key field.

A foreign key allows you to assign data records in the foreign key table and check table. By using the entries in the foreign key fields, one record of the foreign key table uniquely identifies one record of the check table.

Check Field and Value Check

One of the foreign key fields is marked as the check field. The foreign key relationship is maintained for this field.

When you make an entry in the check field, the system checks whether the check table contains a record with the key defined by the values in the foreign key fields. If there is such a record, the entry is valid. Otherwise the system rejects the entry.

The figure above is an example of the usage of check fields. In this example the entry Field2 = 2 and Field4 = 2 would be rejected since T2does not contain a record with the key Field5 = 2 and Field6 = 2.

You have the option of not checking against all the key fields of the check table. To exclude fields of the foreign key table from the assignment of the fields to the check table, use generic and constant foreign keys .

How the Input Check Works

A SELECT statement is generated from the definition of the foreign key. If you make an entry in the check field, the system submits this SELECT statement. If the system finds a suitable record of the check table, the entry is valid. Otherwise the system rejects the entry.

The corresponding SELECT statement has the following form for the foreign key table shown in the figure above:

SELECT * FROM T2 WHERE T2-FIELD5 = T1-FIELD2 AND T2-FIELD6 = T1-FIELD4.

Note

This SQL statement is a pseudo code that explains the corresponding functionality. The syntax of this statement is not the same in ABAP.

A screen entry for check field Field2 is valid if the check table contains a record with the entries made in the screen for Field2 and Field4 as key.

Example

Table SBOOK in the flight model contains the customer's flight bookings for a carrier. The flight bookings can be made by a travel agency or directly at the carrier's sales counter. If the booking was made at a counter, its number is stored together with the booking in field COUNTER in table SBOOK.

You must make sure that only correct counter numbers can be entered in table SCOUNTER. The necessary value check can be defined by creating a foreign key for check field COUNTNUM. This is explained in the figure below.

See also:

Multi-Structured Foreign Keys

Semantic Attributes of Foreign Keys

Creating Foreign Keys