|
ABAP Tutorials -
ABAP Data Dictionary
|
|
Written by admin
|
|
Wednesday, 10 September 2008 17:27 |
|
Relationships between tables are represented in the ABAP Dictionary by foreign keys. A foreign key is a field (or combination of fields) that represents the primary key of another table. For example, if table YORDERS has a field CUSTID indicating which customer placed the order, that field could be established as a foreign key (assuming that CUSTID was the primary key of the YCUSTOMERS table). Note that CUSTID is not necessarily part of the primary key of the table YORDERS.
- The table that is referenced by the foreign key (in our example, YCUSTOMERS) is called the check table. The check table is also known as the “referenced” or “parent” table.
- The table that contains the foreign key fields (in our example, YORDERS) is called the foreign key table. The foreign key table is also known as the “dependent” or “child” table.
Foreign keys are used for:
- Maintaining data integrity
- Providing additional texts in the online help system
- Creating other dictionary objects that are defined over multiple tables (such as views)
Maintaining data integrity is probably the most important reason to use foreign keys. With foreign keys, it is possible to prevent values from being entered into one table that do not already exist in another table. For example, it becomes impossible to enter orders that do not have valid customer numbers. In this way, foreign keys are similar to domain value tables.
Let’s review some of the key terminology relating to foreign keys. Value table: The table containing the set of allowed values attached to a domain. Check table: The table that is referenced by a foreign key. A check table is either identical to a value table, or is another table containing a subset of the records in a value table. Foreign key table: The table containing fields that are the primary key of the other table. The foreign key table is also known own as the “dependent” or “child” table. Using Foreign Keys, you can : Create value checks for input fields and Link several tables in a view or in a lock object. To see a full list of the allowed values for a screen field, place the cursor inside the field and hit the F4 key.
Cardinality
When creating foreign key relationships, you should always specify the cardinality of that relationship. Here is a reminder of the possible values for each side of the n : m notation that SAP uses to specify cardinality. For the left side: n = 1 Each record in the foreign key table refers to exactly one record in the check table. n = C Each record in the foreign key table refers to zero or one records in the check table. For the right side: m = 1 Each record in the check table has exactly one dependent record. m = C Each record in the check table has a zero or one dependent records. m = N Each record in the check table has at least one dependent record. m = CN Each record in the check table has zero, one, or many dependent entities.
If the primary key of a check table has multiple fields (i.e. it has a composite primary key), some type of assignment must be made for each field when creating a foreign key relationship. The options include: - Creating a field-by-field assignment. Every primary key field in the check table is matched with a field in the foreign key table.
- Using a partial foreign key. Some fields will not be a determining factor in deciding what check table records provide acceptable values for the foreign key field being checked.
- Using a constant foreign key. Only check table records with a particular constant (literal) value in a particular field provide acceptable values for the foreign key field being checked.
|