Preface – This post is part of the Differences in ABAP for Interviews series.
Table of Contents
Introduction
Before discussing the difference between, let’s have a short introduction of the two.
Keys are the fundamental part of the relational database as they establish a relation between the tables and ensure each record in the table is uniquely identified.
A database contains a lot of tables which holds thousands of records all of which are unsorted and unorganized. Fetching a particular data from those records can be difficult and challenging. This is where Keys comes to the scenario. The two important keys of the relational schema are Primary Key and Foreign Key. In this article we will discuss the Difference between Primary Key and Foreign Key in detail.
Primary Key
A primary key in a table uniquely identifies each record in the table. It may refer to a column or a combination of columns to implicitly identify the records. The input values of the key columns defined must be unique for each record and not contain NULL values. Each table has can have one Primary key or group of keys that are unique for every record. No duplicate value is allowed in the primary key field.
Foreign Key
A foreign key is a set of one or a combination of columns in a table that is a primary key of another table. It is used in a table is used to establish a relation between tables in the ABAP dictionary, to create value checks for the input fields and to link multiple tables in a view or a lock object. Duplicate value is allowed in the foreign key field.
Table 1 is the foreign key table (dependent table) and Table 2 is the check table(referenced table). Here the Column 3(foreign key of Table 1) points to the Col 1(primary key of Table 2).Column 3 of Table 1 and Col 1 of Table 2 must have same data type and length.
Difference between Primary Key and Foreign Key
Now, let’s have a look at their difference.
Primary Key | Foreign Key |
It uniquely identifies a record in the table | It establishes relation between table and is a primary key in another table |
A table has mianly one primary key | A table can have multiple foreign keys |
It cannot have Null value | It can have Null values |
It cannot have a duplicate value | It can have duplicate values |
It cannot be dropped from the parent table which refers to the foreign key in the foreign key table | It can be dropped from the foreign key table even it is referring to the primary key of the main table |
0 Comments