CRUD Operations in SQL Database Explained

CRUD sql

In SQL, CRUD operation is the four basic operation used to manage and manipulate data in a relational database. CRUD stands for: C – create or insert, R – read or select, U – update, D – delete. Let’s look at the various CRUD operations in details.

CREATE

Create operation is used to create a table and insert values into a table in a relational database. READ HOW TO CREATE A TABLE. The code below creates a table called books_2 and insert corresponding values into the table.

CREATE TABLE books_2 
	(
		book_id INT NOT NULL AUTO_INCREMENT,
		title VARCHAR(100),
		Author_Name VARCHAR(100),
		pages INT,
		PRIMARY KEY(book_id)
	);

INSERT INTO books_2 (title, Author_Name, pages)
VALUES
('The Namesake', 'Ediko', 32),
('48 Laws of Power', 'Robert Grenne', 400),
('100 Ways to Make it Big', 'Edward', 125),
('Back-end Engineering', 'Imtiaz', 345);

READ

The Read operation is commonly referred to as SELECT is used to select or retrieve data from SQL table. The statement below retrives or select all the data from the books_2 table.

SELECT * FROM books_2;

The statement below select data from books_2 table with a condition (where the no of pages is less than 200)

SELECT * FROM books_2 where pages < 200;

UPDATE

The Update operation is used to update or modify and existing table value with a condition. The code below update the Author_Name field in the books_2 table to ‘Victor’ where pages > 300

UPDATE books_2
SET Author_Name = 'Victor'
WHERE pages > 300;

DELETE

The Delete operation is used to delete a row from a table using a condition. The statement below deletes a row or record from books_2 table where Age is less than or equal to 32

DELETE FROM books_2 WHERE Age <= 32;

One thought on “CRUD Operations in SQL Database Explained

Leave a Reply

Your email address will not be published. Required fields are marked *