Tuesday, June 30, 2026

πŸ”‘ SAP ABAP Interview Question: Primary Key vs Unique Key

 One of the most frequently asked SAP ABAP interview questions is:

πŸ’‘ What is the difference between a Primary Key and a Unique Key?


Although both are used to ensure data uniqueness, they serve different purposes in database design. Let's understand them with simple explanations and examples.

πŸ”΄ What is a Primary Key?

A Primary Key is a field (or combination of fields) that uniquely identifies every record in a database table.

It guarantees that every record is unique and can always be identified without ambiguity.

Key Features

πŸ”‘ Uniquely identifies each record.
❌ Duplicate values are not allowed.
🚫 Initial (blank) values are not allowed.
☝️ Only one Primary Key can exist in a table.
⚡ Used for faster data retrieval through indexing.
πŸ”— Establishes relationships between database tables.

πŸ”΅ What is a Unique Key?

A Unique Key also ensures that values remain unique, but it acts as an alternate unique identifier rather than the primary identifier.

Unlike a Primary Key, a table can have multiple Unique Keys.

Key Features

πŸ”’ Ensures uniqueness of data.
❌ Duplicate values are not allowed.
✔️ Initial (blank) values may be allowed (depends on the database).
πŸ”’ Multiple Unique Keys can exist in one table.
⚡ Also improves search performance through indexing.

πŸ’» ABAP Example
DATA: BEGIN OF zstudent,
student_id TYPE numc10,
email TYPE char50,
mobile TYPE char15,
name TYPE char50,
END OF zstudent.

πŸ”‘ Primary Key

student_id

Every student has a unique Student ID.

πŸ”’ Unique Keys

email
mobile

Each student's Email ID and Mobile Number should also be unique.

πŸ“Š Primary Key vs Unique Key

πŸ“Œ FeatureπŸ”‘ Primary KeyπŸ”’ Unique Key
🎯 PurposeIdentifies every record uniquelyEnsures uniqueness of alternate fields
❌ Duplicate ValuesNot AllowedNot Allowed
🚫 Initial (Blank) ValuesNot AllowedMay be Allowed*
πŸ”’ Number AllowedOnly OneMultiple
⚡ PerformanceIndexed & OptimizedIndexed & Optimized
πŸ”— UsageRelationships & Data IntegrityBusiness Validation & Alternate Search

πŸ“ Note: Whether blank/NULL values are allowed in a Unique Key depends on the underlying database implementation.


🌍 Real-Time SAP Example

Consider an Employee Master table.

Employee IDEmailMobile
1001john@company.com      9876543210
1002sara@company.com      9876543211

Here:

πŸ”‘ Employee ID → Primary Key
πŸ“§ Email → Unique Key
πŸ“± Mobile Number → Unique Key

Even though Employee ID is the main identifier, Email and Mobile Number must also remain unique. 

🎯 When Should You Use Them?

πŸ”‘ Use a Primary Key when:

✅ The field permanently identifies a record.
✅ Every record must contain a value.
✅ The field is used in joins and foreign key relationships.

Examples:

πŸ‘¨‍πŸ’Ό Employee ID
πŸ“¦ Material Number
πŸ“„ Sales Order Number
πŸ›’ Purchase Order Number

πŸ”’ Use a Unique Key when:

✅ Another business field must remain unique.
✅ The field is important but isn't the table's primary identifier.

Examples:

πŸ“§ Email Address
πŸ“± Mobile Number
πŸͺͺ Passport Number
πŸ†” PAN Number
🏒 GST Number


πŸ’‘ Interview Tip

A simple trick to remember:

πŸ”‘ Primary Key = Main Identity
➡️ One per table
➡️ Never Blank
➡️ Never Duplicate

πŸ”’ Unique Key = Alternate Identity
➡️ Multiple allowed
➡️ No Duplicate values
➡️ Blank values may be allowed (DB dependent)

πŸŽ“ Interview Questions

❓ Can a table have multiple Primary Keys?

No. A table can have only one Primary Key (it may be composite, consisting of multiple fields).

❓ Can a table have multiple Unique Keys?

Yes. A table can have multiple Unique Keys.

❓ Which one is mandatory?

✅ Every database table should have a Primary Key, while Unique Keys are optional and created based on business requirements.

 Final Takeaway

✔️ Primary Key = Main identifier of a record.
✔️ Unique Key = Alternate unique identifier used for business rules.
✔️ Both help maintain data integrity, prevent duplicate records, and improve database performance.

πŸ’¬ Interview One-Liner:

"A Primary Key uniquely identifies each record and cannot be blank or duplicated, whereas a Unique Key also enforces uniqueness but serves as an alternate identifier, and multiple Unique Keys can exist in a table."

No comments:

Post a Comment