0% found this document useful (0 votes)
20 views8 pages

Experiment No 7

Uploaded by

Anisha Cotta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Experiment No 7

Uploaded by

Anisha Cotta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment No :7 Date: / / 2023

Title: Modifying data in MySQL

Aim: To study the implementation of Data manipulation Language (DML)

Theory:

MySQL INSERT statement


The INSERT statement allows you to insert one or more rows into a table. The following illustrates the
syntax of the INSERT statement

In this syntax,

 First, specify the table name and a list of comma-separated columns inside parentheses after
the INSERT INTO clause.
 Then, put a comma-separated list of values of the corresponding columns inside the parentheses
following the VALUES keyword.
The number of columns and values must be the same. In addition, the positions of columns must be
corresponding with the positions of their values.

To insert multiple rows into a table using a single INSERT statement, you use the following syntax:

MySQL INSERT examples


Let’s create a new table named tasks for practicing the INSERT statement

64
1) MySQL INSERT – simple INSERT example
The following statement inserts a new row into the tasks table:

This query returns data from the tasks table

Here is the output:

In this example, we specified the values for only title and priority columns. For other columns, MySQL
uses the default values.
The task_id column is an AUTO_INCREMENT column. It means that MySQL generates a sequential
integer whenever a row is inserted into the table.
The start_date, due_date, and description columns use NULL as the default value, therefore, MySQL
uses NULL to insert into these columns if you don’t specify their values in the INSERT statement

2) MySQL INSERT – Inserting multiple rows example


The following statement inserts three rows into the tasks table:

65
MySQL UPDATE statement
The UPDATE statement modifies existing data in a table. You can also use the UPDATE statement change
values in one or more columns of a single row or multiple rows.
The following illustrates the basic syntax of the UPDATE statement:

In this syntax:

 First, specify the name of the table that you want to update data after the UPDATE keyword.
 Second, specify which column you want to update and the new value in the SET clause. To update
values in multiple columns, you use a list of comma-separated assignments by supplying a value
in each column’s assignment in the form of a literal value, an expression, or a subquery.
 Third, specify which rows to be updated using a condition in the WHERE clause.
The WHERE clause is optional. If you omit it, the UPDATE statement will update all rows in the
table.
Notice that the WHERE clause is so important that you should not forget. Sometimes, you may want to
update just one row; However, you may forget the WHERE clause and accidentally update all rows of
the table.
MySQL supports two modifiers in the UPDATE statement.

1. The LOW_PRIORITY modifier instructs the UPDATE statement to delay the update until there is no
connection reading data from the table. The LOW_PRIORITY takes effect for the storage
engines that use table-level locking only such as MyISAM, MERGE, and MEMORY.
2. The IGNORE modifier enables the UPDATE statement to continue updating rows even if errors
occurred. The rows that cause errors such as duplicate-key conflicts are not updated.

66
MySQL UPDATE examples
Let’s practice the UPDATE statement.
1) Using MySQL UPDATE to modify values in a single column example
Referring the employees table

In this example, we will update the email of Mary Patterson to the new
email [email protected].
First, find Mary’s email from the employees table using the following SELECT statement

Second, update the email address of Mary to the new email [email protected] :

In this UPDATE statement:

 The WHERE clause specifies the row with employee number 1056 will be updated.
 The SET clause sets the value of the email column to the new email.

67
2) Using MySQL UPDATE to modify values in multiple columns
To update values in the multiple columns, you need to specify the assignments in the SET clause. For
example, the following statement updates both last name and email columns of employee number 1056

MySQL DELETE statement


To delete data from a table, you use the MySQL DELETE statement. The following illustrates the syntax
of the DELETE statement:

Referring to employee table

Suppose you want to delete employees whose the officeNumber are4, you use the DELETE statement
with the WHERE clause as shown in the following query

68
To delete all rows from the employees table, you use the DELETE statement without the WHERE clause
as follows

All rows in the employees table deleted.

MySQL ON DELETE CASCADE

MySQL provides a more effective way called ON DELETE CASCADE referential action for a foreign
key that allows you to delete data from child tables automatically when you delete the data from the
parent table.
MySQL ON DELETE CASCADE example
Let’s take a look at an example of using MySQL ON DELETE CASCADE .
Suppose that we have two tables:buildings and rooms . In this database model, each building has one or
many rooms. However, each room belongs to one only one building. A room would not exist without a
building.
The relationship between the buildings and rooms tables is one-to-many (1:N) as illustrated in the
following database diagram:

When you delete a row from the buildings table, you also want to delete all rows in the rooms table
that references to the row in the buildings table. For example, when you delete a row with building no. 2
in the buildings table as the following query

You also want the rows in the rooms table that refers to building number 2 will be also removed.
The following are steps that demonstrate how the ON DELETE CASCADE referential action works.

69
Step 1. Create the buildings table:

Step 2. Create the rooms table

Notice that the ON DELETE CASCADE clause at the end of the foreign key constraint definition.
Step 3. Insert rows into the buildings table

Step 4. Query data from the buildings table:

We have two rows in the buildings table.


Step 5. Insert rows into the rooms table:

70
Step 6. Query data from the rooms table:

We have three rooms that belong to building no 1 and two rooms that belong to the building no 2.

Step 7. Delete the building with building no. 2:

Step 8. Query data from rooms table

Conclusion: Studied & implemented DML Statements.

71

You might also like