0% found this document useful (0 votes)
43 views

Lab 6 SQL Insert Update Delete 1

Uploaded by

Boutchi TMN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Lab 6 SQL Insert Update Delete 1

Uploaded by

Boutchi TMN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

School of Engineering Applied Sciences and Technology

LABORATORY MANUAL
BCS 306 - Database Management Systems

Spring 2024

Lab 6

INSERT, UPDATE, DELETE Operations


1. Lab Objectives:
Understand the Basics of INSERT, UPDATE, and DELETE Operations in
MySQL:
 Learn how to add new records to a table using the INSERT statement.
 Understand the process of updating existing records in a table.
 Learn how to remove records from a table using the DELETE statement.

2. MySQL INSERT Statement


MySQL INSERT statement is used to store or add data in MySQL table within
the database. We can perform insertion of records in two ways using a single
query in MySQL:

1. Insert record in a single row


2. Insert record in multiple rows

Syntax:
The below is generic syntax of SQL INSERT INTO command to insert a
single record in MySQL table:

1. INSERT INTO table_name ( field1, field2,...fieldN )


2. VALUES
3. ( value1, value2,...valueN );

If we want to insert multiple records within a single command, use the


following statement:

1. INSERT INTO table_name VALUES


2. ( value1, value2,...valueN )
3. ( value1, value2,...valueN )
4. ...........
5. ( value1, value2,...valueN );

3. MySQL UPDATE Query


MySQL UPDATE query is a DML statement used to modify the data of the
MySQL table within the database. In a real-life scenario, records are changed
over a period of time. So, we need to make changes in the values of the
tables also. To do so, it is required to use the UPDATE query.

The UPDATE statement is used with the SET and WHERE clauses. The SET
clause is used to change the values of the specified column. We can update
single or multiple columns at a time.

Syntax
Following is a generic syntax of UPDATE command to modify data into
the MySQL table:

1. UPDATE table_name
2. SET column_name1 = new-value1,
3. column_name2=new-value2, ...
4. [WHERE Clause]

4. MySQL DELETE Statement


MySQL DELETE statement is used to remove records from the MySQL table
that is no longer required in the database. This query in MySQL deletes a
full row from the table and produces the count of deleted rows. It
also allows us to delete more than one record from the table within a single
query, which is beneficial while removing large numbers of records from a
table. By using the delete statement, we can also remove data based on
conditions.

Once we delete the records using this query, we cannot recover it.
Therefore before deleting any records from the table, it is recommended
to create a backup of your database. The database backups allow us to
restore the data whenever we need it in the future.

Syntax:

The following are the syntax that illustrates how to use the DELETE
statement:

DELETE FROM table_name WHERE condition;


5. Exercises for queries: (to able to answer this, you must download and
import this Database)
5.1. Insert a new product in table “products” with the following details:
Name: "Airrpods"
Quantity in stock: 20
Unit price: $20.2

EVIDENCE #1
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.

5.2. Add a new customer in “customers” table:


First name: "Ahmed"
Last name: "Seyam"
Birth date: 1990-05-15
Phone: "555-1234"
Address: "123 Main Street"
City: "Dubai"
State: "DU"
Points: 100

EVIDENCE #2
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.

5.3. add a new order item for an existing order in “order_items” table:
Order ID: 1
Product ID: 3
Quantity: 5
Unit price: $15.99

EVIDENCE #3
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
5.4. Update the quantity in stock for the product with ID 2 to be 30 in table “products”.

EVIDENCE #4
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
5.5. Update the points for all customers by 10 in the "customers" table of the sql_store
database.

EVIDENCE #5
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
5.6. Update the points of all customers whose birthdate is prior to the year 1990 by adding
100 points to each eligible customer.

EVIDENCE #6
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
5.7. Update the shipment date to be ‘2023-10-02' for an existing order in the "orders" table
of the sql_store database. Choose any order id you want

EVIDENCE #7
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
5.8. Delete a customer with the last name "Betchley" from the "customers" table in the
sql_store database.

EVIDENCE #8
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
5.9. Delete a shipper with id 5 from the "shippers" table in the sql_store database,
ensuring that no orders reference this shipper.

EVIDENCE #9
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
5.10. Delete a product with id 10 from the "products" table in the sql_store database,
ensuring that all related order items are automatically deleted.

EVIDENCE #10
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.

You might also like