Home/Blog/Programming/CRUD operations explained: Create, read, update, and delete
Home/Blog/Programming/CRUD operations explained: Create, read, update, and delete

CRUD operations explained: Create, read, update, and delete

Maryam Sulemani
May 29, 2024
6 min read

Become a Software Engineer in Months, Not Years

From your first line of code, to your first day on the job — Educative has you covered. Join 2M+ developers learning in-demand programming skills.

If you’ve ever worked with databases, you’ve probably encountered CRUD operations. For those unfamiliar, CRUD stands for create, read, update, and delete — the four essential functions of any persistent storage system, like a database.

Although CRUD operations can be used to manipulate both SQL and NoSQL databases, this article concentrates on just one type: Structured Query Language (SQL), as it is an especially popular programming language used across many industries today.

As developers, it’s important to understand how basic CRUD operations work. We’ll introduce you to these several examples of its application through SQL queries.

Learn how to work with SQL

Cover
An Introductory Guide to SQL

The ability to work SQL is becoming an increasingly in-demand skill, both for software developers and people in less technical roles. If you’re interested in learning SQL and have no prior experience with it, then this course will be your light in a dark tunnel. You’ll start by covering the basics of SQL such as how to create a database, how to insert, query, and update data. You’ll also learn fundamental concepts that developers and data scientists use everyday such as multi-table operations, nested queries, and how to set up views. Throughout, you’ll get to execute SQL queries in your browser and see results in real-time - you won’t need to worry about set-up. At the end of this course, you’ll also get some hands-on practice with common SQL interview questions, so when the time comes, you’ll be ready and confident to answer any question that comes your way. Let’s get started!

13hrs
Beginner
72 Playgrounds
75 Quizzes

What are CRUD operations?#

Any organization that tracks data (such as user accounts, payment information, or other records) needs systems that provide persistent storage, which is usually organized as a database.

A relational database consists of data organized in tables where each table comprises rows and columns. A table can be connected to other tables using primary and foreign keys.

CRUD refers to the four operations we use to implement persistent storage applications like relational databases. Examples of relational databases include Oracle, Microsoft SQL Server, and MySQL.

Operations such as security control, transaction control, access, and permission, and performance optimization are all based on CRUD.

Letter Operation Function
C Create Create, Insert
R Read Select
U Update Edit
D Delete Delete

Why are CRUD operations so important?#

You can’t interact with databases without using CRUD. CRUD is essential for anything requiring persistent storage. Without CRUD, web developers wouldn’t be able to use REST (Representational State Transfer), which is a superset of CRUD used to access HTTP resources. REST APIs are some of the most commonly used in web development.

Basically, software developers wouldn’t be able to complete any project requiring persistent storage without CRUD operations.

On the other end, CRUD is just as crucial for end-users as it is for application developers. The end-users access CRUD operations through an application’s interface. Without CRUD, actions like registering for websites, creating blogs, or using bookmarks would be impossible.

Benefits of CRUD

  • Facilitates security control by satisfying the various access requirements
  • Simplifies application design and makes it more scalable
  • Better performance compared to ad-hoc SQL statements

CRUD with SQL#

SQL is a standard language used to store, manipulate, and retrieve data from databases. Let’s go over a few different ways you can use CRUD SQL queries to create a database and table, insert data into tables, and perform basic operations on data within tables.

CREATE operation#

The CREATE operation lets you create a new database and its tables.

In SQL, a new database is created using the CREATE DATABASE keyword.

CREATE DATABASE db_name
Similarly, you can create a new table using the `CREATE TABLE` keyword.

CREATE TABLE table_name (col_1, col_2, ...)

You can add new records using the command INSERT INTO. This command starts with the INSERT INTO keyword, followed by the table name, column names, and the values to be inserted.

When using INSERT INTO, you have two options:

INSERT INTO table_name
 VALUES (value1, value2, value3, ...);
INSERT INTO table_name (column1, column2, column3, ...)
 VALUES (value1, value2, value3, ...);

In the example below, you can create a database bakery and table menu. Afterward, you can insert data into your menu table.

CREATE DATABASE bakery;

CREATE TABLE menu (id Int, item varchar(10), date DATE);

INSERT INTO menu
VALUES (1, 'croissant', '2020-12-16');

If you want to insert multiple rows, you can do that using the following syntax:

INSERT INTO menu
VALUES 
(2, 'bread', '2020-12-16' ),
(3, 'eclairs', '2020-12-16' );

This will insert two new rows to the menu table, and give each entry a unique id.

Learn how to work with SQL

Cover
An Introductory Guide to SQL

The ability to work SQL is becoming an increasingly in-demand skill, both for software developers and people in less technical roles. If you’re interested in learning SQL and have no prior experience with it, then this course will be your light in a dark tunnel. You’ll start by covering the basics of SQL such as how to create a database, how to insert, query, and update data. You’ll also learn fundamental concepts that developers and data scientists use everyday such as multi-table operations, nested queries, and how to set up views. Throughout, you’ll get to execute SQL queries in your browser and see results in real-time - you won’t need to worry about set-up. At the end of this course, you’ll also get some hands-on practice with common SQL interview questions, so when the time comes, you’ll be ready and confident to answer any question that comes your way. Let’s get started!

13hrs
Beginner
72 Playgrounds
75 Quizzes

READ operation#

The READ function is similar to a search function, allowing you to retrieve specific records and read their values. In SQL, the read function uses the SELECT keyword.

For example, let’s look at menu items sold in a bakery.

To view the complete list of baked goods, you need to display all the data in your menu table using:

SELECT * FROM menu;

This will not change the menu table. Instead, it will display all of the records within that table.

Here’s an example of how the SELECT keyword can be used to retrieve more specific data:

 SELECT id, item, date
    FROM   menu  
    WHERE  id = '3'; 

In this SQL query, a menu item is selected using the unique ID number assigned to it. This query will display the complete row from the menu table, where the id is 3.

UPDATE operation#

The UPDATE operation is used to modify existing records of a table. When using the UPDATE keyword, you must define the target table and columns to be updated. You may also need to know associated values or specific rows, depending on how specific you want your query to be.

Note: To avoid concurrency issues, limit the number of rows that are updated in each query.

Here is the basic syntax for updating a record:

UPDATE table_name
 SET column1 = value1, column2 = value2, ...
 WHERE condition;

Let’s say you want to update the item and date of a specific item in the menu table. You can use:

UPDATE menu
 SET item = 'chocolate croissant', date = 2022-12-16
 WHERE id = 2;

This will update the table so that the previous record with an ID number of 2 is assigned a new item name, chocolate croissant, and a new date, 2022-12-16.

DELETE operation#

The SQL DELETE operation can remove one or more records from the table of a database. Some relational database applications may permit a hard delete (permanent delete) or soft delete (update row status).

The syntax for the DELETE operation is as follows:

DELETE FROM table_name WHERE condition;

If you want to remove only one item from the table, you can specify which row to delete. You can also use a combination of conditions if multiple rows need deletion.

DELETE FROM menu WHERE item_name='bread';

This will remove the row with the item bread from the table. If you want to delete all the records from the table, you can use:

DELETE FROM menu;

Wrapping up and next steps#

In this article, we went over what CRUD is and how it’s used in SQL. By understanding CRUD operations and APIs, developers can build powerful web applications that can store and retrieve data from databases quickly and easily. This knowledge is essential for building successful web applications today. It’s also a great way to bolster your understanding of computer programming and databases.

You’ll use CRUD in all sorts of applications, databases, and general programming tasks, so there’s a lot to learn, but don’t be intimidated!

If you want to learn more about SQL, you can start with the following topics:

  • Basic SQL
  • Joins
  • Stored Procedures
  • Triggers
  • Programming languages like Python, PHP, Java, and JavaScript

To get started, check out Educative’s Introductory Guide to SQL. You’ll learn about all the basics of SQL and get hands-on with CRUD operations. You will cover everything from creating and updating databases to joins, nested queries, stored procedures, and triggers, all in a hands-on environment.

Happy learning!

Continue reading about SQL#

Frequently Asked Questions

What are the four CRUD operations?

CRUD is a computer programming term for the four basic operations for creating and managing persistent storage applications: create, read, update, and delete.


  

Free Resources