Autor: 06.12.2023
CRUD - Basic Database Operations
CRUD stands for Create, Read, Update, and Delete - fundamental operations that can be performed on data stored in a database. These four operations form the basis for most database management systems, enabling interaction with data in the database.
In this article, we will show you how to perform these four operations using the SQL language.
CRUD - Explore the main operations
Take a look at the 'contacts' table. It will serve as an example for CRUD operations.
The table contains users' contact data: first name, last name, email address, and phone number. The table also has a primary key, ensuring each record has a unique identifier.
In the case of relational databases, CRUD operations are performed using query languages such as SQL (Structured Query Language).
Now, let's discuss the basic operations on this table.
Create
Example: Add a new contact to the 'contacts' table.
INSERT INTO contacts (first_name, last_name, email, phone)
VALUES ('Robert', 'Brown', 'robert@example.com', '111-222-3333');
The query will add a new record to the 'contacts' table and expand our table. Look at the image below; the newly added record will be clearly visible.
Read
Example: Retrieve contact data from the 'contacts' table. We want to retrieve data about a specific user selected by us.
SELECT * FROM contacts WHERE last_name = 'Doe';
The query will retrieve a record from the 'contacts' table, specifically - it will retrieve the record of the user with the last name Doe.
Update
Example: Update contact data in the 'contacts' table.
UPDATE contacts SET email = 'john.doe@example.com' WHERE id = 1;
The query will update a record in the 'contacts' table. The query will set a new email address for the user with the identifier 1.
Delete
Example: Delete a contact from the 'contacts' table.
DELETE FROM contacts WHERE id = 3;
The query will delete a record from the 'contacts' table. The query will delete the user with the identifier 3.
Summary
Now you know the meaning of the CRUD abbreviation. You also know how to perform all four operations - Create, Read, Update, and Delete - using the SQL language. Understanding these operations is essential for working with relational databases.