Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
SQL PRIMARY KEY Constraint
Next article icon

PL/SQL Composite Key

Last Updated : 26 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In relational databases, a composite key is a combination of two or more columns used to uniquely identify a record in a table. PL/SQL composite keys play a crucial role in ensuring data integrity and establishing relationships between the tables.

This article will explain the concept of composite keys in PL/SQL with its usage, examples and also explain their importance in database design.

PL/SQL Composite Key

  • A composite key in PL/SQL is formed by combining two or more columns in a table to create a unique identifier for each row.
  • Unlike a single primary key that uses just one column a composite key depend on the values from multiple columns to ensure that each record is distinct.

Features of PL/SQL Composite Key

  • Uniqueness: Ensures that each record in the table is unique based on the combination of the multiple columns.
  • Data Integrity: Helps maintain the accuracy and consistency of the data across tables.
  • Relationships: It Facilitates the establishment of relationships between the tables in a relational database.

Creating a Composite Key in PL/SQL

Query:

CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);

INSERT INTO OrderDetails (OrderID, ProductID, Quantity)
VALUES (1001, 2001, 10);
INSERT INTO OrderDetails (OrderID, ProductID, Quantity)
VALUES (1001, 2002, 15);

Output:

OrderIDProductIDQuantity
1001200110
1001200215

Explanation:

  • The table OrderDetails has a composite primary key consisting of OrderID and ProductID.
  • Each row must have a unique combination of OrderID and ProductID, ensuring that each record is unique within the table.

Modifying a Table with a Composite Key

Before adding a composite primary key to a table, any existing primary key constraints must be removed. This step ensures that there are no conflicts or restrictions from the previous primary key definition. By dropping the primary key, you clear the way to define a new composite key that fits the updated schema requirements.

1. Dropping an Existing Primary Key

  • This query removes the current primary key constraint from the OrderDetails table.
  • It is essential to drop the existing primary key if you need to redefine the key structure, such as adding a composite primary key.
  • After executing this query, the table no longer enforces the previous uniqueness constraint, allowing you to modify or add a new primary key as needed.

Query:

ALTER TABLE OrderDetails DROP PRIMARY KEY;

Output:

Table altered

Explanation:

  • This query removes the existing primary key constraint from the OrderDetails table.
  • After executing this, the table no longer has a primary key, allowing you to modify or add a new composite primary key.
  • The message Table altered. confirms that the primary key constraint has been successfully dropped.

2. Adding a Composite Primary Key

  • This query establishes a composite primary key on the OrderDetails table, combining OrderID and ProductID to uniquely identify each record.
  • By adding this composite key, you ensure that the combination of these two columns must be unique across all rows, which helps maintain data integrity and prevents duplicate entries for the same order and product.

Query:

ALTER TABLE OrderDetails
ADD PRIMARY KEY (OrderID, ProductID);

Output:

Table altered

Explanation:

  • This query adds a composite primary key constraint to the OrderDetails table.
  • The composite key consists of OrderID and ProductID, ensuring that the combination of these two columns is unique across all rows in the table.
  • The message Table altered. indicates that the composite primary key has been successfully added, enforcing uniqueness for the specified columns.

Conclusion

Composite keys are a fundamental concept in relational database design, crucial for maintaining data integrity and establishing meaningful relationships between tables. Understanding how to implement and manage composite keys in PL/SQL is essential for designing robust and efficient databases.

By applying the principles outlined in this article, you can effectively use composite keys to address complex data modeling challenges in your database projects.


Next Article
SQL PRIMARY KEY Constraint

V

vinod19ldr
Improve
Article Tags :
  • Databases
  • PL/SQL

Similar Reads

  • Composite Key in SQL
    A composite key is a primary key that is made up of more than one column to uniquely identify records in a table. Unlike a single-column primary key, a composite key combines two or more columns to ensure uniqueness. While any of the individual columns in a composite key might not be unique on their
    2 min read
  • PL/SQL Foreign Key
    Maintaining data integrity is essential in relational database management systems. One essential concept in ensuring data consistency is the use of foreign keys. In PL/SQL, a foreign key creates relationships between tables, ensuring that the data in one table corresponds to the data in another. Thi
    8 min read
  • SQL PRIMARY KEY Constraint
    The PRIMARY KEY constraint in SQL is one of the most important constraints used to ensure data integrity in a database table. A primary key uniquely identifies each record in a table, preventing duplicate or NULL values in the specified column(s). Understanding how to properly implement and use the
    5 min read
  • PL/SQL Alternate Key
    In relational databases, alternate keys play a crucial role in maintaining data integrity by ensuring that columns not designated as the primary key still enforce uniqueness. In this article, we will learn about OUTER JOIN in PL/SQL, including its types, syntax, use cases, and examples. PL/SQL Alter
    4 min read
  • PL/SQL Create Index
    In PL/SQL, indexes are used to improve the performance of queries by reducing the number of rows that need to be scanned when retrieving data. The Indexes work like pointers to rows in the table and they can be created on one or more columns of a table to facilitate faster searching. In this article
    6 min read
  • PL/SQL DEFAULT Constraint
    In PL/SQL the DEFAULT constraint is used to automatically assign a default value to a column when an explicit value is not provided during the insertion of a new row. This feature is particularly useful for ensuring that columns always have a meaningful value even when the user or application provid
    5 min read
  • PL/SQL Left Join
    In the world of database management, efficiently retrieving and combining data from multiple tables is crucial. Among these the LEFT JOIN is particularly important because it includes all records from one table even when there are no corresponding records in another table. In this article, we will P
    6 min read
  • SQL FOREIGN KEY Constraint
    A FOREIGN KEY constraint is a fundamental concept in relational databases, ensuring data integrity by enforcing relationships between tables. By linking a child table to a parent table, the foreign key establishes referential integrity. This constraint ensures that the values in the foreign key colu
    5 min read
  • PL/SQL Outer Join
    In SQL, joins are used to retrieve data from two or more tables based on a related column. Joins can be categorized into different types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, etc. In this article, we will learn about OUTER JOIN in PL/SQL, including its types, syntax, use cases, and ex
    6 min read
  • PL/SQL Cross Join
    Combining data from several tables is an ordinary procedure in the field of relational databases. The Cross Join is one of the several table joining techniques offered by PL/SQL, Oracle's procedural SQL extension. To improve your comprehension, this article will provide you with an introduction to C
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences