What is CRUD? Explained
What do you mean by CRUD?
CRUD is an acronym that stands for Create, Read, Update, and Delete – the four fundamental operations that form the backbone of how we interact with persistent data in applications.
Here’s a breakdown of each CRUD operation:
Create: The operation that inserts new data records into a storage system. This might be adding a new user account, uploading a photo, or placing an order.
Read: The operation that retrieves existing data from a system. This could be searching for a product, viewing a profile, or generating a report.
Update: The operation that modifies existing data without creating a new record. Examples include editing profile information, changing a password, or updating inventory counts.
Delete: The operation that removes unwanted data from the system. This might involve removing a social media post, canceling an order, or archiving old records.
These four operations provide a complete model for managing data throughout its lifecycle.
Data Engineer
A data engineer builds the pipelines to connect data input to analysis.Try it for freeWhy CRUD operations matter
When building applications that store data, CRUD provides a memorable framework for implementing complete and usable data models. If an operation cannot be described by one of these four functions, it might need to be a separate model altogether.
The CRUD paradigm is especially important in:
Web application development
API design
Database management systems
Software architecture
CRUD operations in detail
Let’s explore each operation to understand its purpose and implementation.
Create operation
The Create operation adds new records to a database or system. It’s the first step in data lifecycle management.
Key characteristics:
Generates new entries in a database
Often assigns unique identifiers to new records
Validates data before storage
Returns confirmation of successful creation
Example scenario: When a user signs up for a new account on a website, a Create operation stores their information in the user database.
Read operation
The Read operation retrieves existing records from a database. This operation doesn’t modify any data—it simply fetches and displays it.
Key characteristics:
Retrieves either single records or collections of records
Supports filtering, sorting, and pagination
Should not alter the state of the data
Often, the most frequently used operation
Example scenario: When you view your profile information or browse products in an online store, Read operations fetch that data from the database.
Update operation
The Update operation modifies existing records in a database. It changes the values of specific fields without creating new records.
Key characteristics:
Identifies records using unique identifiers
Modifies only specified fields
Validates changes before committing
May return the updated record as confirmation
Example scenario: When you change your password or update your shipping address, an Update operation modifies your existing user record.
Delete operation
The Delete operation removes records from a database. It permanently eliminates data that’s no longer needed.
Key characteristics:
Identifies records using unique identifiers
May perform soft deletes (marking as inactive) or hard deletes (permanent removal)
Often requires confirmation to prevent accidental deletion
May have cascading effects on related data
Example scenario: When you remove an item from your wishlist or delete your account, a Delete operation removes those records from the database.
CRUD in RESTful APIs
REST (Representational State Transfer) is a popular architectural style for designing networked applications. In a RESTful environment, CRUD operations map directly to HTTP methods:
CRUD operation | HTTP method | Purpose |
---|---|---|
Create | POST | Creates a new resource |
Read | GET | Retrieves a resource or collection |
Update | PUT/PATCH | Modifies an existing resource |
Delete | DELETE | Removes a resource |
Let’s explore this mapping with a practical example involving a restaurant menu API.
Create with POST
For example, let’s imagine that we are adding a new food item to the stored list of dishes for this restaurant, and the dish
objects are stored in a dishes
resource. If we wanted to create the new item, we would use a POST request:
Request:
POST http://www.myrestaurant.com/dishes/
Body -
{
"dish": {
"name": "Avocado Toast",
"price": 8
}
}
This creates a new item with a name
value of "Avocado Toast"
and a price
value of 8. Upon successful creation, the server should return a header with a link to the newly-created resource, along with a HTTP response code of 201 (CREATED).
Response:
Status Code - 201 (CREATED)
Body -
{
"dish": {
"id": 1223,
"name": "Avocado Toast",
"price": 8
}
}
From this response, we see that the dish
with name
“Avocado Toast” and price
8 has been successfully created and added to the dishes
resource.
Read with GET
To read resources in a REST environment, we use the GET method. Reading a resource should never change any information — it should only retrieve it. If you call GET on the same information 10 times in a row, you should get the same response on the first call that you get on the last call.
GET can be used to read an entire list of items:
Request:
GET http://www.myrestaurant.com/dishes/
Response: Status Code - 200 (OK)
Body -
{
"dishes": [
{
"id": 1,
"name": "Spring Rolls",
"price": 6
},
{
"id": 2,
"name": "Mozzarella Sticks",
"price": 7
},
...
{
"id": 1223,
"name": "Avocado Toast",
"price": 8
},
{
"id": 1224,
"name": "Muesli and Yogurt",
"price": 5
}
]
}
GET requests can also be used to read a specific item, when its id
is specified in the request:
Request:
GET http://www.myrestaurant.com/dishes/1223
Response: Status Code - 200 (OK)
Body -
{
"id": 1223,
"name": "Avocado Toast",
"price": 8
}
After this request, no information has been changed in the database. The item with id
1223 has been retrieved from the dishes
resource and not modified. When there are no errors, GET will return the HTML or JSON of the desired resource, along with a 200 (OK) response code. If there is an error, it most often will return a 404 (NOT FOUND) response code.
Update with PUT
PUT is the HTTP method used for the CRUD operation, Update.
For example, if the price of Avocado Toast has gone up, we should go into the database and update that information. We can do this with a PUT request.
Request:
PUT http://www.myrestaurant.com/dishes/1223
Body -
{
"dish": {
"name": "Avocado Toast",
"price": 10
}
}
This request should change the item with id
1223 to have the attributes supplied in the request body. This dish
with id
1223 should now still have the name
“Avocado Toast”, but the price
value should now be 10, whereas before it was 8.
Response: Status Code - 200 (OK)
Body -
{
"dish": {
"name": "Avocado Toast",
"price": 10
}
}
The response includes a Status Code of 200 (OK) to signify that the operation was successful. Optionally, the response could use a Status Code of 204 (NO CONTENT) and not include a response body. This decision depends on the context.
Delete with DELETE
The CRUD operation Delete corresponds to the HTTP method DELETE. It is used to remove a resource from the system.
Let’s say that the world avocado shortage has reached a critical point, and we can no longer afford to serve this modern delicacy at all. We should go into the database and delete the item that corresponds to “Avocado Toast”, which we know has an id
of 1223.
Request:
DELETE http://www.myrestaurant.com/dishes/1223
Such a call, if successful, returns a response code of 204 (NO CONTENT), with no response body. The dishes
resource should no longer contain the dish
object with id
1223.
Response: Status Code - 204 (NO CONTENT)
Body - None
Calling GET on the dishes
resource after this DELETE call would return the original list of dishes with the {"id": 1223, "name": "Avocado Toast", "price": 10}
entry removed. All other dish
objects in the dishes
resource should remain unchanged. If we tried to call a GET on the item with id
1223, which we just deleted, we would receive a 404 (NOT FOUND) response code, and the state of the system should remain unchanged.
Calling DELETE on a resource that does not exist should not change the state of the system. The call should return a 404 response code (NOT FOUND) and do nothing.
CRUD in SQL databases
In SQL databases, CRUD operations correspond to specific SQL statements:
CRUD operation | SQL command | Example |
---|---|---|
Create | INSERT | INSERT INTO dishes (name, price, category) VALUES ('Avocado Toast', 8, 'Breakfast'); |
Read | SELECT | SELECT * FROM dishes WHERE id = 1223; |
Update | UPDATE | UPDATE dishes SET price = 10 WHERE id = 1223; |
Delete | DELETE | DELETE FROM dishes WHERE id = 1223; |
SQL databases ensure data integrity through constraints, transactions, and normalization, making them ideal for applications where data consistency is critical.
CRUD in NoSQL databases
NoSQL databases like MongoDB handle CRUD operations differently, using methods instead of SQL statements:
CRUD operation | MongoDB method | Example |
---|---|---|
Create | insertOne/insertMany | db.dishes.insertOne({name: "Avocado Toast", price: 8, category: "Breakfast"}) |
Read | find/findOne | db.dishes.findOne({_id: ObjectId("1223")}) |
Update | updateOne/updateMany | db.dishes.updateOne({_id: ObjectId("1223")}, {$set: {price: 10}}) |
Delete | deleteOne/deleteMany | db.dishes.deleteOne({_id: ObjectId("1223")}) |
NoSQL databases are better at working with data that doesn’t fit neatly into tables. They’re also easier to expand by adding more servers when your application grows, rather than having to upgrade to a more powerful single server, like many SQL databases require.
Best practices for implementing CRUD
To ensure reliable and secure CRUD operations in your applications:
Validate input data: Check all user-supplied data before processing it
Implement proper error handling: Return appropriate status codes and error messages
Use transactions where appropriate: Ensure data consistency during complex operations
Implement access control: Restrict operations based on user roles and permissions
Log important operations: Maintain an audit trail for security and debugging
Consider soft deletes: Mark records as inactive instead of permanently deleting them
Use concurrency control: Handle situations where multiple users update the same data
Optimize performance: Use indexing and query optimization for large datasets
Conclusion
CRUD operations form the foundation of data management in modern applications. Whether you’re building a simple website or a complex enterprise system, understanding how to Create, Read, Update, and Delete data effectively is essential.
By designing your application with the CRUD paradigm in mind, you create a clear separation of concerns and establish consistent patterns for data manipulation. As you continue developing your programming skills, you’ll find that mastering CRUD operations allows you to build more robust and maintainable applications.
Frequently asked questions
1. What are the CRUD methods in HTML?
HTML itself doesn’t implement CRUD operations, but it provides forms and elements that can trigger CRUD operations through HTTP requests:
Create: HTML forms with method=”POST”
Read: HTML links (
<a>
tags) or forms with method=”GET”Update: HTML forms with method=”POST” and hidden input fields (often with PUT or PATCH emulation)
Delete: Similar to Update, using forms with method=”POST” and hidden fields to indicate DELETE
Modern web applications often use JavaScript to perform these operations via fetch or XMLHttpRequest.
2. What is the difference between REST and CRUD?
REST is an architectural style for designing networked applications, while CRUD represents the four basic operations for persistent storage:
Scope: REST is a broad architecture that covers resources, representations, and stateless communication, while CRUD focuses specifically on data operations.
Implementation: REST uses HTTP methods, resource URIs, and representations, while CRUD is a conceptual framework that can be implemented in many ways.
Purpose: REST aims to create scalable, maintainable web services with clear interfaces, while CRUD ensures complete data manipulation capabilities.
REST is one way to implement CRUD operations, but CRUD can also be implemented through other means like RPC or GraphQL.
3. Is CRUD specific to databases?
While CRUD operations are commonly associated with databases, the concept applies to any system that persistently stores data. CRUD can be implemented in:
File systems
APIs
Memory caches
Object storage
Web services
4. How do CRUD operations relate to HTTP status codes?
HTTP status codes provide standardized responses for CRUD operations in web applications:
Create (POST):
201 Created: Resource successfully created
400 Bad Request: Invalid input
409 Conflict: Resource already exists
Read (GET):
200 OK: Resource found and returned
404 Not Found: Resource doesn’t exist
Update (PUT/PATCH):
200 OK: Resource updated successfully
404 Not Found: Resource to update doesn’t exist
400 Bad Request: Invalid update data
Delete (DELETE):
204 No Content: Resource deleted successfully
404 Not Found: Resource to delete doesn’t exist
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
What is REST?
Learn about the REST (Representational State Transfer) paradigm and how rest architecture streamlines communication between web components. - Article
A Guide to the SQL DELETE Statement
Learn how to use the SQL `DELETE` statement to safely remove records from a database. Explore its syntax, use cases, best practices, and data integrity. - Article
HTTP Requests in Velo
Expand your website’s capabilities by using "wix-fetch" to make various HTTP requests.
Learn more on Codecademy
- Career path
Data Engineer
A data engineer builds the pipelines to connect data input to analysis.Includes 17 CoursesWith CertificateBeginner Friendly90 hours - Free course
Learn Node-SQLite
Learn how to interact with a SQL database from within your JavaScript programs!Intermediate2 hours - Free course
Learn Spring: Building an App
Learn about the underlying technology, Beans and Boot, that support the Spring framework to build out an app.Intermediate2 hours