Foreign Key

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Foreign Key

• In ABAP, "foreign" typically refers to a concept


known as "foreign key". A foreign key is a field in a
database table that refers to the primary key of
another table, thereby establishing a relation
between the two tables.
• The purpose of a foreign key is to maintain
referential integrity in the database, which
means that the relationships between tables are
consistent and accurate. A foreign key ensures
that a row in a table that references another
table cannot exist unless the referenced row in
the other table exists. In other words, a foreign
key guarantees that the data in one table is
related to the data in another table in a
meaningful way.
How to establish relationship between two table

In order to establish a relationship between two tables, you


need to define a foreign key in one table that references the
primary key or a unique key of the other table. Here are the
basic steps to create a relationship between two tables in ABAP
using the Data Dictionary.
1. Open the Data Dictionary (SE11) and select the
option to create a new table.

2. Define the fields for the first table and save the
table.

3. Create a new table for the second table, defining


the fields as needed.

4. In the second table, create a new field that will


serve as the foreign key. This field should be
defined with the same data type and length as the
primary key in the first table.
5. In the Data Dictionary, navigate to the "Foreign
Keys" tab for the second table.

6. Create a new foreign key by entering the name of


the foreign key field and the name of the primary
table.

7. Specify the primary key field in the primary table


that corresponds to the foreign key field in the
second table.

8. Define any additional properties for the foreign


key relationship, such as whether it is mandatory
or optional.

9. Save the foreign key relationship.

Once the foreign key relationship has been established, you can use it to
retrieve data from both tables. For example, you could use a JOIN
statement to combine data from both tables based on the foreign key
relationship. This makes it easy to retrieve and analyze data from
multiple tables in a database.
Cardinality and semantic attributes of foreign key

Cardinality and semantic attributes are two important


concepts to consider when defining foreign keys in ABAP.
• Cardinality: Cardinality describes the relationship
between the two tables in terms of the number of
records that can exist in each table. There are three
types of cardinalities:
1. One-to-one (1:1): Each record in the first table can
be associated with only one record in the second
table, and vice versa.
2. One-to-many (1:N): Each record in the first table
can be associated with one or more records in the
second table, but each record in the second table
can be associated with only one record in the first
table.
3. Many-to-many (N:M): Each record in the first table
can be associated with one or more records in the
second table, and vice versa.
When defining a foreign key, you need to specify the
cardinality of the relationship between the two tables.
• Semantic attributes: Semantic attributes describe
the meaning or purpose of the relationship between
the two tables. There are two types of semantic
attributes:
1. Identifying relationship: A foreign key relationship
is an identifying relationship if the foreign key field
is part of the primary key of the second table. This
means that the foreign key uniquely identifies
each record in the second table.
2. Non-identifying relationship: A foreign key
relationship is a non-identifying relationship if the
foreign key field is not part of the primary key of
the second table. This means that the foreign key
does not uniquely identify each record in the
second table.
When defining a foreign key, you also need to specify
whether the relationship is identifying or non-identifying.
By considering both cardinality and semantic attributes
when defining foreign keys, you can ensure that your
database relationships are accurate and efficient, and that
your data is stored and retrieved in a meaningful way.

You might also like