What is CRUD, and Why is it Important?
CRUD stands for Create, Read, Update, and Delete—the fundamental operations for managing data in any application, database, or API. These operations are essential when developing applications that interact with persistent data.
In my previous articles, I have limited the use of technical jargon to make concepts easier to understand and avoid unnecessary confusion.
I believe most people are familiar with e-commerce platforms like Amazon, AliExpress, or eBay, so I will use these platforms as examples to explain CRUD operations practically.
CRUD operations are the backbone of various applications, including web, desktop, mobile, and enterprise software. No matter how complex a program's computations are, it ultimately relies on CRUD operations in some form. Every application either creates, reads, updates, or Deletes data. I have yet to come across a program that doesn’t involve at least one of these operations.
Now, let’s dive into it! 🚀
Create Operation in CRUD
The Create operation, also known as a POST request in HTTP, is used to insert new data into a database or any other storage system used by an application.
Using an e-commerce platform as an example, suppose you're an Amazon seller and want to publish a new product. You log into your account (which itself involves creating a session) and then create a product by providing the required details.
For instance, we define a Product class in Java:
public class Product {
private Long id;
private String name;
private Double price;
private Integer quantity;
private String category;
private String imageUrl;
}
The developers expose an API endpoint that accepts a POST HTTP request containing product details. The server processes the request and stores the product in the database. This entire process represents the Create operation in CRUD (Create, Read, Update, Delete).
Even when you order items from these platforms, all these processes are known as the creation process in CRUD.
READ Operation
The READ operation in CRUD is used to retrieve data from a database or storage system.
For example, on Amazon, when you visit the homepage and see product listings, the website performs a READ operation to fetch and display stored product information. Similarly, searching for a product or clicking on a specific item also triggers a READ operation, retrieving the relevant details from the database.
This operation is performed using the HTTP GET method.
Another great example of this is the SELECT query on the database.
So if you are familiar with SQL queries, whenever you run a SELECT query, you are doing a READ operation.
UPDATE Operations
The UPDATE operation in CRUD is used to modify existing data in a database or storage system. This is often done using HTTP methods like PUT (which replaces the entire resource) or PATCH (which updates specific fields).
Imagine you are an Amazon seller, and you want to change the price of a product you listed. When you go to the seller dashboard and edit the product price, Amazon performs an UPDATE operation through its code to modify the price field in its database.
If you update the entire product details, the system might use a PUT request. If you only update the price, it might use a PATCH request.
SQL Example If an Amazon seller wants to update a product’s price in a MySQL database, the SQL query might look like this:
UPDATE products SET price = 19.99 WHERE product_id = 1234;
DELETE Operation
The DELETE operation in CRUD is used to remove data from a database or storage system. Once a record is deleted, it is no longer accessible unless recovery mechanisms (like soft delete or backups) are in place.
This operation is done using the HTTP DELETE method.
Imagine you are an Amazon seller, and you want to remove a product listing from your store. When you click "Delete Product", Amazon performs a DELETE operation to remove that product from its database.
If an Amazon seller wants to delete a product with product_id = 1234, the SQL query might look like this:
DELETE FROM products WHERE product_id = 1234;