You are on page 1of 19

RDBMS with MySQL Unit 5

Unit 5 Advanced Data Manipulation Language


5.1 Introduction
Objectives
5.2 JOIN
5.3 UNION
5.4 DELETE
5.5 TRUNCATE
5.6 UPDATE
5.7 DO
5.8 HANDLER
5.9 REPLACE
5.10 Summary
5.11 Terminal Questions
5.12 Answers

5.1 Introduction
This unit is an extension to the basic Data Manipulation statements
introduced in the previous unit. It discusses about the JOIN and UNION
statements used to combine two or more database tables or views in many
ways. It also describes the ways in which data or objects can be deleted
using the DELETE command. It describes the differences between
TRUNCATE and DELETE commands. The other DML statements like
UPDATE, DO, REPLACE and HANDLER are also discussed in detail.

Objectives
After studying this unit, you should be able to:
 explain the various methods of joining tables with JOIN statement
 explain the approach of using UNION operator in combining two or
more tables
 distinguish and Differentiate between DELETE and TRUNCATE
Statements
 describe the usage of UPDATE statement in modifying the existing
data
 describe the DO syntax
 describe the usage and applications of HANDLER and REPLACE
statements

Sikkim Manipal University Page No. 114


RDBMS with MySQL Unit 5

5.2 JOIN
Efficiency constraints usually dictate that data be split across multiple tables
and that relationships be created between different tables to make efficient
retrieval of data.
By supporting the creation of links between related pieces of information, a
Relational Database Management System (RDBMS) not only makes it
possible to store information more efficiently (by removing redundancies), it
also brings to the forefront undiscovered relationships between disparate
segments of data and permits efficient exploitation of those relationships.
This section demonstrates how SQL can be used to query multiple tables at
once and to combine the data retrieved from them in different ways.
These multi-table queries are referred to as JOINS because they join
together two or more tables.
MySQL has supported joins well right from its inception and, today, boasts
support for standard SQL2-compliant Join syntax, which makes it possible
to combine table records in a variety of sophisticated ways.
Consider the following sample tables that would be used to demonstrate the
concept of Joins.
The first table named Categories which contains a list of news categories is
structured as follows:

Sikkim Manipal University Page No. 115


RDBMS with MySQL Unit 5

The second table named Headlines which contains a list of news headlines
is structured as follows:

The sample data for the above tables is shown below:

A link exists between the previous two tables: the cid field, which be used to
connect each news item with its category.

Sikkim Manipal University Page No. 116


RDBMS with MySQL Unit 5

The following query retrieves all the records from the table topics:

To retrieve a list of only those headlines in the Current Affairs category or a


list of all the sports headlines for 2003: Querying only a single table in this
case won’t be adequate because category information and headline
summaries are stored in two separate tables.
However, a link does exist between the two tables – the cid column, which is
common to both tables. By equating the cid field in the categories table to
the cid field in the topics table in the SELECT query’s WHERE clause, this
common field makes it possible to create a join between the two tables.
In this case, the WHERE clause has been used to connect the cid fields
within both tables to each other and present a composite picture. Also you
need to filter it down ot only those headlines in the current affairs category.
The WHERE clause comes to your rescue again.

Sikkim Manipal University Page No. 117


RDBMS with MySQL Unit 5

Types of Joins:
 INNER
 OUTER (LEFT, RIGHT, FULL)
 CROSS

- INNER JOIN: They are also known as Equi Joins. They are so called
because the where statement generally compares two columns from two
tables with the equivalence operator =. Many systems use this type as the
default join. This type can be used in situations where selecting only those
rows that have values in common in the columns specified in the ON clause,
is required. In short, the Inner Join returns all rows from both tables where
there is a match.

Products Sales
ProdID ProdName
ID ProdID Quantity
1 Apples
1 3 2300
2 Oranges
2 2 1500
3 Pineapples
3 1 3400
4 Bananas

As both the tables have a ProdID field in common, it is easy to join them.

mysql> SELECT ProdName, Quantity FROM Products, Sales


WHERE Products.ProdID = Sales.ProdID AND
Sales.Quantity > 2000;

ProdName Quantity
Pineapples 2300
Apples 3400

OUTER JOIN: This type of join can be used in situations where it is desired,
to select all rows from the table on the left (or right, or both) regardless of
whether the other table has values in common and (usually) enter NULL
where data is missing.

Sikkim Manipal University Page No. 118


RDBMS with MySQL Unit 5

Depending on which side of the join is to be preserved, SQL defines a left


outer join, and a right outer join.
Left Outer Join: In this type, all the records from the table on the left side of
the join and matching the WHERE clause in appear in the final result set.
Right Outer Join: All the records matching the WHERE clause from the
table on the right appear.

Users groups
uid name
gid name
100 Sue
501 Authors
103 Harry
502 Actors
104 Louis
503 Musicians
107 Sam
504 chefs
110 James
111 Mark
112 Rita

users_groups
uid gid
11 502
107 502
100 503
110 501
112 501
100 501
102 501
104 502
100 502
The following table shows the output of the query list which users belong to
which groups:
mysql> SELECT users.name, groups.name
FROM users, groups, users_groups
WHERE users.uid = users_groups.uid
AND groups.gid = users_groups.gid;

Sikkim Manipal University Page No. 119


RDBMS with MySQL Unit 5

name name
Sam Actors
Sue Musicians
James Authors
Rita Authors
Sue Authors
Louis Actors
Sue Actors

The above list is a symmetrical list, it doesn’t tell you anything about the
users who doesn’t belong to any group or the groups with no members.
Example: (Left Outer Join)
mysql> SELECT * FROM users LEFT JOIN users_groups ON
users.uid = users_groups.uid;
“Select all rows from the left side of the join (table users) and, for each row
selected, either display the matching value (the value satisfying the
constraints in the ON or USING clause) from the right side (table
users_groups) or display a row of NULLS”.

uid name gid uid


100 Sue 503 100
100 Sue 501 100
100 Sue 502 100
103 Harry NULL NULL
104 Louis 502 104
107 Sam 502 107
110 James 501 110
111 Mark NULL NULL
112 rita 501 112

Example: (Right Outer Join)


mysql> SELECT * FROM users_groups
RIGHT JOIN groups

Sikkim Manipal University Page No. 120


RDBMS with MySQL Unit 5

USING (gid);
gid uid gid name
501 110 501 authors
501 112 501 authors
501 100 501 authors
501 102 501 authors
502 11 502 actors
502 107 502 actors
502 104 502 actors
502 100 502 actors
503 100 503 musicians
NULL NULL 504 Chefs

CROSS JOIN: This type of join returns a Cartesian product. i.e. it combines
every row from the left table with every row in the right table. Sometimes this
join produces a mess, but under the right circumstances, it can be very
useful. This type of join can be used in situations where it is desired, to
select all possible combinations of rows and columns from both tables.

Attribute Color

Attribute Color
Eyes Brown
Hair Black
-
Gray

mysql> SELECT * FROM color, attribute;

Color Attribute
Brown Eyes
Black Eyes
Gray Eyes
Brown Hair
Black Hair
Gray Hair

Sikkim Manipal University Page No. 121


RDBMS with MySQL Unit 5

Self Assessment Questions


1. The _________ queries are referred to as JOINS because they join
together two or more tables.

5.3 UNION

SELECT ...
UNION [ALL | DISTINCT] SELECT ...
[UNION [ALL | DISTINCT] SELECT ...]

UNION is used to combine the result from multiple SELECT statements into
a single result set.
The column names from the first SELECT statement are used as the
column names for the results returned. Selected columns listed in
corresponding positions of each SELECT statement should have the same
data type. (For example, the first column selected by the first statement
should have the same type as the first column selected by the other
statements.)
If the data types of corresponding SELECT columns do not match, the types
and lengths of the columns in the UNION result take into account the values
retrieved by all of the SELECT statements.
The default behavior for UNION is that duplicate rows are removed from the
result. The optional DISTINCT keyword has no effect other than the default
because it also specifies duplicate-row removal. With the optional ALL
keyword, duplicate-row removal does not occur and the result includes all
matching rows from all the SELECT statements.
You can mix UNION ALL and UNION DISTINCT in the same query.

Sikkim Manipal University Page No. 122


RDBMS with MySQL Unit 5

Example 1

Example 2

Example 3

Sikkim Manipal University Page No. 123


RDBMS with MySQL Unit 5

Self Assessment Questions


2. In Union operation, the column names from the first _____ statement are
used as the column names for the results returned.
a. Select
b. Update
c. Delete
d. Insert

5.4 DELETE
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM
tbl_name
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

The DELETE statement deletes rows from tbl_name and returns a count of
the number of deleted rows. This count can be obtained by calling the
ROW_COUNT() function.
The WHERE clause, if given, specifies the conditions that identify which
rows to delete. With no WHERE clause, all rows are deleted.
If the ORDER BY clause is specified, the rows are deleted in the order that
is specified. The LIMIT clause places a limit on the number of rows that can
be deleted. As stated, a DELETE statement with no WHERE clause deletes
all rows.
A faster way to do this, when you do not need to know the number of
deleted rows, is to use TRUNCATE TABLE. However, within a transaction
or if you have a lock on the table, TRUNCATE TABLE cannot be used
whereas DELETE can.
Deleting Tables:
You can delete a table with the DROP TABLE command.

Sikkim Manipal University Page No. 124


RDBMS with MySQL Unit 5

Example:

Self Assessment Questions


3. The _______ clause places a limit on the number of rows that can be
deleted

5.5 TRUNCATE

TRUNCATE [TABLE] tbl_name

TRUNCATE TABLE empties a table completely. Logically, this is equivalent


to a DELETE statement that deletes all rows, but there are practical
differences under some circumstances.
For an InnoDB table, InnoDB processes TRUNCATE TABLE by deleting
rows one by one if there are any FOREIGN KEY constraints that reference
the table. If there are no FOREIGN KEY constraints, InnoDB performs fast
truncation by dropping the original table and creating an empty one with the
same definition, which is much faster than deleting rows one by one. The
AUTO_INCREMENT counter is reset by TRUNCATE TABLE, regardless of
whether there is a FOREIGN KEY constraint.
In the case that FOREIGN KEY constraints reference the table, InnoDB
deletes rows one by one and processes the constraints on each one. If the

Sikkim Manipal University Page No. 125


RDBMS with MySQL Unit 5

FOREIGN KEY constraint specifies DELETE CASCADE, rows from the


child (referenced) table are deleted, and the truncated table becomes
empty. If the FOREIGN KEY constraint does not specify CASCADE, the
TRUNCATE statement deletes rows one by one and stops if it encounters a
parent row that is referenced by the child, returning this error:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign


key constraint fails (`test`.`child`, CONSTRAINT `child_ibfk_1`
FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`))

This is the same as a DELETE statement with no WHERE clause.


The count of rows affected by TRUNCATE TABLE is accurate only when it
is mapped to a DELETE statement.
For other storage engines, TRUNCATE TABLE differs from DELETE in the
following ways in MySQL 5.1:
 Truncate operations drop and re-create the table, which is much faster
than deleting rows one by one, particularly for large tables.
 Truncate operations are not transaction-safe; an error occurs when
attempting one in the course of an active transaction or active table lock.
 Truncation operations do not return the number of deleted rows.
 As long as the table format file tbl_name.frm is valid, the table can be re-
created as an empty table with TRUNCATE TABLE, even if the data or
index files have become corrupted.
 The table handler does not remember the last used
AUTO_INCREMENT value, but starts counting from the beginning. This
is true even for MyISAM and InnoDB, which normally do not reuse
sequence values.
 When used with partitioned tables, TRUNCATE TABLE preserves the
partitioning; that is, the data and index files are dropped and re-created,
while the partition definitions (.par) file is unaffected.
 Since truncation of a table does not make any use of DELETE, the
TRUNCATE statement does not invoke ON DELETE triggers.
Self Assessment Questions
4. The ______ count of rows affected by TRUNCATE TABLE is accurate
only when it is mapped to a statement.

Sikkim Manipal University Page No. 126


RDBMS with MySQL Unit 5

5.6 UPDATE

UPDATE [LOW_PRIORITY] [IGNORE] tbl_name


SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

For the single-table syntax, the UPDATE statement updates columns of


existing rows in tbl_name with new values.
The SET clause indicates which columns to modify and the values they
should be given. Each value can be given as an expression, or the keyword
DEFAULT to set a column explicitly to its default value.
The WHERE clause, if given, specifies the conditions that identify which
rows to update. With no WHERE clause, all rows are updated. If the
ORDER BY clause is specified, the rows are updated in the order that is
specified.
The LIMIT clause places a limit on the number of rows that can be updated.
Single-table UPDATE assignments are generally evaluated from left to right.
If you set a column to the value it currently has, MySQL notices this and
does not update it. If you update a column that has been declared NOT
NULL by setting to NULL, the column is set to the default value appropriate
for the data type and the warning count is incremented. The default value is
0 for numeric types, the empty string ('') for string types, and the “zero” value
for date and time types.
Self Assessment Questions
5. The ______ clause in an Update statement indicates which columns to
modify and the values they should be given.

5.7 DO
Syntax:

DO expr [, expr] ...

Sikkim Manipal University Page No. 127


RDBMS with MySQL Unit 5

DO executes the expressions but does not return any results. In most
respects, DO is shorthand for SELECT expr, ..., but has the advantage that
it is slightly faster when you do not care about the result. DO is useful
primarily with functions that have side effects, such as RELEASE_LOCK().

5.8 HANDLER

HANDLER tbl_name OPEN [ [AS] alias]


HANDLER tbl_name READ index_name { = | >= | <= | < } (value1,value2,...)
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name READ index_name { FIRST | NEXT | PREV | LAST }
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name READ { FIRST | NEXT }
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name CLOSE

The HANDLER statement provides direct access to table storage engine


interfaces. It is available for MyISAM and InnoDB tables.
The HANDLER ... OPEN statement opens a table, making it accessible via
subsequent HANDLER ... READ statements. This table object is not shared
by other sessions and is not closed until the session calls HANDLER ...
CLOSE or the session terminates.
If you open the table using an alias, further references to the open table with
other HANDLER statements must use the alias rather than the table name.
The first HANDLER ... READ syntax fetches a row where the index specified
satisfies the given values and the WHERE condition is met.
If you have a multiple-column index, specify the index column values as a
comma-separated list. Either specify values for all the columns in the index,
or specify values for a leftmost prefix of the index columns.
Suppose that an index my_idx includes three columns named col_a, col_b,
and col_c, in that order. The HANDLER statement can specify values for all

Sikkim Manipal University Page No. 128


RDBMS with MySQL Unit 5

three columns in the index, or for the columns in a leftmost prefix. For
example:

HANDLER ... READ my_idx = (col_a_val,col_b_val,col_c_val) ...


HANDLER ... READ my_idx = (col_a_val,col_b_val) ...
HANDLER ... READ my_idx = (col_a_val) ...

To employ the HANDLER interface to refer to a table's PRIMARY KEY, use


the quoted identifier `PRIMARY`:
HANDLER tbl_name READ `PRIMARY` ...
The second HANDLER ... READ syntax fetches a row from the table in
index order that matches the WHERE condition.

5.9 REPLACE
A subtle variation on the ON DUPLICATE KEY UPDATE is the REPLACE
command, which adopts the same syntax as the INSERT command. Unlike
INSERT, though, which produces an error if the record being inserted
contains a duplicate value on a filed marked as UNIQUE, REPLACE
replaces the entire record with new values.
The difference between ON DUPLICATE KEY UPDATE and REPLACE is
this: while the former only updates the named fields with new values, the
latter deletes the old record and replaces it completely with the new one.

Syntax – 1:

REPLACE [LOW_PRIORITY | DELAYED]


[INTO] tbl_name [(col_name,...)]
OR
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...

Syntax – 2:

REPLACE [LOW_PRIORITY | DELAYED]


[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...

Sikkim Manipal University Page No. 129


RDBMS with MySQL Unit 5

OR
Syntax – 3:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT ...

REPLACE works exactly like INSERT, except that if an old row in the table
has the same value as a new row for a PRIMARY KEY or a UNIQUE index,
the old row is deleted before the new row is inserted. REPLACE is a MySQL
extension to the SQL standard. It either inserts, or deletes and inserts.
Note that unless the table has a PRIMARY KEY or UNIQUE index, using a
REPLACE statement makes no sense. It becomes equivalent to INSERT,
because there is no index to be used to determine whether a new row
duplicates another.
Values for all columns are taken from the values specified in the REPLACE
statement. Any missing columns are set to their default values, just as
happens for INSERT. You cannot refer to values from the current row and
use them in the new row. If you use an assignment such as SET col_name
= col_name + 1, the reference to the column name on the right hand side is
treated as DEFAULT(col_name), so the assignment is equivalent to SET
col_name = DEFAULT(col_name) + 1.
To use REPLACE, you must have both the INSERT and DELETE privileges
for the table.
The REPLACE statement returns a count to indicate the number of rows
affected. This is the sum of the rows deleted and inserted. If the count is 1
for a single-row REPLACE, a row was inserted and no rows were deleted. If
the count is greater than 1, one or more old rows were deleted before the
new row was inserted. It is possible for a single row to replace more than
one old row if the table contains multiple unique indexes and the new row
duplicates values for different old rows in different unique indexes.
The affected-rows count makes it easy to determine whether REPLACE
only added a row or whether it also replaced any rows: Check whether the
count is 1 (added) or greater (replaced).

Sikkim Manipal University Page No. 130


RDBMS with MySQL Unit 5

5.10 Summary
This unit covers the advanced data manipulation language statements as
described below:
1. Join: This statement is used to join tables within a database using
different variations like Inner Join, Outer Join, Self Join and so on.
2. Union: This follows the mathematical principles of Joins used to join the
tables and different database objects.
3. Delete: This command is used to delete the data or data objects within a
database.
4. Truncate: This statement is used to empty a table completely. Logically,
this is equivalent to 5. Delete: This statement that deletes all rows, but
there are practical differences under some circumstances.
6. Update: This statement causes modifications to the existing data in
various database objects like tables, views, and so on.
7. DO: DO executes the expressions but does not return any results. In
most respects, DO is shorthand for SELECT expr, ..., but has the
advantage that it is slightly faster when you do not care about the result.
DO is useful primarily with functions that have side effects, such as
RELEASE_LOCK().
8. Handler: The HANDLER statement provides direct access to table
storage engine interfaces. It is available for MyISAM and InnoDB tables.
9. Replace: Adopts the same syntax as the INSERT command. Unlike
INSERT, though, which produces an error if the record being inserted
contains a duplicate value on a field marked as UNIQUE, REPLACE
replaces the entire record with new values.

5.11 Terminal Questions


1. Define the following types of Joins:
a. Inner Join
b. Left Outer Join
c. Right Outer Join
2. Give the syntaxes of the following SQL statements:
a. Union
b. Delete
c. Update
Sikkim Manipal University Page No. 131
RDBMS with MySQL Unit 5

5.12 Answers
Self Assessment Questions
1. multi-table
2. a (Select)
3. LIMIT
4. DELETE
5. SET

Terminal Questions
1.
a. Inner Join: In this join type the where statement generally compares
two columns from two tables with the equivalence operator =. Many
systems use this type as the default join. Also known as Equi-Join
b. Left Outer Join: In this type, all the records from the table on the left
side of the join and matching the WHERE clause in appear in the final
result set.
c. Right Outer Join: All the records matching the WHERE clause from the
table on the right appear.

2.
a. Union Syntax: (Refer Section 5.3)

SELECT ...
UNION [ALL | DISTINCT] SELECT ...
[UNION [ALL | DISTINCT] SELECT ...]

b. Delete Syntax: (Section 5.4)

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name


[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

c. Update Syntax: (Section 5.6)


UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
SET col_name1={expr1|DEFAULT} [,
col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

Sikkim Manipal University Page No. 132

You might also like