DEV Community

Catchthembodies
Catchthembodies

Posted on

SQL: CRUD for Newbies

Image description

What is SQL?

SQL to begin with is a query language that allows the user to manipulate and have access to the database. SQL stands for "Structured Query Language". If you want to make a living as a coder you definitely need to know to work you way around a database. Otherwise, developers could never build or maintain softwares, programs, and functional website. SQL plays a main role since programmers need to access the data. In this blog, we'll cover basics of sql.

Why learn SQL?

The reason to learn SQL is because it tangentially relates to data manipulation and analysis (e.g. data science, back-end programming, cybersecurity, etc.,). SQL is used for the finance sector as well since it helps with big numbers and does the same amount of work in shorter time.

CRUD

Image description
When working with a database you must have used CRUD in the past if you have ever worked with a database. One of the perks of learning CRUD is that it helps a developer to manage data on the webpage. CRUD is an acronym that spells out:

  • "C": to create or add data to a database
  • "R": reading the data would include searching and filtering the results
  • "U": update or edit the rows of data in any table of data
  • "D": delete or removing the data from the tables.

Using CRUD is a very essential part for developer and is used almost on every webpage since it can handle all of the data in fashionable manner.

CREATE

Creating or adding a data to a database is very simple. Create allows user to add a new row to the already made table. The function for adding data by creating is, INSERT INTO, followed by the name given to the table, column names, and values that need to be added. Here is an example of how you might proceed with process of adding data, there are actually two ways you could proceed:

INSERT INTO table_name `
    VALUES (value1, value 2, ...);
Enter fullscreen mode Exit fullscreen mode
INSERT INTO table_name`
    VALUES (value1, value2, ...);
Enter fullscreen mode Exit fullscreen mode

Below is an example, where we will be adding a new menu item to our pizza table:

INSERT INTO menu
VALUES (1, 'meat lover', 1, '2022-06-12');
"What if we wanted to add multiple rows, we can do the following by:"
INSERT INTO menu
VALUES 
(2, 'pepperoni', 3, '2022-11-14' ),
(3, 'vegetarian', 2, '2020-09-05' );
Enter fullscreen mode Exit fullscreen mode

This will end up adding data to your table (menu table) and with each entry you will end up with a unique id.

READ

The read function allows the developer to see all of the information on the specific records pulled. It is very similar to the search function. For instance, if we go back to the menu table, we can display all of the item in that menu by:

SELECT * FROM menu;
Enter fullscreen mode Exit fullscreen mode

Doing this will not allow to edit the menu instead, it will allow the user to see what menu items are already in the database.

UPDATE

Updating is how we change the already existed data in a table, when we try to update be sure to target the table and column. To update an existing record can happen with the following code:

UPDATE table_name
  SET column1 = value1, column 2 = value 3, ...
WHERE condition;
Enter fullscreen mode Exit fullscreen mode

Going back to menu reference, what if we wanted to update the name and the price, do the following:

UPDATE menu
  SET item_name = 'Hawaiian pizza', price = 12.9
  WHERE item_id = 2;
Enter fullscreen mode Exit fullscreen mode

The above code will be changing the name and price of the pizza with the id of 2.

DELETE

It is pretty obvious what this function does to your table, it can either completely remove the object or remove its selected attribute. The delete command is as it follows:

DELETE FROM table_name WHERE condition;
Enter fullscreen mode Exit fullscreen mode

What if we only wanted to remove one item from the table:

DELETE FROM menu WHERE item_name = 'vegetarian';
Enter fullscreen mode Exit fullscreen mode

But how can we delete a whole table in the database, it would look something like this:

DELETE FROM menu;
Enter fullscreen mode Exit fullscreen mode

Conclusion

At the end of the day, this is only the beginning of SQL and CRUD does play a major part of it. In the world of tech, many companies rely on SQL since it can handle databases and help manipulate the database.

Top comments (0)