In this tutorial, we will learn how to perform CRUD (Create, Read, Update, Delete) operations in MySQL using SQL queries. We will use a sample table called employees
with the following columns: id
, first_name
, last_name
, email
, and salary
.
- Create:
To insert a new record into the employees
table, use the INSERT INTO
statement.
INSERT INTO employees (first_name, last_name, email, salary)
VALUES ('John', 'Doe', 'john.doe@example.com', 50000);
This will create a new employee record with the given values.
- Read:
To retrieve data from the employees
table, use the SELECT
statement.
SELECT * FROM employees;
This will display all the records in the employees
table.
To retrieve specific columns, use the SELECT
statement with column names.
SELECT first_name, last_name, email FROM employees;
This will display only the first_name
, last_name
, and email
columns from the employees
table.
- Update:
To update an existing record in the employees
table, use the UPDATE
statement.
UPDATE employees
SET salary = 60000
WHERE id = 1;
This will update the salary of the employee with id
1 to 60000.
- Delete:
To delete a record from the employees
table, use the DELETE
statement.
DELETE FROM employees
WHERE id = 1;
This will delete the employee record with id
1 from the employees
table.
That's it! You have now learned how to perform CRUD operations in MySQL using SQL queries. Remember to always backup your data before performing any delete operations, and use the WHERE
clause carefully to avoid deleting the wrong records.