Operations on Relational Model in DBMS
In a Relational Database Management System (RDBMS), data is organized into tables, and several operations can be performed to manipulate and retrieve this data. The four basic operations that can be performed on a relational model are Insertion, Deletion, Update, and Retrieval. Each of these operations plays a crucial role in managing and maintaining data efficiently.
1. Insert Operation
The Insert operation is used to add new records (rows) into a table. It is one of the most common operations in databases, enabling users to add fresh data as new entities (such as new employees, customers, or products) are introduced.
While inserting data is simple, there are certain conditions that must be met to maintain the integrity of the data:
- No Duplicate Primary Keys: If a column is marked as a primary key, no two rows can have the same value for that column. For example, if you try to insert a student with an ID number that already exists, the operation will fail.
- No NULL in Primary Key: A primary key column cannot contain a NULL value. Every record must have a unique and valid primary key value.
- Foreign Key Integrity: If a column is a foreign key (a reference to a primary key in another table), you cannot insert a value that doesn't exist in the referenced table. For example, if you are inserting a record for an order, and the customer ID is a foreign key referencing the Customers table, you cannot insert a customer ID that is not present in the Customers table.
- Correct Data Type: Values must be of the correct data type and within the acceptable range or domain.
Example:
Consider a table for Students:
Stu_ID
|
Name
|
Age
|
Dept_ID
|
101
|
John
|
21
|
D001
|
102
|
Maria
|
22
|
D002
|
If we try to insert a new student with the same Stu_ID (101), or with a Dept_ID that does not exist in the Department table, the database will not allow the operation.
INSERT INTO Students (Stu_ID, Name, Age, Dept_ID) VALUES (103, 'Sara', 23, 'D001');
This insert will be successful because the Stu_ID is unique, and the Dept_ID exists in the Department table.
2. Delete Operation
The Delete operation is used to remove records from a table. Deleting data is straightforward, but certain complications may arise, especially when dealing with foreign key constraints. If a row that is being deleted is referenced by another table (through a foreign key), the delete operation may fail, or in some cases, it might also delete the related records (if cascading delete is enabled).
Example:
Consider the same Students table:
Stu_ID
|
Name
|
Age
|
Dept_ID
|
101
|
John
|
21
|
D001
|
102
|
Maria
|
22
|
D002
|
103
|
Sara
|
23
|
D001
|
If we want to delete the record where the Stu_ID is 102:
DELETE FROM Students WHERE Stu_ID = 102;
This will remove Maria’s record from the table. However, if Stu_ID is referenced as a foreign key in another table (for instance, in an Enrollments table), and cascading delete is not enabled, the deletion will fail until the foreign key references are resolved.
3. Update Operation
The Update operation allows modifying or changing the values of existing records in a table. This is useful when you need to correct data, update outdated information, or make any other adjustments. There are two important considerations when updating data:
- Updating Primary Key or Foreign Key Values: If you update a primary key or foreign key, you must ensure that the new value is valid and does not violate any constraints.
- Correct Data Type: The updated value must be of the correct data type and conform to any constraints (such as unique, not null, etc.).
Example:
Continuing with our Students table, suppose we want to update the age of Sara (Stu_ID = 103):
UPDATE Students SET Age = 24 WHERE Stu_ID = 103;
This will update Sara’s age from 23 to 24.
However, if we attempt to change Stu_ID to a value that already exists (e.g., changing it to 101), or update Dept_ID to an invalid department, the database will prevent the update to maintain data integrity.
4. Retrieval Operation
The Retrieval operation is used to fetch or retrieve records from the database. This is done using the SELECT statement, which allows users to query the table for specific data. You can retrieve data based on various conditions, such as filtering by a certain attribute, fetching specific columns, or sorting the results.
Example:
If we want to retrieve the names and ages of all students in the Students table, we would use:
SELECT Name, Age FROM Students;
This query will return:
You can also apply conditions to the retrieval operation. For instance, to retrieve students whose age is greater than 21:
SELECT Name, Age FROM Students WHERE Age > 21;
This will return:
Conclusion
The Insert, Delete, Update, and Retrieval operations form the foundation of data manipulation in relational databases. These operations make it easy to manage data while ensuring that the integrity and consistency of the database are maintained through constraints like primary keys, foreign keys, and data types. As demonstrated in the examples, these operations are not only fundamental to relational databases but also incredibly powerful in handling complex data requirements efficiently.