Do you Know ?

Foreign Key creation in SAP ABAP.

Foreign key relationship in SAP ABAP.

How to define foreign key relationship in SAP ABAP
How to create foreign key in SAP ABAP?

Here it is !

Before we just kick start with foreign key creation, let me tell you the basics  you need to know about the foreign key relationship.

3 simple steps no big deal.

1)  In order to have a Foreign key relationship you need 2 tables.
2)     A common field in both the tables.
3)     Foreign key acting on one table is actually a primary key in another table

Okay, lets create the table with primary key in it.

Goto TCode SE11, and select  database table and keep program as let say ZEmployee_F
As per naming standard/conventions provide table name with starting letter ‘Z’.


And  select option “Create”.

Maintain short description : Demo on Foreign key
Delivery class : A.
Table view maintenance : Display maintenance allowed.

Then select fields tab, provide field name say EMPNO and check [X] the key, i.e nothing but 
You are telling it’s a primary key.

Provide data element ( if you don’t know how to create data element and domain jump to this post HERE ) ,hope you would have learned the data element and domain creation J,

Activate the domains and data elements.

Similarly create all the required fields.

Once you are done with this part, save the program, if you wish to save it in your local package then click on local object or simply provide “$tmp” and save it.

If you wish to save your program in custom package, then before saving you should know how to create a package , click here to know how to create a package.



You would have encountered with window saying “Application table should be client-sepecific” ,don’t worry it is not an error J , as you have not maintained mandt ( Client)
That’s why it is informing you same.

In SAP, if you go to any table (almost) every table will have mandt field as composite key (allows duplicate values).

Just ignore and click on right arrow or ENTER.

Now  select “Goto” option in the menu bar and select “Technical Settings” as shown below.


APPL0 -  Master data,transparent tables
Size category – 0 (zero)

Save!

Tip: In SAP Zero will have dot inside the circle,where as alphabet it won’t.

That’s it you are almost done J.

Activate the table, CTRL+F3 and ENTER.


Now you will see this window just click on “No” to ignore the warnings, this is because you are not saving the program in package.

Now you are done with Table creation.

Go ahead and provide the table entries.


Select “Create Entries” option to provide details.


Similarly, create required entries and click on save.


You are done with table 1 now lets create table 2.

Go to SE11 and create table with name let say “Zdept_f.”

Provide the basic things like short description , Delivery class as you are already would learned this while creating table 1.

First provide the deptno field and check the primary key box.

Note: Every table must and should have at least one primary key.

Keeping deptno primary key here in this case ,

Here comes the difficult part of this entire theme of foreign key , provide empno which is gonna be the foreign key in this table.



Note: provide the same data element which is provided in zemployee_f table.

Select the empno as shown in picture and then select foreign key symbol as shown in picture.


Now provide the table name and click on Generate proposal .

Press ENTER or click on “Copy” button.


It should show this message at the bottom of the SAP screen.

Settings:

APPL0 -  Master data,transparent tables
Size category – 0 (zero)

Save!

That’s it your almost done, activate the table now with CTRL+F3.

Select utilities è table contents è create entries.


Save !

Now provide the empno number which it doesn't exists in Zemployee_f table,


At bottom of the screen it shows this.


If you observe keenly ,when you are started entering the wrong empno which is not existing in the table1 ,it turns in RED color.


That’s all folks J.