Sunteți pe pagina 1din 21

IBM Global Services

Foreign Key Relationships

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Objectives
The participants will be able to:
Define and Use Foreign Keys
Understand the Prerequisites for Constructing Foreign Key Relationships
Interpret Cardinality and Foreign Key Field Types
Create Foreign Key Relation

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Definition of Foreign Keys


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.

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Uses of Foreign Keys


Maintain data integrity
Provide help texts
Create aggregate dictionary objects

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Prerequisites for Constructing Foreign Key Relationships


The foreign key field and the primary key of the check table must share the same
domain.

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Foreign Keys: Key Terminology

Value Table

Check Table

Foreign Key Table

The table containing the set of


allowed values attached to a domain

The table that is referenced by a foreign


key

The table containing fields that are


the primary key of the other table

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Foreign Key Terminology: Check Table = Value Table


In many cases, the value table and the check table will be the same

Check
Table

Value
Table

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Foreign Key Terminology: Check Table Value Table


Sometimes check table is another table containing a subset of the records in
a value table.

Check
Table

Value
Table

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Establishing a Foreign Key Relationship in the ABAP


Dictionary
During defining Foreign Key Relationship, system suggests a Foreign Key
Relationship for you.

Data
Dictionary

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Cardinality
When creating foreign key relationships, you should always specify the cardinality
of that relationship

n : m
1

C
N
CN

10

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Creating a Foreign Key Relationship

Foreign key push-button

11

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Maintaining a Foreign Key Relationships Attributes


Enter a description (short text).

Check table

Maintain foreign
key type
Maintain cardinality n : m

12

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Foreign Keys with Multiple Fields


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.

13

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Field-By-Field Assignment
When performing a field-by-field assignment, all key fields in the check table
are used to determine valid entries in the foreign key table.

Foreign Key Table

14

Foreign Key Relationships |

Check Table

Dec-2008

2005 IBM Corporation

IBM Global Services

Partial Foreign Keys


When creating a partial foreign key, certain fields are flagged as generic.
These fields are ignored by the system when validating records that are inserted
into the foreign key table.

15

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Constant Foreign Keys


When creating a constant foreign key, certain fields are assigned a literal value
Only records in the check table with that value in the corresponding field are
used to determine whether records in the foreign key table are valid.

O
I
B

16

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Combined use of Partial and Constant foreign keys

Generic foreign key

Constant foreign key

17

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Demonstration
Creation of a new transparent table for holding the payroll data for employees in
an organization and establish a foreign key relationship with the employee table
created for the previous chapter.

18

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Practice

19

Creation of a new transparent table for holding the payroll data for employees in
an organization and establish a foreign key relationship with the employee table
created for the previous chapter.

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Summary
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.
Foreign keys maintain data integrity & provide help texts.
In order to establish a foreign key relationship in SAP, the fields involved must
share the same domain.

20

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

IBM Global Services

Questions
What is a foreign key ?
What is the need to define a foreign key relationship ?
What is meant by cardinality in a foreign key relationship ?

21

Foreign Key Relationships |

Dec-2008

2005 IBM Corporation

S-ar putea să vă placă și