Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find ForeignKey in SAP Dictionary tables?

Table AUFK has a column called ADRNRA.

The values in column ADRNRA have matching rows in table ADRC.

In this case I found ADRC because someone told me the solution.

Maybe I am blind, but it looks like AUFK-ADRNRA is no explicit foreign key.

Is there a way to do introspection and find the matching related table (in this example ADRC) with a script or with SQL?

like image 696
guettli Avatar asked Dec 10 '18 14:12

guettli


People also ask

What domain is Foreignkey?

What Does Foreign Key Mean? A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It acts as a cross-reference between tables because it references the primary key of another table, thereby establishing a link between them.

What is a check table and foreign key in SAP ABAP?

A foreign key allows you to assign data records in the foreign key table and check table. By using the entries in the foreign key fields, one record of the foreign key table uniquely identifies one record of the check table. Check Field and Value Check. One of the foreign key fields is marked as the check field.


2 Answers

Indeed the field is not marked as foreign key of the field ADDRNUMBER of the table ADRC but should you have looked at the domain of the field ADRNRA of the table AUFK you would have found the reference in just a few seconds.

The domain (in this case AD_ADDRNUM) has an explicitly specified value table which is, surprise, surprise, ADRC.

Domain

Value table

like image 167
Jagger Avatar answered Oct 12 '22 09:10

Jagger


Nope, there is no easy way to reveal foreign keys in an SAP system. :-(

Database management systems don't require you to model foreign keys. You only do that when you want to enforce constraints such as "must refer to a row in the other table" and automatic reactions such as cascading deletion ("delete this row if its 'parent' row in the other table is deleted").

Older SAP applications implemented constraints and reactions like these in the application layer, i.e. the ABAP code. They didn't have a need for modeled relations, and thus people simply didn't model them.

Common means to identify foreign key relationships are:

  • Asking experienced users or the people who designed the database model.
  • Guessing, from identical column names and data types.
  • Inspecting the application code and seeing what tables it joins.

There are also some experimental machine learning algorithms that try to detect relations; google "foreign key discovery" for more information.

As @Sandra points out in her comment, newer SAP applications reveal a lot more relationships because they use CDS views. Modeling relationships there has direct benefits such as automatically generating associations in OData services, selecting along relations, and using them to model business objects.

like image 22
Florian Avatar answered Oct 12 '22 10:10

Florian