CRUD Operations
1. Introduction to CRUD
CRUD operations are the fundamental functions for working with persistent data
in any database system, including relational (SQL) and non-relational (NoSQL)
databases. These operations correspond to the basic actions that allow users to
create, read, update, and delete data.
2. CRUD Operations Breakdown
a. CREATE (C)
Purpose: The CREATE operation is used to insert new records into a
database.
In SQL, the INSERT statement is used for this operation.
In NoSQL, it varies depending on the type of database (e.g., insert() in
MongoDB).
SQL Example:
INSERT INTO Employees (ID, Name, Age, Department)
VALUES (1, 'Alice', 30, 'HR');
NoSQL Example (MongoDB):
db.employees.insertOne({
ID: 1,
Name: "Alice",
Age: 30,
Department: "HR"
});
, b. READ (R)
Purpose: The READ operation is used to retrieve data from a database.
In SQL, the SELECT statement is used to retrieve data.
In NoSQL, it varies based on the database type (e.g., find() in MongoDB).
SQL Example:
SELECT Name, Age, Department
FROM Employees
WHERE Department = 'HR';
NoSQL Example (MongoDB):
db.employees.find({ Department: "HR" });
c. UPDATE (U)
Purpose: The UPDATE operation is used to modify existing records in a
database.
In SQL, the UPDATE statement is used for this operation.
In NoSQL, it varies based on the database (e.g., updateOne() in MongoDB).
SQL Example:
UPDATE Employees
SET Age = 31
WHERE ID = 1;
NoSQL Example (MongoDB):
db.employees.updateOne(
{ ID: 1 },
{ $set: { Age: 31 } }
);
1. Introduction to CRUD
CRUD operations are the fundamental functions for working with persistent data
in any database system, including relational (SQL) and non-relational (NoSQL)
databases. These operations correspond to the basic actions that allow users to
create, read, update, and delete data.
2. CRUD Operations Breakdown
a. CREATE (C)
Purpose: The CREATE operation is used to insert new records into a
database.
In SQL, the INSERT statement is used for this operation.
In NoSQL, it varies depending on the type of database (e.g., insert() in
MongoDB).
SQL Example:
INSERT INTO Employees (ID, Name, Age, Department)
VALUES (1, 'Alice', 30, 'HR');
NoSQL Example (MongoDB):
db.employees.insertOne({
ID: 1,
Name: "Alice",
Age: 30,
Department: "HR"
});
, b. READ (R)
Purpose: The READ operation is used to retrieve data from a database.
In SQL, the SELECT statement is used to retrieve data.
In NoSQL, it varies based on the database type (e.g., find() in MongoDB).
SQL Example:
SELECT Name, Age, Department
FROM Employees
WHERE Department = 'HR';
NoSQL Example (MongoDB):
db.employees.find({ Department: "HR" });
c. UPDATE (U)
Purpose: The UPDATE operation is used to modify existing records in a
database.
In SQL, the UPDATE statement is used for this operation.
In NoSQL, it varies based on the database (e.g., updateOne() in MongoDB).
SQL Example:
UPDATE Employees
SET Age = 31
WHERE ID = 1;
NoSQL Example (MongoDB):
db.employees.updateOne(
{ ID: 1 },
{ $set: { Age: 31 } }
);