RDBMS CRUD Operations
Create, read, update, and delete data using SQL.
CRUD Operations in RDBMS
Every application that works with a database performs four basic tasks: adding data, reading data, updating data, and deleting data. These four actions are known as CRUD operations.
CRUD stands for Create, Read, Update, and Delete.
- Create means adding new data.
- Read means retrieving existing data.
- Update means changing existing data.
- Delete means removing data.
Whether you are using an online shopping website, a banking app, a school management system, or a social media platform, CRUD operations happen behind the scenes every day.
Understanding CRUD operations is important because they form the foundation of working with databases.
What are CRUD Operations?
CRUD operations are the basic actions used to manage records in a database table. Each operation has a corresponding SQL command.
| CRUD Operation | SQL Command |
|---|---|
| Create | INSERT |
| Read | SELECT |
| Update | UPDATE |
| Delete | DELETE |
These commands allow you to manage information stored in database tables efficiently.
Create Operation
The Create operation is used to insert new records into a table.
Imagine a school database where a new student joins the school. A new record must be added to the Students table.
INSERT INTO Students
(StudentID, Name, Age, City)
VALUES
(101, 'Rahul', 18, 'Delhi');After running this command, the student information is stored in the database.
When is Create Used?
Some common examples include:
- Registering a new user
- Adding a new product
- Creating a customer account
- Saving an employee record
Read Operation
The Read operation retrieves information from the database. The SELECT statement is used to display stored records.
To display all student records:
SELECT *
FROM Students;To display only the student's name and city:
SELECT Name, City
FROM Students;To find students from Delhi:
SELECT *
FROM Students
WHERE City = 'Delhi';The WHERE clause helps filter records based on specific conditions.
When is Read Used?
- Viewing customer information
- Displaying product lists
- Searching for employees
- Showing student records
Update Operation
The Update operation modifies existing records.
Suppose Rahul has moved from Delhi to Jaipur. Instead of creating a new record, we update the existing one.
UPDATE Students
SET City = 'Jaipur'
WHERE StudentID = 101;This command changes only Rahul's city.
The WHERE clause is very important. Without it, every record in the table may be updated.
UPDATE Students
SET City = 'Jaipur';Since no condition is given, every student's city becomes Jaipur. Always use the WHERE clause unless you intentionally want to update all records.
When is Update Used?
- Changing an address
- Updating a salary
- Editing a phone number
- Modifying product prices
Delete Operation
The Delete operation removes records from a table.
Suppose a student leaves the school.
DELETE FROM Students
WHERE StudentID = 101;This command removes only the student whose ID is 101.
Just like UPDATE, the WHERE clause is extremely important.
DELETE FROM Students;This command deletes every record from the table. Always double-check your conditions before deleting data.
When is Delete Used?
- Removing inactive users
- Deleting cancelled orders
- Removing duplicate records
- Deleting old employee records
Best Practices for CRUD Operations
When working with databases, follow these good practices:
- Always use the WHERE clause with UPDATE and DELETE unless updating or deleting all records is intended.
- Verify data before inserting new records.
- Use meaningful column names.
- Test SQL queries on sample data before running them on important databases.
- Keep regular backups before making large updates or deletions.