13. Data Definition Language (DDL)
15. Data Query Language (DQL)
MySQL Tutorial - 14. Data Manipulation Language (DML)

14.1 Introduction to Data Manipulation Language (DML)

Overview of DML and its role in database management

In MySQL, DML (Data Manipulation Language) is a set of SQL commands used for managing data within a MySQL database. Here's an overview of DML commands in MySQL with examples:

  1. SELECT: Used to retrieve data from one or more tables.

    Example:

    SELECT * FROM employees;
  2. INSERT: Used to add new rows of data into a table.

    Example:

    INSERT INTO employees (first_name, last_name, age) VALUES ('John', 'Doe', 30);
  3. UPDATE: Used to modify existing data in a table.

    Example:

    UPDATE employees SET age = 31 WHERE first_name = 'John';
  4. DELETE: Used to remove one or more rows from a table.

    Example:

    DELETE FROM employees WHERE last_name = 'Doe';
  5. INSERT...SELECT: Used to insert data into a table from the result of a SELECT statement.

    Example:

    INSERT INTO new_employees (first_name, last_name, age) SELECT first_name, last_name, age FROM employees WHERE department_id = 1;
  6. REPLACE: Similar to INSERT, but if a row with the same primary key already exists, it will be deleted and then inserted.

    Example:

    REPLACE INTO employees (id, first_name, last_name, age) VALUES (1, 'Jane', 'Doe', 25);

These commands allow you to manipulate the data stored in MySQL databases. They are essential for tasks such as querying, adding, updating, and deleting data, enabling efficient data management and ensuring the integrity of your database.

Importance of DML in manipulating and retrieving data from databases

DML (Data Manipulation Language) plays a crucial role in manipulating and retrieving data from databases in MySQL. Let's explore its importance with code examples:

  1. Retrieving Data (SELECT):

    The SELECT statement is used to retrieve data from one or more tables in the database. It allows for filtering, sorting, and aggregating data.

    Example:

    SELECT * FROM employees WHERE department_id = 1;

    This query retrieves all columns from the "employees" table where the department_id is 1.

  2. Adding Data (INSERT):

    The INSERT statement adds new rows of data into a table.

    Example:

    INSERT INTO employees (first_name, last_name, age) VALUES ('John', 'Doe', 30);

    This inserts a new employee into the "employees" table with the specified first name, last name, and age.

  3. Updating Data (UPDATE):

    The UPDATE statement modifies existing data in a table.

    Example:

    UPDATE employees SET age = 31 WHERE first_name = 'John';

    This updates the age of the employee named John to 31 in the "employees" table.

  4. Deleting Data (DELETE):

    The DELETE statement removes one or more rows from a table.

    Example:

    DELETE FROM employees WHERE last_name = 'Doe';

    This deletes all rows from the "employees" table where the last name is 'Doe'.

  5. Inserting Data from Another Table (INSERT...SELECT):

    The INSERT...SELECT statement allows inserting data into a table from the result of a SELECT statement.

    Example:

    INSERT INTO new_employees (first_name, last_name, age) SELECT first_name, last_name, age FROM employees WHERE department_id = 1;

    This inserts employees from department 1 into the "new_employees" table.

DML commands are essential for interacting with the data stored in MySQL databases. They enable the manipulation of data by adding, updating, deleting, and retrieving records, providing the necessary flexibility for efficient database management.

Comparison between DML and other SQL languages (DDL, DCL)

In MySQL, SQL (Structured Query Language) is categorized into different subsets based on their functionality. The main subsets are DDL (Data Definition Language), DML (Data Manipulation Language), and DCL (Data Control Language). Let's compare these subsets with code examples in MySQL:

  1. DDL (Data Definition Language):

    DDL is used to define the structure and schema of the database. It includes commands like CREATE, ALTER, DROP, and TRUNCATE.

    Example:

    CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT );

    This creates a new table named "employees" with columns for id, first name, last name, and age.

  2. DML (Data Manipulation Language):

    DML is used to manipulate the data within the database. It includes commands like SELECT, INSERT, UPDATE, and DELETE.

    Example (SELECT):

    SELECT * FROM employees WHERE department_id = 1;

    This selects all columns from the "employees" table where the department_id is 1.

  3. DCL (Data Control Language):

    DCL is used to control access to data within the database. It includes commands like GRANT and REVOKE.

    Example:

    GRANT SELECT ON employees TO 'user'@'localhost';

    This grants the user 'user' access to select data from the "employees" table.

Comparison:

  • DDL is used to define the structure of the database, including tables, indexes, and constraints.
  • DML is used to manipulate the data within the database, including querying, inserting, updating, and deleting data.
  • DCL is used to control access to the database, including granting and revoking permissions to users.

While DDL is used to create or modify the database schema, DML is used to interact with the data stored within the schema, and DCL is used to manage access rights to the schema and its objects. Each subset serves a distinct purpose in managing databases effectively.


14.2 SELECT Statement

Retrieving data from a single table using SELECT

Certainly! Retrieving data from a single table using the SELECT statement in MySQL is quite common. Here's an example code snippet:

Let's assume we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT, department VARCHAR(50) );

And suppose it contains the following data:

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR | | 2 | Jane | Smith | 28 | Marketing | | 3 | Michael | Johnson | 35 | IT |

Now, let's retrieve all columns from the employees table:

SELECT * FROM employees;

This SQL query will return all rows and columns from the employees table:

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR | | 2 | Jane | Smith | 28 | Marketing | | 3 | Michael | Johnson | 35 | IT |

If you want to retrieve specific columns, you can specify them in the SELECT statement:

SELECT first_name, last_name, age FROM employees;

This query will return only the first_name, last_name, and age columns:

| first_name | last_name | age | |------------|-----------|-----| | John | Doe | 30 | | Jane | Smith | 28 | | Michael | Johnson | 35 |

You can also add conditions to filter the results, for example:

SELECT * FROM employees WHERE department = 'HR';

This query will return only the employees who belong to the 'HR' department:

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR |

That's how you can retrieve data from a single table using the SELECT statement in MySQL!

Specifying columns in the SELECT clause

Certainly! In MySQL, you can specify specific columns that you want to retrieve in the SELECT clause. Here's how you can do it with examples:

Let's consider we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT, department VARCHAR(50) );

And suppose it contains the following data:

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR | | 2 | Jane | Smith | 28 | Marketing | | 3 | Michael | Johnson | 35 | IT |

Now, let's specify columns in the SELECT clause:

  1. Retrieving specific columns:
SELECT first_name, last_name FROM employees;

This query will return only the first_name and last_name columns:

| first_name | last_name | |------------|-----------| | John | Doe | | Jane | Smith | | Michael | Johnson |
  1. Retrieving all columns:

If you want to retrieve all columns, you can use the asterisk (*):

SELECT * FROM employees;

This query will return all columns:

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR | | 2 | Jane | Smith | 28 | Marketing | | 3 | Michael | Johnson | 35 | IT |
  1. Adding aliases to columns:

You can also add aliases to the columns in the SELECT clause:

SELECT first_name AS "First Name", last_name AS "Last Name" FROM employees;

This query will return columns with aliases:

| First Name | Last Name | |------------|-----------| | John | Doe | | Jane | Smith | | Michael | Johnson |

That's how you can specify columns in the SELECT clause in MySQL!

Filtering data using the WHERE clause

Filtering data using the WHERE clause in MySQL allows you to retrieve specific rows based on specified conditions. Here's how you can do it with examples:

Assuming we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT, department VARCHAR(50) );

And it contains the following data:

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR | | 2 | Jane | Smith | 28 | Marketing | | 3 | Michael | Johnson | 35 | IT |

Now, let's filter the data using the WHERE clause:

  1. Filtering based on a single condition:
SELECT * FROM employees WHERE department = 'HR';

This query will return only the rows where the department is 'HR':

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR |
  1. Filtering based on multiple conditions:
SELECT * FROM employees WHERE department = 'HR' AND age > 25;

This query will return only the rows where the department is 'HR' and the age is greater than 25:

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR |
  1. Using comparison operators:
SELECT * FROM employees WHERE age BETWEEN 25 AND 30;

This query will return only the rows where the age is between 25 and 30 (inclusive):

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR | | 2 | Jane | Smith | 28 | Marketing |
  1. Filtering using string comparison:
SELECT * FROM employees WHERE last_name LIKE 'J%';

This query will return only the rows where the last name starts with 'J':

| id | first_name | last_name | age | department | |----|------------|-----------|-----|------------| | 1 | John | Doe | 30 | HR | | 3 | Michael | Johnson | 35 | IT |

These examples demonstrate how you can filter data using the WHERE clause in MySQL based on various conditions.


14.3 Joins

Performing inner joins between tables

Performing inner joins between tables in MySQL allows you to combine rows from two or more tables based on a related column between them. Here's how you can do it with examples:

Assuming we have two tables: employees and departments with the following schemas:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT, department_id INT ); CREATE TABLE departments ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) );

And suppose the employees table contains the following data:

| id | first_name | last_name | age | department_id | |----|------------|-----------|-----|---------------| | 1 | John | Doe | 30 | 1 | | 2 | Jane | Smith | 28 | 2 | | 3 | Michael | Johnson | 35 | 1 |

And the departments table contains the following data:

| id | name | |----|-----------| | 1 | HR | | 2 | Marketing |

Now, let's perform an inner join between these two tables to retrieve information about employees and their departments:

SELECT employees.first_name, employees.last_name, employees.age, departments.name AS department FROM employees INNER JOIN departments ON employees.department_id = departments.id;

This query joins the employees table with the departments table based on the department_id column in the employees table and the id column in the departments table. It retrieves the first name, last name, age of the employee, and the name of their department:

| first_name | last_name | age | department | |------------|-----------|-----|------------| | John | Doe | 30 | HR | | Jane | Smith | 28 | Marketing | | Michael | Johnson | 35 | HR |

Explanation:

  • The INNER JOIN keyword combines rows from both tables where the specified condition is met.
  • employees.department_id = departments.id specifies the condition for the join, where the department_id in the employees table matches the id in the departments table.
  • We use aliases (employees and departments) to reference the tables in the query and distinguish between the columns with the same name in both tables.

This is how you can perform inner joins between tables in MySQL to retrieve combined information from related tables.

Using aliases for table names

Aliases in MySQL allow you to assign temporary names to tables or columns in a query to make the SQL code more readable or to differentiate between tables with similar names. Here's how you can use aliases for table names in MySQL with examples:

Assuming we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT, department_id INT );

And suppose we want to use aliases for the employees table in a query:

SELECT e.id, e.first_name, e.last_name, e.age, d.name AS department FROM employees AS e INNER JOIN departments AS d ON e.department_id = d.id;

In this example:

  • e is an alias for the employees table.
  • d is an alias for the departments table.

The query retrieves data from the employees table and joins it with the departments table using the department_id column. Aliases are used to reference columns from the employees table (e.id, e.first_name, e.last_name, e.age) and the departments table (d.name).

Another example of using aliases for clarity:

SELECT emp.id, emp.first_name, emp.last_name, dept.name AS department FROM employees AS emp INNER JOIN departments AS dept ON emp.department_id = dept.id;

Here:

  • emp is an alias for the employees table.
  • dept is an alias for the departments table.

Using aliases makes the query more concise and readable, especially when dealing with long table names or when joining multiple tables.

It's important to note that while aliases are optional, they can significantly improve the readability of complex queries and are often used in practice for that reason.

Performing outer joins (LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN)

Performing outer joins (LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN) in MySQL allows you to retrieve data from multiple tables while including rows from one or both tables even if there's no matching row in the other table. Here's how you can do it with examples:

Assuming we have two tables: employees and departments with the following schemas:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), department_id INT ); CREATE TABLE departments ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) );

And suppose the employees table contains the following data:

| id | first_name | last_name | department_id | |----|------------|-----------|---------------| | 1 | John | Doe | 1 | | 2 | Jane | Smith | 2 |

And the departments table contains the following data:

| id | name | |----|-----------| | 1 | HR | | 3 | Marketing |

Now, let's perform outer joins (LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN) between these two tables:

  1. LEFT JOIN:
SELECT employees.id, employees.first_name, employees.last_name, departments.name AS department FROM employees LEFT JOIN departments ON employees.department_id = departments.id;

This query will return all rows from the employees table and the matching rows from the departments table based on the department_id, including the rows from the employees table even if there's no matching department:

| id | first_name | last_name | department | |----|------------|-----------|------------| | 1 | John | Doe | HR | | 2 | Jane | Smith | NULL |
  1. RIGHT JOIN:
SELECT employees.id, employees.first_name, employees.last_name, departments.name AS department FROM employees RIGHT JOIN departments ON employees.department_id = departments.id;

This query will return all rows from the departments table and the matching rows from the employees table based on the department_id, including the rows from the departments table even if there's no matching employee:

| id | first_name | last_name | department | |----|------------|-----------|------------| | 1 | John | Doe | HR | | NULL | NULL | NULL | Marketing |
  1. FULL OUTER JOIN:

MySQL doesn't support the FULL OUTER JOIN syntax directly. However, you can achieve similar results using a combination of LEFT JOIN and RIGHT JOIN:

SELECT employees.id, employees.first_name, employees.last_name, departments.name AS department FROM employees LEFT JOIN departments ON employees.department_id = departments.id UNION SELECT employees.id, employees.first_name, employees.last_name, departments.name AS department FROM departments RIGHT JOIN employees ON departments.id = employees.department_id;

This query combines the results of both LEFT JOIN and RIGHT JOIN to achieve a similar effect to FULL OUTER JOIN. It returns all rows from both tables, including the unmatched rows, from both sides:

| id | first_name | last_name | department | |----|------------|-----------|------------| | 1 | John | Doe | HR | | 2 | Jane | Smith | NULL | | NULL | NULL | NULL | Marketing |

These examples demonstrate how you can perform outer joins (LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN) in MySQL to retrieve data from multiple tables, including rows from one or both tables even if there's no matching row in the other table.


14.4 Subqueries

Writing subqueries within SELECT, FROM, WHERE, and HAVING clauses

Subqueries in MySQL allow you to nest one query within another query. You can use subqueries in various clauses such as SELECT, FROM, WHERE, and HAVING. Here's how you can write subqueries in each of these clauses with examples:

  1. Subquery within SELECT clause:

You can use a subquery in the SELECT clause to calculate a value based on the result of another query.

Example:

SELECT first_name, (SELECT COUNT(*) FROM employees WHERE department_id = d.id) AS num_employees FROM departments d;

This query retrieves the department name from the departments table and calculates the number of employees in each department using a subquery.

  1. Subquery within FROM clause:

You can use a subquery in the FROM clause to treat the result of the subquery as a temporary table.

Example:

SELECT * FROM (SELECT * FROM employees WHERE age > 30) AS older_employees;

This query selects all columns from a subset of employees where the age is greater than 30.

  1. Subquery within WHERE clause:

You can use a subquery in the WHERE clause to filter rows based on the result of another query.

Example:

SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'HR');

This query selects all employees who belong to the HR department.

  1. Subquery within HAVING clause:

You can use a subquery in the HAVING clause to filter groups based on the result of another query.

Example:

SELECT department_id, COUNT(*) AS num_employees FROM employees GROUP BY department_id HAVING COUNT(*) > (SELECT AVG(num_employees) FROM (SELECT department_id, COUNT(*) AS num_employees FROM employees GROUP BY department_id) AS department_counts);

This query groups employees by department and selects departments with more employees than the average number of employees per department.

These examples demonstrate how to write subqueries within various clauses in MySQL queries to perform more complex operations and achieve specific filtering or calculation requirements.

Correlated vs. non-correlated subqueries

In MySQL, subqueries can be classified into correlated and non-correlated subqueries based on whether they depend on the outer query. Let's explore the differences between them with examples:

  1. Non-correlated Subqueries:

Non-correlated subqueries are executed independently of the outer query and return a result set that is used by the outer query.

Example:

SELECT first_name, last_name FROM employees WHERE age > (SELECT AVG(age) FROM employees);

In this example, the subquery (SELECT AVG(age) FROM employees) calculates the average age of all employees. This average age is then used in the outer query to select employees whose age is greater than the calculated average.

  1. Correlated Subqueries:

Correlated subqueries are dependent on the outer query and are executed for each row processed by the outer query.

Example:

SELECT e.first_name, e.last_name FROM employees e WHERE e.age > (SELECT AVG(age) FROM employees WHERE department_id = e.department_id);

In this example, the subquery (SELECT AVG(age) FROM employees WHERE department_id = e.department_id) calculates the average age for each department. For each row processed in the outer query, the subquery is executed with a condition based on the department of the current employee (e.department_id). This makes the subquery correlated with the outer query.

Differences:

  • Independence: Non-correlated subqueries are independent of the outer query, whereas correlated subqueries are dependent on the outer query.
  • Execution: Non-correlated subqueries are executed once and their result is used by the outer query, while correlated subqueries are executed for each row processed by the outer query.
  • Performance: Correlated subqueries can potentially be less efficient than non-correlated subqueries, especially for large datasets, because they are executed repeatedly.

Both types of subqueries have their use cases and understanding when to use each type is important for writing efficient SQL queries in MySQL.

Using subqueries for filtering and aggregation

Certainly! Subqueries can be incredibly useful for filtering and aggregation in MySQL. Let's explore examples of using subqueries for filtering and aggregation:

  1. Using Subqueries for Filtering:

You can use subqueries to filter data based on conditions derived from another query result.

Example:

SELECT first_name, last_name FROM employees WHERE department_id IN ( SELECT id FROM departments WHERE name = 'HR' );

In this example, the subquery selects the department id of the 'HR' department from the departments table. The outer query then selects employees whose department_id matches any of the department ids returned by the subquery.

  1. Using Subqueries for Aggregation:

You can use subqueries to perform aggregation operations and use the result in the outer query.

Example:

SELECT department_id, AVG(age) AS avg_age FROM employees GROUP BY department_id HAVING AVG(age) > ( SELECT AVG(age) FROM employees );

In this example, the outer query calculates the average age for each department. The HAVING clause filters out departments whose average age is greater than the overall average age of all employees, which is calculated using the subquery.

  1. Using Subqueries for Both Filtering and Aggregation:

You can combine subqueries for both filtering and aggregation to perform complex queries.

Example:

SELECT department_id, COUNT(*) AS num_employees FROM employees WHERE age > ( SELECT AVG(age) FROM employees ) GROUP BY department_id;

In this example, the subquery calculates the average age of all employees. The outer query then filters employees based on whether their age is greater than the calculated average age and aggregates the count of employees for each department.

These examples demonstrate how you can leverage subqueries in MySQL for filtering and aggregation purposes, enabling you to write more complex and sophisticated queries to meet your data analysis needs.


14.5 Set Operations

Combining query results with UNION, INTERSECT, and EXCEPT

In MySQL, you can combine query results using the UNION, INTERSECT, and EXCEPT (or MINUS) operators. However, MySQL does not directly support the INTERSECT and EXCEPT operators. You can achieve similar functionality using other methods. Let's see examples of combining query results using these operators:

  1. UNION:

The UNION operator is used to combine the results of two or more SELECT statements into a single result set.

Example:

SELECT first_name, last_name FROM employees UNION SELECT first_name, last_name FROM contractors;

This query selects the first name and last name of employees and contractors and combines them into a single result set, removing duplicates.

  1. INTERSECT (Simulated):

The INTERSECT operator returns only the rows that appear in both result sets. In MySQL, you can simulate this behavior using INNER JOINs or EXISTS subqueries.

Example (Simulated INTERSECT using INNER JOIN):

SELECT first_name, last_name FROM employees INNER JOIN contractors ON employees.first_name = contractors.first_name AND employees.last_name = contractors.last_name;

This query selects the first name and last name of individuals who are both employees and contractors.

  1. EXCEPT or MINUS (Simulated):

The EXCEPT (or MINUS) operator returns only the rows that appear in the first result set but not in the second result set. In MySQL, you can simulate this behavior using LEFT JOINs or NOT EXISTS subqueries.

Example (Simulated EXCEPT using LEFT JOIN):

SELECT first_name, last_name FROM employees LEFT JOIN contractors ON employees.first_name = contractors.first_name AND employees.last_name = contractors.last_name WHERE contractors.first_name IS NULL;

This query selects the first name and last name of individuals who are employees but not contractors.

It's important to note that while MySQL does not directly support INTERSECT and EXCEPT operators, you can achieve similar functionality using other methods like INNER JOINs, LEFT JOINs, EXISTS subqueries, or NOT EXISTS subqueries. These alternatives provide flexibility in combining query results to meet your specific requirements.

Understanding set operations and their usage

Set operations in MySQL allow you to perform operations such as union, intersection, and difference on the result sets of two or more SELECT queries. These set operations include UNION, INTERSECT, and EXCEPT (or MINUS). Let's understand each of these operations and their usage in MySQL with examples:

  1. UNION:

The UNION operator is used to combine the results of two or more SELECT queries into a single result set. It removes duplicate rows by default.

Example:

SELECT first_name, last_name FROM employees UNION SELECT first_name, last_name FROM contractors;

This query selects the first name and last name of employees and contractors and combines them into a single result set, removing duplicates.

  1. INTERSECT:

The INTERSECT operator returns only the rows that appear in both result sets. However, MySQL does not directly support the INTERSECT operator. You can simulate this behavior using INNER JOINs or EXISTS subqueries.

Example (Simulated INTERSECT using INNER JOIN):

SELECT first_name, last_name FROM employees INNER JOIN contractors ON employees.first_name = contractors.first_name AND employees.last_name = contractors.last_name;

This query selects the first name and last name of individuals who are both employees and contractors.

  1. EXCEPT (or MINUS):

The EXCEPT (or MINUS) operator returns only the rows that appear in the first result set but not in the second result set. However, MySQL does not directly support the EXCEPT operator. You can simulate this behavior using LEFT JOINs or NOT EXISTS subqueries.

Example (Simulated EXCEPT using LEFT JOIN):

SELECT first_name, last_name FROM employees LEFT JOIN contractors ON employees.first_name = contractors.first_name AND employees.last_name = contractors.last_name WHERE contractors.first_name IS NULL;

This query selects the first name and last name of individuals who are employees but not contractors.

These set operations are useful for combining, comparing, and manipulating result sets in MySQL queries, enabling you to perform various data analysis and reporting tasks efficiently.


14.6 INSERT Statement

Inserting data into a table using INSERT INTO

Sure! The INSERT INTO statement in MySQL is used to add new rows of data into a table. Here's how you can use it with an example:

Suppose we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT );

Now, let's insert some data into the employees table using the INSERT INTO statement:

Example:

INSERT INTO employees (first_name, last_name, age) VALUES ('John', 'Doe', 30);

This statement inserts a new row into the employees table with the specified values for the first_name, last_name, and age columns.

You can also insert multiple rows at once by separating each set of values with commas:

INSERT INTO employees (first_name, last_name, age) VALUES ('Jane', 'Smith', 28), ('Michael', 'Johnson', 35);

This statement inserts two rows into the employees table with the specified values.

If the table has an auto-increment primary key column (like id in our example), you don't need to specify a value for that column. MySQL will automatically generate a unique value for it:

INSERT INTO employees (first_name, last_name, age) VALUES ('Emily', 'Davis', 25);

This statement inserts a new row into the employees table with an automatically generated id.

After executing these INSERT INTO statements, the employees table will contain the following data:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 | | 2 | Jane | Smith | 28 | | 3 | Michael | Johnson | 35 | | 4 | Emily | Davis | 25 |

That's how you can use the INSERT INTO statement to add data into a table in MySQL.

Inserting data from another table with SELECT INTO

In MySQL, you can use the INSERT INTO ... SELECT statement to insert data into a table from the result of a SELECT query. However, MySQL does not support the SELECT INTO syntax. Instead, you can use the INSERT INTO ... SELECT statement for the same purpose. Here's how you can do it with an example:

Suppose we have an existing table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT );

And we have another table named new_employees with the same structure but with some data:

CREATE TABLE new_employees ( first_name VARCHAR(50), last_name VARCHAR(50), age INT );

Now, let's insert data from the new_employees table into the employees table using the INSERT INTO ... SELECT statement:

Example:

INSERT INTO employees (first_name, last_name, age) SELECT first_name, last_name, age FROM new_employees;

This statement inserts data into the employees table by selecting values from the new_employees table. It inserts the first_name, last_name, and age columns from the new_employees table into the corresponding columns of the employees table.

If you want to insert data into specific columns of the target table, you can specify them in the INSERT INTO part of the statement:

INSERT INTO employees (first_name, last_name, age) SELECT first_name, last_name, age FROM new_employees WHERE age > 25;

This statement inserts data into the employees table only for those rows from the new_employees table where the age is greater than 25.

After executing the INSERT INTO ... SELECT statement, the employees table will contain the data from the new_employees table that meets the specified criteria.

That's how you can insert data from another table using the INSERT INTO ... SELECT statement in MySQL.


14.7 UPDATE Statement

Updating existing records in a table with UPDATE

The UPDATE statement in MySQL is used to modify existing records in a table. Here's how you can use it with an example:

Suppose we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT );

And it contains the following data:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 | | 2 | Jane | Smith | 28 | | 3 | Michael | Johnson | 35 |

Now, let's say we want to update the age of the employee with the id 2 from 28 to 29:

Example:

UPDATE employees SET age = 29 WHERE id = 2;

This statement updates the age column of the employee with the id 2 to 29.

After executing the UPDATE statement, the employees table will be modified as follows:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 | | 2 | Jane | Smith | 29 | <-- Updated age | 3 | Michael | Johnson | 35 |

You can also update multiple columns at once:

Example:

UPDATE employees SET first_name = 'Robert', last_name = 'Williams' WHERE id = 3;

This statement updates the first_name and last_name columns of the employee with the id 3 to 'Robert' and 'Williams', respectively.

After executing this statement, the employees table will be modified as follows:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 | | 2 | Jane | Smith | 29 | | 3 | Robert | Williams | 35 | <-- Updated first_name and last_name

That's how you can use the UPDATE statement to modify existing records in a table in MySQL.

Specifying conditions for updating records with the WHERE clause

Certainly! In MySQL, you can specify conditions for updating records using the WHERE clause in the UPDATE statement. This allows you to update only those records that meet certain criteria. Here's how you can do it with an example:

Suppose we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT );

And it contains the following data:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 | | 2 | Jane | Smith | 28 | | 3 | Michael | Johnson | 35 |

Now, let's say we want to update the age of employees who are older than 30 to 40:

Example:

UPDATE employees SET age = 40 WHERE age > 30;

This statement updates the age column of employees whose age is greater than 30 to 40.

After executing the UPDATE statement, the employees table will be modified as follows:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 40 | <-- Updated age | 2 | Jane | Smith | 28 | | 3 | Michael | Johnson | 40 | <-- Updated age

You can also combine multiple conditions using logical operators (AND, OR) in the WHERE clause to specify more complex criteria for updating records.

Example:

UPDATE employees SET age = 45 WHERE age > 30 AND last_name = 'Johnson';

This statement updates the age column of employees whose age is greater than 30 and whose last name is 'Johnson' to 45.

After executing this statement, the employees table will be modified as follows:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 40 | | 2 | Jane | Smith | 28 | | 3 | Michael | Johnson | 45 | <-- Updated age

That's how you can use the WHERE clause to specify conditions for updating records in MySQL.


14.8 DELETE Statement

Deleting records from a table using DELETE FROM

In MySQL, you can use the DELETE FROM statement to remove records from a table based on specified conditions. Here's how you can do it with an example:

Suppose we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT );

And it contains the following data:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 | | 2 | Jane | Smith | 28 | | 3 | Michael | Johnson | 35 |

Now, let's say we want to delete the employee with the id 2:

Example:

DELETE FROM employees WHERE id = 2;

This statement deletes the record with the id 2 from the employees table.

After executing the DELETE FROM statement, the employees table will be modified as follows:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 | | 3 | Michael | Johnson | 35 |

You can also use the DELETE FROM statement without a WHERE clause to delete all records from a table:

Example:

DELETE FROM employees;

This statement removes all records from the employees table.

After executing this statement, the employees table will be empty.

It's important to use the DELETE FROM statement with caution, especially without a WHERE clause, as it can remove all records from a table, resulting in data loss.

That's how you can use the DELETE FROM statement to remove records from a table in MySQL.

Specifying conditions for deleting records with the WHERE clause

Certainly! In MySQL, you can use the WHERE clause with the DELETE statement to specify conditions for deleting records. This allows you to remove only those records that meet certain criteria. Here's how you can do it with an example:

Suppose we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT );

And it contains the following data:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 | | 2 | Jane | Smith | 28 | | 3 | Michael | Johnson | 35 |

Now, let's say we want to delete employees who are younger than 30:

Example:

DELETE FROM employees WHERE age < 30;

This statement deletes records from the employees table where the age is less than 30.

After executing the DELETE statement, the employees table will be modified as follows:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 | | 3 | Michael | Johnson | 35 |

You can also combine multiple conditions using logical operators (AND, OR) in the WHERE clause to specify more complex criteria for deleting records.

Example:

DELETE FROM employees WHERE age > 30 AND last_name = 'Johnson';

This statement deletes records from the employees table where the age is greater than 30 and the last name is 'Johnson'.

After executing this statement, the employees table will be modified as follows:

| id | first_name | last_name | age | |----|------------|-----------|-----| | 1 | John | Doe | 30 |

That's how you can use the WHERE clause to specify conditions for deleting records in MySQL.


14.9 Transactions and Transaction Control

Introduction to transactions in database management

In MySQL, a transaction is a sequence of one or more SQL statements that are treated as a single unit of work. Transactions ensure data integrity by allowing multiple operations to be performed atomically, either all succeeding or all failing.

Here's an introduction to transactions in MySQL with an example:

  1. Starting a Transaction: To start a transaction in MySQL, you use the START TRANSACTION or BEGIN statement.

Example:

START TRANSACTION;

or

BEGIN;
  1. Performing Operations within a Transaction: Once the transaction is started, you can execute multiple SQL statements that form a logical unit of work.

Example:

UPDATE accounts SET balance = balance - 100 WHERE account_id = 1; UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;

In this example, two SQL statements are executed within the transaction. The first statement deducts 100 units from the balance of account_id 1, and the second statement adds 100 units to the balance of account_id 2.

  1. Committing a Transaction: To make the changes performed within the transaction permanent, you use the COMMIT statement.

Example:

COMMIT;

This statement commits all the changes made within the transaction. Once committed, the changes are saved to the database permanently.

  1. Rolling Back a Transaction: If you encounter an error or need to undo the changes made within a transaction, you can use the ROLLBACK statement.

Example:

ROLLBACK;

This statement rolls back (undoes) all the changes made within the transaction, reverting the database to its state before the transaction started.

Here's a complete example of a transaction in MySQL:

START TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE account_id = 1; UPDATE accounts SET balance = balance + 100 WHERE account_id = 2; COMMIT;

In this example, the transaction deducts 100 units from the balance of account_id 1 and adds 100 units to the balance of account_id 2. If any error occurs during the transaction, you can use ROLLBACK to undo the changes made so far.

Transactions in MySQL provide ACID properties (Atomicity, Consistency, Isolation, Durability), ensuring that database operations are reliable and maintain data integrity.

Committing and rolling back transactions

Certainly! Let's demonstrate how to commit and roll back transactions in MySQL with examples:

  1. Committing a Transaction:

To commit a transaction in MySQL, you use the COMMIT statement. This statement makes all the changes performed within the transaction permanent.

Example:

START TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE account_id = 1; UPDATE accounts SET balance = balance + 100 WHERE account_id = 2; COMMIT;

In this example, the transaction deducts 100 units from the balance of account_id 1 and adds 100 units to the balance of account_id 2. The COMMIT statement commits all these changes to the database, making them permanent.

  1. Rolling Back a Transaction:

To roll back a transaction in MySQL, you use the ROLLBACK statement. This statement undoes all the changes made within the transaction, reverting the database to its state before the transaction started.

Example:

START TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE account_id = 1; UPDATE accounts SET balance = balance + 100 WHERE account_id = 2; -- Simulate an error SELECT 1/0; ROLLBACK;

In this example, the transaction performs the same updates as in the previous example. However, before committing the changes, an error is deliberately triggered by dividing by zero in the SELECT 1/0 statement. As a result, the transaction is rolled back using the ROLLBACK statement, and all the changes made within the transaction are undone.

These examples demonstrate how to commit and roll back transactions in MySQL. Committing a transaction makes the changes permanent, while rolling back a transaction undoes all the changes, ensuring data consistency and integrity.

Setting transaction isolation levels

In MySQL, you can set transaction isolation levels to control how transactions interact with each other and with data concurrently accessed by other transactions. MySQL supports several isolation levels, including READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE. You can set the isolation level for a session or for individual transactions. Let's see how to do this with examples:

  1. Setting Transaction Isolation Level for a Session:

To set the transaction isolation level for a session in MySQL, you can use the SET TRANSACTION ISOLATION LEVEL statement.

Example:

SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

This statement sets the transaction isolation level for the current session to READ COMMITTED. All subsequent transactions within this session will use the READ COMMITTED isolation level unless explicitly overridden.

  1. Setting Transaction Isolation Level for an Individual Transaction:

To set the transaction isolation level for an individual transaction in MySQL, you can include the isolation level as part of the START TRANSACTION statement.

Example:

START TRANSACTION READ COMMITTED;

This statement starts a new transaction with the READ COMMITTED isolation level. All subsequent operations within this transaction will use the READ COMMITTED isolation level until the transaction is committed or rolled back.

Here's a brief overview of each isolation level:

  • READ UNCOMMITTED: Allows transactions to read data that has been modified by other transactions but not yet committed. This level offers the lowest level of isolation and does not guarantee data consistency.
  • READ COMMITTED: Ensures that transactions only read data that has been committed by other transactions. This level prevents dirty reads but allows non-repeatable reads and phantom reads.
  • REPEATABLE READ: Ensures that transactions always see the same snapshot of data throughout the transaction. This level prevents dirty reads and non-repeatable reads but allows phantom reads.
  • SERIALIZABLE: Provides the highest level of isolation by ensuring that transactions are completely isolated from each other. This level prevents dirty reads, non-repeatable reads, and phantom reads, but it can lead to increased contention and reduced concurrency.

You can choose the appropriate isolation level based on your application's requirements for consistency, concurrency, and performance.


14.10 Data Manipulation with Views

Updating data through views using the WITH CHECK OPTION clause

In MySQL, you can update data through views using the WITH CHECK OPTION clause to ensure that any data modifications made through the view meet the specified criteria defined by the view's query. This ensures that only data that satisfies the view's conditions can be modified. Here's how you can do it with an example:

Suppose we have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), department VARCHAR(50) );

And we have a view named sales_employees that selects only the employees who belong to the 'Sales' department:

CREATE VIEW sales_employees AS SELECT id, first_name, last_name, department FROM employees WHERE department = 'Sales';

Now, let's say we want to update data through the sales_employees view and ensure that only employees from the 'Sales' department can be modified. We can use the WITH CHECK OPTION clause when creating the view:

CREATE VIEW sales_employees AS SELECT id, first_name, last_name, department FROM employees WHERE department = 'Sales' WITH CHECK OPTION;

Now, let's try to update data through the sales_employees view:

UPDATE sales_employees SET department = 'Marketing' WHERE id = 1;

If the employee with id 1 belongs to the 'Sales' department, the update will succeed. However, if the employee belongs to a different department, the update will fail with an error because it violates the conditions specified by the WITH CHECK OPTION clause.

Using the WITH CHECK OPTION clause ensures that any modifications made through the view comply with the conditions defined by the view's query, providing an additional layer of data integrity and security.

It's important to note that the WITH CHECK OPTION clause only applies to INSERT, UPDATE, and DELETE operations made through the view. It does not affect direct modifications made to the underlying tables.

Inserting and deleting data through views

In MySQL, you can insert and delete data through views, which act as virtual representations of data from one or more underlying tables. When you insert or delete data through a view, the underlying tables are affected accordingly. Here's how you can do it with examples:

  1. Inserting Data Through a View:

To insert data through a view in MySQL, you can use the INSERT INTO statement as you would with a regular table. However, there are certain conditions to consider:

  • The view must be updatable, meaning it must meet certain criteria defined by MySQL for insertability.
  • The columns being inserted must be from the underlying table(s) of the view.

Example: Suppose we have a view named sales_employees that selects employees from the 'Sales' department:

CREATE VIEW sales_employees AS SELECT id, first_name, last_name, department FROM employees WHERE department = 'Sales';

To insert a new employee into the sales_employees view, you can use the INSERT INTO statement:

INSERT INTO sales_employees (first_name, last_name, department) VALUES ('Alice', 'Smith', 'Sales');

This statement inserts a new employee named 'Alice Smith' into the employees table, and since she belongs to the 'Sales' department, she will also appear in the sales_employees view.

  1. Deleting Data Through a View:

To delete data through a view in MySQL, you can use the DELETE FROM statement as you would with a regular table. However, similar to inserting data, there are conditions to consider:

  • The view must be updatable.
  • The rows being deleted must be from the underlying table(s) of the view.

Example: Continuing with the sales_employees view example, let's say we want to delete an employee named 'John Doe' from the 'Sales' department:

DELETE FROM sales_employees WHERE first_name = 'John' AND last_name = 'Doe';

This statement deletes the row corresponding to 'John Doe' from the employees table, and since the view is based on the same criteria, the row will also be removed from the sales_employees view.

It's important to ensure that the underlying table(s) of the view are correctly updated when inserting or deleting data through views to maintain data integrity.


14.11 Data Manipulation Best Practices and Performance

Best practices for efficient data manipulation

Efficient data manipulation in MySQL involves optimizing queries, using appropriate indexes, and following best practices for data modeling and schema design. Here are some best practices for efficient data manipulation in MySQL:

  1. Optimize Queries:

    • Use the EXPLAIN statement to analyze query execution plans and identify performance bottlenecks.
    • Use appropriate indexes to speed up query execution. Index columns commonly used in WHERE clauses, JOIN conditions, and ORDER BY clauses.
    • Avoid using SELECT * when fetching data. Instead, explicitly specify only the columns you need.
    • Use efficient JOIN techniques such as INNER JOIN, LEFT JOIN, and WHERE clauses for filtering.
    • Minimize the use of subqueries and use joins whenever possible.
    • Use aggregate functions like COUNT(), SUM(), AVG(), etc., judiciously, and ensure they are properly indexed.
    • Use LIMIT clause when fetching large result sets to avoid excessive memory consumption.
  2. Optimize Data Types:

    • Use appropriate data types for columns to minimize storage space and improve query performance. For example, use INT for integer values instead of VARCHAR.
    • Avoid using excessively large data types if not necessary, as they consume more memory and disk space.
    • Consider the range and precision requirements of your data when choosing numeric data types.
  3. Optimize Indexes:

    • Regularly analyze and optimize indexes to ensure they are being used efficiently.
    • Avoid over-indexing as it can slow down data modification operations (INSERT, UPDATE, DELETE).
    • Use composite indexes for queries involving multiple columns in the WHERE clause, JOIN conditions, or ORDER BY clauses.
  4. Normalize Data:

    • Normalize your database schema to reduce data redundancy and improve data integrity.
    • Break down large tables into smaller ones to avoid storing unrelated data in the same table.
  5. Optimize Disk I/O:

    • Use SSD storage for better read and write performance compared to traditional HDDs.
    • Optimize disk I/O by distributing data across multiple disks and using RAID configurations.
  6. Use Stored Procedures:

    • Use stored procedures for frequently executed tasks to reduce network overhead and improve performance.
    • Stored procedures can be precompiled and optimized by the MySQL server, resulting in faster execution.
  7. Monitor Performance:

    • Regularly monitor database performance using tools like MySQL Workbench, pt-query-digest, or Percona Monitoring and Management (PMM).
    • Identify slow queries, bottlenecks, and areas for optimization.

Example:

CREATE INDEX idx_last_name ON employees (last_name); SELECT first_name, last_name FROM employees WHERE last_name = 'Smith' ORDER BY first_name LIMIT 10;

In this example, an index is created on the last_name column to speed up the query. The query fetches the first 10 employees with the last name 'Smith' and orders them by their first names.

By following these best practices, you can optimize data manipulation operations in MySQL for improved performance and scalability.

Optimizing DML statements for performance

Optimizing Data Manipulation Language (DML) statements for performance in MySQL involves various techniques such as indexing, query optimization, and schema design. Here are some strategies for optimizing DML statements:

  1. Use Indexes:
    • Ensure that columns involved in WHERE clauses, JOIN conditions, and ORDER BY clauses are properly indexed.
    • Use composite indexes for queries involving multiple columns.
    • Regularly analyze and optimize indexes to ensure they are being used efficiently.

Example:

CREATE INDEX idx_last_name ON employees (last_name);
  1. Limit the Number of Rows Affected:
    • Use the WHERE clause to limit the number of rows affected by UPDATE and DELETE statements.
    • Use the LIMIT clause when selecting a large number of rows to avoid excessive memory consumption.

Example:

DELETE FROM employees WHERE age > 50;
  1. Batch Processing:
    • Break down large DML operations into smaller batches to reduce the impact on system resources.
    • Commit transactions periodically to release locks and free up resources.

Example:

START TRANSACTION; -- Execute batch of DML statements COMMIT;
  1. Avoid Unnecessary Operations:
    • Minimize the use of expensive operations such as full table scans and unnecessary sorting.
    • Use appropriate JOIN techniques to reduce the number of rows processed.

Example:

SELECT * FROM orders WHERE order_date >= '2024-01-01' AND order_date < '2024-02-01';
  1. Optimize Query Performance:
    • Analyze and optimize DML queries using tools like EXPLAIN to understand query execution plans.
    • Rewrite queries to use more efficient techniques such as INNER JOIN instead of subqueries.

Example:

UPDATE employees INNER JOIN departments ON employees.department_id = departments.id SET employees.salary = employees.salary * 1.1 WHERE departments.name = 'Engineering';
  1. Avoid Auto-commit:
    • Disable auto-commit mode when executing multiple DML statements within a transaction to improve performance.
    • Use explicit transactions to control when changes are committed to the database.

Example:

SET AUTOCOMMIT = 0; START TRANSACTION; -- Execute multiple DML statements COMMIT;
  1. Optimize Disk I/O:
    • Optimize disk I/O by distributing data across multiple disks and using RAID configurations.
    • Use SSD storage for better read and write performance compared to traditional HDDs.

By implementing these optimization techniques, you can improve the performance of DML statements in MySQL and enhance the overall efficiency of your database operations.

Using indexes and query optimization techniques

Using indexes and query optimization techniques is crucial for improving the performance of MySQL queries. Here are some strategies along with examples:

  1. Create Indexes:
    • Identify columns frequently used in WHERE clauses, JOIN conditions, and ORDER BY clauses, and create indexes on those columns.
    • Use composite indexes for queries involving multiple columns.

Example:

CREATE INDEX idx_last_name ON employees (last_name);
  1. Optimize Queries:
    • Minimize the use of expensive operations like full table scans and unnecessary sorting.
    • Rewrite queries to use more efficient techniques such as INNER JOIN instead of subqueries.
    • Use appropriate JOIN types (INNER JOIN, LEFT JOIN) based on your data and query requirements.

Example:

SELECT e.first_name, e.last_name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;
  1. Use WHERE Clause Efficiently:
    • Use the WHERE clause to filter rows early in the query execution process.
    • Avoid applying functions on indexed columns in the WHERE clause as it can prevent index usage.

Example:

SELECT * FROM employees WHERE department_id = 1 AND age > 30;
  1. Avoid SELECT * and Use SELECT Column List:
    • Instead of fetching all columns using SELECT *, specify only the required columns in the SELECT list.
    • This reduces unnecessary data transfer and improves query performance.

Example:

SELECT first_name, last_name FROM employees WHERE department_id = 1;
  1. Use EXPLAIN to Analyze Query Execution Plans:
    • Use the EXPLAIN statement to understand how MySQL executes a query and identify potential performance bottlenecks.
    • Analyze the output to see which indexes are used, the order of table access, and the number of rows examined.

Example:

EXPLAIN SELECT * FROM employees WHERE department_id = 1;
  1. Avoid LIKE Operator at the Beginning of a Search String:
    • Using LIKE with a wildcard at the beginning of a search string (e.g., LIKE '%keyword') can prevent index usage and result in slow query performance.
    • Try to avoid leading wildcards in LIKE patterns whenever possible.

Example:

SELECT * FROM products WHERE product_name LIKE 'keyword%';
  1. Regularly Analyze and Optimize Indexes:
    • Regularly monitor and analyze index usage and performance using tools like MySQL Workbench or pt-query-digest.
    • Identify unused or redundant indexes and remove them to improve INSERT and UPDATE performance.

Example:

SHOW INDEX FROM employees;

By applying these techniques and continuously monitoring query performance, you can optimize MySQL queries for better efficiency and scalability.


14.12 Advanced DML Techniques

Using MERGE statement for performing insert, update, and delete operations in a single statement

In MySQL, there isn't a MERGE statement like in some other database systems such as Oracle or SQL Server. However, you can achieve similar functionality using a combination of INSERT, UPDATE, and DELETE statements within a transaction to perform insert, update, and delete operations in a single logical unit of work.

Here's how you can accomplish this:

  1. Start a transaction.
  2. Use INSERT INTO ... ON DUPLICATE KEY UPDATE statement to handle both insertion and updating of existing records.
  3. Use DELETE statement to remove records that no longer exist in the source data.
  4. Commit the transaction to make the changes permanent.

Example:

Suppose you have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), department VARCHAR(50) );

And you have new data to merge into the employees table. You want to insert new records, update existing records if they already exist (based on the id), and delete any records that are not present in the new data.

Here's how you can achieve this:

START TRANSACTION; -- Step 1: Insert new records and update existing records INSERT INTO employees (id, first_name, last_name, department) VALUES (1, 'John', 'Doe', 'Sales'), (2, 'Jane', 'Smith', 'Marketing'), (3, 'Michael', 'Johnson', 'HR') ON DUPLICATE KEY UPDATE first_name = VALUES(first_name), last_name = VALUES(last_name), department = VALUES(department); -- Step 2: Delete records that are not present in the new data DELETE FROM employees WHERE id NOT IN (1, 2, 3); COMMIT;

In this example:

  • The INSERT INTO ... ON DUPLICATE KEY UPDATE statement inserts new records into the employees table. If a record with the same id already exists, it updates the existing record with the values from the new data.
  • The DELETE statement removes any records from the employees table whose id is not included in the new data.

By using a transaction, you ensure that the operations are performed atomically, maintaining data integrity. While MySQL doesn't have a built-in MERGE statement, this approach achieves similar functionality.

Using the OUTPUT clause to return information about affected rows

In MySQL, there isn't a direct equivalent to the OUTPUT clause found in SQL Server. However, you can achieve similar functionality using various techniques, such as using stored procedures or triggers.

Here's an example of how you can use a stored procedure to simulate the OUTPUT clause to return information about affected rows:

Suppose you have a table named employees with the following schema:

CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), department VARCHAR(50) );

Now, let's create a stored procedure that inserts a new record into the employees table and returns information about the affected row:

DELIMITER // CREATE PROCEDURE InsertEmployee ( IN p_first_name VARCHAR(50), IN p_last_name VARCHAR(50), IN p_department VARCHAR(50), OUT p_inserted_id INT, OUT p_inserted_first_name VARCHAR(50), OUT p_inserted_last_name VARCHAR(50), OUT p_inserted_department VARCHAR(50) ) BEGIN -- Insert new record into employees table INSERT INTO employees (first_name, last_name, department) VALUES (p_first_name, p_last_name, p_department); -- Get the ID of the inserted row SET p_inserted_id = LAST_INSERT_ID(); -- Retrieve information about the inserted row SELECT first_name, last_name, department INTO p_inserted_first_name, p_inserted_last_name, p_inserted_department FROM employees WHERE id = p_inserted_id; END // DELIMITER ;

In this stored procedure:

  • Parameters p_first_name, p_last_name, and p_department are used to specify the values of the new employee.
  • Output parameters p_inserted_id, p_inserted_first_name, p_inserted_last_name, and p_inserted_department are used to return information about the affected row.
  • The LAST_INSERT_ID() function retrieves the ID of the last inserted row.
  • The SELECT statement retrieves the values of the inserted row based on its ID and assigns them to the output parameters.

You can then call this stored procedure to insert a new employee and retrieve information about the affected row:

CALL InsertEmployee('John', 'Doe', 'Sales', @inserted_id, @first_name, @last_name, @department); SELECT @inserted_id, @first_name, @last_name, @department;

This approach allows you to simulate the functionality of the OUTPUT clause in MySQL by using a stored procedure to return information about affected rows.

Using Common Table Expressions (CTEs) for complex data manipulation

In MySQL, you can use Common Table Expressions (CTEs) to perform complex data manipulation tasks by defining temporary result sets within a query. CTEs can improve query readability and maintainability by breaking down complex operations into smaller, more manageable parts.

Here's how you can use CTEs in MySQL with an example:

Suppose we have a table named sales_data that stores information about sales transactions:

CREATE TABLE sales_data ( id INT AUTO_INCREMENT PRIMARY KEY, product_id INT, sale_date DATE, amount DECIMAL(10, 2) );

And we have another table named products that stores information about products:

CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), category VARCHAR(50) );

Now, let's say we want to retrieve the total sales amount for each product category. We can achieve this using a CTE to calculate the total sales amount for each category:

WITH category_sales AS ( SELECT p.category, SUM(s.amount) AS total_sales FROM sales_data s INNER JOIN products p ON s.product_id = p.id GROUP BY p.category ) SELECT category, total_sales FROM category_sales;

In this example:

  1. We define a CTE named category_sales that calculates the total sales amount for each product category.
  2. Within the CTE, we join the sales_data table with the products table to get the category for each product and sum up the sales amounts for each category using the SUM() function.
  3. Finally, we select the category and total sales amount from the category_sales CTE.

CTEs can also be used recursively to perform hierarchical queries or to simplify complex joins and subqueries. They provide a powerful tool for organizing and structuring complex data manipulation tasks in MySQL queries.


13. Data Definition Language (DDL)
15. Data Query Language (DQL)