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: 

help

Former Member
0 Kudos

what is key?

what is primary key?

what is composite primary key?

what is foreign key?

please explain me clearly with examples?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

For example you are creating a Transparent Table which contains different fields(columns) to allow you to store and manage data records in a structured way, you hav to declare table fields as key fields.

<b>Key</b> is to uniquely identify the data records of a table. This key can be Primary Key Or Composite Primary Key ...

In the Table SCARR, fields MANDT & CARRID are declared as Key Fields. In this case we can't identify the table unique data records using either of the keys coz different clients(MANDT) may have the same set of CARRID thats why we have to use combination of these fields as Key fields.The combination of these two keyfields are nothing but <b>Primary Key</b> using which we can identify unique records in the table.

Generally Primany Key can be a single key field or combination of key fields. Here in this case Primary Key is combination of MANDT & CARRID. If a Primary Key is made up of more than one key field it can also known as <b>Composite Primary Key</b>.

<b><u>Foreign Key :</u></b>

For example you have 2 tables SCARR and SPFLI. Now you know that combination of MANDT & CARRID is a Primary Key in the table SCARR that means for this combination you will have only one data record in this table.

For the same key fields combination if you have multiple data records in the other table (SPFLI), the same field combination in SPFLI table is known as <b>Foriegn Key</b>. As i said before like Primary Key even Foreign Key can be single field or combination of fields. In this case Foreign Key is the combination of MANDT & CARRID.

Ex: Primay Key - Foreign Key relationship using single key field

Tables T1 & T2 have Primary-Foreign Key relationship based on Key K1

If you have K1(single key field) as Primary Key(i.e, for a given value of K1 will have only one unique data record in the table T1) in table T1, then in table T2 will have multiple records for the same key K1 (ie.,K1 declared as Foreign Key in table T2)

Hope this clarifies your basic doubts on Keys, then you can go to <u>help.sap.com</u> for proper examples.

!!! Reward points if this answers your query !!!

Regards, Srikanth

4 REPLIES 4

Former Member
0 Kudos

Hi,

1.A Key is field in the table which help u to find the dats using that.

2.Primary key is one only it's created when you create a table with KEY Fields.

3. In SAP almost all the tables are having the composite primary key. Because in a client dependent table the primary key a combination of MANDT and Other fields..

I will give you some examples..

tables with out composite primary key : T000.

tables with composite primary key : MARA, MARC, VBAK, VBAP.........

4.Foreign key

A foreign key creates a link between two tables T1 and T2. Every primary key field from T1 (check table) is assigned a field from table T2 (foreign key field). The fields from T2 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.

Foreign keys are also the foundation for defining lock objects, maintenance views and help views. These objects contain fields from several tables. All the tables used in such an object must be linked with foreign keys.

For example Table SFLIGHT.

mandt,carrid,connid,fldate are the keys in the table.

mandt is the primary key.

carrid,connid are the foreign keys with check table spfli.

Hope u understood the scenario.

Regards,

Priyanka.

Former Member
0 Kudos

You can specify the primary key for your container-managed entity bean when you create the bean, or later when you develop the bean business logic.

The bean’s primary key can be a:

· The foreign key table is a text table for the check table, that is the key of the foreign key table only differs from the key of the check table in that it has an additional language key field. This is a special case of the type Key fields/candidates.

reward poitns if it is usefull ..

Girish

Former Member
0 Kudos

hi

good

Primary Key-->Each SAP DB table has a primary key. The primary key is either defined by the database user or generated internally. A user-defined primary key can consist of multiple columns.

Composite Primary Key->

create table a

(

a number,

b number,

c varchar2(20),

primary key(a,b)

);

Foreign Key->

A pointer is called a "foreign key" in database terminology. A foreign key is a value that points to a particular row (the primary key) of another table. In our person & address example where we have three people with the same address, all three people would have the same foreign key, pointing to the same address in our addresses table. Thus, the address only has to be entered once!

thanks

mrutyun^

Former Member
0 Kudos

For example you are creating a Transparent Table which contains different fields(columns) to allow you to store and manage data records in a structured way, you hav to declare table fields as key fields.

<b>Key</b> is to uniquely identify the data records of a table. This key can be Primary Key Or Composite Primary Key ...

In the Table SCARR, fields MANDT & CARRID are declared as Key Fields. In this case we can't identify the table unique data records using either of the keys coz different clients(MANDT) may have the same set of CARRID thats why we have to use combination of these fields as Key fields.The combination of these two keyfields are nothing but <b>Primary Key</b> using which we can identify unique records in the table.

Generally Primany Key can be a single key field or combination of key fields. Here in this case Primary Key is combination of MANDT & CARRID. If a Primary Key is made up of more than one key field it can also known as <b>Composite Primary Key</b>.

<b><u>Foreign Key :</u></b>

For example you have 2 tables SCARR and SPFLI. Now you know that combination of MANDT & CARRID is a Primary Key in the table SCARR that means for this combination you will have only one data record in this table.

For the same key fields combination if you have multiple data records in the other table (SPFLI), the same field combination in SPFLI table is known as <b>Foriegn Key</b>. As i said before like Primary Key even Foreign Key can be single field or combination of fields. In this case Foreign Key is the combination of MANDT & CARRID.

Ex: Primay Key - Foreign Key relationship using single key field

Tables T1 & T2 have Primary-Foreign Key relationship based on Key K1

If you have K1(single key field) as Primary Key(i.e, for a given value of K1 will have only one unique data record in the table T1) in table T1, then in table T2 will have multiple records for the same key K1 (ie.,K1 declared as Foreign Key in table T2)

Hope this clarifies your basic doubts on Keys, then you can go to <u>help.sap.com</u> for proper examples.

!!! Reward points if this answers your query !!!

Regards, Srikanth