Compound, Constant and Adapted Foreign Keys

Explain what do you meant by: 
1. Compound Foreign Keys
2. Constant Foreign Keys
3. Adapted Foreign Keys

Answer:

Adapted Foreign Keys

Foreign key fields of a compound foreign key do not all have to reside in the same table. If they do not, this is known as an adapted foreign key. For example, assume that the country code for the vendor is not stored at all in ztxlfa1, but instead in ztxlfa1cc. When you create the foreign key on the region field, you would change the check table field for land1 to ztxlfa1cc-land1. The validation on the regio field would check to see whether the combination of mandt and regio existed in ztxt005s and whether land1 existed in any row of ztxt005cc. 

Constant Foreign Keys

Foreign key fields of a compound foreign key do not all have to reside in the same table. If they do not, this is known as an adapted foreign key. For example, assume that the country code for the vendor is not stored at all in ztxlfa1, but instead in ztxlfa1cc. When you create the foreign key on the region field, you would change the check table field for land1 to ztxlfa1cc-land1. The validation on the regio field would check to see whether the combination of mandt and regio existed in ztxt005s and whether land1 existed in any row of ztxt005cc. 

It is not always advisable to check a foreign key against all the key fields of the check table. This is true for example for time-dependent check tables and for check tables whose version number is a component of the key. 

You can use generic foreign keys in these cases. Fields are excluded from the assignment to the key fields of the check table here. The check is only against the remaining key fields.

You can also assign a constant value to a key field of the check table. In this case you only have to check against the specified constant. You can use this check if only records of the check table which contain a constant value in this key field are valid.

The corresponding SELECT statement for the screen check has the following form for the foreign key definition:
FOREIGN KEY CHECK TABLE PTAB
TABLE FTAB 

Field 6 <-----------------------> Field 1
Field 7 Field2 <------ GENERIC
Field 8 <------------------------> Field 3
Field 9 Field 4 <------ ConSTANT K

SELECT * FROM PTAB WHERE PTAB-FIELD1 = FTAB-FIELD6 AND PTAB-FIELD3 = FTAB-FIELD8 AND PTAB-FIELD4 = ‘K’.

An entry is only valid in check field Field6 if a record of check table PTAB exists containing the input value for Field6 in PTAB-Field1, the input value for Field8 in PTAB-Field3 and constant K in PTAB-Field4. 

The values entered in the input template for Field7 and Field9 are meaningless when checking against the check table. An entry with Field6 = 1, Field8 = 3 and Field9 = B would not be valid in this case since there is no record with PTAB-Field1 = 1, PTAB-Field3 = 3 and PTAB-Field4 = K in the check table!

Compound Foreign Keys

A compound foreign key is a foreign key composed of two or more fields. In other words, a check is done to compare two fields in the foreign key table against two fields in the check table. The combination of values must exist in the check table before they can be inserted into the foreign key table. However only one field on the screen will trigger the compound foreign key check. 

1) In the field maintenance screen of the table, select the check field and choose key symbol.

If the domain of the check field has a value table, you can have the system create a proposal with the value table as check table. In this case a proposal will be made for the field assignment in the foreign key.

If the domain does not have a value table or if you reject the proposal, the screen for foreign key maintenance appears without proposals. In this case, enter the check table and save your entries. The check table must have a key field to which the domain of the check field is assigned.

You can then let the system make a proposal for assigning of the foreign key fields to the key fields of the check fields. The system attempts to assign the key fields of the check table to fields of the table with the same domain. If you do not want a proposal, the key fields of the check table are listed and you must assign them to suitable fields of the foreign key table.

2) Enter an explanatory short text in the field Short text. 

The short text provides a technical documentation of the meaning of the foreign key.

3) Choose Copy. The foreign key is saved and you return to the maintenance screen for the table.

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.