Lab09 Mysql
Lab09 Mysql
Lab09 Mysql
- INSERT statement
- UPDATE statement
- DELETE statement
1. INSERT statement
The INSERT statement allows adding data rows to a specific table. There are two variants of
the INSERT statement: the first way is to add a row of values, the second way is to add a set of
rows returned from a SELECT statement.
INSERT creates a new row in the table <table_name>. The new line contains the values
specified by the expressions in the VALUES list. If column_name is not included, the sequence
of columns in the table <table_name> is used. If column_name is included, this way, new data
is added to the table by specifying the column names and data for each column.
If the value is unknown, you can use the keyword NULL. Use the default value with the keyword
DEFAULT.
The results of the above command can be verified using the query statement:
The result is a new row of data written to the end of the data table.
Note: If the column names are not specified, when the order of the columns changes, SQL may
put the values in the wrong places. Therefore a good way to avoid this is to determine the column
names associated with the data when adding data to the table.
Add multiple lines with the SELECT statement
Instead of providing data directly, it is possible to select from other tables using the SELECT
statement.
The result of the above command can be verified using the query statement:
2. UPDATE statement
UPDATE statement is used to update the data that already exists in the database tables. The
statement can be used to change the values of a row, a group of rows, or even all rows in a table.
• After the UPDATE keyword, the name of the change table. The SET clause specifies the
column to change and the value to change. The changed value can be a fixed value, an
expression or even a subquery.
• The WHERE clause identifies the table rows to be updated. If the WHERE clause is omitted,
all table rows will be updated.
• WHERE clause is very important and should not be ignored. If we only want to change a
row of a table, but forget the WHERE clause will update the entire table.
• If an UPDATE statement violates any integrity constraints, MySQL will not perform an
update and issue an error message.
Example: In the employees table, if we want to update Diane Murphy's email with
employeeNumber is 1002 to [email protected], execute the following
query:
SELECT firstname,
lastname,
email
FROM employees
WHERE employeeNumber = 1002;
UPDATE employees
SET email = '[email protected]'
Executing the SELECT query again, we will see the email changes to the new value
3. DELETE statement
• After DELETE FROM is the name of the table you want to delete records. WHERE clause
specifies the condition to restrict the lines to be removed. If a record meets the WHERE
condition, it is removed from the database table.
• If the WHERE clause is omitted from the DELETE statement, all table rows will be
deleted. To reduce the dangers of statements like DELETE or UPDATE, always check the
WHERE condition in a SELECT T statement before executing the DELELE or UPDATE
statement.
Example: Delete all employees in the office with officeNumber code of 6, execute the following
query:
DELETE
FROM employees
WHERE officeCode = 6;
Perform the query again on the employee table. There are no more rows in the table with
officeCode = 6.
Note: If you remove the WHERE condition
Will delete all rows of the employees table. Therefore, it is important to note the condition in
the WHERE clause when executing the DELETE statement.
Example: delete all employees who work for an office with officecode 1 and also delete that
office.
DELETE employees,offices
FROM employees,offices
WHERE employees.officeCode = offices.officeCode AND
offices.officeCode = 1;
After executing the above data deletion command, please check the data tables again
The employees table no longer has employee lines with officeCode = 1.
There may be constraints between data tables, for example, foreign key constraints between
products and productlines tables.
If we delete a row of data in the productlines table and there are still rows in the products table
that refer to this row, the default is not allowed.
An error message "Cannot delete or update a parent row: a foreign key constraint fails (`
classicmodels`.`products`, CONSTRAINT `fk_products_productlines` FOREIGN KEY (`
productLine`) REFERENCES `productlines` (` productLine`) ON DELETE is displayed. NO
ACTION ON UPDATE NO ACTION) ”
If the foreign key declaration is with the ON DELETE CASCADE option, the system will
automatically delete the data rows in the products table that refer to this data stream.
If the foreign key is declared with the ON DELETE SET NULL option, the productLine foreign
key of the reference rows will be set to NULL.
❖ Practical Exercises
1. Practice the INSERT, UPDATE and DELETE commands on the tables in the image
below of the classicmodels database.
2. Create a table named temp_orderdetails, then add the data from the latest month in the
orderdetails table to the table above.
3. Replace the entire productline’s name ‘Cars’ with ‘Automobiles’.