Professional Documents
Culture Documents
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
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:
The second table named Headlines which contains a list of news headlines
is structured as follows:
A link exists between the previous two tables: the cid field, which be used to
connect each news item with its category.
The following query retrieves all the records from the table topics:
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.
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.
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;
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”.
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
Color Attribute
Brown Eyes
Black Eyes
Gray Eyes
Brown Hair
Black Hair
Gray Hair
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.
Example 1
Example 2
Example 3
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.
Example:
5.5 TRUNCATE
5.6 UPDATE
5.7 DO
Syntax:
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
three columns in the index, or for the columns in a leftmost prefix. For
example:
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:
Syntax – 2:
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).
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.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 ...]