Open In App

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
Article Tags :

Similar Reads