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
mobile
Each student's Email ID and Mobile Number should also be unique.
π Primary Key vs Unique Key
| π Feature | π Primary Key | π Unique Key |
|---|---|---|
| π― Purpose | Identifies every record uniquely | Ensures uniqueness of alternate fields |
| ❌ Duplicate Values | Not Allowed | Not Allowed |
| π« Initial (Blank) Values | Not Allowed | May be Allowed* |
| π’ Number Allowed | Only One | Multiple |
| ⚡ Performance | Indexed & Optimized | Indexed & Optimized |
| π Usage | Relationships & Data Integrity | Business 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 ID | Mobile | |
|---|---|---|
| 1001 | john@company.com | 9876543210 |
| 1002 | sara@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.
"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