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 creation

Former Member
0 Kudos

1) can i set foreign key for if ihave data element in one table and direct type

2) can i set foreign key for if ihave data element of float and intiger in other table

3)can i create Primary key If i have Domain of one table with other table field has direct type <b>ie</b> (not doinan created directly)

3 REPLIES 3

Former Member
0 Kudos

Just try creating all these. It will be a good learning exp for you and you will get the answers to your query

Regards,

Atish

Former Member
0 Kudos

Hi rambabu,

For creating Primary and foreign key relations both the table fields should be declared with same data element. It is the must.

Former Member
0 Kudos

Hi

<b>Foreign Key Definition</b>

A foreign key creates a link between two tables T1 and T2. Every primary key field from T2 (check table) is assigned a field from table T1 (foreign key field). The fields from T1 assigned to primary key fields are marked as foreign key fields.

The most important function of the foreign key is the support of data integrity. The foreign key fields can only accept values which appear in the primary key of the check table. During input the values of the foreign key fields can thus be checked against the entries of the assigned key fields of the check table.

In the ABAP Dictionary, such relationships between two tables are called foreign keys and they must be defined explicitly for the fields.

Foreign keys are used to ensure that the data is consistent. Data that has been entered is checked against existing data to ensure that it is consistent.

<b>Foreign Key concept with an Example</b>

Example: In this example, the foreign key table is table SBOOK. The purpose of the foreign key is to ensure that only valid counters of carriers can be assigned to a booking. Check table SCOUNTER contains exactly this information. Each counter is identified with three key fields in this table: MANDT, CARRID, and COUNTNUM.

In order to define the foreign key, these three fields are assigned to fields of the foreign key table (foreign key fields) with which the input to be checked is entered on the screen. In table SBOOK these are the fields: MANDT, CARRID, COUNTER. The entry is accepted if it represents a valid counter; otherwise the system will reject it.

The foreign key is defined for field SBOOK-COUNTER (check field), which means that the entry in this field is checked. Field COUNTER is therefore called the check field for this foreign key.

<b>Example foreign key</b>

A foreign key is defined for field COUNTER, table SBOOK, resulting in the following field assignment: Check table Foreign key table SCOUNTER-MANDT SBOOK-MANDT SCOUNTER-CARRID SBOOK-CARRID SCOUNTER-COUNTNUM SBOOK-COUNTER

<b>Violation of Foreign Key check</b>

The flight cannot be booked because American Airlines (AA) does not have a counter 8.

No data record is selected in table SCOUNTER for the entries in the example. The entry for table SBOOK is rejected.

<b>Advantage of Foreign Key with an Example</b>

The check table is the table whose key fields are checked. This table is also called the referenced table.

An entry is to be written in the foreign key table. This entry must be consistent with the key fields of the check table.

The field of the foreign key table to be checked is called the check field.

Foreign keys can only be used in screens. Data records can be written to the table without being checked using an ABAP program.

Example: A new entry is to be written in table SPFLI (flight schedule). There is a check whether the airline carrier entered is stored in table SCARR (carrier) for field SPFLI-CARRID. The record is only copied to table SPFLI (foreign key table) if this is the case. A foreign key is defined for field SPFLI-CARRID (check field), i.e. the checks are on this field. The corresponding check table is table SCARR with the primary key fields MANDT and CARRID.