Ohhh, CRUD!
Published: 2022-10-14 21:44:59

Who doesn't love an acronym? We use them in all facets of life.. The medical field, computer science, and also as a form of communication, iykyk.

So let's talk about the important acronym at hand here. CRUD. Create. Read. Update. Destroy (or Delete). I personally prefer the term Destroy over Delete because it makes me feel like a powerful video game boss. I imagine my boss name would be something along the lines of "Doctor Bot", short for "Doctor Botany". I would use the plants of the earth to attack my opponents! Okay, enough nerdy stuff...

Back to CRUD! While there are other variations of CRUD such as ABCD, DAVE and CRAP, we are just gonna focus on CRUD for this blog article. In computer programming, CRUD can be used to describe a few different operations but we are going to set our sights on CRUD and the major operations it has with databases.

Sometimes CRUD can be referred to as a Data Manipulation Language (DML) because it is used to handle data inside of database tables. For example, using CRUD operations, we are able to manipulate data within database tables, whether it be to update data, delete it or create new data.

We use CRUD in everyday life as web surfers and don't even think about it. When you click on someones profile on facebook, you are performing the R (read) in CRUD. When you make a post, you are using the C (create) in CRUD, and so on! 

## CRUD and SQL

SQL is an acronym that stands for Structured Query Language, which is also a Data Manipulation Language. We use SQL to communicate with databases. SQL is a specialized language that allows users to request information, update information and even delete information if needed. Doesn't this sound awfully familiar to something? Hmm, wait... could it be... CRUD?!

You're on the right track! Each letter of the acronym for CRUD is related to a SQL statement. For example, Create is equivalent to Insert in SQL. Below is a table to show their relationship:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogr499sq2q5205474unn.png)
<br>

## Create
Starting with the C in our acronym, Create! AKA insert or add, Create allows users to create new records in a database.  Such as when we are creating a new post on instagram, we are performing a CRUD operation. Here is an example of what that code may look like in SQL with an already existing table:

```
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
```

The code above is specifying both column names and the values to be inserted. You can also write your INSERT statement without specifying column names if you are adding values for all existing columns. 


## Read

The read operation in CRUD, AKA select, can be thought of as a search function. It allows users to specify what data they want to access and retrieves that information for us. For example, when looking up someone's profile on LinkedIn, that is a read operation in CRUD. The code is quite simple for a select statement, as seen below:

`SELECT * FROM menu;`

This will display all items from the menu table. If we wanted to be more specific of the data we want to retrieve, we can add further parameters if they exist:

`SELECT dinnerItems FROM menu.`

When using the read operation, it will not alter any of our data, it only allows us to access it. 


## Update

The update operation in CRUD, AKA edit, allows us to alter or update existing data. Such as when we edit a post we have already made on Facebook, that would be our U (update) in CRUD. Here is an example below:

```
UPDATE table_name
 SET column1 = value1, column2 = value2, ...
 WHERE condition;
```
As you can see above, it is important that we specify the table name that we would like to update as well as the columns we will be making those changes to. It is also important that we add the associated values to be changed. 

## Destroy

Our lasts of CRUD, Destroy. AKA Delete. Delete is used to completely remove or destroy a record from a table. What is special about SQL is that it allows us to delete more than one record from a database at a time, pretty cool, right?! Below is an example of how we would perform a delete request:

`DELETE FROM table_name WHERE condition;`

## Why is CRUD important?

CRUD is used in many types of applications, SQL is just one of many that utilizes these operations. We use CRUD for many things, including: security control, access and permission, performance optimization, etc. Without CRUD, software engineers would never get anything done. Imagine the internet without it - It would be awfully boring and some would argue that it would be useless.