Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

foreign key

Former Member
0 Kudos

How to create a foreign key and how can we check that to which check table it relates?

And when creating a foreign key and giving a Constant value getting this message u201CConstant foreign key selected (select only one alternative)u201D

Can you please guide me how to create?

1. Compound Foreign Keys

2. Constant Foreign Keys

3. Adapted Foreign Keys.

3 REPLIES 3

prasanth_kasturi
Active Contributor
0 Kudos

hi,

Creating Foreign Keys

1) In the field maintenance screen of the table, select the check field and choose keysymbol

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.

If your foreign key has more than one field then its a compound forign key i.e create a forien key on more tahn one field and its a compund foreign key

http://venus.imp.mx/hilario/Libros/TeachYrslfAbap4/ch04.htm#CompoundForeignKeys

Select Generic to remove a field from the check against the key fields of the check table. If you want to assign a constant to a foreign key field, you must enter it in the Constant field enclosed in apostrophes (for example u2018Constant u2019 ). In both cases you must remove the entries in fields For. key table and Foreign key field.

for constant keys

http://venus.imp.mx/hilario/Libros/TeachYrslfAbap4/ch04.htm#GenericandConstantForeignKeys

for adopted foreign keys

http://venus.imp.mx/hilario/Libros/TeachYrslfAbap4/ch04.htm#AdaptedForeignKeys

for more info http://help.sap.com/saphelp_nw70/helpdata/en/cf/21ea77446011d189700000e8322d00/content.htm

regards

prasanth

Former Member
0 Kudos

HI,

ADAPTED

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 (see Figure 4.12). 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 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 (see Figure 4.12). 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.

COMPOUND

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 keysymbol

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.

PLZ REWARDS POINTS IF HELPFUL,

Ganesh

Former Member
0 Kudos

Hi,

CONSTANT FOREIGN KEY:

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 = u2018Ku2019.

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!

REWARD PTS IF USEFUL

Thanks and Regards

- Rishika