Sei sulla pagina 1di 319

BT 0075

RDBMS with MySQL


Contents
Unit 1
Basics of MySQL

Unit 2
Data Types

24

Unit 3
Data Definition Language (DDL)

52

Unit 4
Data Manipulation Language (DML)

90

Unit 5
Advanced Data Manipulation Language

114

Unit 6
Subqueries

133

Unit 7
Operators and Functions

153

Unit 8
Advanced Functions

183

Unit 9
Transaction Management

214

Unit 10
Stored Procedures

229

Edition: Spring 2009


th

BKID B1003 10 June 2009

Unit 11
Control Statement

242

Unit 12
User Account Management

253

Unit 13
General Security Issues

281

Unit 14
Log Files

294

References

311

Prof. S. Kannan
Director & Dean (in-charge)
Directorate of Distance Education
Sikkim Manipal University of Health, Medical & Technological Sciences (SMU DDE)
Board of Studies
Dr. U. B. Pavanaja (Chairman)
General Manager Academics
Manipal Universal Learning Pvt. Ltd.
Bangalore.
Prof. Bhushan Patwardhan
Chief Academics
Manipal Education
Bangalore.
Dr. Harishchandra Hebbar
Director
Manipal Centre for Info. Sciences,
Bangalore
Dr. N. V. Subba Reddy
HOD-CSE
Manipal Institute of Technology, Manipal
Dr. Ashok Hegde
Vice President
MindTree Consulting Ltd., Bangalore
Dr. Ramprasad Varadachar
Director, Computer Studies
Dayanand Sagar College of Engg. Bangalore.
Content Preparation Team
Content Writing
Mr. Nirmal Kumar Nigam
Assistant Professor, HoP IT
Sikkim Manipal University DDE
Manipal.
Instructional Design
Mr. Kulwinder Pal
Senior Lecturer (Education)
Sikkim Manipal University DDE, Manipal

Nirmal Kumar Nigam


HOP IT
Sikkim Manipal University DDE
Manipal.
Dr. A. Kumaran
Research Manager (Multilingual)
Microsoft Research Labs India
Bangalore.
Ravindranath P. S.
Director (Quality)
Yahoo India
Bangalore.
Dr. Ashok Kallarakkal
Vice President
IBM India, Bangalore
H. Hiriyannaiah
Group Manager
EDS Mphasis, Bangalore

Content Editing
Mr. Vinayak G. Pai
Assistant Professor, Dept. of IT
Sikkim Manipal University DDE
Manipal.

Edition: Spring 2009


This book is a distance education module comprising a collection of learning
material for our students. All rights reserved. No part of this work may be
reproduced in any form by any means without permission in writing from Sikkim
Manipal University of Health, Medical and Technological Sciences, Gangtok,
Sikkim. Printed and published on behalf of Sikkim Manipal University of Health,
Medical and Technological Sciences, Gangtok, Sikkim by Mr. Rajkumar Mascreen,
GM, Manipal Universal Learning Pvt. Ltd., Manipal 576 104. Printed at Manipal
Press Limited, Manipal.

SUBJECT INTRODUCTION
RDBMS with MySQL is a 4 credit subject in third semester of BSc IT
program.
MySQL, the most popular Open Source SQL database management
system, is developed, distributed, and supported by MySQL AB.
Open Source means that it is possible for anyone to use and modify the
software. Anybody can download the MySQL software from the Internet and
use it without paying anything. If you wish, you may study the source code
and change it to suit your needs. The MySQL software uses the GPL (GNU
General Public License), http://www.fsf.org/licenses/, to define what you
may and may not do with the software in different situations.
The MySQL Database Server is very fast, reliable, and easy to use. MySQL
Server works in client/server or embedded systems.
MySQL is named after co-founder Monty Widenius's daughter, My.
In this book on MySQL, we would lay the emphasis on the applications of
MySQL in solving the database related problems and also deal with the
coverage of every aspect related to MySQL.
The SLM of this subject is divided into 14 units, the overview of which is
given below.
Unit 1 Basics of MySQL:
This unit introduces the user to the basic theoretical concepts and features
behind MySQL.
Unit 2 MySQL Data Types:
This unit introduces the theoretical and practical aspects of dealing with
various data types of MySQL database, their syntax, usage and applications
in the real world.
Unit 3 Data Definition Language (DDL):
This unit starts with an introduction of Data Definition Language Statements
used in MySQL. It describes the syntaxes of creating and modifying
Databases, Indexes and Tables.

Unit 4 Data Manipulation Language (DML):


The Data Manipulation Language or DML statements are used to
manipulate the data inside a database. This unit introduces various DML
Statements built into MySQL.
Unit 5 Advanced Data Manipulation Language:
This unit deals with the advanced DML Statements which is an extension of
unit 4. It deals with Join and Union operations used to join database objects
like tables. It also deals with various other operations of truncating the data,
Updating or Modifying the existing data. It speaks about the specific
operator Do, Handler and Replace.
Unit 6 Subqueries:
This unit deals with subqueries. A subquery is a query written within a query
where in the result processing is segregated among the inner and outer
queries.
Unit 7 Operators and Functions:
This unit introduces the reader with all varieties of operators used in both
SQL and also in case of Functions and procedures used along with them.
Unit 8 Advanced Functions:
This unit deals in detail with the next level of functions used in addition to
the functions discussed in the previous unit. It discusses regarding the Full
text search functions, Cast Functions Encryption Functions, and the usage
of Group By clause with various functions, and modifiers.
Unit 9 Transaction Management:
A Transaction is a series of one or more SQL Statements that are logically
related or a series of operations performed on table data. This unit
introduces to the reader with various statements used in MySQL to treat
with Transactions like Starting Transactions, making the output of the
transaction permanent by using Commit Statements.
Unit 10 Stored Procedures
A stored procedure is a set of SQL commands that can be stored in the
server. This unit discusses the commands used to create, alter, and Drop
procedures and / or functions.

Unit 11 Control Statements:


This unit deals with various flow control constructs used in case of stored
procedures or functions.
Unit 12 User Account Management:
This section describes how to set up accounts for clients of your MySQL
server. The meaning of account names and passwords as used in MySQL
and how that compares to names and passwords used by your operating
system is discussed.
Unit 13 General Security Issues:
This unit describes some general security issues to be aware of and what
you can do to make your MySQL installation more secure against attack or
misuse. It also describes the methods to maintain security while transferring
data into MySQL from external resources
Unit 14 Log Files:
This unit describes the importance of maintaining log files within MySQL. It
describes various types of log files maintained in MySQL like Error, General
Query, Binary, etc.
Objectives of studying this subject
After studying this subject, you should be able to:
discuss the features and use MySQL for database applications
define and use the appropriate data types in your applications
Describe the syntaxes of DML Statements and use them appropriately
Define and explain the syntaxes of DDL statements
explain the usage of subqueries as applicable
Describe the usage of various available operators and functions
Discuss the concepts of Transaction Management
describe the usage of Stored Procedures and functions in conjunction
with flow control statements
Explain the importance of User Account Management
Define general security guidelines in the usage of database servers
Describe the need of maintaining different types of log files

The subject demands knowledge and understanding of the subject titled


Database Management Systems bearing the subject code BT0066 of
BSc IT First Semester with respect to both the theoretical and practical
concepts.

RDBMS with MySQL

Unit 1

Unit 1

Basics of MySQL

Structure
1.1 Introduction
Objectives
1.2 Features of MySQL
1.3 Top 10 Reasons to use MySQL
1.4 MySQL Development Roadmap
1.5 Connecting to and Disconnecting from the Server
1.6 Accessing and Creating Databases and Tables
1.7 Loading Data
1.8 Summary
1.9 Terminal Questions
1.10 Answers

1.1 Introduction
MySQL, the most popular Open Source SQL database management
system, is developed, distributed, and supported by MySQL AB. MySQL AB
is a commercial company, founded by the MySQL developers. It is a second
generation Open Source company that unites Open Source values and
methodology with a successful business model.
The MySQL Web site (http://www.mysql.com/) provides the latest
information about MySQL software and MySQL AB.
MySQL is a Database Management System
A database is a structured collection of data. It may be anything from a
simple shopping list to a picture gallery or the vast amounts of
information in a corporate network. To add, access, and process data
stored in a computer database, you need a database management
system such as MySQL Server.
MySQL is a Relational Database Management System
A Relational Database stores data in separate tables rather than putting
all the data in one big storeroom. This adds speed and flexibility.
MySQL software is Open Source
Open Source means that it is possible for anyone to use and modify the
software. Anybody can download the MySQL software from the Internet
and use it without paying anything. If you wish, you may study the
Sikkim Manipal University

Page No. 1

RDBMS with MySQL

Unit 1

source code and change it to suit your needs. The MySQL


software uses the GPL (GNU General Public License),
http://www.fsf.org/licenses/, to define what you may and may not do with
the software in different situations. If you feel uncomfortable with the
GPL or need to embed MySQL code into a commercial application, you
can buy a commercially licensed version from us.
The MySQL Database Server is very fast, reliable, and easy to use.
MySQL Server works in client/server or embedded systems.
The MySQL Database Software is a client/server system that consists of
a multi-threaded SQL server that supports different backends, several
different client programs and libraries, administrative tools, and a wide
range of application programming interfaces (APIs).
A large amount of contributed MySQL software is available.
It is very likely that your favorite application or language supports the
MySQL Database Server.
The official way to pronounce MySQL is My Ess Que Ell (not my
sequel), but we don't mind if you pronounce it as my sequel or in
some other localized way.
In this unit, we would be dealing with the features of MySQL followed by
the development roadmap and the steps in starting and stopping the
MySQL server. In addition, we will be dealing with the commands used
to create and access databases and tables in the database server.
Objectives
After studying this unit, you should be able to:
explain the features of MySQL
describe the process of connecting to and disconnecting from MySQL
server
explain the operations of accessing and creating databases and tables
using MySQL
discuss the steps in Loading data from external data sources

1.2 Features of MySQL


This section describes some of the important characteristics of the MySQL
Database Software.

Sikkim Manipal University

Page No. 2

RDBMS with MySQL

Unit 1

Internals and Portability:


Written in C and C++.
Tested with a broad range of different compilers.
Works on many different platforms.
Uses GNU Automake, Autoconf, and Libtool for portability.
The MySQL Server design is multi-layered with independent modules.
Fully multi-threaded using kernel threads. It can easily use multiple
CPUs if they are available.
Provides transactional and non-transactional storage engines.
Uses very fast B-tree disk tables (MyISAM) with index compression.
Relatively easy to add other storage engines. This is useful if you want
to provide an SQL interface for an in-house database.
A very fast thread-based memory allocation system.
Very fast joins using an optimized one-sweep multi-join.
In-memory hash tables, which are used as temporary tables.
SQL functions are implemented using a highly optimized class library
and should be as fast as possible. Usually there is no memory allocation
at all after query initialization.
The MySQL code is tested with Purify (a commercial memory leakage
detector)
as
well
as
with
Valgrind,
a
GPL
tool
(http://developer.kde.org/~sewardj/).
The server is available as a separate program for use in a client/server
networked environment. It is also available as a library that can be
embedded (linked) into standalone applications. Such applications can
be used in isolation or in environments where no network is available.
Data Types:
Supports the following data types: signed/unsigned integers 1, 2, 3, 4,
and 8 bytes long, FLOAT, DOUBLE, CHAR, VARCHAR, TEXT, BLOB,
DATE, TIME, DATETIME, TIMESTAMP, YEAR, SET, ENUM, and
OpenGIS spatial types.
Fixed-length and variable-length records.
Statements and Functions:
Full operator and function support in the SELECT list and WHERE
clause of queries. For example:

Sikkim Manipal University

Page No. 3

RDBMS with MySQL

Unit 1

mysql> SELECT CONCAT(first_name, ' ', last_name)


-> FROM citizen
-> WHERE income/dependents > 10000 AND age > 30;

Full support for SQL GROUP BY and ORDER BY clauses. Support for
group functions (COUNT(), COUNT(DISTINCT ...), AVG(), STD(),
SUM(), MAX(), MIN(), and GROUP_CONCAT()).
Support for LEFT OUTER JOIN and RIGHT OUTER JOIN with both
standard SQL and ODBC syntax.
Support for aliases on tables and columns as required by standard SQL.
DELETE, INSERT, REPLACE, and UPDATE return the number of rows
that were changed (affected). It is possible to return the number of rows
matched instead by setting a flag when connecting to the server.
The MySQL-specific SHOW statement can be used to retrieve
information about databases, storage engines, tables, and indexes.
MySQL 5.0 adds support for the INFORMATION_SCHEMA database,
implemented according to standard SQL.
The EXPLAIN statement can be used to determine how the optimizer
resolves a query.
Function names do not clash with table or column names. For example,
ABS is a valid column name. The only restriction is that for a function
call, no spaces are allowed between the function name and the ( that
follows it.
You can refer to tables from different databases in the same statement.
Security:
A privilege and password system that is very flexible and secure, and
that allows host-based verification.
Passwords are secure because all password traffic is encrypted when
you connect to a server.
Scalability and Limits:
Handles large databases. We use MySQL Server with databases that
contain 50 million records. We also know of users who use MySQL
Server with 60,000 tables and about 5,000,000,000 rows.
Up to 64 indexes per table are allowed (32 before MySQL 4.1.2). Each
index may consist of 1 to 16 columns or parts of columns. The maximum
Sikkim Manipal University

Page No. 4

RDBMS with MySQL

Unit 1

index width is 1000 bytes (767 for InnoDB); before MySQL 4.1.2, the
limit is 500 bytes. An index may use a prefix of a column for CHAR,
VARCHAR, BLOB, or TEXT column types.
Connectivity:
Clients can connect to MySQL Server using several protocols:
Clients can connect using TCP/IP sockets on any platform.
On Windows systems in the NT family (NT, 2000, XP, 2003, or
Vista), clients can connect using named pipes if the server is started
with the enable-named-pipe option. In MySQL 4.1 and higher,
Windows servers also support shared-memory connections if started
with the shared-memory option. Clients can connect through
shared memory by using the protocol=memory option.
On Unix systems, clients can connect using Unix domain socket
files.
MySQL client programs can be written in many languages. A client
library written in C is available for clients written in C or C++, or for any
language that provides C bindings.
APIs for C, C++, Eiffel, Java, Perl, PHP, Python, Ruby, and Tcl are
available, allowing MySQL clients to be written in many languages.
The Connector/ODBC (MyODBC) interface provides MySQL support for
client programs that use ODBC (Open Database Connectivity)
connections. For example, you can use MS Access to connect to your
MySQL server. Clients can be run on Windows or Unix. MyODBC
source is available. All ODBC 2.5 functions are supported, as are many
others.
The Connector/J interface provides MySQL support for Java client
programs that use JDBC connections. Clients can be run on Windows or
Unix. Connector/J source is available.
MySQL Connector/NET enables developers to easily create .NET
applications that require secure, high-performance data connectivity with
MySQL. It implements the required ADO.NET interfaces and integrates
into ADO.NET aware tools. Developers can build applications using their
choice of .NET languages. MySQL Connector/NET is a fully managed
ADO.NET driver written in 100% pure C#.

Sikkim Manipal University

Page No. 5

RDBMS with MySQL

Unit 1

Localization:
The server can provide error messages to clients in many languages.
Full support for several different character sets, including latin1
(cp1252), german, big5, ujis, and more. For example, the Scandinavian
characters , and are allowed in table and column names.
Unicode support is available as of MySQL 4.1.
All data is saved in the chosen character set.
Sorting and comparisons are done according to the chosen character
set and collation (using latin1 and Swedish collation by default). It is
possible to change this when the MySQL server is started. To see an
example of very advanced sorting, look at the Czech sorting code.
MySQL Server supports many different character sets that can be
specified at compile time and runtime.
As of MySQL 4.1, the server time zone can be changed dynamically,
and individual clients can specify their own time zone.
Clients and Tools:
MySQL AB provides several client and utility programs. These include
both command-line programs such as mysqldump and mysqladmin,
and graphical programs such as MySQL Administrator and MySQL
Query Browser.
MySQL Server has built-in support for SQL statements to check,
optimize, and repair tables. These statements are available from the
command line through the mysqlcheck client. MySQL also includes
myisamchk, a very fast command-line utility for performing these
operations on MyISAM tables.
MySQL programs can be invoked with the --help or -? option to obtain
online assistance.
Self Assessment Questions
1. MySQL is a _______________ .
2. The _______ is a privileged and Password system that is very flexible
and secure, and that allows host-based verification.

Sikkim Manipal University

Page No. 6

RDBMS with MySQL

Unit 1

1.3 Top 10 reasons to use MySQL


1. Scalability and Flexibility
The MySQL database server provides the ultimate in scalability, sporting
the capacity to handle deeply embedded applications with a footprint of
only 1MB to running massive data warehouses holding terabytes of
information. Platform flexibility is a stalwart feature of MySQL with all
flavors of Linux, UNIX, and Windows being supported. And, of course,
the open source nature of MySQL allows complete customization for
those wanting to add unique requirements to the database server.
2. High Performance
A unique storage-engine architecture allows database professionals to
configure the MySQL database server specifically for particular
applications, with the end result being amazing performance results.
Whether the intended application is a high-speed transactional
processing system or a high-volume web site that services a billion
queries a day, MySQL can meet the most demanding performance
expectations of any system. With high-speed load utilities, distinctive
memory caches, full text indexes, and other performance-enhancing
mechanisms, MySQL offers all the right ammunition for today's critical
business systems.
3. High Availability
Rock-solid reliability and constant availability are hallmarks of MySQL,
with customers relying on MySQL to guarantee around-the-clock uptime.
MySQL offers a variety of high-availability options from high-speed
master/slave replication configurations, to specialized Cluster servers
offering instant failover, to third party vendors offering unique highavailability solutions for the MySQL database server.
4. Robust Transactional Support
MySQL offers one of the most powerful transactional database engines
on the market. Features include complete ACID (atomic, consistent,
isolated, durable) transaction support, unlimited row-level locking,
distributed transaction capability, and multi-version transaction support
where readers never block writers and vice-versa. Full data integrity is
also assured through server-enforced referential integrity, specialized
transaction isolation levels, and instant deadlock detection.
Sikkim Manipal University

Page No. 7

RDBMS with MySQL

Unit 1

5. Web and Data Warehouse Strengths


MySQL is the de-facto standard for high-traffic web sites because of its
high-performance query engine, tremendously fast data insert capability,
and strong support for specialized web functions like fast full text
searches. These same strengths also apply to data warehousing
environments where MySQL scales up into the terabyte range for either
single servers or scale-out architectures. Other features like main
memory tables, B-tree and hash indexes, and compressed archive
tables that reduce storage requirements by up to eighty-percent make
MySQL a strong standout for both web and business intelligence
applications.
6. Strong Data Protection
Because guarding the data assets of corporations is the number one job
of database professionals, MySQL offers exceptional security features
that ensure absolute data protection. In terms of database
authentication, MySQL provides powerful mechanisms for ensuring only
authorized users have entry to the database server, with the ability to
block users down to the client machine level being possible. SSH and
SSL support are also provided to ensure safe and secure connections. A
granular object privilege framework is present so that users only see the
data they should, and powerful data encryption and decryption functions
ensure that sensitive data is protected from unauthorized viewing.
Finally, backup and recovery utilities provided through MySQL and third
party software vendors allow for complete logical and physical backup
as well as full and point-in-time recovery.
7. Comprehensive Application Development
One of the reasons MySQL is the world's most popular open source
database is that it provides comprehensive support for every application
development need. Within the database, support can be found for stored
procedures, triggers, functions, views, cursors, ANSI-standard SQL, and
more. For embedded applications, plug-in libraries are available to
embed MySQL database support into nearly any application. MySQL
also provides connectors and drivers (ODBC, JDBC, etc.) that allow all
forms of applications to make use of MySQL as a preferred data
management server. It doesn't matter if it's PHP, Perl, Java, Visual

Sikkim Manipal University

Page No. 8

RDBMS with MySQL

Unit 1

Basic, or .NET, MySQL offers application developers everything they


need to be successful in building database-driven information systems.
8. Management Ease
MySQL offers exceptional quick-start capability with the average time
from software download to installation completion being less than fifteen
minutes. This rule holds true whether the platform is Microsoft Windows,
Linux, Macintosh, or UNIX. Once installed, self-management features
like automatic space expansion, auto-restart, and dynamic configuration
changes take much of the burden off already overworked database
administrators. MySQL also provides a complete suite of graphical
management and migration tools that allow a DBA to manage,
troubleshoot, and control the operation of many MySQL servers from a
single workstation. Many third party software vendor tools are also
available for MySQL that handle tasks ranging from data design and
ETL, to complete database administration, job management, and
performance monitoring.
9. Open Source Freedom and 24 x 7 Support
Many corporations are hesitant to fully commit to open source software
because they believe they can't get the type of support or professional
service safety nets they currently rely on with proprietary software to
ensure the overall success of their key applications. The questions of
indemnification come up often as well. These worries can be put to rest
with MySQL as complete around-the-clock support as well as
indemnification is available through MySQL Network. MySQL is not a
typical open source project as all the software is owned and supported
by MySQL AB, and because of this, a unique cost and support model
are available that provides a unique combination of open source
freedom and trusted software with support.
10. Lowest Total Cost of Ownership
By migrating current database-drive applications to MySQL, or using
MySQL for new development projects, corporations are realizing cost
savings that many times stretch into seven figures. Accomplished
through the use of the MySQL database server and scale-out
architectures that utilize low-cost commodity hardware, corporations are
finding that they can achieve amazing levels of scalability and
performance, all at a cost that is far less than those offered by
Sikkim Manipal University

Page No. 9

RDBMS with MySQL

Unit 1

proprietary and scale-up software vendors. In addition, the reliability and


easy maintainability of MySQL means that database administrators don't
waste time troubleshooting performance or downtime issues, but instead
can concentrate on making a positive impact on higher level tasks that
involve the business side of data.

1.4 MySQL Development Roadmap


This section describes the general MySQL development roadmap, provides
an overview about features that have been implemented in previous series
and that are new in this current release series (5.1), and an overview about
upcoming additions or changes in the next release series (6.0).
The maturity level of the release series covered in this manual (5.1) is
general availability.
The most requested features and the versions in which they were
implemented or are scheduled for implementation are summarized in the
following table:
Feature
Unions
Subqueries
R-trees
Stored procedures
Views
Cursors
XA transactions
Triggers
Event scheduler
Partitioning
Pluggable storage engine API
Plugin API
Row-based replication
Server log tables
Foreign keys

Sikkim Manipal University

MySQL Series
4.0
4.1
4.1 (for the MyISAM storage
engine)
5.0
5.0
5.0
5.0
5.0 and 5.1
5.1
5.1
5.1
5.1
5.1
5.1
6.x (implemented in 3.23 for
InnoDB)

Page No. 10

RDBMS with MySQL

Unit 1

Whats New in MySQL 5.1 ?


The following features have been added to MySQL 5.1.
Partitioning. This capability enables distributing portions of individual
tables across a file system, according to rules which can be set when
the table is created. In effect, different portions of a table are stored as
separate tables in different locations, but from the user point of view, the
partitioned table is still a single table. Syntactically, this implements a
number of new extensions to the CREATE TABLE, ALTER TABLE, and
EXPLAIN ... SELECT statements. As of MySQL 5.1.6, queries against
partitioned tables can take advantage of partition pruning. In some
cases, this can result in query execution that is an order of magnitude
faster than the same query against a non-partitioned version of the
same table.
Row-Based Replication. Replication capabilities in MySQL originally
were based on propagation of SQL statements from master to slave.
This is called statement-based replication. As of MySQL 5.1.5, another
basis for replication is available. This is called row-based replication.
Instead of sending SQL statements to the slave, the master writes
events to its binary log that indicate how individual table rows are
effected. As of MySQL 5.1.8, a third option is available: mixed. This will
use statement-based replication by default, and only switch to rowbased replication in particular cases.
Plugin API. MySQL 5.1 adds support for a very flexible plugin API that
enables loading and unloading of various components at runtime,
without restarting the server. Although the work on this is not finished
yet, plugin full-text parsers are a first step in this direction. This allows
users to implement their own input filter on the indexed text, enabling
full-text search capability on arbitrary data such as PDF files or other
document formats. A pre-parser full-text plugin performs the actual
parsing and extraction of the text and hands it over to the built-in MySQL
full-text search.
Event Scheduler. MySQL Events are tasks that run according to a
schedule. When you create an event, you are creating a named
database object containing one or more SQL statements to be executed
at one or more regular intervals, beginning and ending at a specific date
Sikkim Manipal University

Page No. 11

RDBMS with MySQL

Unit 1

and time. Conceptually, this is similar to the idea of the Unix crontab
(also known as a cron job) or the Windows Task Scheduler.
Server Log Tables. Before MySQL 5.1, the server writes general query
log and slow query log entries to log files. As of MySQL 5.1, the server's
logging capabilities for these logs are more flexible. Log entries can be
written to log files (as before) or to the general_log and slow_log tables
in the mysql database. If logging is enabled, either or both destinations
can be selected. The --log-output option controls the destination or
destinations of log output.
Upgrade Program. The mysql_upgrade program (available as of
MySQL 5.1.7) checks all existing tables for incompatibilities with the
current version of MySQL Server and repairs them if necessary. This
program should be run for each MySQL upgrade.
MySQL Cluster. MySQL Cluster is now released as a separate
product, based on MySQL 5.1 but with the addition of the
NDBCLUSTER storage engine. Clustering support is longer available in
mainline MySQL 5.1 releases. MySQL Cluster releases are identified by
a 3-part NDB version number; currently, the MySQL Cluster NDB 6.2
and MySQL Cluster NDB 6.3 release series are available for production
use.
Self Assessment Questions
3. The number of indexes allowed per table in MySQL are _______.

1.5 Connecting to and Disconnecting from the Server


To connect to the server, you will usually need to provide a MySQL user
name when you invoke mysql and, most likely, a password. If the server
runs on a machine other than the one where you log in, you will also need to
specify a host name. Contact your administrator to find out what connection
parameters you should use to connect (that is, what host, user name, and
password to use). Once you know the proper parameters, you should be
able to connect like this:
shell> mysql -h host -u user -p
Enter password: ********

Sikkim Manipal University

Page No. 12

RDBMS with MySQL

Unit 1

host and user represent the host name where your MySQL server is
running and the user name of your MySQL account. Substitute appropriate
values for your setup. The ******** represents your password; enter it when
mysql displays the Enter password: prompt.
If that works, you should see some introductory information followed by a
mysql> prompt:
shell> mysql -h host -u user -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 25338 to server version: 5.1.30-standard
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

The mysql> prompt tells you that mysql is ready for you to enter
commands.
If you are logging in on the same machine that MySQL is running on, you
can omit the host, and simply use the following:
shell> mysql -u user -p

If, when you attempt to log in, you get an error message such as ERROR
2002 (HY000): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2), it means that that MySQL server daemon (Unix) or
service (Windows) is not running. Consult the administrator
Some MySQL installations allow users to connect as the anonymous
(unnamed) user to the server running on the local host. If this is the case on
your machine, you should be able to connect to that server by invoking
mysql without any options:
shell> mysql

After you have connected successfully, you can disconnect any time by
typing QUIT (or \q) at the mysql> prompt:
mysql> QUIT
Bye

Sikkim Manipal University

Page No. 13

RDBMS with MySQL

Unit 1

Most examples in the following sections assume that you are connected to
the server. They indicate this by the mysql> prompt.

1.6 Accessing and Creating Databases and Tables


The following screen shots demonstrate a step-by-step approach in
connecting to the MySQL database server using a specific user name and
password as created during the installation steps.
User is asked for the password before logging in.

User enters the password.

Sikkim Manipal University

Page No. 14

RDBMS with MySQL

Unit 1

The following screen shot shows the appearance of mysql> prompt after the
user types in the correct password.

The following screen shot demonstrates the usage of Show Databases


command which shows all the available databases in the MySQL Server.

The following screen shot shows an error message wherein the user tries to
access a table without using any database; i.e. as s first step, the user must
select a database already present by using the USE <DATABASE-NAME>
command and then access the tables, views or any other data structures
within the database.
Other wise the error message Error 1046: No database Selected would be
generated by the database server.

Sikkim Manipal University

Page No. 15

RDBMS with MySQL

Unit 1

The screen shot below shows the command Create Database <DatabaseName> used to create a database by the user.
After successful execution of the above command, the user can type in the
Show Databases command to find out the successful creation of the
database created by him / her. Then the user can start creating his data
objects like tables inside the database.

Sikkim Manipal University

Page No. 16

RDBMS with MySQL

Unit 1

The following screenshot shows how the user can select his own database
created in the previous step and start creating tables using Create Table
command. The DESC command can be used to show the metadata
regarding the table created.

1.7 Loading Data


The following screen shots shows the step-by-step execution of the process
of loading data from a notepad (.txt file) into the MySQL server database
table.
It demonstrates from the first step of creating a notepad file to the stage of
loading and checking the data in your database table.

Sikkim Manipal University

Page No. 17

RDBMS with MySQL

Unit 1

Step 1: Opening a Notepad file from the Start menu of the Windows OS

Step 2: The data should be tab separated and the number of columns
correpsond to the number of columns of the base table. The data typed in
the notepad file should correspond to the data type set in the underlying
table.

Sikkim Manipal University

Page No. 18

RDBMS with MySQL

Unit 1

Step - 3: Saving in the notepad in the C drive of your hard disk.

Sikkim Manipal University

Page No. 19

RDBMS with MySQL

Unit 1

Step 4: Creation of the underlying base table to which the data values
should be populated from the notepad file created in the C drive of your hard
disk.
In this process we use the LOAD DATA INFILE C:\FileName.txt
command which enables us to load the data into the specified database
table.

Sikkim Manipal University

Page No. 20

RDBMS with MySQL

Unit 1

1.8 Summary
This unit has covered the following topics:
1. Features of MySQL: Some of the major features discussed here are
Internals and Portability, Data Type support, Security Issues, etc.

Sikkim Manipal University

Page No. 21

RDBMS with MySQL

Unit 1

2. Top Reasons for using MySQL: The top 10 reasons for using MySQL
by majority of the user community are discussed here like Scalability &
Flexibility, High Performance, Robust Transactional support, Web and
Data Warehouse strengths, Stronger data protection, etc.
3. MySQL Development Roadmap: This topic provides an overview
about features that have been implemented in previous series and that
are new in this current release series (5.1), and an overview about
upcoming additions or changes in the next release series (6.0).
4. Connecting to and Disconnecting from the Server: This section
discusses about the syntax used to connect to the database server and
disconnect from the database server using command line interface.
5. Accessing and Creating Databases and Tables: This topic discusses
various methods and functions used to access and create databases
and tables within the MySQL server database.
6. Loading Data: This topic discusses about the methods of loading data
into the MySQL database tables from external files such as notepad
data.

1.9 Terminal Questions


1. Mention 4 features of MySQL.
2. Write the syntax used to connect to MySQL server.
3. Give the syntax of Loading the data from a Notepad file into a table.

1.10 Answers
Self Assessment Questions
1. Relational Database Management System
2. Security
3. 64
Terminal Questions
1. 1. Portability
2. Fast Thread based Memory Allocation System
3. Full operator and Functional Support
4. Support for Group By and Order By Clauses
(Refer to section 1.2)
Sikkim Manipal University

Page No. 22

RDBMS with MySQL

Unit 1

2. shell> mysql -h host -u user -p


Enter password: ********
(Refer to section 1.5)
3. LOAD DATA LOCAL INFILE C:\empdata.txt INTO TABLE emp1;
(Refer to section 1.7)

Sikkim Manipal University

Page No. 23

RDBMS with MySQL

Unit 2

Unit 2

Data Types

Structure
2.1
Introduction
Objectives
2.2
Numeric Types
2.3
String Types
The CHAR and VARCHAR Types
The BLOB and TEXT Types
The ENUM Type
The SET Type
2.4
Date and Time Types
The DATETIME, DATE, and TIMESTAMP Types
The TIME Type
The YEAR Type
Y2K Issues and Date Types
2.5
Column Type Storage Requirements
2.6
Choosing the Right Type for a Column
2.7
Using Column Types from Other Database Engines
2.8
Summary
2.9
Terminal Questions
2.10 Answers

2.1 Introduction
In this unit the information regarding the data types uses in MySQL is
presented. An exhaustive discussion regarding the Numeric data types is
made with suitable examples is given. The String data type is used to
manipulate strings and characters; In this regards the data types such as
CHAR, VARCHAR, BLOB, and so on are discussed.
The Date and Time data types are also discussed.
In addition, the column storage requirements of different data types are
mentioned, so that the user can choose the right column types for his
database.

Sikkim Manipal University

Page No. 24

RDBMS with MySQL

Unit 2

Objectives
After studying this unit, you should be able to:
describe the importance of data types
apply the Numeric, String and Date & Time data types
describe the column storage requirements in MySQL
explain how to choose a right column type for individual columns
describe how to use column types from other database engines

2.2 Numeric Types


MySQL supports a number of data types in several categories: numeric
types, date and time types, and string (character) types. This chapter first
gives an overview of these data types, and then provides a more detailed
description of the properties of the types in each category, and a summary
of the data type storage requirements.
MySQL also supports extensions for handing spatial data.
Data type descriptions use these conventions:
M indicates the maximum display width for integer types. For floatingpoint and fixed-point types, M is the total number of digits that can be
stored. For string types, M is the maximum length. The maximum
allowable value of M depends on the data type.
D applies to floating-point and fixed-point types and indicates the
number of digits following the decimal point. The maximum possible
value is 30, but should be no greater than M2.
Square brackets ([ and ]) indicate optional parts of type definitions.
M indicates the maximum display width for integer types. The maximum
legal display width is 255. Display width is unrelated to the range of values a
type can contain.
For floating-point and fixed-point types, M is the total number of digits that
can be stored.
If you specify ZEROFILL for a numeric column, MySQL automatically adds
the UNSIGNED attribute to the column.
Numeric data types that allow the UNSIGNED attribute also allow SIGNED.
However, these data types are signed by default, so the SIGNED attribute
has no effect.
Sikkim Manipal University

Page No. 25

RDBMS with MySQL

Unit 2

SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT


UNIQUE.
SERIAL DEFAULT VALUE in the definition of an integer column is an alias
for NOT NULL AUTO_INCREMENT UNIQUE.
BIT[(M)]: A bit-field type. M indicates the number of bits per value, from
1 to 64. The default is 1 if M is omitted.
TINYINT[(M)] [UNSIGNED] [ZEROFILL]: A very small integer. The
signed range is -128 to 127. The unsigned range is 0 to 255.
BOOL, BOOLEAN: These types are synonyms for TINYINT(1). A value
of zero is considered false. Non-zero values are considered true:

Sikkim Manipal University

Page No. 26

RDBMS with MySQL

Unit 2

However, the values TRUE and FALSE are merely aliases for 1 and 0,
respectively, as shown here:

The last two statements display the results shown because 2 is equal to
neither 1 nor 0.
We intend to implement full boolean type handling, in accordance with
standard SQL, in a future MySQL release.
SMALLINT[(M)] [UNSIGNED] [ZEROFILL]: A small integer. The signed
range is -32768 to 32767. The unsigned range is 0 to 65535.
MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL]: A medium-sized integer.
The signed range is -8388608 to 8388607. The unsigned range is 0 to
16777215.
INT[(M)] [UNSIGNED] [ZEROFILL]: A normal-size integer. The signed
range is -2147483648 to 2147483647. The unsigned range is 0 to
4294967295.
INTEGER[(M)] [UNSIGNED] [ZEROFILL]: This type is a synonym for
INT.
Sikkim Manipal University

Page No. 27

RDBMS with MySQL

Unit 2

BIGINT[(M)] [UNSIGNED] [ZEROFILL]: A large integer. The signed


range is 9223372036854775808 to 9223372036854775807. The
unsigned range is 0 to
18446744073709551615.
SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
UNIQUE.
Some things you should be aware of with respect to BIGINT columns:
All arithmetic is done using signed BIGINT or DOUBLE values, so you
should not use unsigned big integers larger than 9223372036854775807
(63 bits) except with bit functions! If you do that, some of the last digits in the
result may be wrong because of rounding errors when converting a BIGINT
value to a DOUBLE.
You can always store an exact integer value in a BIGINT column by
storing it using a string. In this case, MySQL performs a string-to-number
conversion
that
involves
no
intermediate
double-precision
representation.
The -, +, and * operators use BIGINT arithmetic when both operands are
integer values. This means that if you multiply two big integers (or
results from functions that return integers), you may get unexpected
results when the result is larger than 9223372036854775807.
FLOAT [(M,D)] [UNSIGNED] [ZEROFILL]: A small (single-precision)
floating-point number. Allowable values are -3.402823466E+38 to 1.175494351E-38, 0, and
1.175494351E-38 to 3.402823466E+38.
These are the theoretical limits, based on the IEEE standard. The actual
range might be slightly smaller depending on your hardware or operating
system.
M is the total number of digits and D is the number of digits following the
decimal point. If M and D are omitted, values are stored to the limits allowed
by the hardware. A single-precision floating-point number is accurate to
approximately 7 decimal places.
UNSIGNED, if specified, disallows negative values.

Sikkim Manipal University

Page No. 28

RDBMS with MySQL

Unit 2

Note: Using FLOAT might give you some unexpected problems because all
calculations in MySQL are done with double precision.
DOUBLE [(M,D)] [UNSIGNED] [ZEROFILL]:
A normal - size (double-precision) floating-point number.
Allowable values are -1.7976931348623157E+308 to 2.2250738585072014E-308, 0, and 2.2250738585072014E-308 to
1.7976931348623157E+308.
These are the theoretical limits, based on the IEEE standard. The actual
range might be slightly smaller depending on your hardware or operating
system.
M is the total number of digits and D is the number of digits following the
decimal point. If M and D are omitted, values are stored to the limits allowed
by the hardware. A double-precision floating-point number is accurate to
approximately 15 decimal places.
UNSIGNED, if specified, disallows negative values.
DOUBLE PRECISION [(M,D)] [UNSIGNED] [ZEROFILL], REAL[(M,D)]
[UNSIGNED] [ZEROFILL]: These types are synonyms for DOUBLE.
Exception: If the REAL_AS_FLOAT SQL mode is enabled, REAL is a
synonym for FLOAT rather than DOUBLE.
FLOAT(p) [UNSIGNED] [ZEROFILL]: A floating - point number. p
represents the precision in bits, but MySQL uses this value only to
determine whether to use FLOAT or DOUBLE for the resulting data type. If
p is from 0 to 24, the data type becomes FLOAT with no M or D values. If p
is from 25 to 53, the data type becomes DOUBLE with no M or D values.
The range of the resulting column is the same as for the single-precision
FLOAT or double-precision DOUBLE data types described earlier in this
section.
Note: FLOAT(p) syntax is provided for ODBC compatibility.
DECIMAL [(M[,D])] [UNSIGNED] [ZEROFILL]: A packed exact fixed-point
number. M is the total number of digits (the precision) and D is the number
of digits after the decimal point (the scale). The decimal point and (for
negative numbers) the - sign are not counted in M. If D is 0, values have
no decimal point or fractional part. The maximum number of digits (M) for
Sikkim Manipal University

Page No. 29

RDBMS with MySQL

Unit 2

DECIMAL is 65. The maximum number of supported decimals (D) is 30. If D


is omitted, the default is 0. If M is omitted, the default is 10.
UNSIGNED, if specified, disallows negative values.
All basic calculations (+, -, *, /) with DECIMAL columns are done with a
precision of 65 digits.
DEC[(M[,D])] [UNSIGNED] [ZEROFILL], NUMERIC[(M[,D])] [UNSIGNED]
[ZEROFILL], FIXED[(M[,D])] [UNSIGNED] [ZEROFILL]:
These types are synonyms for DECIMAL. The FIXED synonym is available
for compatibility with other database systems.

2.3 String Types


The string types are CHAR, VARCHAR, BINARY, VARBINARY, BLOB,
TEXT, ENUM, and SET. This section describes how these types work and
how to use them in your queries.
2.3.1 The CHAR and VARCHAR Types
The CHAR and VARCHAR types are similar, but differ in the way they are
stored and retrieved. They also differ in maximum length and in whether
trailing spaces are retained.
The CHAR and VARCHAR types are declared with a length that indicates
the maximum number of characters you want to store. For example,
CHAR(30) can hold up to 30 characters.
The length of a CHAR column is fixed to the length that you declare when
you create the table. The length can be any value from 0 to 255. When
CHAR values are stored, they are right-padded with spaces to the specified
length. When CHAR values are retrieved, trailing spaces are removed
unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.
Values in VARCHAR columns are variable-length strings. The length can be
specified as a value from 0 to 65,535. The effective maximum length of a
VARCHAR is subject to the maximum row size (65,535 bytes, which is
shared among all columns) and the character set used.
In contrast to CHAR, VARCHAR values are stored as a one-byte or twobyte length prefix plus data. The length prefix indicates the number of bytes
in the value. A column uses one length byte if values require no more than
255 bytes, two length bytes if values may require more than 255 bytes.
Sikkim Manipal University

Page No. 30

RDBMS with MySQL

Unit 2

If strict SQL mode is not enabled and you assign a value to a CHAR or
VARCHAR column that exceeds the column's maximum length, the value is
truncated to fit and a warning is generated. For truncation of non-space
characters, you can cause an error to occur (rather than a warning) and
suppress insertion of the value by using strict SQL mode.
For VARCHAR columns, trailing spaces in excess of the column length are
truncated prior to insertion and a warning is generated, regardless of the
SQL mode in use. For CHAR columns, truncation of excess trailing spaces
from inserted values is performed silently regardless of the SQL mode.
VARCHAR values are not padded when they are stored. Trailing spaces are
retained when values are stored and retrieved, in conformance with
standard SQL.
The following table illustrates the differences between CHAR and
VARCHAR by showing the result of storing various string values into
CHAR(4) and VARCHAR(4) columns (assuming that the column uses a
single-byte character set such as latin1):
Table 2.1: Differences between Char and Varchar Variables
Value

CHAR(4)

ab

Storage
Required

VARCHAR(4)

Storage
Required

4 bytes

1 bytes

ab

4 bytes

ab

3 bytes

abcd

abcd

4 bytes

abcd

5 bytes

abcdefgh

abcd

4 bytes

abcd

5 bytes

The values shown as stored in the last row of the table apply only when not
using strict mode; if MySQL is running in strict mode, values that exceed the
column length are not stored, and an error results.
If a given value is stored into the CHAR(4) and VARCHAR(4) columns, the
values retrieved from the columns are not always the same because trailing
spaces are removed from CHAR columns upon retrieval. The following
example illustrates this difference:

Sikkim Manipal University

Page No. 31

RDBMS with MySQL

Unit 2

Values in CHAR and VARCHAR columns are sorted and compared


according to the character set collation assigned to the column.
All MySQL collations are of type PADSPACE. This means that all CHAR
and VARCHAR values in MySQL are compared without regard to any
trailing spaces. For example:

This is true for all MySQL versions, and that it is not affected by the server
SQL mode.

Sikkim Manipal University

Page No. 32

RDBMS with MySQL

Unit 2

For those cases where trailing pad characters are stripped or comparisons
ignore them, if a column has an index that requires unique values, inserting
into the column values that differ only in number of trailing pad characters
will result in a duplicate-key error. For example, if a table contains 'a', an
attempt to store 'a' causes a duplicate-key error.
2.3.2 The BLOB and TEXT Types
A BLOB is a binary large object that can hold a variable amount of data. The
four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB.
These differ only in the maximum length of the values they can hold. The
four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT.
These correspond to the four BLOB types and have the same maximum
lengths and storage requirements.
BLOB columns are treated as binary strings (byte strings). TEXT columns
are treated as non-binary strings (character strings). BLOB columns have no
character set, and sorting and comparison are based on the numeric values
of the bytes in column values. TEXT columns have a character set, and
values are sorted and compared based on the collation of the character set.
If strict SQL mode is not enabled and you assign a value to a BLOB or
TEXT column that exceeds the column's maximum length, the value is
truncated to fit and a warning is generated. For truncation of non-space
characters, you can cause an error to occur (rather than a warning) and
suppress insertion of the value by using strict SQL mode. Beginning with
MySQL 5.1.24, truncation of excess trailing spaces from values to be
inserted into TEXT columns always generates a warning, regardless of the
SQL mode. (Bug#30059)
If a TEXT column is indexed, index entry comparisons are space-padded at
the end. This means that, if the index requires unique values, duplicate-key
errors will occur for values that differ only in the number of trailing spaces.
For example, if a table contains 'a', an attempt to store 'a ' causes a
duplicate-key error. This is not true for BLOB columns.
In most respects, you can regard a BLOB column as a VARBINARY column
that can be as large as you like. Similarly, you can regard a TEXT column
as a VARCHAR column. BLOB and TEXT differ from VARBINARY and
VARCHAR in the following ways:
Sikkim Manipal University

Page No. 33

RDBMS with MySQL

Unit 2

For indexes on BLOB and TEXT columns, you must specify an index
prefix length. For CHAR and VARCHAR, a prefix length is optional.
BLOB and TEXT columns cannot have DEFAULT values.
LONG and LONG VARCHAR map to the MEDIUMTEXT data type. This is a
compatibility feature. If you use the BINARY attribute with a TEXT data type,
the column is assigned the binary collation of the column character set.
MySQL Connector/ODBC defines BLOB values as LONGVARBINARY and
TEXT values as LONGVARCHAR.
Because BLOB and TEXT values can be extremely long, you might
encounter some constraints in using them:
Only the first max_sort_length bytes of the column are used when
sorting. The default value of max_sort_length is 1024. This value can be
changed using the --max_sort_length=N option when starting the mysqld
server.
You can make more bytes significant in sorting or grouping by increasing
the value of max_sort_length at runtime. Any client can change the value of
its session max_sort_length variable:
mysql> SET max_sort_length = 2000;
mysql> SELECT id, comment FROM t
-> ORDER BY comment;

Another way to use GROUP BY or ORDER BY on a BLOB or TEXT column


containing long values when you want more than max_sort_length bytes to
be significant is to convert the column value into a fixed-length object. The
standard way to do this is with the SUBSTRING() function. For example, the
following statement causes 2000 bytes of the comment column to be taken
into account for sorting:
mysql> SELECT id, SUBSTRING(comment,1,2000) FROM t
-> ORDER BY SUBSTRING(comment,1,2000);

The maximum size of a BLOB or TEXT object is determined by its type, but
the largest value you actually can transmit between the client and server is
determined by the amount of available memory and the size of the
Sikkim Manipal University

Page No. 34

RDBMS with MySQL

Unit 2

communications buffers. You can change the message buffer size by


changing the value of the max_allowed_packet variable, but you must do so
for both the server and your client program.
Each BLOB or TEXT value is represented internally by a separately
allocated object. This is in contrast to all other data types, for which storage
is allocated once per column when the table is opened.
2.3.3 The ENUM Type
An ENUM is a string object with a value chosen from a list of allowed values
that are enumerated explicitly in the column specification at table creation
time.
An enumeration value must be a quoted string literal; it may not be an
expression, even one that evaluates to a string value. For example, you can
create a table with an ENUM column like this:
CREATE TABLE sizes (
name ENUM('small', 'medium', 'large')
);

2.3.4 The SET Type


A string type that can accept zero or more values from a set of previously
defined possible values. This data type allows the selection of any number
of values from a predefined set of string values.
SET column values that consist of multiple set members are specified with
members separated by commas (,). A consequence of this is that SET
member values should not themselves contain commas.
A SET can have a maximum of 64 different members.
Duplicate values in the definition cause a warning, or an error if strict SQL
mode is enabled.
Trailing spaces are automatically deleted from SET member values in the
table definition when a table is created.

Sikkim Manipal University

Page No. 35

RDBMS with MySQL

Unit 2

This makes the SET data type a good choice for multiple-choice selections
as shown in the example given below:

Note: As with ENUM type, any attempt to use a value that is not part of the
predefined set will cause MySQL to print an error message instead as
shown in the example below:

Self Assessment Questions


1. If MySQL is running in ____ mode, values that exceed the column
length are not stored, and an error results.
2. The ______ columns have no character set, and sorting and comparison
are based on the numeric values of the bytes in column values.

2.4 Date and Time Types


The date and time types for representing temporal values are DATETIME,
DATE, TIMESTAMP, TIME, and YEAR. Each temporal type has a range of
legal values, as well as a zero value that may be used when you specify
Sikkim Manipal University

Page No. 36

RDBMS with MySQL

Unit 2

an illegal value that MySQL cannot represent. The TIMESTAMP type has
special automatic updating behavior, described later on.
MySQL gives warnings or errors if you try to insert an illegal date. By setting
the SQL mode to the appropriate value, you can specify more exactly what
kind of dates you want MySQL to support.
You can get MySQL to accept certain dates, such as '2009-11-31', by using
the ALLOW_INVALID_DATES SQL mode. This is useful when you want to
store a possibly wrong value which the user has specified (for example, in
a web form) in the database for future processing.
Under this mode, MySQL verifies only that the month is in the range from 0
to 12 and that the day is in the range from 0 to 31. These ranges are defined
to include zero because MySQL allows you to store dates where the day or
month and day are zero in a DATE or DATETIME column.
This is extremely useful for applications that need to store a birthdate for
which you do not know the exact date. In this case, you simply store the
date as '2009-00-00' or '2009-01-00'.
If you store dates such as these, you should not expect to get correct results
for functions such as DATE_SUB() or DATE_ADD() that require complete
dates. (If you do not want to allow zero in dates, you can use the
NO_ZERO_IN_DATE SQL mode).
Prior to MySQL 5.1.18, when DATE values are compared with DATETIME
values, the time portion of the DATETIME value is ignored, or the
comparison could be performed as a string compare.
Starting from MySQL 5.1.18, a DATE value is coerced to the DATETIME
type by adding the time portion as '00:00:00'. To mimic the old behavior, use
the CAST() function to cause the comparison operands to be treated as
previously.
For example:
date_col = CAST(NOW() AS DATE);

MySQL also allows you to store '0000-00-00' as a dummy date (if you are
not using the NO_ZERO_DATE SQL mode). This is in some cases more
convenient (and uses less data and index space) than using NULL values.
Sikkim Manipal University

Page No. 37

RDBMS with MySQL

Unit 2

Following are the general considerations to keep in mind when working with
date and time types:
MySQL retrieves values for a given date or time type in a standard
output format, but it attempts to interpret a variety of formats for input
values that you supply (for example, when you specify a value to be
assigned to or compared to a date or time type). Only the formats
described in the following sections are supported. It is expected that you
supply legal values. Unpredictable results may occur if you use values in
other formats.
Dates containing two-digit year values are ambiguous because the
century is unknown. MySQL interprets two-digit year values using the
following rules:
o Year values in the range 70-99 are converted to 1970-1999.
o Year values in the range 00-69 are converted to 2000-2069.
Although MySQL tries to interpret values in several formats, dates
always must be given in year-month-day order (for example, '98-0904'), rather than in the month-day-year or day-month-year orders
commonly used elsewhere (for example, '09-04-98', '04-09-98').
MySQL automatically converts a date or time type value to a number if
the value is used in a numeric context and vice versa.
By default, when MySQL encounters a value for a date or time type that
is out of range or otherwise illegal for the type (as described at the
beginning of this section), it converts the value to the zero value for
that type. The exception is that out-of-range TIME values are clipped to
the appropriate endpoint of the TIME range.
The following table shows the format of the zero value for each type.
Note that the use of these values produces warnings if the
NO_ZERO_DATE SQL mode is enabled.
Data Type

Zero Value

DATETIME

'0000-00-00 00:00:00'

DATE

'0000-00-00'

TIMESTAMP

'0000-00-00 00:00:00'

TIME

'00:00:00'

YEAR

0000

Sikkim Manipal University

Page No. 38

RDBMS with MySQL

Unit 2

The table below lists the various date and time data types, together with
their allowed ranges and formats:
Table 2.2: Date and Time Data Types

Type

Size
(bytes)

Range

Format

Used For

DATE

1000-01-01 to
9999-12-31

YYYY-MM-DD

Date Values

TIME

-838:59:59 to
838:59:59

HH:MM:SS

Time values or
durations

YEAR

1901 to 2155

YYYY

Year Values

DATETIME

1000-01-01
00:00:00 to
9999-12-31
23:59:59

YYYY-MM-DD
HH:MM:SS

Combined date
and time values

TIMESTAMP

1970-01-01
00:00:01 to
2038-01-09
03:14:07

YYYYMMDDHHM
MSS

Combined date
and time values,
timestamps

2.4.1 The DATETIME, DATE, and TIMESTAMP Types


The DATETIME, DATE, and TIMESTAMP types are related. This section
describes their characteristics, how they are similar, and how they differ.
The DATETIME type is used when you need values that contain both date
and time information. MySQL retrieves and displays DATETIME values in
'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01
00:00:00' to '9999-12-31 23:59:59'.
The DATE type is used when you need only a date value, without a time
part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format.
The supported range is '1000-01-01' to '9999-12-31'.
For the DATETIME and DATE range descriptions, supported means that
although earlier values might work, there is no guarantee.
The TIMESTAMP data type has a range of '1970-01-01 00:00:01' UTC to
'2038-01-09 03:14:07' UTC. It has varying properties, depending on the
MySQL version and the SQL mode the server is running in. These
properties are described later in this section.
Sikkim Manipal University

Page No. 39

RDBMS with MySQL

Unit 2

You can specify DATETIME, DATE, and TIMESTAMP values using any of a
common set of formats:
As a string in either 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD
HH:MM:SS' format. A relaxed syntax is allowed: Any punctuation
character may be used as the delimiter between date parts or time parts.
For example, '98-12-31 11:30:45', '98.12.31 11+30+45', '98/12/31
11*30*45', and '98@12@31 11^30^45' are equivalent.
As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A relaxed
syntax is allowed here, too. For example, '98-12-31', '98.12.31',
'98/12/31', and '98@12@31' are equivalent.
As a string with no delimiters in either 'YYYYMMDDHHMMSS' or
'YYMMDDHHMMSS' format, provided that the string makes sense as a
date. For example, '20070523091528' and '070523091528' are
interpreted as '2007-05-23 09:15:28', but '071122129015' is illegal (it has
a nonsensical minute part) and becomes '0000-00-00 00:00:00'.
As a string with no delimiters in either 'YYYYMMDD' or 'YYMMDD'
format, provided that the string makes sense as a date. For example,
'20070523' and '070523' are interpreted as '2007-05-23', but '071332' is
illegal (it has nonsensical month and day parts) and becomes '0000-0000'.
As a number in either YYYYMMDDHHMMSS or YYMMDDHHMMSS
format, provided that the number makes sense as a date. For example,
19830905132800 and 830905132800 are interpreted as '1983-09-05
13:28:00'.
As a number in either YYYYMMDD or YYMMDD format, provided that
the number makes sense as a date. For example, 19830905 and
830905 are interpreted as '1983-09-05'.
As the result of a function that returns a value that is acceptable in a
DATETIME, DATE, or TIMESTAMP context, such as NOW() or
CURRENT_DATE.
A microseconds part is allowable in temporal values in some contexts, such
as in literal values, and in the arguments to or return values from some

Sikkim Manipal University

Page No. 40

RDBMS with MySQL

Unit 2

temporal functions. Microseconds are specified as a trailing .uuuuuu part in


the value. Example:
mysql> SELECT MICROSECOND('2010-12-10 14:12:09.019473');
+-------------------------------------------+
| MICROSECOND('2010-12-10 14:12:09.019473') |
+-------------------------------------------+
|
19473 |
+-------------------------------------------+

However, microseconds cannot be stored into a column of any temporal


data type. Any microseconds part is discarded.
Conversion of TIME or DATETIME values to numeric form (for example, by
adding +0) results in a double value with a microseconds part of .000000:
mysql> SELECT CURTIME(), CURTIME()+0;
+-----------+---------------------------+
| CURTIME() | CURTIME()+0 |
+-----------+---------------------------+
| 10:41:36 | 104136.000000 |
+-----------+---------------------------+
mysql> SELECT NOW(), NOW()+0;
+---------------------+--------------------------------------------+
| NOW()
| NOW()+0
|
+---------------------+---------------------------------------------+
| 2007-11-30 10:41:47 | 20071130104147.000000 |
+---------------------+----------------------------------------------+

Illegal DATETIME, DATE, or TIMESTAMP values are converted to the


zero value of the appropriate type ('0000-00-00 00:00:00' or '0000-00-00').
TIMESTAMP Properties
TIMESTAMP columns are displayed in the same format as DATETIME
columns. In other words, the display width is fixed at 19 characters, and the
format is 'YYYY-MM-DD HH:MM:SS'.
TIMESTAMP values are converted from the current time zone to UTC for
storage, and converted back from UTC to the current time zone for retrieval.
(This occurs only for the TIMESTAMP data type, not for other types such as

Sikkim Manipal University

Page No. 41

RDBMS with MySQL

Unit 2

DATETIME.) By default, the current time zone for each connection is the
server's time. The time zone can be set on a per-connection basis.
As long as the time zone setting remains constant, you get back the same
value you store. If you store a TIMESTAMP value, and then change the time
zone and retrieve the value, the retrieved value is different from the value
you stored. This occurs because the same time zone was not used for
conversion in both directions.
The current time zone is available as the value of the time_zone system
variable.
The TIMESTAMP data type offers automatic initialization and updating. You
can choose whether to use these properties and which column should have
them
2.4.2 The TIME Type
MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or
'HHH:MM:SS' format for large hours values). TIME values may range from '838:59:59' to '838:59:59'. The hours part may be so large because the
TIME type can be used not only to represent a time of day (which must be
less than 24 hours), but also elapsed time or a time interval between two
events (which may be much greater than 24 hours, or even negative).
You can specify TIME values in a variety of formats:
As a string in 'D HH:MM:SS.fraction' format. You can also use one of
the following relaxed syntaxes: 'HH:MM:SS.fraction', 'HH:MM:SS',
'HH:MM', 'D HH:MM:SS', 'D HH:MM', 'D HH', or 'SS'. Here D represents
days and can have a value from 0 to 34. Note that MySQL does not
store the fraction part.
As a string with no delimiters in 'HHMMSS' format, provided that it
makes sense as a time. For example, '101112' is understood as
'10:11:12', but '109712' is illegal (it has a nonsensical minute part) and
becomes '00:00:00'.
As a number in HHMMSS format, provided that it makes sense as a
time. For example, 101112 is understood as '10:11:12'. The following
alternative formats are also understood: SS, MMSS, HHMMSS,
HHMMSS.fraction. Note that MySQL does not store the fraction part.

Sikkim Manipal University

Page No. 42

RDBMS with MySQL

Unit 2

As the result of a function that returns a value that is acceptable in a


TIME context, such as CURRENT_TIME.
A trailing .uuuuuu microseconds part of TIME values is allowed under the
same conditions as for other temporal values, The DATETIME, DATE, and
TIMESTAMP Types. This includes the property that any microseconds part
is discarded from values stored into TIME columns.
For TIME values specified as strings that include a time part delimiter, it is
not necessary to specify two digits for hours, minutes, or seconds values
that are less than 10. '8:3:2' is the same as '08:03:02'.
Be careful about assigning abbreviated values to a TIME column. Without
colons, MySQL interprets values using the assumption that the two
rightmost digits represent seconds. (MySQL interprets TIME values as
elapsed time rather than as time of day.) For example, you might think of
'1112' and 1112 as meaning '11:12:00' (12 minutes after 11 o'clock), but
MySQL interprets them as '00:11:12' (11 minutes, 12 seconds). Similarly,
'12' and 12 are interpreted as '00:00:12'. TIME values with colons, by
contrast, are always treated as time of the day. That is, '11:12' mean
'11:12:00', not '00:11:12'.
By default, values that lie outside the TIME range but are otherwise legal
are clipped to the closest endpoint of the range. For example, '-850:00:00'
and '850:00:00' are converted to '-838:59:59' and '838:59:59'.
Illegal TIME values are converted to '00:00:00'. Note that because '00:00:00'
is itself a legal TIME value, there is no way to tell, from a value of '00:00:00'
stored in a table, whether the original value was specified as '00:00:00' or
whether it was illegal.
For more restrictive treatment of invalid TIME values, enable strict SQL
mode to cause errors to occur.
2.4.3 The YEAR Type
The YEAR type is a one-byte type used for representing years. It can be
declared as YEAR(2) or YEAR(4) to specify a display width of two or four
characters. The default is four characters if no width is given.
For four-digit format, MySQL displays YEAR values in YYYY format, with a
range of 1901 to 2155. For two-digit format, MySQL displays values with a
range of 70 (1970) to 69 (2069).
Sikkim Manipal University

Page No. 43

RDBMS with MySQL

Unit 2

You can specify input YEAR values in a variety of formats:


As a four-digit string in the range '1901' to '2155'.
As a four-digit number in the range 1901 to 2155.
As a two-digit string in the range '00' to '99'. Values in the ranges '00' to
'69' and '70' to '99' are converted to YEAR values in the ranges 2000 to
2069 and 1970 to 1999.
As a two-digit number in the range 1 to 99. Values in the ranges 1 to 69
and 70 to 99 are converted to YEAR values in the ranges 2001 to 2069
and 1970 to 1999. Note that the range for two-digit numbers is slightly
different from the range for two-digit strings, because you cannot specify
zero directly as a number and have it be interpreted as 2000. You must
specify it as a string '0' or '00' or it is interpreted as 0000.
As the result of a function that returns a value that is acceptable in a
YEAR context, such as NOW(). Illegal YEAR values are converted to
0000.
2.4.4 Y2K Issues and Date Types
MySQL Server itself has no problems with Year 2000 (Y2K) compliance:
MySQL Server uses Unix time functions that handle dates into the year
2038 for TIMESTAMP values. For DATE and DATETIME values, dates
through the year 9999 are accepted.
All MySQL date functions are implemented in one source file,
sql/time.cc, and are coded very carefully to be year 2000-safe.
In MySQL, the YEAR data type can store the years 0 and 1901 to 2155
in one byte and display them using two or four digits. All two-digit years
are considered to be in the range 1970 to 2069, which means that if you
store 01 in a YEAR column, MySQL Server treats it as 2001.
Although MySQL Server itself is Y2K-safe, you may run into problems if you
use it with applications that are not Y2K-safe. For example, many old
applications store or manipulate years using two-digit values (which are
ambiguous) rather than four-digit values. This problem may be compounded
by applications that use values such as 00 or 99 as missing value
indicators. Unfortunately, these problems may be difficult to fix because
Sikkim Manipal University

Page No. 44

RDBMS with MySQL

Unit 2

different applications may be written by different programmers, each of


whom may use a different set of conventions and date-handling functions.
Thus, even though MySQL Server has no Y2K problems, it is the
application's responsibility to provide unambiguous input. Any value
containing a two-digit year is ambiguous, because the century is unknown.
Such values must be interpreted into four-digit form because MySQL stores
years internally using four digits.
For DATETIME, DATE, TIMESTAMP, and YEAR types, MySQL interprets
dates with ambiguous year values using the following rules:
Year values in the range 00-69 are converted to 2000-2069.
Year values in the range 70-99 are converted to 1970-1999.
Remember that these rules are only heuristics that provide reasonable
guesses as to what your data values mean. If the rules used by MySQL do
not produce the correct values, you should provide unambiguous input
containing four-digit year values.
Self Assessment Questions
3. Each _____ type has a range of legal values, as well as a zero value
that may be used when you specify an illegal value that MySQL cannot
represent.
4. You can get MySQL to accept certain dates, such as '2009-11-31', by
using the ______ SQL mode.
5. The format of the timestamp column in MySQL is ________________.

2.5 Column Type Storage Requirements


The storage requirements for each of the column types supported by
MySQL are listed by category.
The maximum size of a row in a MyISAM table is 65534 bytes. Each BLOB
and TEXT column accounts for only 5-9 bytes toward this size.
If a table includes any variable-length column types, the record format will
also be variable-length.
Note that when a table is created, MySQL may, under certain conditions,
change a column from a variable-length type to a fixed-length type, or viceversa.
Sikkim Manipal University

Page No. 45

RDBMS with MySQL

Unit 2

1. Numeric Types:
Table 2.3: Numeric Data Types

Column Type

Storage Required
(in bytes)

TINYINT

SMALLINT

MEDIUMINT

INT

INTEGER

BIGINT

FLOAT(X)

4 if 0 <= X <= 24,


8 if 25 <= X <= 53

FLOAT

DOUBLE

DOUBLE PRECISION

REAL

DECIMAL(M,D), NUMERIC(M,D)

M+2 bytes if D > 0,


M+1 bytes if D = 0 (D+2, if M < D)

Date and Time Types:


Column Type

Storage Required (in bytes)

DATE

DATETIME

TIMESTAMP

TIME

YEAR

Sikkim Manipal University

Page No. 46

RDBMS with MySQL

Unit 2

String Types:
Table 2.4: String Data Type Storage Requirements

Column Type
CHAR(M)
VARCHAR(M)
TINYBLOB, TINYTEXT
BLOB, TEXT
MEDIUMBLOB, MEDIUMTEXT
LONGBLOB, LONGTEXT
ENUM('value1','value2',...)

SET('value1','value2',...)

Storage Required
(in bytes)
M bytes, 0 <= M <= 255
L+1 bytes, where L <= M and 0 <= M <=
255
L+1 bytes, where L < 2^8
L+2 bytes, where L < 2^16
L+3 bytes, where L < 2^24
L+4 bytes, where L < 2^32
1 or 2 bytes, depending on the number of
enumeration
values (65,535 values maximum)
1, 2, 3, 4, or 8 bytes, depending on the
number of set members
(64 members maximum)

VARCHAR and the BLOB and TEXT types are variable-length types, for
which the storage requirements depend on the actual length of column
values (represented by L in the preceding table), rather than on the types
maximum possible size.
For example, a VARCHAR(10) column can hold a string with a maximum
length of 10 characters. The actual storage required is the length of the
string (L), plus 1 byte to record the length of the string. For the string 'abcd',
L is 4 and the storage requirement is 5 bytes.
The BLOB and TEXT types require 1, 2, 3, or 4 bytes to record the length of
the column
value, depending on the maximum possible length of the type.
The size of an ENUM object is determined by the number of different
enumeration values. One byte is used for enumerations with up to 255
possible values. Two bytes are used for enumerations with up to 65535
values.

Sikkim Manipal University

Page No. 47

RDBMS with MySQL

Unit 2

The size of a SET object is determined by the number of different set


members. If the set size is N, the object occupies (N+7)/8 bytes, rounded
up to 1, 2, 3, 4, or 8 bytes. A SET can have a maximum of 64 members.

2.6 Choosing the Right Type for a Column


For the most efficient use of storage, try to use the most precise type in all
cases. For example, if an integer column will be used for values in the range
from 1 to 99999, MEDIUMINT UNSIGNED is the best type. Of the types that
represent all the required values, it uses the least amount of storage.
Accurate representation of monetary values is a common problem. In
MySQL, you should use the DECIMAL type. This is stored as a string, so no
loss of accuracy should occur. If accuracy is not too important, the DOUBLE
type may also be good enough.
For high precision, you can always convert to a fixed-point type stored in a
BIGINT. This allows you to do all calculations with integers and convert
results back to floating-point values only when necessary.

2.7 Using Column Types from Other Database Engines


To make it easier to use code written for SQL implementations from other
vendors, MySQL maps column types as shown in the following table. These
mappings make it easier to import table definitions from other database
engines into MySQL:
Column type mapping occurs at table creation time. If you create a table
with types used by other vendors and then issue a DESCRIBE tbl_name
statement, MySQL reports the table structure using the equivalent MySQL
types.
Table 2.5: Mapping of MySQL Data Types and Other Vendor Types

Other Vendor Type

MySQL Type

BINARY(NUM)

CHAR(NUM) BINARY

CHAR VARYING(NUM)

VARCHAR(NUM)

FLOAT4

FLOAT

FLOAT8

DOUBLE

INT1

TINYINT

Sikkim Manipal University

Page No. 48

RDBMS with MySQL

Unit 2

INT2

SMALLINT

INT3

MEDIUMINT

INT4

INT

INT8

BIGINT

LONG VARBINARY

MEDIUMBLOB

LONG VARCHAR

MEDIUMTEXT

LONG

MEDIUMTEXT (MySQL 4.1.0 on)

MIDDLEINT

MEDIUMINT

VARBINARY(NUM)

VARCHAR(NUM) BINARY

Column type mapping occurs at table creation time. If you create a table
with types used by other vendors and then issue a DESCRIBE tbl_name
statement, MySQL reports the table structure using the equivalent MySQL
types.

2.8 Summary
This unit covers the following topics with respect to data types in MySQL:
1. Numeric Types: This topic discusses various Numeric data types like
Bit, Float, Integer, and so on and their various syntaxes available for
usage in numerical calculations of SQL statements.
2. String Types: This topic discusses the character data types and their
variants like CHAR and VARCHAR, String data types and their
variations.
3. Date and Time Types: This topic discusses the different variants of
Date, DateTime, and TimeStamp data types used in MySQL along with
their syntaxes and the context of usage.
4. Column Type Storage Requirements: This topic discusses the storage
requirements for each of the column types so that the user can judge the
optimum data types for his database for saving the storage space.
5. Using Column Types from other Database Engines: To make it
easier to use code written for SQL implementations from other vendors,
MySQL maps column types. These mappings make it easier to import
table definitions from other database engines into MySQL.
Sikkim Manipal University

Page No. 49

RDBMS with MySQL

Unit 2

2.9 Terminal Questions


1. Write about float and double data types
2. Write the differences between char and varchar data types in MySQL.
3. List the various date and time data types, together with their allowed
ranges and formats
4. Describe the storage requirements of String Column types

2.10 Answers
Self Assessment Questions
1. strict
2. BLOB
3. temporal
4. ALLOW_INVALID_DATES
5. 'YYYY-MM-DD HH:MM:SS'
Terminal Questions
1. FLOAT [(M,D)] [UNSIGNED] [ZEROFILL]: A small (single-precision)
floating-point number. Allowable values are -3.402823466E+38 to 1.175494351E-38, 0, and
1.175494351E-38 to 3.402823466E+38.
DOUBLE [(M,D)] [UNSIGNED] [ZEROFILL]: A normal - size (doubleprecision)
floating-point
number.
Allowable
values
are
1.7976931348623157E+308 to -2.2250738585072014E-308, 0, and
2.2250738585072014E-308 to 1.7976931348623157E+308.
M is the total number of digits and D is the number of digits following the
decimal point.
(Refer Section 2.2)
2. The CHAR and VARCHAR types are declared with a length that
indicates the maximum number of characters you want to store. For
example, CHAR(30) can hold up to 30 characters.
The length of a CHAR column is fixed to the length that you declare
when you create the table. The length can be any value from 0 to 255.
Values in VARCHAR columns are variable-length strings. The length
can be specified as a value from 0 to 65,535. The effective maximum
Sikkim Manipal University

Page No. 50

RDBMS with MySQL

Unit 2

length of a VARCHAR is subject to the maximum row size (65,535


bytes, which is shared among all columns) and the character set used.
(Refer Section 2.3)
3. (Refer Section 2.4)
Type

Size
(bytes)

Range

Format

Used For

DATE

1000-01-01 to
9999-12-31

YYYY-MM-DD

Date Values

TIME

-838:59:59 to
838:59:59

HH:MM:SS

Time values
or durations

YEAR

1901 to 2155

YYYY

Year Values

DATETIME

1000-01-01
00:00:00 to
9999-12-31
23:59:59

YYYY-MM-DD
HH:MM:SS

Combined
date and time
values

TIMESTAMP

1970-01-01
00:00:01 to
2038-01-09
03:14:07

YYYYMMDDHHMMSS

Combined
date and time
values,
timestamps

4. (Refer Section 2.5)


Column Type

Storage Required
(in bytes)

CHAR(M)

M bytes, 0 <= M <= 255

VARCHAR(M)

L+1 bytes, where L <= M and 0 <= M <= 255

TINYBLOB, TINYTEXT

L+1 bytes, where L < 2^8

BLOB, TEXT

L+2 bytes, where L < 2^16

MEDIUMBLOB, MEDIUMTEXT

L+3 bytes, where L < 2^24

LONGBLOB, LONGTEXT

L+4 bytes, where L < 2^32

ENUM('value1','value2',...)

1 or 2 bytes, depending on the number of


enumeration
values (65,535 values maximum)

SET('value1','value2',...)

1, 2, 3, 4, or 8 bytes, depending on the


number of set members
(64 members maximum)

Sikkim Manipal University

Page No. 51

RDBMS with MySQL

Unit 3

Unit 3

Data Definition Language (DDL)

Structure
3.1
Introduction
Objectives
3.2
CREATE DATABASE
3.3
CREATE INDEX
3.4
CREATE TABLE
3.5
ALTER DATABASE
3.6
ALTER TABLE
3.7
DROP DATABASE
3.8
DROP INDEX
3.9
DROP TABLE
3.10 DESCRIBE
3.11 Summary
3.12 Terminal Questions
3.13 Answers

3.1 Introduction
The Data Definition Language (DDL) in any database server is used to
define the structures used to store the data. The Data Definition language
presented in this unit is used to create indexes and tables within the
specified database. The methods used to alter or modify the existing data
structures like tables or indexes are discussed with appropriate examples.
To view the structure or metadata information regarding the created
structures in the database, we use the Describe command whose syntax is
presented
Objectives
After studying this units, you should be able to:
explain various Data Definition Language (DDL) Statements
apply various CREATE syntaxes to create the appropriate structures
apply various ALTER syntaxes to modify the appropriate structures
explain and apply the DROP syntaxes
explain the usage of various Describe syntaxes

Sikkim Manipal University

Page No. 52

RDBMS with MySQL

Unit 3

3.2 CREATE DATABASE Syntax


CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.
An error occurs if the database exists and you did not specify IF NOT
EXISTS.
create_specification options specify database characteristics. Database
characteristics are stored in the db.opt file in the database directory. The
CHARACTER SET clause specifies the default database character set. The
COLLATE clause specifies the default database collation.
A database in MySQL is implemented as a directory containing files that
correspond to tables in the database. Because there are no tables in a
database when it is initially created, the CREATE DATABASE statement
creates only a directory under the MySQL data directory and the db.opt file.
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ...
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name

If you manually create a directory under the data directory (for example, with
mkdir), the server considers it as a database directory and is shown in the
output of SHOW_DATABASES.
Self Assessment Questions
1. ______________ is a synonym for CREATE DATABASE.

3.3 CREATE INDEX Syntax


Indexes are used to find rows with specific column values quickly. Without
an index, MySQL must begin with the first row and then read through the
entire table to find the relevant rows. The larger the table, the more is the
cost. If the table has an index for the columns in question, MySQL can
quickly determine the position to seek to in the middle of the data file without
having to look at all the data. If a table has 1,000 rows, this is at least 100

Sikkim Manipal University

Page No. 53

RDBMS with MySQL

Unit 3

times faster than reading sequentially. If you need to access most of the
rows, it is faster to read sequentially, because this minimizes disk seeks.
Most MySQL indexes (PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT)
are stored in B-trees. Exceptions are that indexes on spatial data types use
R-trees, and that MEMORY tables also support hash indexes.
MySQL uses indexes for these operations:
To find the rows matching a WHERE clause quickly.
To eliminate rows from consideration. If there is a choice between
multiple indexes, MySQL normally uses the index that finds the smallest
number of rows.
To retrieve rows from other tables when performing joins. MySQL can
use indexes on columns more efficiently if they are declared as the
same type and size. In this context, VARCHAR and CHAR are
considered the same if they are declared as the same size. For
example, VARCHAR(10) and CHAR(10) are the same size, but
VARCHAR(10) and CHAR(15) are not.
Comparison of dissimilar columns may prevent use of indexes if values
cannot be compared directly without conversion. Suppose that a
numeric column is compared to a string column. For a given value such
as 1 in the numeric column, it might compare equal to any number of
values in the string column such as '1', ' 1', '00001', or '01.e1'. This rules
out use of any indexes for the string column.
To find the MIN() or MAX() value for a specific indexed column key_col.
This is optimized by a preprocessor that checks whether you are using
WHERE key_part_N = constant on all key parts that occur before
key_col in the index. In this case, MySQL does a single key lookup for
each MIN() or MAX() expression and replaces it with a constant. If all
expressions are replaced with constants, the query returns at once. For
example:

SELECT MIN(key_part2),MAX(key_part2) FROM tbl_name WHERE


key_part1=10;

Sikkim Manipal University

Page No. 54

RDBMS with MySQL

Unit 3

To sort or group a table if the sorting or grouping is done on a leftmost


prefix of a usable key (for example, ORDER BY key_part1, key_part2). If
all key parts are followed by DESC, the key is read in reverse order.
In some cases, a query can be optimized to retrieve values without
consulting the data rows. If a query uses only columns from a table that
are numeric and that form a leftmost prefix for some key, the selected
values may be retrieved from the index tree for greater speed:
SELECT key_part3 FROM tbl_name WHERE key_part1=1

Suppose that you issue the following SELECT statement:


mysql> SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;

If a multiple-column index exists on col1 and col2, the appropriate rows can
be fetched directly. If separate single-column indexes exist on col1 and col2,
the optimizer will attempt to use the Index Merge optimization, or attempt
to find the most restrictive index by deciding which index finds fewer rows
and using that index to fetch the rows.
Index Merge optimization: The Index Merge method is used to retrieve
rows with several range scans and to merge their results into one. The
merge can produce unions, intersections, or unions-of-intersections of its
underlying scans. This access method merges index scans from a single
table; it does not merge scans across multiple tables.
CREATE INDEX is mapped to an ALTER TABLE statement to create
indexes. CREATE INDEX cannot be used to create a PRIMARY KEY; use
ALTER TABLE instead. Normally, you create all indexes on a table at the
time the table itself is created with CREATE TABLE. CREATE INDEX
enables you to add indexes to existing tables. A column list of the form
(col1,col2,...) creates a multiple-column index. Index values are formed by
concatenating the values of the given columns.
Indexes can be created that use only the leading part of column values,
using col_name(length) syntax to specify an index prefix length:
Prefixes can be specified for CHAR, VARCHAR, BINARY, and
VARBINARY columns.
Sikkim Manipal University

Page No. 55

RDBMS with MySQL

Unit 3

BLOB and TEXT columns also can be indexed, but a prefix length must
be given.
Prefix lengths are given in characters for non-binary string types and in
bytes for binary string types. That is, index entries consist of the first
length characters of each column value for CHAR, VARCHAR, and
TEXT columns, and the first length bytes of each column value for
BINARY, VARBINARY, and BLOB columns.
For spatial columns, prefix values cannot be given.
The statement shown here creates an index using the first 10 characters of
the name column:
CREATE INDEX part_of_name ON customer (name(10));

If names in the column usually differ in the first 10 characters, this index
should not be much slower than an index created from the entire name
column. Also, using column prefixes for indexes can make the index file
much smaller, which could save a lot of disk space and might also speed up
INSERT operations.
Prefix lengths are storage engine-dependent (for example, a prefix can be
up to 1000 bytes long for MyISAM tables, 767 bytes for InnoDB tables).
Note that prefix limits are measured in bytes, whereas the prefix length in
CREATE INDEX statements is interpreted as number of characters for nonbinary data types (CHAR, VARCHAR, TEXT). Take this into account when
specifying a prefix length for a column that uses a multi-byte character set.
FULLTEXT indexes are supported only for MyISAM tables and can include
only CHAR, VARCHAR, and TEXT columns. Indexing always happens over
the entire column; column prefix indexing is not supported and any prefix
length is ignored if specified.
In MySQL 5.1:
You can add an index on a column that can have NULL values only if
you are using the MyISAM, InnoDB, or MEMORY storage engine.
You can add an index on a BLOB or TEXT column only if you are using
the MyISAM, or InnoDB storage engine.
An index_col_name specification can end with ASC or DESC. These
keywords are allowed for future extensions for specifying ascending or
Sikkim Manipal University

Page No. 56

RDBMS with MySQL

Unit 3

descending index value storage. Currently, they are parsed but ignored;
index values are always stored in ascending order.
Self Assessment Questions
2. Database characteristics are stored in the ___ file in the database
directory.
3. BLOB and TEXT columns also can be indexed, but a ______ length
must be given.

3.4 CREATE TABLE Syntax


CREATE TABLE creates a table with the given name. You must have the
CREATE privilege for the table.
By default, the table is created in the default database. An error occurs if the
table exists, if there is no default database, or if the database does not exist.
The table name can be specified as db_name.tbl_name to create the table
in a specific database. This works regardless of whether there is a default
database, assuming that the database exists. If you use quoted identifiers,
quote the database and table names separately. For example, write
`mydb`.`mytbl`, not `mydb.mytbl`.
You can use the TEMPORARY keyword when creating a table. A
TEMPORARY table is visible only to the current connection, and is dropped
automatically when the connection is closed. This means that two different
connections can use the same temporary table name without conflicting with
each other or with an existing non-TEMPORARY table of the same name.
(The existing table is hidden until the temporary table is dropped.) To create
temporary tables, you must have the CREATE TEMPORARY TABLES
privilege.
Note: CREATE TABLE does not automatically commit the current active
transaction if you use the TEMPORARY keyword.
The keywords IF NOT EXISTS prevent an error from occurring if the table
exists. However, there is no verification that the existing table has a
structure identical to that indicated by the CREATE TABLE statement.

Sikkim Manipal University

Page No. 57

RDBMS with MySQL

Unit 3

Note: If you use IF NOT EXISTS in a CREATE TABLE ... SELECT


statement, any rows selected by the SELECT part are inserted regardless of
whether the table already exists.
MySQL represents each table by an .frm table format (definition) file in the
database directory. The storage engine for the table might create other files
as well. In the case of MyISAM tables, the storage engine creates data and
index files. Thus, for each MyISAM table tbl_name, there are three disk files:
File
tbl_name.frm
tbl_name.MYD
tbl_name.MYI

Purpose
Table format (definition)
file
Data file
Index file

CREATE TABLE Syntax


CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_option] ...
[partition_options]

OR
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_option] ...
[partition_options]
select_statement

OR
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }

Sikkim Manipal University

Page No. 58

RDBMS with MySQL

Unit 3

create_definition:
col_name column_definition
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
| {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
[index_name] [index_type] (index_col_name,...)
[index_option] ...
| {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name,...) reference_definition
| CHECK (expr)

column_definition:
data_type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string'] [reference_definition]
[COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
[STORAGE {DISK|MEMORY|DEFAULT}]

index_col_name:
col_name [(length)] [ASC | DESC]
index_type:
USING {BTREE | HASH | RTREE}
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name

reference_definition:
REFERENCES tbl_name (index_col_name,...)
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION

Sikkim Manipal University

Page No. 59

RDBMS with MySQL

Unit 3

select_statement:
[IGNORE | REPLACE] [AS] SELECT ... (Some legal select statement)

Storage Engine describes what files each storage engine creates to


represent tables. If a table name contains special characters, the names for
the table files contain encoded versions of those characters
data_type represents the data type in a column definition. spatial_type
represents a spatial data type. The data type syntax shown is representative
only.
Some attributes do not apply to all data types. AUTO_INCREMENT applies
only to integer and floating-point types. DEFAULT does not apply to the
BLOB or TEXT types.
If neither NULL nor NOT NULL is specified, the column is treated as
though NULL had been specified.
An integer or floating-point column can have the additional attribute
AUTO_INCREMENT. When you insert a value of NULL (recommended)
or 0 into an indexed AUTO_INCREMENT column, the column is set to
the next sequence value. Typically this is value+1, where value is the
largest value for the column currently in the table. AUTO_INCREMENT
sequences begin with 1.
To retrieve an AUTO_INCREMENT value after inserting a row, use the
LAST_INSERT_ID() SQL function or the mysql_insert_id() C API function.
If the NO_AUTO_VALUE_ON_ZERO SQL mode is enabled, you can store
0 in AUTO_INCREMENT columns as 0 without generating a new sequence
value.
Note: There can be only one AUTO_INCREMENT column per table, it must
be indexed, and it cannot have a DEFAULT value. An AUTO_INCREMENT
column works properly if it contains only positive values. Inserting a negative
number is regarded as inserting a very large positive number. This is done
to avoid precision problems when numbers wrap over from positive to
negative and also to ensure that you do not accidentally get an
AUTO_INCREMENT column that contains 0.

Sikkim Manipal University

Page No. 60

RDBMS with MySQL

Unit 3

To make MySQL compatible with some ODBC applications, you can find the
AUTO_INCREMENT value for the last inserted row with the following query:
SELECT * FROM tbl_name WHERE auto_col IS NULL

Character data types (CHAR, VARCHAR, TEXT) can include CHARACTER


SET and COLLATE attributes to specify the character set and collation for
the column. CHARSET is a synonym for CHARACTER SET.
CREATE TABLE t (c CHAR(20) CHARACTER SET utf8 COLLATE utf8_bin);

MySQL 5.1 interprets length specifications in character column definitions in


characters.
The DEFAULT clause specifies a default value for a column. With one
exception, the default value must be a constant; it cannot be a function or an
expression. This means, for example, that you cannot set the default for a
date column to be the value of a function such as NOW() or
CURRENT_DATE.
The
exception
is
that
you
can
specify
CURRENT_TIMESTAMP as the default for a TIMESTAMP column.
Note: BLOB and TEXT columns cannot be assigned a default value.
A comment for a column can be specified with the COMMENT option, up
to 255 characters long. The comment is displayed by the SHOW
CREATE TABLE and SHOW FULL COLUMNS statements.
Beginning with MySQL Cluster NDB 6.2.5 and MySQL Cluster NDB
6.3.2, it is also possible to specify a data storage format for individual
columns of NDB tables using COLUMN_FORMAT. Allowable column
formats are FIXED, DYNAMIC, and DEFAULT. FIXED is used to specify
fixed-width storage, DYNAMIC allows the column to be variable-width,
and DEFAULT causes the column to use fixed-width or variable-width
storage as determined by the column's data type (possibly overridden by
a ROW_FORMAT specifier).
For NDB tables, the default value for COLUMN_FORMAT is DEFAULT.
COLUMN_FORMAT currently has no effect on columns of tables using
storage engines other than NDB.
For NDB tables, beginning with MySQL Cluster NDB 6.2.5 and MySQL
Cluster NDB 6.3.2, it is also possible to specify whether the column is
Sikkim Manipal University

Page No. 61

RDBMS with MySQL

Unit 3

stored on disk or in memory by using a STORAGE clause. STORAGE


DISK causes the column to be stored on disk, and STORAGE MEMORY
causes in-memory storage to be used. The CREATE TABLE statement
used must still include a TABLESPACE clause:
mysql> CREATE TABLE t1 (
-> c1 INT STORAGE DISK,
-> c2 INT STORAGE MEMORY
-> ) ENGINE NDB;
ERROR 1005 (HY000): Can't create table 'c.t1' (errno: 140)
mysql> CREATE TABLE t1 (
-> c1 INT STORAGE DISK,
-> c2 INT STORAGE MEMORY
-> ) TABLESPACE ts_1 ENGINE NDB;
Query OK, 0 rows affected (1.06 sec)

For NDB tables, STORAGE DEFAULT is equivalent to STORAGE


MEMORY.
The STORAGE clause has no effect on tables using storage engines other
than NDB.
KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY
can also be specified as just KEY when given in a column definition.
This was implemented for compatibility with other database systems.
A UNIQUE index creates a constraint such that all values in the index
must be distinct. An error occurs if you try to add a new row with a key
value that matches an existing row. For all engines, a UNIQUE index
allows multiple NULL values for columns that can contain NULL.
A PRIMARY KEY is a unique index where all key columns must be
defined as NOT NULL. If they are not explicitly declared as NOT NULL,
MySQL declares them so implicitly (and silently). A table can have only
one PRIMARY KEY. If you do not have a PRIMARY KEY and an
application asks for the PRIMARY KEY in your tables, MySQL returns
the first UNIQUE index that has no NULL columns as the PRIMARY
KEY.
In the created table, a PRIMARY KEY is placed first, followed by all
UNIQUE indexes, and then the non-unique indexes. This helps the
Sikkim Manipal University

Page No. 62

RDBMS with MySQL

Unit 3

MySQL optimizer to prioritize which index to use and also more quickly
to detect duplicated UNIQUE keys.
A PRIMARY KEY can be a multiple-column index. However, you cannot
create a multiple-column index using the PRIMARY KEY key attribute in
a column specification. Doing so only marks that single column as
primary. You must use a separate PRIMARY KEY(index_col_name, ...)
clause.
If a PRIMARY KEY or UNIQUE index consists of only one column that
has an integer type, you can also refer to the column as _rowid in
SELECT statements.
In MySQL, the name of a PRIMARY KEY is PRIMARY. For other
indexes, if you do not assign a name, the index is assigned the same
name as the first indexed column, with an optional suffix (_2, _3, ...) to
make it unique. You can see index names for a table using SHOW
INDEX FROM tbl_name.
Some storage engines allow you to specify an index type when creating
an index. The syntax for the index_type specifier is USING type_name.
Example:
CREATE TABLE lookup
(id INT, INDEX USING BTREE (id))
ENGINE = MEMORY;

In MySQL 5.1, only the MyISAM, InnoDB, and MEMORY storage


engines support indexes on columns that can have NULL values. In
other cases, you must declare indexed columns as NOT NULL or an
error results.
For CHAR, VARCHAR, BINARY, and VARBINARY columns, indexes
can be created that use only the leading part of column values, using
col_name(length) syntax to specify an index prefix length. BLOB and
TEXT columns also can be indexed, but a prefix length must be given.
Prefix lengths are given in characters for non-binary string types and in
bytes for binary string types. That is, index entries consist of the first
length characters of each column value for CHAR, VARCHAR, and
TEXT columns, and the first length bytes of each column value for
Sikkim Manipal University

Page No. 63

RDBMS with MySQL

Unit 3

BINARY, VARBINARY, and BLOB columns. Indexing only a prefix of


column values like this can make the index file much smaller. Only the
MyISAM and InnoDB storage engines support indexing on BLOB and
TEXT columns.
An index_col_name specification can end with ASC or DESC. These
keywords are allowed for future extensions for specifying ascending or
descending index value storage. Currently, they are parsed but ignored;
index values are always stored in ascending order.
When you use ORDER BY or GROUP BY on a TEXT or BLOB column
in a SELECT, the server sorts values using only the initial number of
bytes indicated by the max_sort_length system variable.
InnoDB tables support checking of foreign key constraints. Note that the
FOREIGN KEY syntax in InnoDB is more restrictive than the syntax
presented for the CREATE TABLE statement at the beginning of this
section: The columns of the referenced table must always be explicitly
named. InnoDB supports both ON DELETE and ON UPDATE actions on
foreign keys.
There is a hard limit of 4096 columns per table, but the effective maximum
may be less for a given table. The exact limit depends on several interacting
factors, listed in the following discussion.
Every table has a maximum row size of 65,535 bytes. This maximum
applies to all storage engines, but a given engine might have additional
constraints that result in a lower effective maximum row size.
The maximum row size constrains the number of columns because the
total width of all columns cannot exceed this size. For example, utf8
characters require up to three bytes per character, so for a CHAR(255)
CHARACTER SET utf8 column, the server must allocate 255 3 = 765
bytes per value. Consequently, a table cannot contain more than 65,535
/ 765 = 85 such columns.
Storage for variable-length columns includes length bytes, which are
assessed against the row size. For example, a VARCHAR(255)
CHARACTER SET utf8 column takes two bytes to store the length of the
value, so each value can take up to 767 bytes.

Sikkim Manipal University

Page No. 64

RDBMS with MySQL

Unit 3

BLOB and TEXT columns count from one to four plus eight bytes each
toward the row-size limit because their contents are stored separately.
Declaring columns NULL can reduce the maximum number of columns
allowed. NULL columns require additional space in the row to record
whether or not their values are NULL.
For MyISAM tables, each NULL column takes one bit extra, rounded up
to the nearest byte.
The maximum row length in bytes can be calculated as follows:
Row length = 1
+ (sum of column lengths)
+ (number of NULL columns + delete_flag + 7)/8
+ (number of variable-length columns)

delete_flag is 1 for tables with static row format. Static tables use a bit in the
row record for a flag that indicates whether the row has been deleted.
delete_flag is 0 for dynamic tables because the flag is stored in the dynamic
row header.
These calculations do not apply for InnoDB tables, for which storage size is
no different for NULL columns than for NOT NULL columns.
Each table has an .frm file that contains the table definition. The .frm file
size limit is fixed at 64KB. If a table definition reaches this size, no more
columns can be added. The expression that checks information to be
stored in the .frm file against the limit looks like this:
if (info_length+(ulong) create_fields.elements*FCOMP+288+
n_length+int_length+com_length > 65535L || int_count > 255)

The relevant factors in this expression are:


o

info_length is space needed for screens. This is related to MySQL's


Unireg heritage.

create_fields.elements is the number of columns.

FCOMP is 17.

n_length is the total length of all column names, including one byte
per name as a separator.

int_length is related to the list of values for SET and ENUM columns.

Sikkim Manipal University

Page No. 65

RDBMS with MySQL

Unit 3

com_length is the total length of column and table comments.

Thus, using long column names can reduce the maximum number of
columns, as can the inclusion of ENUM or SET columns, or use of
column or table comments.
The TABLESPACE and STORAGE table options were both introduced in
MySQL 5.1.6. In MySQL 5.1, they are employed only with NDBCLUSTER
tables. The tablespace named tablespace_name must already have been
created using CREATE TABLESPACE. STORAGE determines the type of
storage used (disk or memory), and can be one of DISK, MEMORY, or
DEFAULT.
TABLESPACE ... STORAGE DISK assigns a table to a MySQL Cluster Disk
Data tablespace.
Important Note: A STORAGE clause cannot be used in a CREATE TABLE
statement without a TABLESPACE clause.
The ENGINE table option specifies the storage engine for the table.
The ENGINE table option takes the storage engine names shown in the
table 3.1.
Table 3.1: Storage Engines and Descriptions
Storage Engine
ARCHIVE
CSV
EXAMPLE
FEDERATED
HEAP
ISAM (OBSOLETE)

InnoDB
MEMORY

Description
The archiving storage engine
Tables that store rows in comma-separated values
format.
An example engine.
Storage engine that accesses remote tables.
This is a synonym for MEMORY.
Not available in MySQL 5.1. If you are upgrading to
MySQL 5.1 from a previous version, you should convert
any existing ISAM tables to MyISAM before performing
the upgrade.
Transaction-safe tables with row locking and foreign
keys.
The data for this storage engine is stored only in
memory.

Sikkim Manipal University

Page No. 66

RDBMS with MySQL

Unit 3

MERGE

A collection of MyISAM tables used as one table. Also


known as MRG_MyISAM.

MyISAM

The binary portable storage engine that is the default


storage engine used by MySQL.

NDBCLUSTER

Clustered, fault-tolerant, memory-based tables. Also


known as NDB.

If a storage engine is specified that is not available, MySQL uses the default
engine instead. Normally, this is MyISAM. For example, if a table definition
includes the ENGINE=INNODB option but the MySQL server does not
support INNODB tables, the table is created as a MyISAM table. This makes
it possible to have a replication setup where you have transactional tables
on the master but tables created on the slave are non-transactional (to get
more speed). In MySQL 5.1, a warning occurs if the storage engine
specification is not honored.
Engine substitution can be controlled by the setting of the
NO_ENGINE_SUBSTITUTION SQL mode.
The other table options are used to optimize the behavior of the table. In
most cases, you do not have to specify any of them. These options apply to
all storage engines unless otherwise indicated. Options that do not apply to
a given storage engine may be accepted and remembered as part of the
table definition. Such options then apply if you later use ALTER TABLE to
convert the table to use a different storage engine.
AUTO_INCREMENT: The initial AUTO_INCREMENT value for the
table. In MySQL 5.1, this works for MyISAM, MEMORY, and InnoDB
tables. It also works for ARCHIVE tables as of MySQL 5.1.6. To set the
first auto-increment value for engines that do not support the
AUTO_INCREMENT table option, insert a dummy row with a value one
less than the desired value after creating the table, and then delete the
dummy row.
For engines that support the AUTO_INCREMENT table option in
CREATE TABLE statements, you can also use ALTER TABLE tbl_name
AUTO_INCREMENT = N to reset the AUTO_INCREMENT value. The
Sikkim Manipal University

Page No. 67

RDBMS with MySQL

Unit 3

value cannot be set lower than the maximum value currently in the
column.
AVG_ROW_LENGTH: An approximation of the average row length for
your table. You need to set this only for large tables with variable-size
rows.
When you create a MyISAM table, MySQL uses the product of the
MAX_ROWS and AVG_ROW_LENGTH options to decide how big the
resulting table is. If you don't specify either option, the maximum size for
MyISAM data and index files is 256TB by default. (If your operating
system does not support files that large, table sizes are constrained by
the file size limit.) If you want to keep down the pointer sizes to make the
index smaller and faster and you don't really need big files, you can
decrease
the
default
pointer
size
by
setting
the
myisam_data_pointer_size system variable. If you want all your tables to
be able to grow above the default limit and are willing to have your
tables slightly slower and larger than necessary, you can increase the
default pointer size by setting this variable. Setting the value to 7 allows
table sizes up to 65,536TB.
[DEFAULT] CHARACTER SET: Specify a default character set for the
table. CHARSET is a synonym for CHARACTER SET. If the character
set name is DEFAULT, the database character set is used.
CHECKSUM: Set this to 1 if you want MySQL to maintain a live
checksum for all rows (that is, a checksum that MySQL updates
automatically as the table changes). This makes the table a little slower
to update, but also makes it easier to find corrupted tables. The
CHECKSUM TABLE statement reports the checksum. (MyISAM only.)
[DEFAULT] COLLATE: Specify a default collation for the table.
COMMENT: A comment for the table, up to 60 characters long.
CONNECTION: The connection string for a FEDERATED table.
DATA DIRECTORY, INDEX DIRECTORY: By using DATA
DIRECTORY='directory' or INDEX DIRECTORY='directory' you can
specify where the MyISAM storage engine should put a table's data file
and index file. The directory must be the full pathname to the directory,
not a relative path.
Sikkim Manipal University

Page No. 68

RDBMS with MySQL

Unit 3

If a MyISAM table is created with no DATA DIRECTORY option, the .MYD


file is created in the database directory. By default, if MyISAM finds an
existing .MYD file in this case, it overwrites it. The same applies to .MYI files
for tables created with no INDEX DIRECTORY option. As of MySQL 5.1.23,
to suppress this behavior, start the server with the --keep_files_on_create
option, in which case MyISAM will not overwrite existing files and returns an
error instead.
If a MyISAM table is created with a DATA DIRECTORY or INDEX
DIRECTORY option and an existing .MYD or .MYI file is found, MyISAM
always returns an error. It will not overwrite a file in the specified directory.
DELAY_KEY_WRITE: Set this to 1 if you want to delay key updates for
the table until the table is closed.
INSERT_METHOD: If you want to insert data into a MERGE table, you
must specify with INSERT_METHOD the table into which the row should
be inserted. INSERT_METHOD is an option useful for MERGE tables
only. Use a value of FIRST or LAST to have inserts go to the first or last
table, or a value of NO to prevent inserts.
KEY_BLOCK_SIZE: This option provides a hint to the storage engine
about the size in bytes to use for index key blocks. The engine is
allowed to change the value if necessary. A value of 0 indicates that the
default value should be used. Individual index definitions can specify a
KEY_BLOCK_SIZE value of their own to override the table value.
KEY_BLOCK_SIZE was added in MySQL 5.1.10.
MAX_ROWS: The maximum number of rows you plan to store in the
table. This is not a hard limit, but rather a hint to the storage engine that
the table must be able to store at least this many rows.
MIN_ROWS: The minimum number of rows you plan to store in the
table.
PACK_KEYS: PACK_KEYS takes effect only with MyISAM tables. Set
this option to 1 if you want to have smaller indexes. This usually makes
updates slower and reads faster. Setting the option to 0 disables all
packing of keys. Setting it to DEFAULT tells the storage engine to pack
only long CHAR, VARCHAR, BINARY, or VARBINARY columns.
Sikkim Manipal University

Page No. 69

RDBMS with MySQL

Unit 3

If you do not use PACK_KEYS, the default is to pack strings, but not
numbers. If you use PACK_KEYS=1, numbers are packed as well.
When packing binary number keys, MySQL uses prefix compression:
o Every key needs one extra byte to indicate how many bytes of the
previous key are the same for the next key.
o The pointer to the row is stored in high-byte-first order directly after
the key, to improve compression.
This means that if you have many equal keys on two consecutive rows, all
following same keys usually only take two bytes (including the pointer to
the row). Compare this to the ordinary case where the following keys takes
storage_size_for_key + pointer_size (where the pointer size is usually 4).
Conversely, you get a significant benefit from prefix compression only if you
have many numbers that are the same. If all keys are totally different, you
use one byte more per key, if the key is not a key that can have NULL
values. (In this case, the packed key length is stored in the same byte that is
used to mark if a key is NULL.)
PASSWORD: This option is unused. If you have a need to scramble
your .frm files and make them unusable to any other MySQL server,
please contact our sales department.
RAID_TYPE: RAID support has been removed as of MySQL 5.0.
ROW_FORMAT: Defines how the rows should be stored. For MyISAM
tables, the option value can be FIXED or DYNAMIC for static or variablelength row format. myisampack sets the type to COMPRESSED.
Note: When executing a CREATE TABLE statement, if you specify a row
format which is not supported by the storage engine that is used for the
table, the table is created using that storage engine's default row format.
The information reported in this column in response to SHOW TABLE
STATUS is the actual row format used. This may differ from the value in the
Create_options column because the original CREATE TABLE definition is
retained during creation.
UNION: UNION is used when you want to access a collection of
identical MyISAM tables as one. This works only with MERGE tables.

Sikkim Manipal University

Page No. 70

RDBMS with MySQL

Unit 3

You must have SELECT, UPDATE, and DELETE privileges for the
tables you map to a MERGE table.
partition_options can be used to control partitioning of the table created
with CREATE TABLE.
If used, a partition_options clause begins with PARTITION BY. This
clause contains the function that is used to determine the partition; the
function returns an integer value ranging from 1 to num, where num is
the number of partitions. (The maximum number of user-defined
partitions which a table may contain is 1024; the number of subpartitions
discussed later in this section is included in this maximum.) The
choices that are available for this function in MySQL 5.1 are shown in
the following list:
o HASH(expr): Hashes one or more columns to create a key for
placing and locating rows. expr is an expression using one or more
table columns. This can be any legal MySQL expression (including
MySQL functions) that yields a single integer value.
o KEY(column_list): This is similar to HASH, except that MySQL
supplies the hashing function so as to guarantee an even data
distribution. The column_list argument is simply a list of table
columns.
o For tables that are partitioned by key, you can employ linear
partitioning by using the LINEAR keyword. This has the same effect
as with tables that are partitioned by HASH. That is, the partition
number is found using the & operator rather than the modulus. You
may not use either VALUES LESS THAN or VALUES IN clauses
with PARTITION BY KEY.
o RANGE: In this case, expr shows a range of values using a set
of VALUES LESS THAN operators. When using range
partitioning, you must define at least one partition using VALUES
LESS THAN. You cannot use VALUES IN with range partitioning.

VALUES LESS THAN can be used with either a literal value or


an expression that evaluates to a single value.
LIST(expr): This is useful when assigning partitions based on a
table column with a restricted set of possible values, such as a
state or country code. In such a case, all rows pertaining to a

Sikkim Manipal University

Page No. 71

RDBMS with MySQL

Unit 3

certain state or country can be assigned to a single partition, or a


partition can be reserved for a certain set of states or countries. It
is similar to RANGE, except that only VALUES IN may be used
to specify allowable values for each partition. When using list
partitioning, you must define at least one partition using VALUES
IN. You cannot use VALUES LESS THAN with PARTITION BY
LIST.
The number of partitions may optionally be specified with a
PARTITIONS num clause, where num is the number of
partitions. If both this clause and any PARTITION clauses are
used, num must be equal to the total number of any partitions
that are declared using PARTITION clauses.
A partition may optionally be divided into a number of
subpartitions. This can be indicated by using the optional
SUBPARTITION BY clause. Subpartitioning may be done by
HASH or KEY. Either of these may be LINEAR. These work in
the same way as previously described for the equivalent
partitioning types. (It is not possible to subpartition by LIST or
RANGE.)

The number of subpartitions can be indicated using the


SUBPARTITIONS keyword followed by an integer value.
o MySQL 5.1.12 introduces rigorous checking of the value used in
a PARTITIONS or SUBPARTITIONS clause. Beginning with this
version, this value must adhere to the following rules:
The value must be a positive, non-zero integer.
No leading zeroes are permitted.
The value must be an integer literal, and cannot not be an
expression.
Each partition may be individually defined using a partition_definition
clause. The individual parts making up this clause are as follows:
PARTITION partition_name: This specifies a logical name for the
partition.
A VALUES clause: For range partitioning, each partition must
include a VALUES LESS THAN clause; for list partitioning, you must
specify a VALUES IN clause for each partition. This is used to
determine which rows are to be stored in this partition.
Sikkim Manipal University

Page No. 72

RDBMS with MySQL

Unit 3

An optional COMMENT clause may be used to specify a string that


describes the partition.
DATA DIRECTORY and INDEX DIRECTORY may be used to indicate
the directory where, respectively, the data and indexes for this partition
are to be stored. Both the data_dir and the index_dir must be absolute
system pathnames.
DATA DIRECTORY and INDEX DIRECTORY behave in the same way
as in the CREATE TABLE statement's table_option clause as used for
MyISAM tables.
One data directory and one index directory may be specified per
partition. If left unspecified, the data and indexes are stored by default in
the table's database directory.
On Windows, the DATA DIRECTORY and INDEX DIRECTORY options
are not supported for individual partitions or subpartitions. Beginning
with MySQL 5.1.24, these options are ignored on Windows, except that
a warning is generated. (Bug#30459)
MAX_ROWS and MIN_ROWS may be used to specify, respectively, the
maximum and minimum number of rows to be stored in the partition. The
values for max_number_of_rows and min_number_of_rows must be
positive integers. As with the table-level options with the same names,
these act only as suggestions to the server and are not hard limits.
The optional TABLESPACE clause may be used to designate a
tablespace for the partition. Used for MySQL Cluster only.
The partitioning handler accepts a [STORAGE] ENGINE option for both
PARTITION and SUBPARTITION. Currently, the only way in which this
can be used is to set all partitions or all subpartitions to the same
storage engine, and an attempt to set different storage engines for
partitions or subpartitions in the same table will give rise to the error
ERROR 1469 (HY000): The mix of handlers in the partitions is not
allowed in this version of MySQL. We expect to lift this restriction on
partitioning in a future MySQL release.
The NODEGROUP option can be used to make this partition act as part
of the node group identified by node_group_id. This option is applicable
only to MySQL Cluster.

Sikkim Manipal University

Page No. 73

RDBMS with MySQL

Unit 3

The partition definition may optionally contain one or more


subpartition_definition clauses. Each of these consists at a minimum of
the SUBPARTITION name, where name is an identifier for the
subpartition. Except for the replacement of the PARTITION keyword with
SUBPARTITION, the syntax for a subpartition definition is identical to
that for a partition definition.
Subpartitioning must be done by HASH or KEY, and can be done only
on RANGE or LIST partitions.
Partitions can be modified, merged, added to tables, and dropped from
tables.
You can create one table from another by adding a SELECT statement
at the end of the CREATE TABLE statement:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;

MySQL creates new columns for all elements in the SELECT. For
example:
mysql>

CREATE

TABLE

test

(a

INT

NOT

NULL

AUTO_INCREMENT,
-> PRIMARY KEY (a), KEY(b))
-> ENGINE=MyISAM SELECT b,c FROM test2;

This creates a MyISAM table with three columns, a, b, and c. Notice


that the columns from the SELECT statement are appended to the
right side of the table, not overlapped onto it.
Consider the following example:
mysql> SELECT * FROM foo;
+---+
|n|
+---+
|1|
+---+
mysql> CREATE TABLE bar (m INT) SELECT n FROM foo;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0

Sikkim Manipal University

Page No. 74

RDBMS with MySQL

Unit 3

mysql> SELECT * FROM bar;


+------+---+
|m |n|
+------+---+
| NULL | 1 |
+------+---+
1 row in set (0.00 sec)

For each row in table foo, a row is inserted in bar with the values
from foo and default values for the new columns.
In a table resulting from CREATE TABLE ... SELECT, columns
named only in the CREATE TABLE part come first. Columns named
in both parts or only in the SELECT part come after that. The data
type of SELECT columns can be overridden by also specifying the
column in the CREATE TABLE part.
If any errors occur while copying the data to the table, it is
automatically dropped and not created.
CREATE TABLE ... SELECT does not automatically create any
indexes for you. This is done intentionally to make the statement as
flexible as possible. If you want to have indexes in the created table,
you should specify these before the SELECT statement:
mysql> CREATE TABLE bar (UNIQUE (n)) SELECT n FROM foo;

3.4.1 Silent Column Specification Changes


In some cases, MySQL silently changes column specifications from those
given in a CREATE TABLE or ALTER TABLE statement. These might be
changes to a data type, to attributes associated with a data type, or to an
index specification.
TIMESTAMP display sizes are discarded.
Also note that TIMESTAMP columns are NOT NULL by default.
Columns that are part of a PRIMARY KEY are made NOT NULL
even if not declared that way.
Trailing spaces are automatically deleted from ENUM and SET
member values when the table is created.
MySQL maps certain data types used by other SQL database
vendors to MySQL types.
Sikkim Manipal University

Page No. 75

RDBMS with MySQL

Unit 3

If you include a USING clause to specify an index type that is not


legal for a given storage engine, but there is another index type
available that the engine can use without affecting query results, the
engine uses the available type.
If strict SQL mode is not enabled, a VARCHAR column with a length
specification greater than 65535 is converted to TEXT, and a
VARBINARY column with a length specification greater than 65535
is converted to BLOB. Otherwise, an error occurs in either of these
cases.
Specifying the CHARACTER SET binary attribute for a character
data type causes the column to be created as the corresponding
binary data type: CHAR becomes BINARY, VARCHAR becomes
VARBINARY, and TEXT becomes BLOB. For the ENUM and SET
data types, this does not occur; they are created as declared.
Suppose that you specify a table using this definition:
CREATE TABLE t
(
c1 VARCHAR(10) CHARACTER SET binary,
c2 TEXT CHARACTER SET binary,
c3 ENUM('a','b','c') CHARACTER SET binary
);

The resulting table has this definition:


CREATE TABLE t
(
c1 VARBINARY(10),
c2 BLOB,
c3 ENUM('a','b','c') CHARACTER SET binary
);

Self Assessment Questions


4. A _______ table is visible only to the current connection, and is dropped
automatically when the connection is closed.
5. _______ does not apply to the BLOB or TEXT types.

Sikkim Manipal University

Page No. 76

RDBMS with MySQL

Unit 3

3.5 ALTER DATABASE Syntax


ALTER DATABASE enables you to change the overall characteristics of a
database. These characteristics are stored in the db.opt file in the database
directory. To use ALTER DATABASE, you need the ALTER privilege on the
database. ALTER SCHEMA is a synonym for ALTER DATABASE.
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name

The CHARACTER SET clause changes the default database character set.
The COLLATE clause changes the default database collation.
You can see what character sets and collations are available using,
respectively, the SHOW CHARACTER SET and SHOW COLLATION
statements.
The database name can be omitted from the first syntax, in which case the
statement applies to the default database.
The syntax that includes the UPGRADE DATA DIRECTORY NAME clause
was added in MySQL 5.1.23. It updates the name of the directory
associated with the database to use the encoding implemented in MySQL
5.1 for mapping database names to database directory names. This clause
is for use under these conditions:
It is intended when upgrading MySQL to 5.1 or later from older
versions.
It is intended to update a database directory name to the current
encoding format if the name contains special characters that need
encoding.
The statement is used by mysqlcheck (as invoked by
mysql_upgrade).
For example,if a database in MySQL 5.0 has a name of a-b-c, the name
contains instance of the - character. In 5.0, the database directory is also
Sikkim Manipal University

Page No. 77

RDBMS with MySQL

Unit 3

named a-b-c, which is not necessarily safe for all filesystems. In MySQL 5.1
and up, the same database name is encoded as a@002db@002dc to
produce a filesystem-neutral directory name.

3.6 ALTER TABLE Syntax


You can later an existing table with the ALTER TABLE command, which
allows you to add, remove, or modify table fields or indices without the need
to re-create the entire table. The syntax is given below:
ALTER TABLE table-name (action field-definition,
action field definition, );

The action component here can be any of the keywords ADD, DROP,
ALTER, or CHANGE, and is followed by a field definition similar to that used
by the CREATE TABLE command. This definition consists of the name of
the field to be modified and (depending on the operation) a field definition
consisting of a new field name, type, and constraints.
Given below is an example which first creates a table and then adds a new
column to it.

Sikkim Manipal University

Page No. 78

RDBMS with MySQL

Unit 3

The following example adds a primary key column to the existing members
table:

Sikkim Manipal University

Page No. 79

RDBMS with MySQL

Unit 3

The following example changes the name and type of an existing field:

The following example shows a query for deleting a field or a key.

You can use the ALTER TABLE command to rename a table as shown
below:

Sikkim Manipal University

Page No. 80

RDBMS with MySQL

Unit 3

You can also use the equivalent RENAME TABLE command as shown
below:

You can set (or remove) a default value for a field with the SET DEFAULT
and DROP DEFAULT clauses, and you can control the position of fields with
the FIRST and AFTER clauses to the ALTER TABLE command.

Sikkim Manipal University

Page No. 81

RDBMS with MySQL

Unit 3

Adding an AUTO_INCREMENT field to a table causes all existing records in


that table to be automatically numbered. For example you have a table
named movies as shown below:

Now you decide to add an AUTO_INCREMENT primary key to each field as


follows:

When adding a UNIQUE key to a table that contains duplicate values on


that key, you can control how MySQL handles the situation (either delete all
duplicate records or abort the alteration) by including or omitting the optional
IGNORE clause. Consider the following table, which contains two records
with the same value in the name field:
Sikkim Manipal University

Page No. 82

RDBMS with MySQL

Unit 3

Now, if you were to set the name field to be UNIQUE, MySQL would return
an error about duplicate values in that field.

However, if you specify the IGNORE clause to the ALTER TABLE


command, then MySQL removes all records with duplicate values on that
key from the table, retaining only the first instance, as the shown in the
following illustration:

Sikkim Manipal University

Page No. 83

RDBMS with MySQL

Unit 3

Self Assessment Questions


6. For all engines, a _____ index allows multiple NULL values for columns
that can contain NULL.
7. Each table has an ____ file that contains the table definition.

3.7 DROP DATABASE Syntax


DROP DATABASE drops all tables in the database and deletes the
database. Be very careful with this statement! To use DROP DATABASE,
you need the DROP privilege on the database. DROP SCHEMA is a
synonym for DROP DATABASE.
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name

Important Note: When a database is dropped, user privileges on the


database are not automatically dropped.
IF EXISTS is used to prevent an error from occurring if the database
does not exist.
If you use DROP DATABASE on a symbolically linked database,
both the link and the original database are deleted.
DROP DATABASE returns the number of tables that were removed.
This corresponds to the number of .frm files removed.
The DROP DATABASE statement removes from the given database
directory those files and directories that MySQL itself may create
during normal operation:
Sikkim Manipal University

Page No. 84

RDBMS with MySQL

All files with these extensions:


.BAK .DAT
.HSH
.MYD .MYI
.TRG
.db
.frm
.ibd
.par

Unit 3

.MRG
.TRN
.ndb

The db.opt file, if it exists.


If other files or directories remain in the database directory after MySQL
removes those just listed, the database directory cannot be removed. In this
case, you must remove any remaining files or directories manually and
issue the DROP DATABASE statement again.
You can also drop databases with mysqladmin.

3.8 DROP INDEX Syntax


If you find you no longer have any need of an index, you can nuke it with the
DROP index command.
DROP INDEX drops the index named index_name from the table tbl_name.
This statement is mapped to an ALTER TABLE statement to drop the index.
Syntax:
DROP INDEX index-name ON table-name

Example: The following command deletes the username index created on


uname column.
mysql> DROP INDEX username on sysusers;

Beginning with MySQL 5.1.7, indexes on variable-width columns are


dropped online; that is, dropping the indexes does not require any copying
or locking of the table. This is done automatically by the server whenever it
determines that it is possible to do so; you do not have to use any special
SQL syntax or server options to cause it to happen.

3.9 DROP TABLE Syntax


DROP TABLE removes one or more tables. You must have the DROP
privilege for each table. All table data and the table definition are removed,
Sikkim Manipal University

Page No. 85

RDBMS with MySQL

Unit 3

so be careful with this statement! If any of the tables named in the argument
list do not exist, MySQL returns an error indicating by name which nonexisting tables it was unable to drop, but it also drops all of the tables in the
list that do exist.
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]

Important Note: When a table is dropped, user privileges on the table are
not automatically dropped.
Note that for a partitioned table, DROP TABLE permanently removes the
table definition, all of its partitions, and all of the data which was stored in
those partitions. It also removes the partitioning definition (.par) file
associated with the dropped table.
Use IF EXISTS to prevent an error from occurring for tables that do not
exist. A NOTE is generated for each non-existent table when using IF
EXISTS.
RESTRICT and CASCADE are allowed to make porting easier. In MySQL
5.1, they do nothing.
Note: DROP TABLE automatically commits the current active transaction,
unless you use the TEMPORARY keyword.
The TEMPORARY keyword has the following effects:
The statement drops only TEMPORARY tables.
The statement does not end an ongoing transaction.
No access rights are checked. (A TEMPORARY table is visible only to
the client that created it, so no check is necessary.)
Using TEMPORARY is a good way to ensure that you do not accidentally
drop a non-TEMPORARY table.

3.10 DESCRIBE syntax


You can obtain the information on the structure of a table its fields, field
types, keys, and defaults with the DESCRIBE command.
Sikkim Manipal University

Page No. 86

RDBMS with MySQL

Unit 3

DESCRIBE provides information about the columns in a table. It is a


shortcut for SHOW COLUMNS FROM. These statements also display
information for views.
col_name can be a column name, or a string containing the SQL % and _
wildcard characters to obtain output only for the columns with names
matching the string. There is no need to enclose the string within quotes
unless it contains spaces or other special characters.
The DESCRIBE statement is provided for compatibility with Oracle.
Syntax:
{DESCRIBE | DESC} tbl_name [col_name | wild]

3.11 Summary
This unit covers the following topics with respect to data definition language
statements in MySQL:
1. CREATE DATABASE: This topic covers the starting point of creating
the databases before any data objects are built into the databases. The
user must first create a database and build objects like tables, views,
stored procedures and so on inside the database. It gives the syntax
associated with creation of these databases within the MySQL server.
2. CREATE INDEX: The importance and syntaxes used in the creation of
indexes is mentioned in this topic.
Sikkim Manipal University

Page No. 87

RDBMS with MySQL

Unit 3

3. CREATE TABLE: Tables are the basic data structures used to store
data. The syntaxes and variations of creating the tables are discussed
here.
4. ALTER DATABASE: Database modifications and the respective
syntaxes are described along with suitable examples are discussed.
5. ALTER TABLE: Modifications to the existing tables and the
corresponding syntaxes are described with suitable examples are
discussed here.
6. DROP DATABASE: Databases can be dropped as and when required.
The details of this is discussed.
7. DROP INDEX: Indexes can be dropped depending on the requirement
and size of the database objects.
8. DROP TABLE: Tables Indexes can be dropped depending on the
requirement of the user or application.
9. DESCRIBE: Anyone who needs to know the structure and data types of
the database tables can use this command for this purpose.

3.12 Terminal Questions


1. Write about the operations supported by indexes in MySQL
2. Write and explain the ALTER DATABASE Syntax
3. Write the SQL statements to perform the following actions using ALTER
TABLE command:
a. Create a table and then add a new column to it.
b. Add a primary key column to an existing table.
c. Change the name and type of an existing field.

3.13 Answers
Self Assessment Questions
1. CREATE SCHEMA
2. db.opt
3. prefix
4. TEMPORARY
5. DEFAULT
6. UNIQUE
7. .frm

Sikkim Manipal University

Page No. 88

RDBMS with MySQL

Unit 3

Terminal Questions
1.
To find the rows matching a WHERE clause quickly.
To eliminate rows from consideration.
To retrieve rows from other tables when performing joins.
To find the MIN() or MAX() value for a specific indexed column
key_col.
(Refer Section 3.3)
2.
ALTER DATABASE enables you to change the overall characteristics of a
database. These characteristics are stored in the db.opt file in the database
directory. To use ALTER DATABASE, you need the ALTER privilege on the
database. ALTER SCHEMA is a synonym for ALTER DATABASE.
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name

The CHARACTER SET clause changes the default database character set.
The COLLATE clause changes the default database collation.
You can see what character sets and collations are available using,
respectively, the SHOW CHARACTER SET and SHOW COLLATION
statements.
The database name can be omitted from the first syntax, in which case the
statement applies to the default database.
(Refer Section 3.5)
3.
a. CREATE TABLE members(mid INT(3), mname CHAR(8), mpass
VARCHAR(25));
ALTER TABLE members ADD email VARCHAR(255) NOT NULL;
b. ALTER TABLE members ADD PRIMARY KEY(email);
c. ALTER TABLE members CHANGE mid id INT(8) AUTO_INCREMENT
UNIQUE;
(Refer Section 3.6)
Sikkim Manipal University

Page No. 89

RDBMS with MySQL

Unit 4

Unit 4

Data Manipulation Language (DML)

Structure
4.1 Introduction
Objectives
4.2 Loading Data into a Table
INSERT
INSERT ... SELECT
INSERT DELAYED
LOAD DATA INFILE
4.3 Retrieving Information from a Table
Retrieving Specific Rows and Columns
Sorting Query Results
Grouping Query Results
4.4 Summary
4.5 Terminal Questions
4.6 Answers

4.1 Introduction
The Data Manipulation (DML) statements are used to manipulate the data
from the underlying data structures like tables, views, functions and so on.
They follow the standard SQL norms. There are many Data Manipulation
statements like Insert, Delete Load, and Update pertaining to MySQL.
These DML statements are used commonly for the operations mentioned
above. In this unit we are going to have the syntaxes and the corresponding
examples discussed in detail. This unit takes a walkthrough of all the DML
statements.
Objectives
After studying this unit, you should be able to:
explain the importance of DML (Data Manipulation Language)
Statements
explain various methods of loading data into tables in MySQL
describe various methods used to select the data
describe the various ways of grouping and sorting of the output data

Sikkim Manipal University

Page No. 90

RDBMS with MySQL

Unit 4

4.2 Loading Data into a Table


MySQL offers a number of different commands, each designed for a specific
purpose when it comes to inserting, updating, or otherwise changing the
contents of a table.
4.2.1 INSERT
Once a database and its tables have been created, the next step is to enter
data into them. This is accomplished by means of the INSERT command,
whose basic syntax is given below:
INSERT INTO table-name (field-name, field-name, )
VALUES (field-value, field-value, )

Example: Let us create a simple table called addressbook as shown


below:

Now we will use the INSERT statement to put some data into the table.

Sikkim Manipal University

Page No. 91

RDBMS with MySQL

Unit 4

The table should now contain a single record for Rob Rabbit. To verify this
you can issue a simple select statement on the table as follows:

You can also use an abbreviated form of the INSERT statement, in which
the field list is left unspecified. The following example, an equivalent of the
previous INSERT statement is given below:
mysql> INSERT INTO addressbook VALUES(Rob, Rabbit, 674 1536,
382 8364, rob@some.domain);

Note: When using this shorter format, the order in which values are inserted
must correspond to the sequence of fields in the table, which can be easily
determined by issuing the DESCRIBE command on the respective table.
We can also INSERT the results of calculations and function calls as below:

Sikkim Manipal University

Page No. 92

RDBMS with MySQL

Unit 4

Values containing quotation marks should be escaped by a preceding


backslash (\) as shown in the example below:

Sikkim Manipal University

Page No. 93

RDBMS with MySQL

Unit 4

Note:
1. When inserting string values (and some date values) into a table, you
must always surround them with quotation marks or MySQL will treat
them as field names.
2. Numeric values need not be quoted.
MySQL also supports the following two non-standard variants of the
INSERT statement:
1. Multiple records can be entered in a single INSERT statement through
the use of multiple VALUES() clauses within the same statement. For
example instead of doing this,

Sikkim Manipal University

Page No. 94

RDBMS with MySQL

Unit 4

You can do this:

You can also do away with the INSERTVALUES form altogether in favor
of syntax similar to that used by the UPDATE statement. This involves
using the SET clause to set values for each column individually.
So instead of doing this,

Sikkim Manipal University

Page No. 95

RDBMS with MySQL

Unit 4

You could do this:

Working with Default Values:


Fields not named in the field list of the INSERT query will be automatically
assigned their default values. Consider the following example:

Sikkim Manipal University

Page No. 96

RDBMS with MySQL

Unit 4

MySQL 4.0.3 and higher also support a new DEFAULT keyword, which can
be used to instruct MySQL to use the columns default value for that record.

Working with AUTOINCREMENT Fields:


When creating sequences with AUTOINCREMENT columns, omitting the
field name in the INSERT statement will cause MySQL generate the next
number in the sequence. The number serves well as a primary key for your
table.
Consider the following example which illustrates by creating a table with an
AUTOINCREMENT column as primary key and then populating it with data.

Sikkim Manipal University

Page No. 97

RDBMS with MySQL

Unit 4

For every record inserted, MySQL will insert a unique, automatically


incremented number in the uid field.
4.2.2 INSERT ... SELECT
With INSERT ... SELECT, you can quickly insert many rows into a table
from one or many tables.
Syntax:
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

Example:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

The following conditions hold for a INSERT ... SELECT statements:


Specify IGNORE to ignore rows that would cause duplicate-key
violations.
DELAYED is ignored with INSERT ... SELECT.
The target table of the INSERT statement may appear in the FROM
clause of the SELECT part of the query. (This was not possible in some
older versions of MySQL.) In this case, MySQL creates a temporary
Sikkim Manipal University

Page No. 98

RDBMS with MySQL

Unit 4

table to hold the rows from the SELECT and then inserts those rows into
the target table. However, it remains true that you cannot use INSERT
INTO t ... SELECT ... FROM t when t is a TEMPORARY table, because
TEMPORARY tables cannot be referred to twice in the same statement.
AUTO_INCREMENT columns work as usual.
To ensure that the binary log can be used to re-create the original
tables, MySQL does not allow concurrent inserts for INSERT ... SELECT
statements.
Currently, you cannot insert into a table and select from the same table
in a subquery.
To avoid ambiguous column reference problems when the SELECT and
the INSERT refer to the same table, provide a unique alias for each
table used in the SELECT part, and qualify column names in that part
with the appropriate alias.
In the values part of ON DUPLICATE KEY UPDATE, you can refer to
columns in other tables, as long as you do not use GROUP BY in the
SELECT part. One side effect is that you must qualify non-unique column
names in the values part.
4.2.3 INSERT DELAYED Syntax
The DELAYED option for the INSERT statement is a MySQL extension to
standard SQL that is very useful if you have clients that cannot or need not
wait for the INSERT to complete. This is a common situation when you use
MySQL for logging and you also periodically run SELECT and UPDATE
statements that take a long time to complete.
When a client uses INSERT DELAYED, it gets an okay from the server at
once, and the row is queued to be inserted when the table is not in use by
any other thread.
Another major benefit of using INSERT DELAYED is that inserts from many
clients are bundled together and written in one block. This is much faster
than performing many separate inserts.
Note that INSERT DELAYED is slower than a normal INSERT if the table is
not otherwise in use. There is also the additional overhead for the server to
handle a separate thread for each table for which there are delayed rows.
This means that you should use INSERT DELAYED only when you are
really sure that you need it.
Sikkim Manipal University

Page No. 99

RDBMS with MySQL

Unit 4

The queued rows are held only in memory until they are inserted into the
table. This means that if you terminate mysqld forcibly (for example, with kill
-9) or if mysqld dies unexpectedly, any queued rows that have not been
written to disk are lost.
Syntax:
INSERT DELAYED ...

4.2.4 LOAD DATA INFILE


The LOAD DATA INFILE statement reads rows from a text file into a table at
a very high speed. The filename must be given as a literal string.
LOAD DATA INFILE is the complement of SELECT ... INTO OUTFILE.
To write data from a table to a file, use SELECT ... INTO OUTFILE.
To read the file back into a table, use LOAD DATA INFILE.
The syntax of the FIELDS and LINES clauses is the same for both
statements.
Both clauses are optional, but FIELDS must precede LINES if both are
specified.
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = expr,...]

The character set indicated by the character_set_database system variable


is used to interpret the information in the file. SET NAMES and the setting of
character_set_client do not affect interpretation of input. If the contents of
the input file use a character set that differs from the default, it is usually
Sikkim Manipal University

Page No. 100

RDBMS with MySQL

Unit 4

preferable to specify the character set of the file by using the CHARACTER
SET clause, which is available as of MySQL 5.1.17.
LOAD DATA INFILE interprets all fields in the file as having the same
character set, regardless of the data types of the columns into which field
values are loaded. For proper interpretation of file contents, you must
ensure that it was written with the correct character set. For example, if you
write a data file with mysqldump -T or by issuing a SELECT ... INTO
OUTFILE statement in mysql, be sure to use a default-character-set
option with mysqldump or mysql so that output is written in the character
set to be used when the file is loaded with LOAD DATA INFILE.
Note: It is currently not possible to load data files that use the ucs2
character set.
You can also load data files by using the mysqlimport utility; it operates by
sending a LOAD DATA INFILE statement to the server. The local option
causes mysqlimport to read data files from the client host. You can specify
the compress option to get better performance over slow networks if the
client and server support the compressed protocol.
If you use LOW_PRIORITY, execution of the LOAD DATA statement is
delayed until no other clients are reading from the table. This affects only
storage engines that use only table-level locking (MyISAM, MEMORY,
MERGE).
If you specify CONCURRENT with a MyISAM table that satisfies the
condition for concurrent inserts (that is, it contains no free blocks in the
middle), other threads can retrieve data from the table while LOAD DATA is
executing. Using this option affects the performance of LOAD DATA a bit,
even if no other thread is using the table at the same time.
CONCURRENT is not replicated when using statement-based replication;
however, it is replicated when using row-based replication.
The LOCAL keyword, if specified, is interpreted with respect to the client
end of the connection:
If LOCAL is specified, the file is read by the client program on the client
host and sent to the server. The file can be given as a full pathname to
specify its exact location. If given as a relative pathname, the name is
interpreted relative to the directory in which the client program was
started.
Sikkim Manipal University

Page No. 101

RDBMS with MySQL

Unit 4

If LOCAL is not specified, the file must be located on the server host and
is read directly by the server. The server uses the following rules to
locate the file:
o If the filename is an absolute pathname, the server uses it as given.
o If the filename is a relative pathname with one or more leading
components, the server searches for the file relative to the server's
data directory.
o If a filename with no leading components is given, the server looks
for the file in the database directory of the default database.
Note that, in the non-LOCAL case, these rules mean that a file named as
./myfile.txt is read from the server's data directory, whereas the file named
as myfile.txt is read from the database directory of the default database. For
example, if db1 is the default database, the following LOAD DATA
statement reads the file data.txt from the database directory for db1, even
though the statement explicitly loads the file into a table in the db2
database:
LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;

Windows pathnames are specified using forward slashes rather than


backslashes. If you do use backslashes, you must double them.
For security reasons, when reading text files located on the server, the files
must either reside in the database directory or be readable by all. Also, to
use LOAD DATA INFILE on server files, you must have the FILE privilege.
The REPLACE and IGNORE keywords control handling of input rows that
duplicate existing rows on unique key values:
If you specify REPLACE, input rows replace existing rows. In other
words, rows that have the same value for a primary key or unique index
as an existing row.
If you specify IGNORE, input rows that duplicate an existing row on a
unique key value are skipped. If you do not specify either option, the
behavior depends on whether the LOCAL keyword is specified. Without
LOCAL, an error occurs when a duplicate key value is found, and the
rest of the text file is ignored. With LOCAL, the default behavior is the
same as if IGNORE is specified; this is because the server has no way
to stop transmission of the file in the middle of the operation.
Sikkim Manipal University

Page No. 102

RDBMS with MySQL

Unit 4

If you want to ignore foreign key constraints during the load operation, you
can issue a SET FOREIGN_KEY_CHECKS=0 statement before executing
LOAD DATA.
In some extreme cases, you can create the indexes even faster by turning
them off with ALTER TABLE ... DISABLE KEYS before loading the file into
the table and using ALTER TABLE ... ENABLE KEYS to re-create the
indexes after loading the file.
For both the LOAD DATA INFILE and SELECT ... INTO OUTFILE
statements, the syntax of the FIELDS and LINES clauses is the same. Both
clauses are optional, but FIELDS must precede LINES if both are specified.
If you specify a FIELDS clause, each of its subclauses (TERMINATED BY,
[OPTIONALLY] ENCLOSED BY, and ESCAPED BY) is also optional,
except that you must specify at least one of them.
If you specify no FIELDS clause, the defaults are the same as if you had
written this:
FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'

If you specify no LINES clause, the defaults are the same as if you had
written this:
LINES TERMINATED BY '\n' STARTING BY ''

The defaults cause LOAD DATA INFILE to act as follows when reading
input:
Look for line boundaries at new lines.
Do not skip over any line prefix.
Break lines into fields at tabs.
Do not expect fields to be enclosed within any quoting characters.
Interpret occurrences of tab, newline, or \ preceded by \ as literal
characters that are part of field values.
Conversely, the defaults cause SELECT ... INTO OUTFILE to act as
follows when writing output:
Write tabs between fields.
Do not enclose fields within any quoting characters.
Use \ to escape instances of tab, newline, or \ that occur within field
values.
Write newlines at the ends of lines.
Sikkim Manipal University

Page No. 103

RDBMS with MySQL

Unit 4

Backslash is the MySQL escape character within strings, so to write


FIELDS ESCAPED BY '\\', you must specify two backslashes for the
value to be interpreted as a single backslash.
Self Assessment Questions
1. When creating sequences with _________ columns, omitting the field
name in the INSERT statement will cause MySQL generate the next
number in the sequence.

4.3 Retrieving Information from a Table


MySQL offers a number of different commands to write data to the
database. However, when it comes to retrieving data, it only offers one: the
SELECT statement.
SELECT is used to retrieve rows selected from one or more tables, and can
include UNION statements and subqueries.
The most commonly used clauses of SELECT statements are these:
Each select_expr indicates a column that you want to retrieve. There
must be at least one select_expr.
table_references indicates the table or tables from which to retrieve
rows.
The WHERE clause, if given, indicates the condition or conditions that
rows must satisfy to be selected. where_condition is an expression that
evaluates to true for each row to be selected. The statement selects all
rows if there is no WHERE clause. In the WHERE clause, you can use
any of the functions and operators that MySQL supports, except for
aggregate (summary) functions.
Syntax:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr, ...
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]

Sikkim Manipal University

Page No. 104

RDBMS with MySQL

Unit 4

[ORDER BY {col_name | expr | position}


[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]

SELECT can also be used to retrieve rows computed without reference to


any table.

The SELECT statement can also be used as a expression evaluator, as


shown below:

Sikkim Manipal University

Page No. 105

RDBMS with MySQL

Unit 4

You are allowed to specify DUAL as a dummy table name in situations


where no tables are referenced:

In general, clauses used must be given in exactly the order shown in the
syntax description. For example, a HAVING clause must come after any
GROUP BY clause and before any ORDER BY clause. The exception is
that the INTO clause can appear either as shown in the syntax description
or immediately following the select_expr list.
A select_expr can be given an alias using AS alias_name. The alias is used
as the expression's column name and can be used in GROUP BY, ORDER
BY, or HAVING clauses.
The AS keyword is optional when aliasing a select_expr. It is good practice
to be in the habit of using AS explicitly when specifying column aliases.
The FROM table_references clause indicates the table or tables from which
to retrieve rows. If you name more than one table, you are performing a join.
For each table specified, you can optionally specify an alias.
tbl_name [[AS] alias] [index_hint]

The use of index hints provides the optimizer with information about how to
choose indexes during query processing.
You can use SET max_seeks_for_key=value as an alternative way to force
MySQL to prefer key scans instead of table scans.
You can refer to a table within the default database as tbl_name, or as
db_name.tbl_name to specify a database explicitly. You can refer to a
column as col_name, tbl_name.col_name, or db_name.tbl_name.col_name.
Sikkim Manipal University

Page No. 106

RDBMS with MySQL

Unit 4

You need not specify a tbl_name or db_name.tbl_name prefix for a column


reference unless the reference would be ambiguous.
A table reference can be aliased using tbl_name AS alias_name or
tbl_name alias_name:
mysql> SELECT t1.name, t2.salary FROM employee AS t1, info AS t2
WHEREt1.name = t2.name;
mysql> SELECT t1.name, t2.salary FROM employee t1, info t2 WHERE
t1.name = t2.name;

Columns selected for output can be referred to in ORDER BY and GROUP


BY clauses using column names, column aliases, or column positions.
Column positions are integers and begin with 1:
mysql> SELECT college, region, seed FROM tournament
ORDER BY region, seed;
mysql> SELECT college, region AS r, seed AS s FROM tournament ORDER BY r, s;
mysql> SELECT college, region, seed FROM tournament
ORDER BY 2, 3;

To sort in reverse order, add the DESC (descending) keyword to the name
of the column in the ORDER BY clause that you are sorting by. The default
is ascending order; this can be specified explicitly using the ASC keyword.
If ORDER BY occurs within a subquery and also is applied in the outer
query, the outermost ORDER BY takes precedence. For example, results
for the following statement are sorted in descending order, not ascending
order:
(SELECT ... ORDER BY a) ORDER BY a DESC;

If you use GROUP BY, output rows are sorted according to the GROUP BY
columns as if you had an ORDER BY for the same columns. To avoid the
overhead of sorting that GROUP BY produces, add ORDER BY NULL:
SELECT a, COUNT(b) FROM test_table GROUP BY a ORDER BY NULL;

MySQL extends the GROUP BY clause so that you can also specify ASC
and DESC after columns named in the clause:
SELECT a, COUNT(b) FROM test_table GROUP BY a DESC;

Sikkim Manipal University

Page No. 107

RDBMS with MySQL

Unit 4

4.3.1 Retrieving Specific Rows and Columns


To restrict the result set to contain a particular set of columns from the table,
you can specify these fields as a comma-separated list after the SELECT
keyword (in place of the * wildcard).
Example:

The following query is used to return only those records matching a


particular condition by adding the WHERE clause to the SELECT statement.
The WHERE clause must be followed by a conditional expression, which will
be used to filter out non-matching records from the result set.
Retrieve a list of only those accounts with balances lower than $1000.00.
mysql> SELECT accountName, accountBalance FROM accounts WHERE
accountBalance < 1000;

Sikkim Manipal University

Page No. 108

RDBMS with MySQL

Unit 4

The following query retrieves a list of all accounts created in the year 2002.

The LIMIT keyword can be used to restrict the total number of records
returned by a SELECT query.
Example: The following query restricts the result set to 5 records.

4.3.2 Sorting Query Results


The query results can be sorted by a specific field (or fields) with the
addition of the ORDER BY clause to the SELECT statement.

Sikkim Manipal University

Page No. 109

RDBMS with MySQL

Unit 4

Example: The following example sorts the query results on accounts table
by creation date.

The ASC and DESC keywords can be added to each field name in the
ORDER BY clause to customize the sorting methods further.
4.3.3 Grouping Query Results
MySQL makes it possible to break the records in a result set into distinct
groups on the basis of a specific attribute with the GROUP BY clause.
Because each group created in this manner is represented as a single row
(even though it contains multiple records), this capability is primarily used in
operations involving MySQLs numerous aggregate functions.
Example: The records in the accounts table are to be grouped on the basis
of acccreationplace. There are 5 distinct locations spread over the 11
records in the table. MySQL creates 5 distinct groups of records and
represents each group as a single row in the result set.

Sikkim Manipal University

Page No. 110

RDBMS with MySQL

Unit 4

Aggregate functions can now be applied to the records constituting each


individual group.
Example: The following query returns the total number of accounts at each
location.

Self Assessment Questions


2. A ______clause must come after any GROUP BY clause and before
any ORDER BY clause.
3. If ORDER BY occurs within a subquery and also is applied in the outer
query, the ______ ORDER BY takes precedence.

Sikkim Manipal University

Page No. 111

RDBMS with MySQL

Unit 4

4.4 Summary
This unit covers the following topics with respect to data manipulation
language statements in MySQL:
1. Loading Data into a table: Data loading into tables either from the user
through SQL statements or through external resources or from some
other existent tables is discussed here.
2. Retrieving information from a Table: Information retrieval is a major
task for the database users. This can be done in many ways through the
usage of appropriate SELECT statements in conjunction with clauses
like GROUP BY or ORDER BY.

4.5 Terminal Questions


1. Discuss the syntax of Insert statement with a suitable example.
2. Write the SQL statements to demonstrate the following using SELECT
command:
a. Expression Evaluation
b. Using table aliases
c. ORDER BY
3. The records in an accounts table are to be grouped on the basis of
acccreationplace. There are 5 distinct locations spread over the 11
records in the table. Write a query in MySQL that creates 5 distinct
groups of records and represent each group as a single row in the result
set. Also list the total number of accounts at each location

4.6 Answers
Self Assessment Questions
1. AUTOINCREMENT
2. HAVING
3. outermost
Terminal Questions
1.
Syntax:
INSERT INTO table-name (field-name, field-name, )
VALUES (field-value, field-value, )

Sikkim Manipal University

Page No. 112

RDBMS with MySQL

Unit 4

Example:
Create a table called student as follows:
CREATE TABLE student(studno INT(5) PRIMARY KEY, Studname
VARCHAR(30) NOT NULL);
The following Insert statement is used to insert values into student table:
INSERT INTO student(studno,studname) VALUES (10,abc);
INSERT INTO student(studno,studname) VALUES (20,def);
(Refer Section 4.2)
2.
a. SELECT 50<=(3 + 2);
b. SELECT t1.name, t2.salary FROM employee t1, info t2 WHERE
t1.name = t2.name;
c. SELECT college, region AS r, seed AS s FROM tournament ORDER
BY r, s;
(Refer Section 4.3)
3. SELECT accreationplace AS LOC, COUNT(*) FROM accounts
GROUP BY accreationplace;
(Refer Section 4.3)

Sikkim Manipal University

Page No. 113

RDBMS with MySQL

Unit 5
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12

Unit 5

Advanced Data Manipulation Language

Introduction
Objectives
JOIN
UNION
DELETE
TRUNCATE
UPDATE
DO
HANDLER
REPLACE
Summary
Terminal Questions
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 wont 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 querys 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
ProdID
1
2
3
4

Sales

ProdName
Apples
Oranges
Pineapples
Bananas

ID
1
2
3

ProdID
3
2
1

Quantity
2300
1500
3400

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
Pineapples
Apples

Quantity
2300
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
uid
100
103
104
107
110
111
112

groups
name
Sue
Harry
Louis
Sam
James
Mark
Rita

gid
501
502
503
504

name
Authors
Actors
Musicians
chefs

users_groups
uid
11
107
100
110
112
100
102
104
100

gid
502
502
503
501
501
501
501
502
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

name
Sam
Sue
James
Rita
Sue
Louis
Sue

Unit 5

name
Actors
Musicians
Authors
Authors
Authors
Actors
Actors

The above list is a symmetrical list, it doesnt tell you anything about the
users who doesnt 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
100
100
100
103
104
107
110
111
112

name
Sue
Sue
Sue
Harry
Louis
Sam
James
Mark
rita

gid
503
501
502
NULL
502
502
501
NULL
501

uid
100
100
100
NULL
104
107
110
NULL
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
501
501
501
501
502
502
502
502
503
NULL

uid
110
112
100
102
11
107
104
100
100
NULL

gid
501
501
501
501
502
502
502
502
503
504

name
authors
authors
authors
authors
actors
actors
actors
actors
musicians
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
Eyes
Hair

Color
Brown
Black
Gray

mysql> SELECT * FROM color, attribute;


Color
Brown
Black
Gray
Brown
Black
Gray
Sikkim Manipal University

Attribute
Eyes
Eyes
Eyes
Hair
Hair
Hair
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 recreated 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,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...

OR
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

RDBMS with MySQL

Unit 6

Unit 6

Subqueries

Structure
6.1 Introduction
Objectives
6.2 The Subquery as Scalar Operand
6.3 Comparisons Using Subqueries
6.4 Subqueries with ANY, IN, and SOME
6.5 Subqueries with ALL
6.6 Correlated Subqueries
6.7 EXISTS and NOT EXISTS
6.8 Row Subqueries
6.9 Subqueries in the FROM clause
6.10 Summary
6.11 Terminal Questions
6.12 Answers

6.1 Introduction
Normally, the query results are restricted through the addition of a WHERE
or HAVING clause, which contains one or more conditional expressions
used to filter out irrelevant records from the result set.
A situation may arise where in the conditional test used by one query
depends on the result generated by another query (or Subquery). In all such
cases, the results generated by one query depend on the data generated by
another, and the use of a constant value in the outer querys conditional test
becomes infeasible.
Prior to version 4.1 of MySQL, the only way to address this type of
requirement was to perform each query individually, and to use the data
from one in the clause of the other. MySQL 4.1 (and later) does away with
this by introducing one of the most frequently asked for features on the
MySQL mailing lists: subqueries.
A Subquery is exactly what it sounds like: a SELECT query that is
subordinate to another query. MySQL 4.1 (and latter) enables to you nest
queries within one another, and to use the result set generated by an inner
query within an outer one. As a result, instead of executing two (or more)
Sikkim Manipal University

Page No. 133

RDBMS with MySQL

Unit 6

separate queries, you execute a single query containing one (or more)
subqueries.
When it encounters such a nested query, MySQL begins with the innermost
query and moves outward and upward from it to the outermost (main) query.
The result set generated by each query on the journey is assigned to the
enclosing parent query, which is, in turn, executed, and its results are further
assigned to its parent.
A Subquery works just like a regular SELECT query, except that its result
set always consists of a single column containing one or more values. A
Subquery can be used anywhere an expression can be used, it must be
enclosed in parentheses, and, like a regular SELECT query, it must contain
a field list, a FROM clause with one or more table names, and optional
WHERE, HAVING, and GROUP BY clauses.
The main advantages of subqueries are:
They allow queries that are structured so that it is possible to isolate
each part of a statement.
They provide alternative ways to perform operations that would
otherwise require complex joins and unions.
They are, in many people's opinion, more readable than complex joins
or unions. Indeed, it was the innovation of subqueries that gave people
the original idea of calling the early SQL Structured Query Language.
Objectives
After studying this unit, you should be able to:
describe the usage of subquery as a scalar operand
explain how comparison of columns or tuples can be made using
subqueries
give examples of subqueries using the operators ALL, ANY, IN, and
SOME
solve the problems related to Correlated Subqueries
give examples of subqueries using the operators EXISTS and NOT
EXISTS
construct queries using Row Subqueries
demonstrate the usage of FROM clause in subqueries

Sikkim Manipal University

Page No. 134

RDBMS with MySQL

Unit 6

6.2 The Subquery as Scalar Operand


A subquery is a scalar subquery that returns a single value. A scalar
subquery is a simple operand, and you can use it almost anywhere a single
column value or literal is legal, and you can expect it to have those
characteristics that all operands have: a data type, a length, an indication
whether it can be NULL, and so on.

Example: We need a list of all the branch offices belonging to Rabbit Foods
Inc. Now you could do this by running two SELECT queries, one after
another, to first get the customer ID of Rabbit Foods Inc., and then using
that ID (104) in another query to get the list of branch offices liked to that
customer,

Sikkim Manipal University

Page No. 135

RDBMS with MySQL

Unit 6

by equi-joining the clients and branches tables.

OR with a Subquery:

Sikkim Manipal University

Page No. 136

RDBMS with MySQL

Unit 6

Thus a Subquery makes it possible to combine two or more queries into a


single statement and to use the results of one query in the conditional
clause of the other.
A Subquery must return a single column of results, or else MySQL will not
know how to handle the result set. You can nest subqueries to any depth, so
long as the basic rules discussed previously are followed.
Example: This example demonstrates by listing the services used by Sharp
Eyes Detective Agency:

Self Assessment Questions


1. A subquery is a ______ subquery that returns a single value.

6.3 Comparisons Using Subqueries


MySQL enables you to include subqueries in either a WHERE clause (to
constraint the records returned by the enclosing SELECTWHERE) or a
HAVING clause (to constrain the groups created by the enclosing
SELECTGROUP BY). The subquery, which is enclosed in parentheses,
can be preceded by comparison and logical operators, the IN operator, or
the EXISTS operator.
If a subquery produces a single value, you can use MySQLs comparison
operators to compare it with the conditional expression specified in the outer
querys WHERE or HAVING clause.
Example: List all those customers with exactly two branch offices. Normally,
we first need to obtain the number of branch offices per customer,

Sikkim Manipal University

Page No. 137

RDBMS with MySQL

Unit 6

And then filter out those with only two offices with a HAVING clause,

And then hand the client ID over to the clients table to get the client name.

Sikkim Manipal University

Page No. 138

RDBMS with MySQL

Unit 6

The following subquery takes care of the previous three steps for you in one
go:

In this case, the inner query is executed first this query takes care of
grouping the branches by customer ID and counting the number of records
(branch offices) in each group. Those customers who have exactly two
branch offices can easily be filtered out with a HAVING clause, and the
corresponding customer IDs returned to the main query. This then maps the
IDs into the customers table and returns the corresponding customer name.
Example Select all those customers using the service with the maximum
service fee.

Self Assessment Questions


2. The subquery, which is enclosed in ______ , can be preceded by
comparison and logical operators, the IN operator, or the EXISTS
operator.

Sikkim Manipal University

Page No. 139

RDBMS with MySQL

Unit 6

6.4 Subqueries with ANY, IN, and SOME

Subqueries with IN Operator


Comparison operators are appropriate only so long as the subquery
returns a result column consisting of a single value. In case, the result
set returns more than one value (or a list of values), the comparison
operators must be substituted by the IN operator.
The IN operator is used to test the match of data within the result set
available and perform the outer query operation accordingly.
Example: List all services used by a particular branch office. You should
first get the list of all service IDs for the specified branch.

Then look up each service ID in the services table for the corresponding
name.

With a subquery and the IN test, this becomes redundant.


Sikkim Manipal University

Page No. 140

RDBMS with MySQL

Unit 6

In the query above, MySQL will select only those records from the services
table that match the service ID collection returned by the subquery.

Subqueries with ANY Operator


The ANY keyword must follow a comparison operator.
This operator returns TRUE if the comparison is TRUE for ANY of the
values in the column that the subquery returns.
Example: Consider the following two tables (t01 and t02) with the
corresponding values:

Sikkim Manipal University

Page No. 141

RDBMS with MySQL

Unit 6

The following query shows the usage of the operator ANY with a subquery:

Suppose that there is a row in table t1 containing (10). The expression is


TRUE if table t2 contains (21,14,7) because there is a value 7 in t2 that is
less than 10. The expression is FALSE if table t2 contains (20,10), or if table
t2 is empty. The expression is unknown if table t2 contains
(NULL,NULL,NULL).
When used with a subquery, the word IN is an alias for = ANY. Thus, these
two statements are the same:
SELECT s1 FROM t1 WHERE s1 = ANY (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 IN (SELECT s1 FROM t2);

Sikkim Manipal University

Page No. 142

RDBMS with MySQL

Unit 6

The word SOME is an alias for ANY. Thus, these two statements are the
same:
SELECT s1 FROM t1 WHERE s1 <> ANY (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 <> SOME(SELECT s1 FROM t2);

6.5 Subqueries with ALL


The syntax for the above said operator is as follows:
operand comparison_operator ALL (subquery)

The word ALL must follow a comparison operator.


It returns TRUE if the comparison is TRUE for ALL of the values in the
column that the subquery returns.
Finally, if table t2 is empty, the result is TRUE. So, the following statement is
TRUE when table t2 is empty:

In general, tables containing NULL values and empty tables are edge
cases. When writing subquery code, always consider whether you have
taken those two possibilities into account.
NOT IN is an alias for <> ALL. Thus, these two statements are the same:
SELECT s1 FROM t1 WHERE s1 <> ALL (SELECT s1 FROM
t2);
SELECT s1 FROM t1 WHERE s1 NOT IN (SELECT s1 FROM
t2);

Self Assessment Questions


3. ________ is an alias for <> ALL.

Sikkim Manipal University

Page No. 143

RDBMS with MySQL

Unit 6

6.6 Correlated Subqueries


Most of the time, if a subquery is going to produce the same result set every
time it runs, it makes sense, performance-wise, to run it only once and use
the same result set to test every record generated in the main query.
Sometimes a situation arises in which a subquery uses a field from the main
query in its clause. Such a reference by a subquery to a field in its enclosing
query, is called an outer reference, and the corresponding subquery is
called a correlated subquery, because its correlated with the result set of
one or more of the queries enclosing it.
When an outer reference appears within a subquery, MySQL has to run the
subquery once for every record generated by the outer query, and therefore
test the subquery as many times as there are records in the outer querys
result set. In such a context, the EXISTS operator comes in handy, to filter
out certain records from the final result set.
Example:
SELECT * FROM t1 WHERE column1 = ANY
(SELECT column1 FROM t2 WHERE t2.column2 = t1.column2);

The subquery in the above example contains a reference to a column of t1,


even though the subquery's FROM clause does not mention a table t1.
Therefore, MySQL looks outside the subquery, and finds t1 in the outer
query.
Example: Assume that table t1 contains a row where column1 = 5 and
column2 = 6; meanwhile, table t2 contains a row where column1 = 5 and
column2 = 7. The simple expression ... WHERE column1 = ANY (SELECT
column1 FROM t2) would be TRUE, but in this example, the WHERE clause
within the subquery is FALSE (because (5,6) is not equal to (6,7)), so the
subquery as a whole is FALSE.

Sikkim Manipal University

Page No. 144

RDBMS with MySQL

Unit 6

Example:

If you look at the data of the corresponding tables mentioned in the above
example query, you will see in fact, only one branch is using four or more
services (branch ID 1011).
In this case since the inner query contains a reference to a field in the outer
query, MySQL cannot run the inner query only once. Rather, it has to run it
over and over, once for every row in the outer table, substitute the value of
the named field from that row in the subquery, and then decide whether to
include that outer row in the final result set on the basis of whether the
corresponding subquery returns a result.
Self Assessment Questions
4. A reference by a subquery to a field in its enclosing query, is called an
___ reference.

6.7 EXISTS and NOT EXISTS


The Exists operator can be used to check if a subquery produces any
results at all. This makes it possible to conditionally execute the outer query
only if the EXISTS test returns true.
Example:

Sikkim Manipal University

Page No. 145

RDBMS with MySQL

Unit 6

In this case, because the subquery returns an empty result set no


branches are using five or more services the EXISTS test will return false
and the outer query will not execute.
Here is one more modified version of the previous query which returns a
result set with the value TRUE being returned by the EXISTS operator.

In this case, because some branches are using four or more services, the
inner query will return a result set consisting of at least one row, the EXISTS
test will return true, and the outer query will be executed.
If the result set generated by the previous query is itself immaterial, we
could accomplish the same thing with the following query:

The EXISTS operator is most often used in the context of outer references.
Outer References: Most of the time, if a subquery is going to produce the
same result set every time it runs, it makes sense, performance wise, to run
Sikkim Manipal University

Page No. 146

RDBMS with MySQL

Unit 6

it only once and use the same result set to test every record generated in
the main query.
However, situations sometimes arise in which a subquery uses a field from
the main query in its clause. Such a reference, by a subquery to a field in its
enclosing query, is called an outer reference, and the corresponding
subquery is called a Correlated subquery, because its correlated with the
result set of one or more of the queries enclosing it.
When an outer reference appears within a subquery, MySQL has to run the
subquery once for every record generated by the outer query and, therefore,
test the subquery as many times as there are records in the outer querys
result set.
In such a context, the EXISTS operator comes in handy, to filter out certain
records from the final result set.
Example: Consider the following query, which reruns the previous example
with an outer reference to make the result set more useful:

If you look at the data, only one branch is using four or more services
(branch ID 1011).
In this case because the inner query contains a reference to a field in the
outer query, MySQL cannot run the inner query only once (as it usually
does). Rather, it has to run it over and over, once for every row in the outer
table, substitute the value of the named field from that row in the subquery,
and then decide whether to include that outer row in the final result set on
Sikkim Manipal University

Page No. 147

RDBMS with MySQL

Unit 6

the basis of whether the corresponding subquery returns a result set. This is
obviously expensive in terms of performance, and outer references should
be avoided unless absolutely necessary.
Self Assessment Questions
5. The ____ operator is most often used in the context of outer references.

6.8 Row Subqueries


A row subquery is a subquery variant that returns a single row and can thus
return more than one column value.
Example:
SELECT * FROM t1 WHERE (1,2) = (SELECT column1, column2 FROM t2);
SELECT * FROM t1 WHERE ROW(1,2) = (SELECT column1, column2 FROM t2);

The queries here are both TRUE if table t2 has a row where column1 = 1
and column2 = 2.
The expressions (1,2) and ROW(1,2) are sometimes called row
constructors. The two are equivalent. They are legal in other contexts as
well. For example, the following two statements are semantically equivalent
(although the first one cannot be optimized until MySQL 5.1.12):
SELECT * FROM t1 WHERE (column1,column2) = (1,1);
SELECT * FROM t1 WHERE column1 = 1 AND column2 = 1;

The normal use of row constructors is for comparisons with subqueries that
return two or more columns. For example, the following query answers the
request, find all rows in table t1 that also exist in table t2:
SELECT column1,column2,column3
FROM t1
WHERE (column1,column2,column3) IN
(SELECT column1,column2,column3 FROM t2);

Self Assessment Questions


6. A _____ subquery is a subquery variant that returns a single row and
can thus return more than one column value.

Sikkim Manipal University

Page No. 148

RDBMS with MySQL

Unit 6

6.9 Subqueries in the FROM clause


Subqueries are legal in a SELECT statement's FROM clause. The actual
syntax is:
SELECT ... FROM (subquery) [AS] name ...

The [AS] name clause is mandatory, because every table in a FROM clause
must have a name. Any columns in the subquery select list must have
unique names.
For the sake of illustration, assume that you have this table:

Here is how to use a subquery in the FROM clause, using the example
table:

Subqueries in the FROM clause can return a scalar, column, row, or table.
Subqueries in the FROM clause cannot be correlated subqueries, unless
used within the ON clause of a JOIN operation.
Sikkim Manipal University

Page No. 149

RDBMS with MySQL

Unit 6

6.10 Summary
A Subquery is exactly what it sounds like: a SELECT query that is
subordinate to another query. This unit covers the various concepts of
subqueries.
1. The subquery as scalar operand: A scalar subquery is a simple
operand, and you can use it almost anywhere a single column value or
literal is legal, and you can expect it to have those characteristics that all
operands have: a data type, a length, an indication whether it can be
NULL, and so on.
2. Comparisons Using Subqueries: The output of the subqueries can be
used to compare the output of the outer most queries. This can be done
using various comparison operators like >, <, IN, and so on.
3. Subqueries with ANY, IN, and SOME: These type of subqueries can
be used in cases where a set of values need to be compared and the
output determined.
4. Subqueries with ALL: These type of subqueries can be used in cases
where a set of values are the output generated by the inner or
subqueries and the output is possible only if all the values match with
the outer query.
5. Correlated Subqueries: Sometimes a situation arises in which a
subquery uses a field from the main query in its clause. Such a
reference by a subquery to a field in its enclosing query, is called an
outer reference, and the corresponding subquery is called a correlated
subquery, because its correlated with the result set of one or more of
the queries enclosing it.
6. EXISTS and NOT EXISTS: The Exists operator can be used to check if
a subquery produces any results at all. The NOT EXISTS operator is
exactly the opposite of the output produced by NOT EXISTS.
7. Row Subqueries: A row subquery is a subquery variant that returns a
single row and can thus return more than one column value.
8. Subqueries in the FROM Clause: Subqueries are legal in a SELECT
statement's FROM clause. Any columns in the subquery select list must
have unique names.
Sikkim Manipal University

Page No. 150

RDBMS with MySQL

Unit 6

6.11 Terminal Questions


1. Give the advantages of subqueries.
2. Define a correlated subquery with an example.
3. Define a row subquery with an example.

6.12 Answers
Self Assessment Questions
1. scalar
2. parentheses
3. NOT IN
4. outer
5. EXISTS
6. row
Terminal Questions
1. (Refer Section 6.1)
They allow queries that are structured so that it is possible to isolate
each part of a statement.
They provide alternative ways to perform operations that would
otherwise require complex joins and unions.
They are, in many people's opinion, more readable than complex
joins or unions.
2. Sometimes a situation arises in which a subquery uses a field from the
main query in its clause. Such a reference by a subquery to a field in its
enclosing query, is called an outer reference, and the corresponding
subquery is called a correlated subquery, because its correlated with
the result set of one or more of the queries enclosing it.
When an outer reference appears within a subquery, MySQL has to run
the subquery once for every record generated by the outer query, and
therefore test the subquery as many times as there are records in the
outer querys result set. In such a context, the EXISTS operator comes
in handy, to filter out certain records from the final result set.

Sikkim Manipal University

Page No. 151

RDBMS with MySQL

Unit 6

Example:
SELECT * FROM t1 WHERE column1 = ANY
(SELECT column1 FROM t2 WHERE t2.column2 =
t1.column2);

The subquery in the above example contains a reference to a column of


t1, even though the subquery's FROM clause does not mention a table
t1. Therefore, MySQL looks outside the subquery, and finds t1 in the
outer query.
3. A row subquery is a subquery variant that returns a single row and can
thus return more than one column value.
Example:
SELECT * FROM t1 WHERE (1,2) = (SELECT column1, column2
FROM t2);
SELECT * FROM t1 WHERE ROW(1,2) = (SELECT column1,
column2
t2);TRUE if table t2 has a row where column1 =
The queries
here FROM
are both

1 and column2 = 2.
The expressions (1,2) and ROW(1,2) are sometimes called row
constructors. The two are equivalent. They are legal in other contexts as
well.

Sikkim Manipal University

Page No. 152

RDBMS with MySQL

Unit 7

Unit 7

Operators and Functions

Structure
7.1 Introduction
Objectives
7.2 Operators
Parentheses
Comparison Operators
Logical Operators
Case Sensitivity Operators
7.3 Control Flow Functions
7.4 String Functions
7.5 Numeric Functions
Arithmetic Operators
Mathematical Functions
7.6 Date and Time Functions
7.7 Summary
7.8 Terminal Questions
7.9 Answers

7.1 Introduction
We have already seen in the previous units, how MySQL statements such
as the SELECT statement can use comparison operators to retrieve only
those records matching a specified condition or a set of conditions. The
operators can be used to build complex expressions and clauses that allow
you to perform sophisticated comparison and conversion operations on the
data in your MySQL tables.
This unit will introduce you to MySQLs numerous operators, explaining the
usage of each using appropriate examples, perform calculations and
comparisons, create conditional groups, search for regular expressions in
strings, and data conversion methods.
The MySQL operators can be classified into the following four categories:
1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
4. Bit Operators
Sikkim Manipal University

Page No. 153

RDBMS with MySQL

Unit 7

Objectives
After studying this unit, you should be able to:
discuss the importance and applications of various operators
describe and apply various Operators like Parenthesis, Comparison, etc.
explain the importance of various control flow functions along with
applications
describe and apply various string functions
describe various Arithmetic operators and Mathematical Functions
explain the date and time functions with relevant examples

7.2 Operators
7.2.1 Parenthesis
( ... )

Use parentheses to force the order of evaluation in an expression.


Example:

mysql> SELECT 1+2*3;


-> 7
mysql> SELECT (1+2)*3;
-> 9

7.2.2 Comparison Operators


When working with MySQL SELECT queries, you may find the need to use
the numerous comparison operators that allow you to compare the left side
of an expression with its right side. The result of such a comparison operator
is always 1(TRUE) or 0 (FALSE) or NULL (cannot be determined).
The following table lists various comparison operators available in MySQL:
Table 7.1: Comparison Operators in MySQL
Operator
Function
=
Equals to
<> aka !=
Not equal to
<= >
NULL safe equal to
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to
Sikkim Manipal University

Page No. 154

RDBMS with MySQL

BETWEEN
IN
IS NULL
IS NOT NULL
LIKE
REGEXP aka RLIKE

Unit 7

Exists in specified range


Exists in specified set
Is NULL
Is not NULL
Wildcard Match
Regular Expression Match

The comparison operators can be used to compare both numbers and


strings. Numbers are compared as floating point values, while strings are
compared in a case-insensitive manner.
The = operator is used to test whether both sides of an expression are
equal:

The = operator can also be used for comparison of string values:

The opposite of the = operator is the <> operator, which is used to test
whether the two sides of an expression are unequal; it returns true if they
are and false if they are not.
Sikkim Manipal University

Page No. 155

RDBMS with MySQL

Unit 7

The <> operator can also be used with string values:

The <> operator can also be used for comparison with NULL values:

The BETWEEN operator is used to test whether or not a value (or an


expression evaluating to a value) lies within a specified range:

Sikkim Manipal University

Page No. 156

RDBMS with MySQL

Unit 7

A comparison test using the BETWEEN operator returns true if the


expression being tested lies between the specified range, inclusive of both
end points of the range. This works not just with numbers, but with strings
as well, as shown in the example below:

You can reverse the results of a BETWEEN test by adding the NOT logical
operator; this returns true only if the expression lies outside the given range:

The IN operator is used to test whether a value (or an expression evaluating


to a value) is included in a named set of values.
Example:

A comparison test using the IN operator returns true if the value being
tested exists in the specified set. This operator can be used both with strings
as well as numbers.

Sikkim Manipal University

Page No. 157

RDBMS with MySQL

Unit 7

You can perform the case-sensitive comparison with the BINARY keyword:

As with other comparison operators, the IN operator returns NULL if either


the expression to be evaluated or any of the values in the (non-matching)
set are NULL:

Example:
To test for the presence or absence of actual NULL values in an expression,
we use the IS NULL and IS NOT NULL operators, which are designed
specifically to perform comparisons involving NULL values.
The following example demonstrates how the IS NULL operator can be
used to test whether or not a value is null:

Sikkim Manipal University

Page No. 158

RDBMS with MySQL

Unit 7

The <= > operator, referred to as a NULL safe equality operator. If, in a
deviation from its normal behavior, MySQL returns a true or false result for
the comparison even when the expressions involved in the comparison
contain a NULL value.

The above three mentioned operators come in handy when dealing with
MySQL columns containing NULL data.
To perform wildcard searches of data, you should use the LIKE operator. It
allows the selection of records matching all or part of a specified string by
allowing the use of special wild card characters in an expression.
Example:

In this case, the % wildcard character tells MySQL to match all those values
zero or more occurrences of the substring ll; for example hello, ball, roller,
and so on.

Sikkim Manipal University

Page No. 159

RDBMS with MySQL

Unit 7

You can use the _ (Underscore character) to match a single character


instead of a sequence.

The NOT Logical Operator: Adding this operator reverses the test,
returning those records that do not match the specified string.

The REGEXP operator: Allows you to perform more complex string


comparisons, this time using UNIX regular expressions. The REGEXP
operator returns true if the match is found as shown in the examples below:
Example - 1:

Example - 2:

Sikkim Manipal University

Page No. 160

RDBMS with MySQL

Unit 7

Addition of the NOT logical operator reverses the result, returning false if a
match is found.

The table below lists a set of useful meta characters along with the function
of each:
Table 7.2: Meta characters and their meanings
Meta Character

Function or Meaning

Match one or more occurrences of the preceding character

Match zero or more occurrences of the preceding character

Match zero or one occurrences of the preceding character

Match any character

Match at the beginning of a string

Match at the end of a string

\s

Match a single white space character, including tabs and


white-space characters

\S

Match everything that is not a white space character

\d

Match numbers from 0 to 9

\w

Match letters, numbers, and underscore

\W

Match anything that does not match with \w

7.2.3 Logical Operators


MySQL uses four logical operators, which makes it possible to test the
logical validity of one or more expressions (or sets of expressions). The
result of an operation involving these operators is always 1(true), 0 (false),
or NULL (could not be determined).

Sikkim Manipal University

Page No. 161

RDBMS with MySQL

Unit 7

The table below lists the logical operators:


Table 7.3: Logical Operators
Operator

Function or Meaning

NOT aka !

Logical NOT

AND aka &&

Logical AND

OR aka ||

Logical OR

XOR

Logical XOR (Exclusive OR)

The NOT Operator: It reverses the logical sense of the test following it,
turning true into false and false into true.

The AND Operator: This operator makes it possible to test the validity of
two or more values (or expressions evaluating to values); it returns true if all
its components are true and not NULL, and it returns false otherwise.

The OR Operator: This operator returns true if any of the values or


expressions involved are true and NOT NULL, and false otherwise.

Sikkim Manipal University

Page No. 162

RDBMS with MySQL

Unit 7

The XOR (Exclusive OR) Operator: This operator is included in the


MySQL 4.x or later versions. This operator returns true if if either one (but
not both) of its arguments is true.

7.2.4 Case Sensitivity Operators


BINARY The BINARY operator casts the string following it to a binary string.
This is an easy way to force a column comparison to be case sensitive even
if the column isnt defined as BINARY or BLOB.
Example -1:

Example -2:

mysql> SELECT 'a' = 'A';


-> 1

mysql> SELECT BINARY 'a' = 'A';


-> 0

BINARY str is a shorthand for CAST (str AS BINARY). BINARY was


introduced in MySQL 3.23.0.
Note: In some contexts, if you cast an indexed column to BINARY, MySQL
will not be able to use the index efficiently.
If you want to compare a BLOB value in case-insensitive fashion, you can
do so as follows:

Before MySQL 4.1.1, use the UPPER() function to convert the BLOB
value to uppercase before performing the comparison:
Example:

SELECT 'A' LIKE UPPER(blob_col) FROM tbl_name;

If the comparison value is lowercase, convert the BLOB value using


LOWER() instead.
Sikkim Manipal University

Page No. 163

RDBMS with MySQL

Unit 7

For MySQL 4.1.1 and up, BLOB columns have a character set of binary,
which has no concept of lettercase. To perform a case-insensitive
comparison, use the CONVERT() function to convert the BLOB value to
a character set that is not case sensitive. The result is a non-binary
string, so the LIKE operation is not case sensitive:
Example:
SELECT 'A' LIKE CONVERT(blob_col USING latin1) FROM
tbl_name;

To use a different character set, substitute its name for latin1 in the
preceding statement.
4. Bit Operators
MySQL has 6 operators specifically designed for bit manipulations. The
table below shows the list:
Table 7.4: Bit wise operators
Operator

Function or Meaning

&

Bitwise AND

Bitwise OR

Bitwise XOR (Exclusive OR)

Bit Inversion

>>

Bitwise Right Shift

<<

Bitwise Left Shift

The | operator is used to perform a bitwise OR, while the & operator is
used to perform a bitwise AND.

Sikkim Manipal University

Page No. 164

RDBMS with MySQL

Unit 7

You can shift the bits to the left and right with the << AND >> operators,
respectively consider the example, at the top of the next page, which
demonstrates:

The ^ operator performs a bitwise XOR operation.

Sikkim Manipal University

Page No. 165

RDBMS with MySQL

Unit 7

Self Assessment Questions


1. We can use _________ to force the order of evaluation in an
expression.
2. A comparison test using the BETWEEN operator returns true if the
expression being tested lies between the specified range, ____ of both
end points of the range.

7.3 Control Flow Functions


1. Case
CASE value WHEN [compare-value] THEN result [WHEN [compare-value]
THEN result
...] [ELSE result] END
CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...]
[ELSE
result] END

The first version returns the result where value=compare-value. The


second version returns the result for the first condition that is true. If
there was no matching result value, the result after ELSE is returned, or
NULL if there is no ELSE part.
Example:
mysql> SELECT CASE 1 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE
'more' END;
-> 'one'
mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
-> 'true'
mysql> SELECT CASE BINARY 'B' WHEN 'a' THEN 1 WHEN 'b' THEN 2
END;
-> NULL

The type of the return value (INTEGER, DOUBLE or STRING) is the


same as the type of the first returned value (the expression after the first
THEN).
2. IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns
expr2, else it returns expr3. IF() returns a numeric or string value,
depending on the context in which it is used.

Sikkim Manipal University

Page No. 166

RDBMS with MySQL

Unit 7

Example:
mysql> SELECT IF(1>2,2,3);
-> 3
mysql> SELECT IF(1<2,'yes','no');
-> 'yes'
mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
-> 'no'

If only one of expr2 or expr3 is explicitly NULL, the result type of the IF()
function is the type of non-NULL expression.
expr1 is evaluated as an integer value, which means that if you are testing
floating-point or string values, you should do so using a comparison
operation.
Example:

mysql> SELECT IF(0.1,1,0);


-> 0
mysql> SELECT IF(0.1<>0,1,0);
-> 1

In the first case shown, IF(0.1) returns 0 because 0.1 is converted to an


integer value, resulting in a test of IF(0). This may not be what you
expect.
In the second case, the comparison tests the original floating-point value
to see whether it is non-zero. The result of the comparison is used as an
integer.
3. IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1, else it returns expr2.
IFNULL() returns a numeric or string value, depending on the context in
which it is used.
Example:
mysql> SELECT IFNULL(1,0);
-> 1
mysql> SELECT IFNULL(NULL,10);
-> 10
mysql> SELECT IFNULL(1/0,10);
-> 10
mysql> SELECT IFNULL(1/0,'yes');
-> 'yes'

Sikkim Manipal University

Page No. 167

RDBMS with MySQL

Unit 7

4. NULLIF(expr1,expr2)
Returns NULL if expr1 = expr2 is true, else returns expr1. This is the
same as CASE WHEN x = y THEN NULL ELSE x END.
mysql> SELECT NULLIF(1,1);
-> NULL
mysql> SELECT NULLIF(1,2);
-> 1

Note: MySQL evaluates expr1 twice if the arguments are not equal.
Self Assessment Questions
3. A comparison test using the BETWEEN operator returns true if the
expression being tested lies between the specified range, ____ of both
end points of the range.

7.4 String Functions


String-valued functions return NULL if the length of the result would be
greater than the value of the max_allowed_packet system variable. For
functions that operate on string positions, the first position is numbered 1.
Function Name
ASCII(str)

BIN(N)

Sikkim Manipal University

Table 7.5: String Functions


Description
Example
Returns the numeric
1. SELECT ASCII('2');
value of the leftmost
-> 50
character of the string
str. Returns 0 if str is
2. SELECT ASCII(2);
the empty string.
-> 50
Returns NULL if str is
NULL. ASCII() works for 3. SELECT ASCII('dx');
characters with numeric
-> 100
values from 0 to 255.
Returns a string
SELECT BIN(12);
representation of the
-> '1100'
binary value of N,
where N is a longlong
(BIGINT) number.
This is equivalent to
CONV(N,10,2). Returns
NULL if N is NULL.

Page No. 168

RDBMS with MySQL

Unit 7

BIT_LENGTH(str)

Returns the length of


the string str in bits.

CHAR(N,...)

CHAR() interprets the


arguments as integers
and returns a string
consisting of the
characters given by the
code values of those
integers. NULL values
are skipped.
Returns the length of
the string str, measured
in characters. A multibyte character counts
as a single character.
This means that for a
string containing five
two-byte characters,
LENGTH() returns 10,
whereas
CHAR_LENGTH()
returns 5.
Returns the string that
results from
concatenating the
arguments. Returns
NULL if any argument is
NULL. May have one or
more arguments. A
numeric argument is
converted to its
equivalent string form.
Returns the string str,
with the substring
beginning at position
pos and len characters
long replaced by the
string newstr.

CHAR_LENGTH(str)

CONCAT(str1,str2,...)

INSERT(str,pos,len,
newstr)

Sikkim Manipal University

SELECT
BIT_LENGTH('text');
-> 32
1.SELECT
CHAR(77,121,83,81,'76');
-> 'MySQL'
2.SELECT
CHAR(77,77.3,'77.3');
-> 'MMM'

1. SELECT CONCAT('My',
'S', 'QL');
-> 'MySQL'
2. SELECT CONCAT('My',
NULL, 'QL');
-> NULL

SELECT
INSERT('Quadratic', 3, 4,
'What');
-> 'QuWhattic'

Page No. 169

RDBMS with MySQL

INSTR(str,substr)

LEFT(str,len)

LENGTH(str)

LOCATE(substr,str)
LOCATE(substr,str,
pos)

LOWER(str)

Sikkim Manipal University

Unit 7

Returns the position of


the first occurrence of
substring substr in
string str. This is the
same as the twoargument form of
LOCATE(), except that
the arguments are
swapped.
Returns the leftmost len
characters from the
string str.
Returns the length of
the string str, measured
in bytes. A multi-byte
character counts as
multiple bytes. This
means that for a string
containing five two-byte
characters, LENGTH()
returns 10, whereas
CHAR_LENGTH()
returns 5.
The first syntax returns
the position of the first
occurrence of substring
substr in string str. The
second syntax returns
the position of the first
occurrence of substring
substr in string str,
starting at position pos.
Returns 0 if substr is
not in str.
Returns the string str
with all characters
changed to lowercase
according to the current
character set mapping
(the default is ISO8859-1 Latin1).

1.SELECT
INSTR('foobarbar', 'bar');
-> 4
2. SELECT INSTR('xbar',
'foobar');
-> 0

SELECT LEFT('foobarbar',
5);
-> 'fooba'
SELECT LENGTH('text');
-> 4

1. SELECT LOCATE('bar',
'foobarbar');
-> 4
2. SELECT
LOCATE('xbar', 'foobar');
-> 0

SELECT
LOWER('QUADRATICALL
Y');
-> 'quadratically'

Page No. 170

RDBMS with MySQL

LPAD(str,len,padstr)

LTRIM(str)

REPEAT(str,count)

REPLACE(str,from_
str,to_str)

REVERSE(str)

RIGHT(str,len)

RPAD(str,len,padstr)

Sikkim Manipal University

Unit 7

Returns the string str,


left-padded with the
string padstr to a length
of len characters. If str
is longer than len, the
return value is
shortened to len
characters.

1. SELECT
LPAD('hi',4,'??');
-> '??hi'

Returns the string str


with leading space
characters removed.
Returns a string
consisting of the string
str repeated count
times. If count <= 0,
returns an empty string.
Returns NULL if str or
count are NULL.
Returns the string str
with all occurrences of
the string from_str
replaced by the string
to_str.
Returns the string str
with the order of the
characters reversed.
Returns the rightmost
len characters from the
string str.
Returns the string str,
right-padded with the
string padstr to a length
of len characters. If str
is longer than len, the
return value is
shortened to len
characters.

SELECT LTRIM(' barbar');


-> 'barbar'

2. SELECT
LPAD('hi',1,'??');
-> 'h'

SELECT
REPEAT('MySQL', 3);
-> 'MySQLMySQLMySQL'

SELECT
REPLACE('www.mysql.co
m', 'w', 'Ww');
-> 'WwWwWw.mysql.com'
SELECT REVERSE('abc');
-> 'cba'
SELECT
RIGHT('foobarbar', 4);
-> 'rbar'
SELECT RPAD('hi',5,'?');
-> 'hi???'

Page No. 171

RDBMS with MySQL

RTRIM(str)

SUBSTRING(str,pos)
SUBSTRING(str
FROM pos)
SUBSTRING(str,pos,l
en)
SUBSTRING(str
FROM pos FOR len)

UPPER(str)

Unit 7

Returns the string str


with trailing space
characters removed.
The forms without a len
argument return a
substring from string str
starting at position pos.
The forms with a len
argument return a
substring len characters
long from string str,
starting at position pos.
The forms that use
FROM are standard
SQL syntax.

SELECT RTRIM('barbar ');


-> 'barbar'

Returns the string str


with all characters
changed to uppercase
according to the current
character set mapping

SELECT UPPER('Hej');
-> 'HEJ'

1. SELECT
SUBSTRING('Quadraticall
y',5);
-> 'ratically'
2. SELECT
SUBSTRING('foobarbar'
FROM 4);
-> 'barbar'
3. SELECT
SUBSTRING('Quadraticall
y',5,6);
-> 'ratica'

7.4.1 String Comparison Functions


MySQL automatically converts numbers to strings as necessary, and viceversa.
mysql> SELECT 1+'1';
-> 2
mysql> SELECT CONCAT(2,' test');
-> '2 test'

If you want to convert a number to a string explicitly, use the CAST() or


CONCAT() function:
mysql> SELECT 38.8, CAST(38.8 AS CHAR);
-> 38.8, '38.8'
mysql> SELECT 38.8, CONCAT(38.8);
-> 38.8, '38.8'

Sikkim Manipal University

Page No. 172

RDBMS with MySQL

Unit 7

CAST() is preferable, but it is unavailable before MySQL 4.0.2.


If a string function is given a binary string as an argument, the resulting
string is also a binary string. A number converted to a string is treated as a
binary string. This only affects comparisons.
Normally, if any expression in a string comparison is case sensitive, the
comparison is performed in case-sensitive fashion.
1. expr LIKE pat [ESCAPE 'escape-char']
Pattern matching using SQL simple regular expression comparison.
Returns 1 (TRUE) or 0 (FALSE). If either expr or pat is NULL, the result
is NULL.
With LIKE you can use the following two wildcard characters in the
pattern:
Table 7.6: Wildcard Characters
SNo
1

Character
%

_
(Underscore)

Description
Matches any number of
characters, even zero
characters
Matches exactly one
character

Example
SELECT
'David!'
LIKE 'David_';
-> 1
SELECT
'David!'
LIKE '%D%v%';
-> 1

2. expr REGEXP pat


expr RLIKE pat
Performs a pattern match of a string expression expr against a pattern
pat. The pattern can be an extended regular expression.
Returns 1 if expr matches pat, otherwise returns 0.
If either expr or pat is NULL, the result is NULL. RLIKE is a synonym for
REGEXP, provided for mSQL compatibility.
Note: Because MySQL uses the C escape syntax in strings (for
example, \n to represent newline), you must double any \ that you use
in your REGEXP strings. As of MySQL 3.23.4, REGEXP is not case
sensitive for normal (not binary) strings.

Sikkim Manipal University

Page No. 173

RDBMS with MySQL

Unit 7

Example:
mysql> SELECT 'Monty!' REGEXP 'm%y%%';
-> 0
mysql> SELECT 'Monty!' REGEXP '.*';
-> 1
mysql> SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';
-> 1
mysql> SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
-> 1 0
mysql> SELECT 'a' REGEXP '^[a-d]';
-> 1

3. STRCMP(expr1,expr2)
STRCMP() returns 0 if the strings are the same, -1 if the first argument is
smaller than the second according to the current sort order, and 1
otherwise.
Example:
mysql> SELECT STRCMP('text', 'text2');
-> -1
mysql> SELECT STRCMP('text2', 'text');
-> 1
mysql> SELECT STRCMP('text', 'text');
-> 0

Self Assessment Questions


4. The output of the function ASCII('dx') is _______.
5. The output of the string function CONCAT('My', NULL, 'QL') is ______.
6. The output of the string function LPAD('hi',1,'??') is _______.
7. The output after execution of the following SQL statement is _____:
SELECT 'David!' LIKE '%D%v%';

7.5 Numeric Functions


7.5.1 Arithmetic Operators
Table 7.7: Arithmetic Operators

Name

Description

DIV(v4.1.0)
/
%
+
*

Integer division
Division operator
Minus operator
Modulo operator
Addition operator
Times operator
Change the sign of the
argument

Sikkim Manipal University

Page No. 174

RDBMS with MySQL

Unit 7

The usual arithmetic operators are available. The result is determined


according to the following rules:
In the case of -, + , and *, the result is calculated with BIGINT(64-bit)
precision if both arguments are integers.
If one of the arguments is an unsigned integer, and the other argument
is also an integer, the result is an unsigned integer.
If any of the operands of a +,- , /, *, % is a real or string value, then the
precision of the result is the precision of the argument with the maximum
precision.
In division performed with /, the scale of the result when using two exact
values is the scale of the first argument plus the value of the
div_precision_increment system variable (which is 4 by default). For
example, the result of the expression 5.05 / 0.014 has a scale of six
decimal places (360.714286).
These rules are applied for each operation, such that nested calculations
imply the precision of each component. Hence, (14620 / 9432456) / (24250 /
9432456), would resolve first to (0.0014) / (0.0026), with the final result
having 8 decimal places (0.60288653).
Because of these rules and the way they are applied, care should be taken
to ensure that components and sub-components of a calculation use the
appropriate level of precision.
Example:
mysql> SELECT 3+5;
-> 8

7.5.2 Mathematical Functions


Table 7.8: Mathematical Functions
Description
ABS()
Return the absolute value
ACOS()
Return the arc cosine
ASIN()
Return the arc sine
ATAN2(), ATAN()
Return the arc tangent of the two arguments
ATAN()
Return the arc tangent
Return the smallest integer value not less than
CEIL()
the argument
Return the smallest integer value not less than
CEILING()
the argument
Name

Sikkim Manipal University

Page No. 175

RDBMS with MySQL

CONV()
COS()
COT()
CRC32()(v4.1.0)
DEGREES()
EXP()
FLOOR()
LN()
LOG10()
LOG2()
LOG()
MOD()
OCT()
PI()
POW()
POWER()
RADIANS()
RAND()
ROUND()
SIGN()
SIN()
SQRT()
TAN()
TRUNCATE()

Unit 7

Convert numbers between different number


bases
Return the cosine
Return the cotangent
Compute a cyclic redundancy check value
Convert radians to degrees
Raise to the power of
Return the largest integer value not greater
than the argument
Return the natural logarithm of the argument
Return the base-10 logarithm of the argument
Return the base-2 logarithm of the argument
Return the natural logarithm of the first
argument
Return the remainder
Return an octal representation of a decimal
number
Return the value of pi
Return the argument raised to the specified
power
Return the argument raised to the specified
power
Return argument converted to radians
Return a random floating-point value
Round the argument
Return the sign of the argument
Return the sine of the argument
Return the square root of the argument
Return the tangent of the argument
Truncate to specified number of decimal places

All mathematical functions return NULL in the event of an error.


Example 1:
ABS(X): Returns the absolute value of X.
mysql> SELECT ABS(2);
-> 2
mysql> SELECT ABS(-32);
-> 32

Sikkim Manipal University

Page No. 176

RDBMS with MySQL

Unit 7

This function is safe to use with BIGINT values.


Example 2:
ACOS(X): Returns the arc cosine of X, that is, the value whose cosine is X.
Returns NULL if X is not in the range -1 to 1.
mysql> SELECT ACOS(1);
-> 0
mysql> SELECT ACOS(1.0001);
-> NULL
mysql> SELECT ACOS(0);
-> 1.5707963267949

Self Assessment Questions


8. The output of the Numeric function ACOS(1.0001) is ________.

7.6 Date and Time Functions


This section describes the functions that can be used to manipulate
temporal values.
Table 7.9: Date and Time Functions
Name

Description

ADDDATE()(v4.1.1)

Add dates

ADDTIME()(v4.1.1)

Add time

CONVERT_TZ()(v4.1.3)

Convert from one timezone to another

CURDATE()

Return the current date

CURRENT_DATE(), CURRENT_DATE

Synonyms for CURDATE()

CURRENT_TIME(), CURRENT_TIME

Synonyms for CURTIME()

CURRENT_TIMESTAMP(),
CURRENT_TIMESTAMP

Synonyms for NOW()

CURTIME()

Return the current time

DATE_ADD()

Add two dates

DATE_FORMAT()

Format date as specified

DATE_SUB()

Subtract two dates

DATE()(v4.1.1)

Extract the date part of a date or


datetime expression

DATEDIFF()(v4.1.1)

Subtract two dates

DAY()(v4.1.1)

Synonym for DAYOFMONTH()

DAYNAME()(v4.1.21)

Return the name of the weekday

Sikkim Manipal University

Page No. 177

RDBMS with MySQL

Unit 7

DAYOFMONTH()

Return the day of the month (0-31)

DAYOFWEEK()

Return the weekday index of the


argument

DAYOFYEAR()

Return the day of the year (1-366)

EXTRACT

Extract part of a date

FROM_DAYS()

Convert a day number to a date

FROM_UNIXTIME()

Format UNIX timestamp as a date

GET_FORMAT()(v4.1.1)

Return a date format string

HOUR()

Extract the hour

LAST_DAY(v4.1.1)

Return the last day of the month for


the argument

LOCALTIME(), LOCALTIME

Synonym for NOW()

LOCALTIMESTAMP,
LOCALTIMESTAMP()(v4.0.6)

Synonym for NOW()

MAKEDATE()(v4.1.1)

Create a date from the year and day of


year

MAKETIME(v4.1.1)

MAKETIME()

MICROSECOND()(v4.1.1)

Return
the
argument

MINUTE()

Return the minute from the argument

MONTH()

Return the month from the date


passed

MONTHNAME()(v4.1.21)

Return the name of the month

NOW()

Return the current date and time

PERIOD_ADD()

Add a period to a year-month

PERIOD_DIFF()

Return the number of months between


periods

QUARTER()

Return the
argument

SEC_TO_TIME()

Converts
format

SECOND()

Return the second (0-59)

STR_TO_DATE()(v4.1.1)

Convert a string to a date

SUBDATE()

A synonym for DATE_SUB() when


invoked with three arguments

SUBTIME()(v4.1.1)

Subtract times

Sikkim Manipal University

microseconds

quarter

seconds

from
to

from

date

'HH:MM:SS'

Page No. 178

RDBMS with MySQL

Unit 7

SYSDATE()

Return the time at which the function


executes

TIME_FORMAT()

Format as time

TIME_TO_SEC()

Return the argument converted to


seconds

TIME()(v4.1.1)

Extract the time


expression passed

TIMEDIFF()(v4.1.1)

Subtract time

TIMESTAMP()(v4.1.1)

With a single argument, this function


returns
the
date
or
datetime
expression; with two arguments, the
sum of the arguments

TIMESTAMPADD()(v5.0.0)

Add an interval
expression

TIMESTAMPDIFF()(v5.0.0)

Subtract an interval from a datetime


expression

TO_DAYS()

Return the date argument converted to


days

UNIX_TIMESTAMP()

Return a UNIX timestamp

UTC_DATE()(v4.1.1)

Return the current UTC date

UTC_TIME()(v4.1.1)

Return the current UTC time

UTC_TIMESTAMP()(v4.1.1)

Return the current UTC date and time

WEEK()

Return the week number

WEEKDAY()

Return the weekday index

WEEKOFYEAR()(v4.1.1)

Return the calendar week of the date


(0-53)

YEAR()

Return the year

YEARWEEK()

Return the year and week

portion

to

of

the

datetime

Example: This uses date functions. The following query selects all rows
with a date_col value from within the last 30 days:
mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;

The query also selects rows with dates that lie in the future.
Functions that expect date values usually accept datetime values and ignore
the time part. Functions that expect time values usually accept datetime
values and ignore the date part.
Sikkim Manipal University

Page No. 179

RDBMS with MySQL

Unit 7

Some date functions can be used with zero dates or incomplete dates
such as '2001-11-00', whereas others cannot. Functions that extract parts of
dates typically work with incomplete dates and thus can return 0 when you
might otherwise expect a non-zero value. For example:
mysql> SELECT DAYOFMONTH('2001-11-00'), MONTH('2005-00-00');
-> 0, 0

Other functions expect complete dates and return NULL for incomplete
dates. These include functions that perform date arithmetic or that map
parts of dates to names.
Example:
mysql> SELECT DATE_ADD('2006-05-00',INTERVAL 1 DAY);
-> NULL
mysql> SELECT DAYNAME('2006-05-00');
-> NULL

Self Assessment Questions


9. The ______ function is used to create a date from the year and day of
year.

7.7 Summary
This topic covered various operators and functions used in SQL statements
as well as stored procedures and functions:
1. Operators: Operators are the objects used inside a query or a subquery
for performing various operations like precedence overriding,
comparisons, Logical operations and so on.
2. Control Flow Functions: These are the functions that control the flow
of operations in a procedure or a function within programs. They include
functions like CASE, IFELSE structures and so on.
3. String Functions: These are the functions that are used to manipulate
individual characters or a set of characters clubbed together as strings.
They include functions like ASCII, BIT, Length, Insert, and so on.
4. Numeric Functions: These are the functions that deal with arithmetic
operations like additions, subtractions, and so on. They even cover the
trigonometric and statistical functions.

Sikkim Manipal University

Page No. 180

RDBMS with MySQL

Unit 7

5. Date and Time Functions: This topic discusses various functions that
can be used to manipulate the dates. These functions include date
arithmetic, manipulation and formatting.

7.8 Terminal Questions


1. Define the following terms:
a. Parentheses
b. Comparison Operators
c. Logical Operators
2. Discuss any five string functions

7.9 Answers
Self Assessment Questions
1. parentheses
2. inclusive
3. numeric
4. 100
5. NULL
6. 'h'
7. 1
8. NULL
9. MAKEDATE
Terminal Questions
1. (Refer Section 7.2)
a. Parentheses: It is used to force the order of evaluation in an
expression.
b. Comparison Operators: These operators allow you to compare the
left side of an expression with its right side. The result of such a
comparison operator is always 1(TRUE) or 0 (FALSE) or NULL
(cannot be determined).
c. MySQL uses four logical operators, which makes it possible to test
the logical validity of one or more expressions (or sets of
expressions). The result of an operation involving these operators is
always 1(true), 0 (false), or NULL (could not be determined).

Sikkim Manipal University

Page No. 181

RDBMS with MySQL

Unit 7

The table below lists the logical operators:


Operator
NOT aka !
AND aka &&
OR aka ||
XOR

Function or Meaning
Logical NOT
Logical AND
Logical OR
Logical XOR (Exclusive OR)

2. (Refer Section 7.4)


Function Name
CONCAT(str1,str2,...)

INSERT(str,pos,len,newstr)

LOWER(str)

LTRIM(str)

SUBSTRING(str,pos)
SUBSTRING(str FROM
pos)
SUBSTRING(str,pos,len)
SUBSTRING(str FROM pos
FOR len)

Sikkim Manipal University

Description

Example

Returns the string that


results from
concatenating the
arguments. Returns
NULL if any argument is
NULL. May have one or
more arguments. A
numeric argument is
converted to its
equivalent string form.
Returns the string str,
with the substring
beginning at position
pos and len characters
long replaced by the
string newstr.
Returns the string str
with all characters
changed to lowercase
according to the current
character set mapping
(the default is ISO-88591 Latin1).
Returns the string str
with leading space
characters removed.

1. SELECT
CONCAT('My', 'S', 'QL');
-> 'MySQL'

The forms without a len


argument return a
substring from string str
starting at position pos.
The forms with a len
argument return a
substring len characters
long from string str,
starting at position pos.
The forms that use
FROM are standard
SQL syntax.

2. SELECT
CONCAT('My', NULL,
'QL');
-> NULL
SELECT
INSERT('Quadratic', 3,
4, 'What');
-> 'QuWhattic'

SELECT
LOWER('QUADRATICA
LLY');
-> 'quadratically'

SELECT LTRIM('
barbar');
-> 'barbar'
1. SELECT
SUBSTRING('Quadratic
ally',5);
-> 'ratically'
2. SELECT
SUBSTRING('foobarbar'
FROM 4);
-> 'barbar'
3. SELECT
SUBSTRING('Quadratic
ally',5,6);
-> 'ratica'

Page No. 182

RDBMS with MySQL

Unit 8
8.1
8.2

8.3
8.4

8.5

8.6
8.7
8.8

Unit 8

Advanced Functions

Introduction
Objectives
Full-text Search Functions
Boolean Full-text Searches
Full-text Searches with Query Expansion
Full-text Restrictions
Fine-tuning MySQL Full-text Search
Full-text Search TODO
Cast Functions
Other Functions
Bit Functions
Encryption Functions
Information Functions
Functions and Modifiers for Use with GROUP BY Clauses
GROUP BY (Aggregate) Functions
GROUP BY Modifiers
Summary
Terminal Questions
Answers

8.1 Introduction
This unit is an extension to the previous unit, where in different operators
and functions were discussed in detail. This unit exclusively discusses the
text based search functions. It also describes the Cast, Bit, Encryption and
Information functions. It also discusses in detail the set of functions and
modifiers that can be used in conjunction with GROUP BY modifier with
appropriate examples.

Objectives
After studying this unit, you should be able to:
explain and apply Full text search functions
describe various cast functions
discuss the importance of Bit, Encryption and Information functions
discuss the functions and modifiers for use with Group BY clauses

Sikkim Manipal University

Page No. 183

RDBMS with MySQL

Unit 8

8.2 Full-Text Search Functions


MATCH (col1,col2,...) AGAINST (expr [search_modifier])
search_modifier:
{
IN BOOLEAN MODE
| IN NATURAL LANGUAGE MODE
| IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
| WITH QUERY EXPANSION
}

MySQL has support for full-text indexing and searching:


A full-text index in MySQL is an index of type FULLTEXT.
Full-text indexes can be used only with MyISAM tables, and can be
created only for CHAR, VARCHAR, or TEXT columns.
A FULLTEXT index definition can be given in the CREATE TABLE
statement when a table is created, or added later using ALTER TABLE
or CREATE INDEX.
For large data sets, it is much faster to load your data into a table that
has no FULLTEXT index and then create the index after that, than to
load data into a table that has an existing FULLTEXT index.
Full-text searching is performed using MATCH() ... AGAINST syntax.
MATCH() takes a comma-separated list that names the columns to be
searched. AGAINST takes a string to search for, and an optional modifier
that indicates what type of search to perform. The search string must be a
literal string, not a variable or a column name. There are three types of fulltext searches:
A boolean search interprets the search string using the rules of a special
query language. The string contains the words to search for. It can also
contain operators that specify requirements such that a word must be
present or absent in matching rows, or that it should be weighted higher
or lower than usual. Common words such as some or then are
stopwords and do not match if present in the search string. The IN
BOOLEAN MODE modifier specifies a boolean search.
A natural language search interprets the search string as a phrase in
natural human language (a phrase in free text). There are no special
operators. The stopword list applies. In addition, words that are present
in 50% or more of the rows are considered common and do not match.
Sikkim Manipal University

Page No. 184

RDBMS with MySQL

Unit 8

Full-text searches are natural language searches if the IN NATURAL


LANGUAGE MODE modifier is given or if no modifier is given.
A query expansion search is a modification of a natural language
search. The search string is used to perform a natural language search.
Then words from the most relevant rows returned by the search are
added to the search string and the search is done again. The query
returns the rows from the second search. The IN NATURAL
LANGUAGE MODE WITH QUERY EXPANSION or WITH QUERY
EXPANSION modifier specifies a query expansion search.
The IN NATURAL LANGUAGE MODE and IN NATURAL LANGUAGE
MODE WITH QUERY EXPANSION modifiers were added in MySQL 5.1.7.
8.2.1 Boolean Full-text Searches
MySQL can perform boolean full-text searches using the IN BOOLEAN
MODE modifier:
mysql> SELECT * FROM articles WHERE MATCH (title,body)
-> AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);
+------+-----------------------+-----------------------------------------------------------------------+
| id |
title
|
body
|
+------+-----------------------+------------------------------------------------------------------------+
| 1 | MySQL Tutorial
| DBMS stands for DataBase ...
|
| 2 | How To Use MySQL Well | After you went through a ...
|
| 3 | Optimizing MySQL
| In this tutorial we will show ...
|
| 4 | 1001 MySQL Tricks
| 1. Never run mysqld as root. 2. ...
|
| 6 | MySQL Security
| When configured properly, MySQL ... |
+----+-----------------------+---------------------------------------------------------------------------+

The + and - operators indicate that a word is required to be present or


absent, respectively, for a match to occur. Thus, this query retrieves all the
rows that contain the word MySQL but that do not contain the word
YourSQL.
Note: In implementing this feature, MySQL uses what is sometimes referred
to as implied Boolean logic, in which:
+ stands for AND
stands for NOT
[no operator] implies OR
Sikkim Manipal University

Page No. 185

RDBMS with MySQL

Unit 8

Boolean full-text searches have these characteristics:


They do not use the 50% threshold.
They do not automatically sort rows in order of decreasing
relevance. You can see this from the preceding query result: The
row with the highest relevance is the one that contains MySQL
twice, but it is listed last, not first.
They can work even without a FULLTEXT index, although a search
executed in this fashion would be quite slow.
The minimum and maximum word length full-text parameters apply.
The stop word list applies.
The boolean full-text search capability supports the following operators:
+: A leading plus sign indicates that this word must be present in
each row that is returned.
-: A leading minus sign indicates that this word must not be present
in any of the rows that are returned.
Note: The - operator acts only to exclude rows that are otherwise
matched by other search terms. Thus, a boolean-mode search that
contains only terms preceded by - returns an empty result. It does
not return all rows except those containing any of the excluded
terms.
(no operator): By default (when neither + nor - is specified) the word
is optional, but the rows that contain it are rated higher. This mimics
the behavior of MATCH() ... AGAINST() without the IN BOOLEAN
MODE modifier.
> <: These two operators are used to change a word's contribution to
the relevance value that is assigned to a row. The > operator
increases the contribution and the < operator decreases it.
( ): Parentheses group words into subexpressions. Parenthesized
groups can be nested.
~: A leading tilde acts as a negation operator, causing the word's
contribution to the row's relevance to be negative. This is useful for
marking noise words. A row containing such a word is rated lower
than others, but is not excluded altogether, as it would be with the operator.
*: The asterisk serves as the truncation (or wildcard) operator. Unlike
the other operators, it should be appended to the word to be
Sikkim Manipal University

Page No. 186

RDBMS with MySQL

Unit 8

affected. Words match if they begin with the word preceding the *
operator.
If a stopword or too-short word is specified with the truncation
operator, it will not be stripped from a boolean query. For example, a
search for '+word +stopword*' will likely return fewer rows than a
search for '+word +stopword' because the former query remains as
is and requires stopword* to be present in a document. The latter
query is transformed to +word.
": A phrase that is enclosed within double quote (") characters
matches only rows that contain the phrase literally, as it was typed.
The full-text engine splits the phrase into words, performs a search
in the FULLTEXT index for the words. Non-word characters need not
be matched exactly: Phrase searching requires only that matches
contain exactly the same words as the phrase and in the same
order. For example, "test phrase" matches "test, phrase".
If the phrase contains no words that are in the index, the result is
empty. For example, if all words are either stopwords or shorter than
the minimum length of indexed words, the result is empty.
8.2.2 Full-text Searches with Query Expansion
Full-text search supports query expansion (and in particular, its variant
blind query expansion). This is generally useful when a search phrase is
too short, which often means that the user is relying on implied knowledge
that the full-text search engine lacks. For example, a user searching for
database may really mean that MySQL, Oracle, DB2, and RDBMS
all are phrases that should match databases and should be returned, too.
This is implied knowledge.
Blind query expansion (also known as automatic relevance feedback) is
enabled by adding WITH QUERY EXPANSION or IN NATURAL
LANGUAGE MODE WITH QUERY EXPANSION following the search
phrase. It works by performing the search twice, where the search phrase
for the second search is the original search phrase concatenated with the
few most highly relevant documents from the first search. Thus, if one of
these documents contains the word databases and the word MySQL, the
second search finds the documents that contain the word MySQL even if
they do not contain the word database. The following example shows this
difference:
Sikkim Manipal University

Page No. 187

RDBMS with MySQL

Unit 8

mysql> SELECT * FROM articles


-> WHERE MATCH (title,body)
-> AGAINST ('database' IN NATURAL LANGUAGE MODE);
+----+-----------------------------+------------------------------------------------------+
| id | title
| body
|
+----+-----------------------------+------------------------------------------------------+
| 5 | MySQL vs. YourSQL | In the following database comparison ... |
| 1 | MySQL Tutorial
| DBMS stands for DataBase ...
|
+----+-------------------+------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM articles
-> WHERE MATCH (title,body)
-> AGAINST ('database' WITH QUERY EXPANSION);
+----+----------------------------+----------------------------------------------------------+
| id | title
| body
|
+----+----------------------------+----------------------------------------------------------+
| 1 | MySQL Tutorial
| DBMS stands for DataBase ...
|
| 5 | MySQL vs. YourSQL | In the following database comparison ... |
| 3 | Optimizing MySQL | In this tutorial we will show ...
|
+----+-------------------+-------------------------------------------------------------------+
3 rows in set (0.00 sec)

Note: Because blind query expansion tends to increase noise significantly


by returning non-relevant documents, it is meaningful to use only when a
search phrase is rather short.
8.2.3 Full-text Restrictions
Full-text searches are supported for MyISAM tables only.
Full-text searches can be used with most multi-byte character sets. The
exception is that for Unicode, the utf8 character set can be used, but not
the ucs2 character set. However, although FULLTEXT indexes on ucs2
columns cannot be used, you can perform IN BOOLEAN MODE
searches on a ucs2 column that has no such index.
Ideographic languages such as Chinese and Japanese do not have
word delimiters. Therefore, the FULLTEXT parser cannot determine
where words begin and end in these and other such languages.
Although the use of multiple character sets within a single table is
supported, all columns in a FULLTEXT index must use the same
character set and collation.
The MATCH() column list must match exactly the column list in some
FULLTEXT index definition for the table, unless this MATCH() is IN

Sikkim Manipal University

Page No. 188

RDBMS with MySQL

Unit 8

BOOLEAN MODE. Boolean-mode searches can be done on nonindexed columns, although they are likely to be slow.
The argument to AGAINST() must be a constant string.
8.2.4 Fine - tuning MySQL Full-text Search
MySQL's full-text search capability has few user-tunable parameters. You
can exert more control over full-text searching behavior if you have a
MySQL source distribution because some changes require source code
modifications..
Note that full-text search is carefully tuned for the most effectiveness.
Modifying the default behavior in most cases can actually decrease
effectiveness. Do not alter the MySQL sources unless you know what you
are doing.
Most full-text variables described in this section must be set at server
startup time. A server restart is required to change them; they cannot be
modified while the server is running.
Some variable changes require that you rebuild the FULLTEXT indexes in
your tables. Instructions for doing this are given at the end of this section.
The minimum and maximum lengths of words to be indexed are
defined by the ft_min_word_len and ft_max_word_len system
variables. The default minimum value is four characters; the default
maximum is version dependent. If you change either value, you must
rebuild your FULLTEXT indexes. For example, if you want threecharacter words to be searchable, you can set the ft_min_word_len
variable by putting the following lines in an option file:
[mysqld]
ft_min_word_len=3

Then you must restart the server and rebuild your FULLTEXT indexes. Note
particularly the remarks regarding myisamchk in the instructions following
this list.
To override the default stopword list, set the ft_stopword_file system
variable. The variable value should be the pathname of the file
containing the stopword list, or the empty string to disable stopword
filtering. After changing the value of this variable or the contents of

Sikkim Manipal University

Page No. 189

RDBMS with MySQL

Unit 8

the stopword file, restart the server and rebuild your FULLTEXT
indexes.

The stopword list is free-form. That is, you may use any nonalphanumeric character such as newline, space, or comma to
separate stopwords. Exceptions are the underscore character (_)
and a single apostrophe (') which are treated as part of a word. The
character set of the stopword list is the server's default character set
The 50% threshold for natural language searches is determined by
the particular weighting scheme chosen. To disable it, look for the
following line in storage/myisam/ftdefs.h:
#define GWS_IN_USE
GWS_PROB

Change that line to this:


#define GWS_IN_USE
GWS_FREQ

Then recompile MySQL. There is no need to rebuild the indexes in


this case.
Note: By making this change, you severely decrease MySQL's
ability to provide adequate relevance values for the MATCH()
function. If you really need to search for such common words, it
would be better to search using IN BOOLEAN MODE instead, which
does not observe the 50% threshold.
8.2.5 Full-text Search TODO
Improved performance for all FULLTEXT operations.
Proximity operators
Support for always-index words. These could be any strings the user
wants to treat as words, such as C++, AS/400, or TCP/IP.
Support for full-text search in MERGE tables.
Support for UCS-2.
Make the stopword list dependent on the language of the dataset.
Stemming (dependent on the language of the dataset).
Generic user-suppliable UDF preparser.

Sikkim Manipal University

Page No. 190

RDBMS with MySQL

Unit 8

Make the model more flexible (by adding some adjustable parameters to
FULLTEXT in CREATE TABLE and ALTER TABLE statements).

Self Assessment Questions


1. A ______ index definition can be given in the CREATE TABLE
statement when a table is created, or added later using ALTER TABLE
or CREATE INDEX.

8.3 Cast Functions


CAST(expr AS type)
CONVERT(expr,type)
CONVERT(expr USING transcoding_name)
The CAST() and CONVERT() functions may be used to take a value of one
type and produce a value of another type.
The type value can be one of the following:
BINARY
CHAR
DATE
DATETIME
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
CAST() and CONVERT() are available as of MySQL 4.0.2. The CHAR
conversion type is available as of 4.0.6. The USING form of CONVERT() is
available as of 4.1.0.
CAST() and CONVERT(... USING ...) are standard SQL syntax. The nonUSING form of CONVERT() is ODBC syntax.
CONVERT() with USING is used to convert data between different character
sets. In MySQL, transcoding names are the same as the corresponding
character set names. For example, this statement converts the string 'abc' in
the servers default character set to the corresponding string in the utf8
character set:
The cast functions are useful when you want to create a column with a
specific type in a
CREATE ... SELECT statement:
Sikkim Manipal University

Page No. 191

RDBMS with MySQL

Unit 8

CREATE TABLE new_table SELECT CAST('2000-01-01' AS DATE);

The functions also can be useful for sorting ENUM columns in lexical order.
Normally sorting of ENUM columns occurs using the internal numeric
values. Casting the values to CHAR results in a lexical sort:
SELECT enum_col FROM tbl_name ORDER BY CAST(enum_col AS CHAR);

CAST(str AS BINARY) is the same thing as BINARY str. CAST(expr AS


CHAR) treats the expression as a string with the default character set.
Note: You should not use CAST() to extract data in different formats but
instead use string functions like LEFT() or EXTRACT().
To cast a string to a numeric value, you dont normally have to do anything;
just use the string value as it were a number:

If you use a number in string context, the number automatically will be


converted to a BINARY string.

Self Assessment Questions


2. The ____ and ____ functions may be used to take a value of one type
and produce a value of another type.

Sikkim Manipal University

Page No. 192

RDBMS with MySQL

Unit 8

8.4 Other Functions


8.4.1 Bit Functions
MySQL uses BIGINT (64-bit) arithmetic for bit operations, so these
operators have a maximum range of 64 bits.
| Bitwise OR:

The result is an unsigned 64-bit integer.


& Bitwise AND:

The result is an unsigned 64-bit integer.


^ Bitwise XOR:

Sikkim Manipal University

Page No. 193

RDBMS with MySQL

Unit 8

The result is an unsigned 64-bit integer.


XOR was added in MySQL 4.0.2.
<<: Shifts a longlong (BIGINT) number to the left.
mysql> SELECT 1 << 2;
-> 4

The result is an unsigned 64-bit integer.


>>: Shifts a longlong (BIGINT) number to the right.
mysql> SELECT 4 >> 2;
-> 1

The result is an unsigned 64-bit integer.


~: Invert all bits.
mysql> SELECT 5 & ~1;
-> 4

The result is an unsigned 64-bit integer.


BIT_COUNT(N): Returns the number of bits that are set in the argument N.
mysql> SELECT BIT_COUNT(29);
-> 4

8.4.2 Encryption Functions


- AES_ENCRYPT(str,key_str)
- AES_DECRYPT(str,key_str)
These functions allow encryption/decryption of data using the official AES
(Advanced Encryption Standard) algorithm, previously known as Rijndael.
Encoding with a 128-bit key length is used, but you can extend it up to 256
bits by modifying the source. We chose 128 bits because it is much faster
and it is usually secure enough.
The input arguments may be any length. If either argument is NULL, the
result of this function is also NULL.

Sikkim Manipal University

Page No. 194

RDBMS with MySQL

Unit 8

Because AES is a block-level algorithm, padding is used to encode uneven


length strings and so the result string length may be calculated as
16*(trunc(string_length/16)+1).
If AES_DECRYPT() detects invalid data or incorrect padding, it returns
NULL.
However, it is possible for AES_DECRYPT() to return a non-NULL value
(possibly garbage) if the input data or the key is invalid.
You can use the AES functions to store data in an encrypted form by
modifying your queries:

You can get even more security by not transferring the key over the
connection for each query, which can be accomplished by storing it in a
server-side variable at connection time.
Example:
SELECT @password:='my password';
INSERT INTO t VALUES (1,AES_ENCRYPT('text',@password));

AES_ENCRYPT() and AES_DECRYPT() were added in MySQL 4.0.2, and


can be considered the most cryptographically secure encryption functions
currently available in MySQL.
DECODE(crypt_str,pass_str): Descrypts the encrypted string crypt_str
using pass_str as the password. crypt_str should be a string returned from
ENCODE().
Sikkim Manipal University

Page No. 195

RDBMS with MySQL

Unit 8

ENCODE(str,pass_str): Encrypt str using pass_str as the password. To


decrypt the result, use DECODE(). The result is a binary string of the same
length as string. If you want to save it in a column, use a BLOB column type.
DES_DECRYPT(str_to_decrypt [, key_str]): Decrypts a string encrypted
with DES_ENCRYPT(). On error, this function returns NULL. Note that this
function works only if MySQL has been configured with SSL support.
If no key_str argument is given, DES_DECRYPT() examines the first byte of
the encrypted string to determine the DES key number that was used to
encrypt 606 MySQL Technical Reference for Version 5.0.0-alpha the
original string, and then reads the key from the DES key file to decrypt the
message. For this to work, the user must have the SUPER privilege. The
key file can be specified with the --des-key-file server option.
If you pass this function a key_str argument, that string is used as the key
for decrypting the message.
If the str_to_decrypt argument doesnt look like an encrypted string, MySQL
will return the given str_to_decrypt.
DES_ENCRYPT(str_to_encrypt [, (key_num | key_str) ] )

Encrypts the string with the given key using the Triple-DES algorithm. On
error, this function returns NULL.
Note that this function works only if MySQL has been configured with SSL
support.
The encryption key to use is chosen based on the second argument to
DES_ENCRYPT(), if one was given:
SNo

Argument

Description

1
2

No argument
key_num

key_str

The first key from the DES key file is used.


The given key number (0-9) from the DES key file
is used.
The given key string is used to encrypt
str_to_encrypt.

The key file can be specified with the des-key-file server option. The return
string is a binary string where the first character is CHAR(128 | key_num).

Sikkim Manipal University

Page No. 196

RDBMS with MySQL

Unit 8

The 128 is added to make it easier to recognize an encrypted key. If you


use a string key, key_num will be 127. The string length for the result will be
new_len = orig_len + (8-(orig_len % 8))+1.
The DES key file has the following format:
key_num des_key_str
key_num des_key_str

Each key_num must be a number in the range from 0 to 9. Lines in the file
may be in any order. des_key_str is the string that will be used to encrypt
the message. Between the number and the key there should be at least one
space.
The first key is the default key that is used if you dont specify any key
argument to DES_ENCRYPT()
You can tell MySQL to read new key values from the key file with the
FLUSH DES_KEY_FILE command. This requires the RELOAD privilege.
One benefit of having a set of default keys is that it gives applications a way
to check for the existence of encrypted column values, without giving the
end user the right to decrypt those values.
ENCRYPT(str[,salt])
Encrypt str using the Unix crypt() system call. The salt argument should be a
string with two characters. (As of MySQL 3.22.16, salt may be longer than
two characters.)
mysql> SELECT ENCRYPT('hello');
-> 'VxuFAJXVARROc'

ENCRYPT() ignores all but the first 8 characters of str, at least on some
systems. This behavior is determined by the implementation of the
underlying crypt() system call. If crypt() is not available on your system,
ENCRYPT() always returns NULL. Because of this, we recommend that you
use MD5() or SHA1() instead, because these two functions exist on all
platforms.

Sikkim Manipal University

Page No. 197

RDBMS with MySQL

Unit 8

MD5(str)
Calculates an MD5 128-bit checksum for the string. The value is returned as
a string of 32 hex digits, or NULL if the argument was NULL. The return
value can, for example, be used as a hash key.
mysql> SELECT MD5('testing');
-> 'ae2b1fca515949e5d54fb22b8ed95575'

This is the "RSA Data Security, Inc. MD5 Message-Digest Algorithm".


PASSWORD(str)
OLD_PASSWORD(str)
Calculates and returns a password string from the plaintext password str, or
NULL if the argument was NULL. This is the function that is used for
encrypting MySQL passwords for storage in the Password column of the
user grant table.
mysql> SELECT PASSWORD('badpwd');
-> '7f84554057dd964b'

PASSWORD() encryption is one-way (not reversible).


PASSWORD() does not perform password encryption in the same way that
Unix passwords are encrypted.
Note: The PASSWORD() function is used by the authentication system in
MySQL Server, you should not use it in your own applications.
For that purpose, use MD5() or SHA1() instead. Also see RFC 2195 for
more information about handling passwords and authentication securely in
your application.
SHA1(str)
SHA(str) Calculates an SHA1 160-bit checksum for the string, as described
in RFC 3174 (Secure Hash Algorithm). The value is returned as a string of
40 hex digits, or NULL if the argument was NULL. One of the possible uses
for this function is as a hash key. You can also use it as cryptographically
safe function for storing passwords.
mysql> SELECT SHA1('abc');
-> 'a9993e364706816aba3e25717850c26c9cd0d89d'

Sikkim Manipal University

Page No. 198

RDBMS with MySQL

Unit 8

SHA1() was added in MySQL 4.0.2, and can be considered a


cryptographically more secure equivalent of MD5(). SHA() is synonym for
SHA1().
8.4.3 Information Functions
BENCHMARK(count,expr)
The BENCHMARK() function executes the expression expr repeatedly count
times. It may be used to time how fast MySQL processes the expression.
The result value is always 0. The intended use is from within the mysql
client, which reports query execution times:

The time reported is elapsed time on the client end, not CPU time on the
server end. It may be advisable to execute BENCHMARK() several times,
and interpret the result with regard to how heavily loaded the server
machine is.
CHARSET(str)
Returns the character set of the string argument.

CHARSET() was added in MySQL 4.1.0.


COERCIBILITY(str)
Returns the collation coercibility value of the string argument.

Sikkim Manipal University

Page No. 199

RDBMS with MySQL

Unit 8

The return values have the following meanings:


Coercibility
Meaning
0
Explicit collation
1
No collation
2
Implicit collation
3
Coercible
Lower values have higher precedence.
COERCIBILITY() was added in MySQL 4.1.1.
COLLATION(str): Returns the collation for the character set of the string
argument.

COLLATION() was added in MySQL 4.1.0.


Sikkim Manipal University

Page No. 200

RDBMS with MySQL

Unit 8

CONNECTION_ID()
Returns the connection ID (thread ID) for the connection. Every connection
has its own unique ID.

CURRENT_USER()
Returns the username and hostname combination that the current session
was authenticated as. This value corresponds to the account that is used for
assessing your access privileges. It may be different than the value of
USER().

Sikkim Manipal University

Page No. 201

RDBMS with MySQL

Unit 8

DATABASE()
Returns the default (current) database name.

If there is no default database, DATABASE() returns NULL as of MySQL


4.1.1, and the empty string before that.
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();

The second SELECT will return a number indicating how many rows the first
SELECT would have returned had it been written without the LIMIT clause.
(If the preceding SELECT statement does not include the
SQL_CALC_FOUND_ROWS option, then FOUND_ROWS() may return a
different result when LIMIT is used than when it is not.)
Note: If you are using SELECT SQL_CALC_FOUND_ROWS, MySQL must
calculate how many rows are in the full result set. However, this is faster
than running the query again without LIMIT, because the result set need not
be sent to the client.

Sikkim Manipal University

Page No. 202

RDBMS with MySQL

Unit 8

SQL_CALC_FOUND_ROWS and FOUND_ROWS() can be useful in


situations when you want to restrict the number of rows that a query returns,
but also determine the number of rows in the full result set without running
the query again. An example is a Web script that presents a paged display
containing links to the pages that show other sections of a search result.
Using FOUND_ROWS() allows you to determine how many other pages are
needed for the rest of the result.
LAST_INSERT_ID()
LAST_INSERT_ID(expr)
Returns the last automatically generated value that was inserted into an
AUTO_INCREMENT column.

The last ID that was generated is maintained in the server on a perconnection basis. This means the value the function returns to a given client
is the most recent AUTO_INCREMENT value generated by that client. The
value cannot be affected by other clients, even if they generate
AUTO_INCREMENT values of their own. This behavior ensures that you
can retrieve your own ID without concern for the activity of other clients, and
without the need for locks or transactions.
The value of LAST_INSERT_ID() is not changed if you update the
AUTO_INCREMENT column of a row with a non-magic value (that is, a
value that is not NULL and not 0). If you insert many rows at the same time
with an insert statement, LAST_INSERT_ID() returns the value for the first
inserted row. The reason for this is to make it possible to easily reproduce
the same INSERT statement against some other server.
If expr is given as an argument to LAST_INSERT_ID(), then the value of the
argument is returned by the function, and is set as the next value to be
returned by LAST_INSERT_ID(). This can be used to simulate sequences:
Sikkim Manipal University

Page No. 203

RDBMS with MySQL

Unit 8

The UPDATE statement increments the sequence counter and causes the
next call to LAST_INSERT_ID() to return the updated value. The SELECT
statement retrieves that value.
VERSION()
Returns a string that indicates the MySQL server version.

Note: If your version string ends with -log this means that logging is enabled
8.4.4 Miscellaneous Functions
FORMAT(X,D)
Formats the number X to a format like '#,###,###.##', rounded to D
decimals, and returns the result as a string. If D is 0, the result will have no
decimal point or fractional part.

Sikkim Manipal University

Page No. 204

RDBMS with MySQL

Unit 8

Example 1:

Example 2:

Example 3:

Self Assessment Questions


3. The output of the following Bitwise AND operation 29 & 15 is _____.
4. It is possible for AES_DECRYPT() to return a _______ (possibly
garbage) if the input data or the key is invalid.

8.5 Functions and Modifiers for Use with GROUP BY Clauses


8.5.1 GROUP BY (Aggregate) Functions
This section describes group (aggregate) functions that operate on sets of
values. Unless otherwise stated, group functions ignore NULL values.

Sikkim Manipal University

Page No. 205

RDBMS with MySQL

Unit 8

If you use a group function in a statement containing no GROUP BY clause,


it is equivalent to grouping on all rows.
The SUM() and AVG() aggregate functions do not work with temporal
values. (They convert the values to numbers, which loses the part after the
first non-numeric character.)
To work around this problem, you can convert to numeric units, perform the
aggregate operation, and convert back to a temporal value.
Examples:
1. SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_col))) FROM tbl_name;
2. SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROM tbl_name;

AVG([DISTINCT] expr)
Returns the average value of expr. The DISTINCT option can be used as of
MySQL 5.0.3 to return the average of the distinct values of expr.
AVG() returns NULL if there were no matching rows.
mysql> SELECT student_name, AVG(test_score)
->
FROM student
->
GROUP BY student_name;

BIT_AND(expr)
Returns the bitwise AND of all bits in expr. The calculation is performed with
64-bit (BIGINT) precision.
This function returns 18446744073709551615 if there were no matching
rows. (This is the value of an unsigned BIGINT value with all bits set to 1.)
BIT_OR(expr)
Returns the bitwise OR of all bits in expr. The calculation is performed with
64-bit (BIGINT) precision.
This function returns 0 if there were no matching rows.
BIT_XOR(expr)
Returns the bitwise XOR of all bits in expr. The calculation is performed with
64-bit (BIGINT) precision.
This function returns 0 if there were no matching rows.
Sikkim Manipal University

Page No. 206

RDBMS with MySQL

Unit 8

COUNT(expr)
Returns a count of the number of non-NULL values in the rows retrieved by
a SELECT statement.
Example:
mysql> SELECT student.student_name,COUNT(*)
-> FROM student,course
-> WHERE student.student_id=course.student_id
-> GROUP BY student_name;

COUNT(*) is somewhat different in that it returns a count of the number of


rows retrieved, whether or not they contain NULL values.
COUNT(DISTINCT expr,[expr...])
Returns a count of the number of different non-NULL values.
Example:
mysql> SELECT COUNT (DISTINCT results) FROM student;

GROUP_CONCAT(expr)
This function returns a string result with the concatenated values from a
group.
The full syntax is as follows:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col ...]]
[SEPARATOR str_val])

GROUP_CONCAT() was added in MySQL 4.1.


Example:
mysql> SELECT student_name,
-> GROUP_CONCAT(test_score)
-> FROM student
-> GROUP BY student_name;

OR

Sikkim Manipal University

Page No. 207

RDBMS with MySQL

Unit 8

mysql> SELECT student_name,


-> GROUP_CONCAT(DISTINCT test_score
-> ORDER BY test_score DESC SEPARATOR ' ')
-> FROM student
-> GROUP BY student_name;

MIN(expr)
MAX(expr)
Returns the minimum or maximum value of expr. MIN() and MAX() may
take a string argument; in such cases they return the minimum or
maximum string value.
Example:
mysql> SELECT student_name, MIN(test_score), MAX(test_score)
-> FROM student
-> GROUP BY student_name;

STD(expr)
STDDEV(expr)
Returns the standard deviation of expr (the square root of VARIANCE()).
This is an extension to standard SQL. The STDDEV() form of this
function is provided for Oracle compatibility.

SUM(expr)
Returns the sum of expr. Note that if the return set has no rows, it returns
NULL!
VARIANCE(expr)
Returns the standard variance of expr (considering rows as the whole
population, not as a sample; so it has the number of rows as denominator).
This is an extension to standard SQL, available only in MySQL 4.1 or later.
8.5.2 GROUP BY Modifiers
As of MySQL 4.1.1, the GROUP BY clause allows a WITH ROLLUP
modifier that causes extra rows to be added to the summary output. These
rows represent higher-level (or superaggregate) summary operations.
ROLLUP thus allows you to answer questions at multiple levels of analysis
Sikkim Manipal University

Page No. 208

RDBMS with MySQL

Unit 8

with a single query. It can be used, for example, to provide support for
OLAP (Online Analytical Processing) operations.
As an illustration, suppose that a table named sales has year, country,
product, and profit columns for recording sales profitability:
CREATE TABLE sales
(
year INT NOT NULL,
country VARCHAR(20) NOT NULL,
product VARCHAR(32) NOT NULL,
profit INT
);

The tables contents can be summarized per year with a simple GROUP BY
like this:
mysql> SELECT year, SUM(profit) FROM sales GROUP BY year;
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 | 4525
|
| 2001 | 3010
|

+------+-------------+

This output shows the total profit for each year, but if you also want to
determine the total profit summed over all years, you must add up the
individual values yourself or run an additional query.
Or you can use ROLLUP, which provides both levels of analysis with a
single query. Adding a WITH ROLLUP modifier to the GROUP BY clause
causes the query to produce another row that shows the grand total over all
year values:
mysql> SELECT year, SUM(profit) FROM sales GROUP BY year
WITH ROLLUP;
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 | 4525 |
| 2001 | 3010 |
| NULL | 7535 |
+------+-------------+

Sikkim Manipal University

Page No. 209

RDBMS with MySQL

Unit 8

The grand total super-aggregate line is identified by the value NULL in the
year column.
ROLLUP has a more complex effect when there are multiple GROUP BY
columns. In this case, each time there is a break (change in value) in any
but the last grouping column, the query produces an extra super-aggregate
summary row.
For example, without ROLLUP, a summary on the sales table based on
year, country, and product might look like this:
mysql> SELECT year, country, product, SUM(profit)
-> FROM sales
-> GROUP BY year, country, product;
+------+---------+------------+-------------+
| year | country | product | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | India | Calculator | 150 |
+------+---------+------------+-------------+

The output indicates summary values only at the year/country/product level


of analysis.
When ROLLUP is added, the query produces several extra rows:
mysql> SELECT year, country, product, SUM(profit)
-> FROM sales
-> GROUP BY year, country, product WITH ROLLUP;
+------+---------+------------+-------------+
| year | country | product | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | Finland | NULL | 1600 |
| 2000 | Finland | Computer | 1500 |
+------+---------+------------+-------------+

For this query, adding ROLLUP causes the output to include summary
information at four levels of analysis, not just one. Heres how to interpret
the ROLLUP output:

Sikkim Manipal University

Page No. 210

RDBMS with MySQL

Unit 8

Following each set of product rows for a given year and country, an
extra summary row is produced showing the total for all products.
These rows have the product column set to NULL.
Following each set of rows for a given year, an extra summary row is
produced showing the total for all countries and products. These
rows have the country and products columns set to NULL.
Finally, following all other rows, an extra summary row is produced
showing the grand total for all years, countries, and products. This
row has the year, country, and products columns set to NULL.

Self Assessment Questions


5. The SUM() and AVG() aggregate functions do not work with ____ values.

8.6 Summary
This unit covered various text based search functions, casting functions, bit,
encryption and information functions. It then deals with various functions
and modifiers that can be used along with the GROUP BY clause.
1. Full text Search Functions: These functions are used to perform full
text search or string based functions. These functions include Boolean
searches, query expansion searches, and so on.
2. Cast Functions: The CAST() and CONVERT() functions may be used
to take a value of one type and produce a value of another type. These
functions are mainly used for performing conversions between different
data types.
3. Other Functions: The other functions include Bit, Encryption and
Information functions. These are used to work on Bit manipulations,
Encryption of data, and Information gathering from the data sources.
4. Group by Clauses: The Group By clause is basically used in
conjunction with the Aggregate functions like Sum, Average, and so on.
They may also be used to modify the output of a query depending on the
requirement.

Sikkim Manipal University

Page No. 211

RDBMS with MySQL

Unit 8

8.7 Terminal Questions


1. Describe the operators that support the boolean full-text searches.
2. Explain few Encryption functions.

8.8 Answers
Self Assessment Questions
1. FULLTEXT
2. CAST() and CONVERT()
3. 13
4. Non - NULL value
5. Temporal
Terminal Questions
1. (Refer to Section 8.2)
+: A leading plus sign indicates that this word must be present in each row
that is returned.
-: A leading minus sign indicates that this word must not be present in any of
the rows that are returned.
(no operator): By default (when neither + nor - is specified) the word is
optional, but the rows that contain it are rated higher. This mimics the
behavior of MATCH() ... AGAINST() without the IN BOOLEAN MODE
modifier.
> <: These two operators are used to change a word's contribution to the
relevance value that is assigned to a row. The > operator increases the
contribution and the < operator decreases it.
( ): Parentheses group words into subexpressions. Parenthesized groups
can be nested.
~: A leading tilde acts as a negation operator, causing the word's
contribution to the row's relevance to be negative.
2. (Refer to Section 8.4.2)
- AES_ENCRYPT(str,key_str)
- AES_DECRYPT(str,key_str)
These functions allow encryption/decryption of data using the official AES
(Advanced Encryption Standard) algorithm, previously known as Rijndael.
Encoding with a 128-bit key length is used, but you can extend it up to 256
bits by modifying the source. We chose 128 bits because it is much faster

Sikkim Manipal University

Page No. 212

RDBMS with MySQL

Unit 8

and it is usually secure enough. The input arguments may be any length. If
either argument is NULL, the result of this function is also NULL.
DECODE(crypt_str,pass_str): Descrypts the encrypted string crypt_str
using pass_str as the password. crypt_str should be a string returned from
ENCODE().
ENCODE(str,pass_str): Encrypt str using pass_str as the password. To
decrypt the result, use DECODE(). The result is a binary string of the same
length as string. If you want to save it in a column, use a BLOB column type.
DES_DECRYPT(str_to_decrypt [, key_str]): Decrypts a string encrypted
with DES_ENCRYPT(). On error, this function returns NULL. Note that this
function works only if MySQL has been configured with SSL support.

Sikkim Manipal University

Page No. 213

RDBMS with MySQL

Unit 9

Unit 9

Transaction Management

Structure
9.1
Introduction
Objectives
9.2
START TRANSACTION, COMMIT, and ROLLBACK Syntax
9.3
Statements That Cannot Be Rolled Back
9.4
Statements That Cause an Implicit Commit
9.5
SAVEPOINT and ROLLBACK TO SAVEPOINT Syntax
9.6
LOCK TABLES and UNLOCK TABLES Syntax
9.7
SET TRANSACTION Syntax
9.8
Summary
9.9
Terminal Questions
9.10 Answers

9.1 Introduction
This unit discusses about various transaction processing statements like
Start Transaction, Commit, and Rollback. It describes the statements that
cannot be rolled back after execution of a transaction. It also describes the
statements that cause an implicit commit after execution.
It discusses about the usage of Savepoints within a transaction and their
affect or usage during transaction processing.
Objectives
After studying this unit, you should be able to:
discuss the importance of Start transaction, commit and rollback
statements
describe the statements that cannot rollback
describe the statements that cause an implicit commit with relevant
examples
explain the importance of the statements Savepoint and Rollback to
Savepoint
demonstrate the process involved in Locking and unlocking the tables
with examples
explain the usage of Set Transaction syntax

Sikkim Manipal University

Page No. 214

RDBMS with MySQL

Unit 9

9.2 START TRANSACTION, COMMIT, and ROLLBACK Syntax


MySQL supports local transactions (within a given client session) through
statements such as SET autocommit, START TRANSACTION, COMMIT,
and ROLLBACK. XA transaction support enables MySQL to participate in
distributed transactions as well.
START TRANSACTION [WITH CONSISTENT SNAPSHOT] | BEGIN
[WORK]
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
SET autocommit = {0 | 1}

The START TRANSACTION or BEGIN statement begins a new


transaction.
COMMIT commits the current transaction, making its changes
permanent.
ROLLBACK rolls back the current transaction, canceling its changes.
The SET autocommit statement disables or enables the default
autocommit mode for the current session.
The optional WORK keyword is supported for COMMIT and
ROLLBACK, as are the CHAIN and RELEASE clauses.
CHAIN and RELEASE can be used for additional control over
transaction completion.
The value of the completion_type system variable determines the
default completion behavior.
The AND CHAIN clause causes a new transaction to begin as soon as
the current one ends, and the new transaction has the same isolation
level as the just-terminated transaction.
The RELEASE clause causes the server to disconnect the current client
session after terminating the current transaction. Including the NO
keyword suppresses CHAIN or RELEASE completion, which can be
useful if the completion_type system variable is set to cause chaining or
release completion by default.
By default, MySQL runs with autocommit mode enabled. This means
that as soon as you execute a statement that updates (modifies) a
table, MySQL stores the update on disk to make it permanent.
To disable autocommit mode, use the following statement:
SET autocommit=0;
Sikkim Manipal University

Page No. 215

RDBMS with MySQL

Unit 9

After disabling autocommit mode by setting the AUTOCOMMIT variable to


zero, you must use COMMIT to store your changes to disk or ROLLBACK if
you want to ignore the changes you have made since the beginning of your
transaction. If you want to disable autocommit mode for a single series of
statements, you can use the START TRANSACTION statement:
START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summmary=@A WHERE type=1;
COMMIT;

With START TRANSACTION, autocommit remains disabled until you end


the transaction with COMMIT or ROLLBACK. The autocommit mode then
reverts to its previous state.
BEGIN and BEGIN WORK can be used instead of START TRANSACTION
to initiate a transaction.
Beginning a transaction causes any pending transaction to be committed.
Beginning a transaction also causes table locks acquired with LOCK
TABLES to be released, as though you had executed UNLOCK TABLES.
Beginning a transaction does not release a global read lock acquired with
FLUSH TABLES WITH READ LOCK.
START TRANSACTION was added in MySQL 4.0.11. This is standard SQL
syntax and is the recommended way to start an ad-hoc transaction. BEGIN
and BEGIN WORK are available from MySQL 3.23.17 and 3.23.19,
respectively.
Note that if you are not using transaction-safe tables, any changes are
stored at once, regardless of the status of autocommit mode.
If you issue a ROLLBACK statement after updating a non-transactional table
within a transaction, an ER_WARNING_NOT_COMPLETE_ROLLBACK
warning occurs. Changes to transaction-safe tables will be rolled back, but
not changes to non-transaction-safe tables.
If you are using START TRANSACTION or SET AUTOCOMMIT=0, you
should use the MySQL binary log for backups instead of the older update

Sikkim Manipal University

Page No. 216

RDBMS with MySQL

Unit 9

log. Transactions are stored in the binary log in one chunk, upon COMMIT.
Transactions that are rolled back are not logged.
(Exception: Modifications to non-transactional tables cannot be rolled back.
If a transaction that is rolled back includes modifications to non-transactional
tables, the entire transaction is logged with a ROLLBACK statement at the
end to ensure that modifications to the non-transactional tables are
replicated.)
You can change the isolation level
TRANSACTION ISOLATION LEVEL.

for

transactions

with

SET

Self Assessment Questions


1. With START TRANSACTION, autocommit remains ____ until you end
the transaction with COMMIT or ROLLBACK.

9.3 Statements That Cannot Be Rolled Back


Some statements cannot be rolled back. In general, these include data
definition language (DDL) statements, such as those that create or drop
databases, or those that create, drop, or alter tables.
You should design your transactions not to include such statements. If you
issue a statement that cannot be rolled back early in a transaction, and then
another statement later fails, the full effect of the transaction cannot be
rolled back by issuing a ROLLBACK statement.

9.4 Statements That Cause an Implicit Commit


The following statements implicitly end a transaction (as if you had done a
COMMIT before executing the statement):

ALTER TABLE
BEGIN
CREATE INDEX
DROP DATABASE
DROP INDEX
DROP TABLE
LOAD MASTER DATA
LOCK TABLES
RENAME TABLE

Sikkim Manipal University

Page No. 217

RDBMS with MySQL

Unit 9

SET AUTOCOMMIT=1
START TRANSACTION
TRUNCATE TABLE
UNLOCK TABLES also ends a transaction if any tables currently are locked.
Prior to MySQL 4.0.13, CREATE TABLE ends a transaction if the binary
update log is enabled.
Transactions cannot be nested. This is a consequence of the implicit
COMMIT performed for any current transaction when you issue a START
TRANSACTION statement or one of its synonyms.
ALTER TABLE, CREATE TABLE, and DROP TABLE do not commit a
transaction if the TEMPORARY keyword is used. (This does not apply to
other operations on temporary tables such as CREATE INDEX, which do
cause a commit.).
However, although no implicit commit occurs, neither can the statement be
rolled back. Therefore, use of such statements will violate transaction
atomicity: For example, if you use CREATE TEMPORARY TABLE and then
roll back the transaction, the table remains in existence.
The CREATE TABLE statement in InnoDB is processed as a single
transaction. This means that a ROLLBACK from the user does not undo
CREATE TABLE statements the user made during that transaction.
Beginning with MySQL 5.1.3, ALTER VIEW, CREATE TRIGGER, CREATE
VIEW, DROP TRIGGER, and DROP VIEW cause an implicit commit.
Beginning with MySQL 5.1.15, CREATE TABLE ... SELECT causes an
implicit commit before and after the statement is executed when you are
creating non-temporary tables. (No commit occurs for CREATE
TEMPORARY TABLE ... SELECT.)
This is to prevent an issue during replication where the table could be
created on the master after a rollback, but fail to be recorded in the binary
log, and therefore not replicated to the slave.
Statements that implicitly use or modify tables in the mysql
database: Beginning with MySQL 5.1.3, CREATE USER, DROP USER,
and RENAME USER cause an implicit commit. Beginning with MySQL
Sikkim Manipal University

Page No. 218

RDBMS with MySQL

Unit 9

5.1.23, GRANT, REVOKE, and SET PASSWORD statements cause an


implicit commit.
Transaction-control and locking statements: BEGIN, LOCK TABLES,
SET autocommit = 1 (if the value is not already 1), START
TRANSACTION, UNLOCK TABLES.
UNLOCK TABLES commits a transaction only if any tables currently
have been locked with LOCK TABLES. This does not occur for UNLOCK
TABLES following FLUSH TABLES WITH READ LOCK because the
latter statement does not acquire table-level locks.
Transactions cannot be nested. This is a consequence of the implicit
commit performed for any current transaction when you issue a START
TRANSACTION statement or one of its synonyms.
Statements that cause an implicit commit cannot be used in an XA
transaction while the transaction is in an ACTIVE state.
The BEGIN statement differs from the use of the BEGIN keyword that
starts a BEGIN ... END compound statement. The latter does not cause
an implicit commit.
Data loading statements: LOAD DATA INFILE. Before MySQL 5.1.12,
LOAD DATA INFILE caused an implicit commit for all storage engines.
As of MySQL 5.1.12, it causes an implicit commit only for tables using
the NDB storage engine.
Administrative statements: CACHE INDEX, LOAD INDEX INTO
CACHE. Beginning with MySQL 5.1.10, ANALYZE TABLE, CHECK
TABLE, OPTIMIZE TABLE, and REPAIR TABLE cause an implicit
commit.

Self Assessment Questions


2. The _____ command also ends a transaction if any tables currently are
locked.

9.5 SAVEPOINT and ROLLBACK TO SAVEPOINT Syntax


SAVEPOINT identifier
ROLLBACK TO SAVEPOINT identifier

Sikkim Manipal University

Page No. 219

RDBMS with MySQL

Unit 9

Starting from MySQL 4.0.14 and 4.1.1, InnoDB supports the SQL
statements SAVEPOINT and ROLLBACK TO SAVEPOINT.
The SAVEPOINT statement sets a named transaction savepoint with a
name of identifier. If the current transaction already has a savepoint with the
same name, the old savepoint is deleted and a new one is set.
The ROLLBACK TO SAVEPOINT statement rolls back a transaction to the
named savepoint. Modifications that the current transaction made to rows
after the savepoint was set are undone in the rollback, but InnoDB does not
release the row locks that were stored in memory after the savepoint. (Note
that for a new inserted row, the lock information is carried by the transaction
ID stored in the row; the lock is not separately stored in memory. In this
case, the row lock is released in the undo.) Savepoints that were set at a
later time than the named savepoint are deleted.
If the statement returns the following error, it means that no savepoint with
the specified name exists:
ERROR 1181: Got error 153 during ROLLBACK
All savepoints of the current transaction are deleted if you execute a
COMMIT, or a ROLLBACK that does not name a savepoint.
Self Assessment Questions
3. The ______ statement sets a named transaction savepoint with a name
of identifier.

9.6 LOCK TABLES and UNLOCK TABLES Syntax


LOCK TABLES
tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}
[, tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}] ...
UNLOCK TABLES

LOCK TABLES locks tables for the current thread. UNLOCK TABLES
releases any locks held by the current thread. All tables that are locked by
the current thread are implicitly unlocked when the thread issues another
LOCK TABLES, or when the connection to the server is closed.
Note: LOCK TABLES is not transaction-safe and implicitly commits any
active transactions before attempting to lock the tables.

Sikkim Manipal University

Page No. 220

RDBMS with MySQL

Unit 9

As of MySQL 4.0.2, to use LOCK TABLES you must have the global LOCK
TABLES privilege and a SELECT privilege for the involved tables. In MySQL
3.23, you must have SELECT, INSERT, DELETE, and UPDATE privileges
for the tables.
The main reasons to use LOCK TABLES are for emulating transactions or
to get more speed when updating tables.
If a thread obtains a READ lock on a table, that thread (and all other
threads) can only read from the table. If a thread obtains a WRITE lock on a
table, only the thread holding the lock can read from or write to the table.
Other threads are blocked.
The difference between READ LOCAL and READ is that READ LOCAL
allows non-conflicting INSERT statements to execute while the lock is held.
However, this cant be used if you are going to manipulate the database files
outside MySQL while you hold the lock.
When you use LOCK TABLES, you must lock all tables that you are going to
use in your queries.
If you are using a table multiple times in a query (with aliases), you must get
a lock for each alias. While the locks obtained with a LOCK TABLES
statement are in effect, you cannot access any tables that were not locked
by the statement.
If your queries refer to a table using an alias, then you must lock the table
using that same alias. It will not work to lock the table without specifying the
alias:
mysql> LOCK TABLE t READ;
mysql> SELECT * FROM t AS myalias;
ERROR 1100: Table 'myalias' was not locked with LOCK TABLES

Conversely, if you lock a table using an alias, you must refer to it in your
queries using that alias:
mysql> LOCK TABLE t AS myalias READ;
mysql> SELECT * FROM t;
ERROR 1100: Table 't' was not locked with LOCK TABLES
mysql> SELECT * FROM t AS myalias;

Sikkim Manipal University

Page No. 221

RDBMS with MySQL

Unit 9

WRITE locks normally have higher priority than READ locks, to ensure that
updates are processed as soon as possible. This means that if one thread
obtains a READ lock and then another thread requests a WRITE lock,
subsequent READ lock requests will wait until the WRITE thread has gotten
the lock and released it. You can use LOW_PRIORITY WRITE locks to
allow other threads to obtain READ locks while the thread is waiting for the
WRITE lock.
You should use LOW_PRIORITY WRITE locks only if you are sure that
there will eventually be a time when no threads will have a READ lock.
LOCK TABLES works as follows:
1. Sort all tables to be locked in an internally defined order. From the user
standpoint, this order is undefined.
2. If a table is locked with a read and a write lock, put the write lock before
the read lock.
3. Lock one table at a time until the thread gets all locks.

If you are going to run many operations on a set of tables, its much
faster to lock the tables you are going to use. The downside is that no
thread can update a READ-locked table (including the one holding the
lock) and no thread can read a WRITE-locked table other than the one
holding the lock.
The reason some operations are faster under LOCK TABLES is that
MySQL will not flush the key cache for the locked tables until UNLOCK
TABLES is called. Normally, the key cache is flushed after each SQL
statement. Locking MyISAM tables speeds up inserting, updating, or
deleting on them.

If you are using a storage engine in MySQL that doesnt support


transactions, you must use LOCK TABLES if you want to ensure that no
other thread comes between a SELECT and an UPDATE. The example
shown here requires LOCK TABLES to execute safely:
mysql> LOCK TABLES trans READ, customer WRITE;
mysql> SELECT SUM(value) FROM trans WHERE customer_id=some_id;
mysql> UPDATE customer
-> SET total_value=sum_from_previous_statement
-> WHERE customer_id=some_id;
mysql> UNLOCK TABLES;

Sikkim Manipal University

Page No. 222

RDBMS with MySQL

Unit 9

Without LOCK TABLES, it is possible that another thread might insert a new
row in the trans table between execution of the SELECT and UPDATE
statements.
You can avoid using LOCK TABLES in many cases by using relative
updates (UPDATE customer SET value=value+new_value) or the
LAST_INSERT_ID() function.
You can also avoid locking tables in some cases by using the user-level
advisory lock functions GET_LOCK() and RELEASE_LOCK(). These locks
are saved in a hash table in the server and implemented with
pthread_mutex_lock() and pthread_mutex_unlock() for high speed.
You can lock all tables in all databases with read locks with the FLUSH
TABLES WITH READ
LOCK statement. This is a very convenient way to get backups if you have a
file system such as Veritas that can take snapshots in time.
NOTE: If you use ALTER TABLE on a locked table, it may become
unlocked.
Self Assessment Questions
4. The ______ command releases any locks held by the current thread.
5. If a table is locked with a read and a write lock, put the ___ lock before
the ____ lock.

9.7 SET TRANSACTION Syntax


SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL
{READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ |
SERIALIZABLE}

This statement sets the transaction isolation level for the next transaction,
globally, or for the current session.
The default behavior of SET TRANSACTION is to set the isolation level for
the next (not yet started) transaction.

Sikkim Manipal University

Page No. 223

RDBMS with MySQL

Unit 9

If you use the GLOBAL keyword, the statement sets the default transaction
level globally for all new connections created from that point on. Existing
connections are unaffected. You need the SUPER privilege to do this.
Using the SESSION keyword sets the default transaction level for all future
transactions performed on the current connection.
You can set the initial default global isolation level for mysqld with the -transaction- isolation option.
To set the global default isolation level at server startup, use the -transaction-isolation=level option to mysqld on the command line or in an
option file.
Values of level for this option use dashes rather than spaces, so the
allowable values are READUNCOMMITTED, READ-COMMITTED,
REPEATABLE-READ, or SERIALIZABLE.
For example, to set the default isolation level to REPEATABLE READ, use
these lines in the [mysqld] section of an option file:
[mysqld]
transaction-isolation = REPEATABLE-READ

To determine the global and session transaction isolation levels at runtime,


check the value of the tx_isolation system variable:
SELECT @@GLOBAL.tx_isolation, @@tx_isolation;

The following list describes how MySQL supports the different transaction
levels:
READ UNCOMMITTED
SELECT statements are performed in a non-locking fashion, but a
possible earlier version of a row might be used. Thus, using this isolation
level, such reads are not consistent. This is also called a dirty read.
Otherwise, this isolation level works like READ COMMITTED.
READ COMMITTED

Sikkim Manipal University

Page No. 224

RDBMS with MySQL

Unit 9

A somewhat Oracle-like isolation level with respect to consistent (nonlocking) reads: Each consistent read, even within the same transaction,
sets and reads its own fresh snapshot.
For locking reads (SELECT with FOR UPDATE or LOCK IN SHARE
MODE), InnoDB locks only index records, not the gaps before them, and
thus allows the free insertion of new records next to locked records. For
UPDATE and DELETE statements, locking depends on whether the
statement uses a unique index with a unique search condition (such as
WHERE id = 100), or a range type search condition (such as WHERE id
> 100). For a unique index with a unique search condition, InnoDB locks
only the index record found, not the gap before it. For range-type
searches, InnoDB locks the index range scanned, using gap locks or
nextkey (gap plus index-record) locks to block insertions by other
sessions into the gaps covered by the range. This is necessary because
phantom rows must be blocked for MySQL replication and recovery to
work.
REPEATABLE READ
This is the default isolation level for InnoDB. For consistent reads, there
is an important difference from the READ COMMITTED isolation level:
All consistent reads within the same transaction read the snapshot
established by the first read. This convention means that if you issue
several plain (non-locking) SELECT statements within the same
transaction, these SELECT statements are consistent also with respect
to each other.
For locking reads (SELECT with FOR UPDATE or LOCK IN SHARE
MODE), UPDATE, and DELETE statements, locking depends on
whether the statement uses a unique index with a unique search
condition, or a range-type search condition. For a unique index with a
unique search condition, InnoDB locks only the index record found, not
the gap before it. For other search conditions, InnoDB locks the index
range scanned, using gap locks or next-key (gap plus index-record)
locks to block insertions by other sessions into the gaps covered by the
range.
SERIALIZABLE

Sikkim Manipal University

Page No. 225

RDBMS with MySQL

Unit 9

This level is like REPEATABLE READ, but InnoDB implicitly converts all
plain SELECT statements to SELECT ... LOCK IN SHARE MODE if
autocommit is disabled. If autocommit is enabled, the SELECT is its own
transaction. It therefore is known to be read only and can be serialized if
performed as a consistent (non-locking) read and need not block for
other transactions. (This means that to force a plain SELECT to block if
other transactions have modified the selected rows, you should disable
autocommit.)
Self Assessment Questions
6. Using the _____ keyword sets the default transaction level for all future
transactions performed on the current connection.

9.8 Summary
This unit covers the transaction processing aspects of MySQL.
1. Start Transaction, Commit, and Rollback Syntax: This topic gives the
syntax and examples of Starting a transaction, committing the changes
done during a transaction, and rolling back any unwanted changes done
during a transaction.
2. Statements that cannot be rolled back: This topic discusses the
theory behind taking care of the transactions that cannot be rolled back.
3. Statements that cause an implicit Commit: This topic gives a list of
SQL statements that cause an implicit commit to the databases.
4. Savepoint and Rollback to Savepoint Syntax: The SAVEPOINT
statement sets a named transaction savepoint with a name of identifier.
The ROLLBACK TO SAVEPOINT statement rolls back a transaction to
the named savepoint.
5. Lock Tables and Unlock Tables Syntax: LOCK TABLES locks tables
for the current thread. UNLOCK TABLES releases any locks held by the
current thread. This topic also gives suitable examples for concept
understanding.
6. Set Transaction Syntax: This statement sets the transaction isolation
level for the next transaction, globally, or for the current session. The
default behavior of SET TRANSACTION is to set the isolation level for
the next (not yet started) transaction.
Sikkim Manipal University

Page No. 226

RDBMS with MySQL

Unit 9

9.9 Terminal Questions


1. Discuss some of the transactional statements that do cause an implicit
commit.
2. Describe the Savepoint and Rollback to Savepoint Syntaxes.

9.10 Answers
Self Assessment Questions
1. disabled
2. UNLOCK TABLES
3. SAVEPOINT
4. UNLOCK TABLES
5. Write, Read
6. SESSION
Terminal Questions
1. (Refer Section 9.4)
The following statements implicitly end a transaction (as if you had done a
COMMIT before executing the statement):
UNLOCK TABLES also ends a transaction if any tables currently are locked.
Prior to MySQL 4.0.13, CREATE TABLE ends a transaction if the binary
update log is enabled.
Transactions cannot be nested. This is a consequence of the implicit
COMMIT performed for any current transaction when you issue a START
TRANSACTION statement or one of its synonyms.
ALTER TABLE, CREATE TABLE, and DROP TABLE do not commit a
transaction if the TEMPORARY keyword is used. (This does not apply to
other operations on temporary tables such as CREATE INDEX, which do
cause a commit.)
However, although no implicit commit occurs, neither can the statement be
rolled back. Therefore, use of such statements will violate transaction
atomicity: For example, if you use CREATE TEMPORARY TABLE and then
roll back the transaction, the table remains in existence.
Sikkim Manipal University

Page No. 227

RDBMS with MySQL

Unit 9

The CREATE TABLE statement in InnoDB is processed as a single


transaction. This means that a ROLLBACK from the user does not undo
CREATE TABLE statements the user made during that transaction.
2. (Refer Section 9.5)
SAVEPOINT identifier
ROLLBACK TO SAVEPOINT identifier

Starting from MySQL 4.0.14 and 4.1.1, InnoDB supports the SQL
statements SAVEPOINT and ROLLBACK TO SAVEPOINT.
The SAVEPOINT statement sets a named transaction savepoint with a
name of identifier. If the current transaction already has a savepoint with the
same name, the old savepoint is deleted and a new one is set.
The ROLLBACK TO SAVEPOINT statement rolls back a transaction to the
named savepoint. Modifications that the current transaction made to rows
after the savepoint was set are undone in the rollback, but InnoDB does not
release the row locks that were stored in memory after the savepoint. (Note
that for a new inserted row, the lock information is carried by the transaction
ID stored in the row; the lock is not separately stored in memory. In this
case, the row lock is released in the undo.) Savepoints that were set at a
later time than the named savepoint are deleted.

Sikkim Manipal University

Page No. 228

RDBMS with MySQL

Unit 10

Unit 10

Stored Procedures

Structure
10.1 Introduction
Objectives
10.2 Stored Procedure and Routines
10.3 Maintaining Stored Procedures
CREATE PROCEDURE and CREATE FUNCTION
ALTER PROCEDURE and ALTER FUNCTION
DROP PROCEDURE and DROP FUNCTION
SHOW CREATE PROCEDURE and SHOW CREATE
FUNCTION
10.4 SHOW PROCEDURE STATUS and SHOW FUNCTION STATUS
10.5 CALL Statement
10.6 Summary
10.7 Terminal Questions
10.8 Answers

10.1 Introduction
This unit discusses stored programs and views, which are database objects
defined in terms of SQL code that is stored on the server for later invocation.
The stored procedures themselves can be written using PL/SQL. Stored
procedures exist to perform data-intensive operations that cannot be
accomplished using just SQL, and they perform these operations inside the
database where network performance is a moot issue.
Objectives
After studying this unit, you should be able to:
define and Distinguish between Stored Procedures and Routines
explain with examples the syntaxes of Create Procedure and Create
Function
explain with examples the syntaxes of Alter Procedure and Alter
Function
describe with examples the syntaxes of Drop Procedure and Drop
Function

Sikkim Manipal University

Page No. 229

RDBMS with MySQL

Unit 10

explain the importance of Show Create Procedure and Show Create


Function
explain the importance of Show Procedure Status and Show Function
Status
describe the usage of Call Statement

10.2 Stored Procedures & Routines


A Stored Procedure actually refers collectively to standalone stored
functions, standalone procedures, packaged functions, and procedures.
To call a stored procedure, you need to know the procedure name and know
about any parameters that will be passed to the procedure. In the case of a
function, you also need to know the return type.
Stored programs include these objects:
a) Stored Routines, that is, stored functions and procedures. A stored
function is used much like a built-in function. You invoke it in an
expression and it returns a value during expression evaluation. A stored
procedure is invoked using the CALL statement. A procedure does not
have a return value but can modify its parameters for later inspection by
the caller. It can also generate result sets to be returned to the client
program.
b) Triggers. A trigger is a named database object that is associated with a
table and that is activated when a particular event occurs for the table,
such as an insert or update.
c) Events. An event is a task that runs according to schedule.
Views are stored queries that when invoked produce a result set. A view
acts as a virtual table.
This chapter describes how to use each type of stored program and views.
Additional information about SQL syntax for statements related to these
objects is available in the following locations:
For each object type, there are CREATE, ALTER, and DROP
statements that control which objects exist and how they are defined.
The CALL statement is used to invoke stored procedures.
Stored program definitions contain a body that may use compound
statements, loops, conditionals, and declared variables.
Sikkim Manipal University

Page No. 230

RDBMS with MySQL

Unit 10

Each stored program contains a body that consists of an SQL statement.


This statement may be a compound statement made up of several
statements separated by semicolon (;) characters. For example, the
following stored procedure has a body made up of a BEGIN ... END block
that contains a SET statement and a REPEAT loop that itself contains
another SET statement:
CREATE PROCEDURE dorepeat(p1 INT)
BEGIN
SET @x = 0;
REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
END

If you use the mysql client program to define a stored program that contains
the semicolon characters within its definition, a problem arises. By default,
mysql itself recognizes semicolon as a statement delimiter, so you must
redefine the delimiter temporarily to cause mysql to pass the entire stored
program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The following
example shows how to do this for the dorepeat() procedure just shown. The
delimiter is changed to // to enable the entire definition to be passed to the
server as a single statement, and then restored to ; before invoking the
procedure. This allows the; delimiter used in the procedure body to be
passed through to the server rather than being interpreted by mysql itself.
mysql> delimiter //
mysql> CREATE PROCEDURE dorepeat(p1 INT)
-> BEGIN
-> SET @x = 0;
-> REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

Sikkim Manipal University

Page No. 231

RDBMS with MySQL

Unit 10

mysql> CALL dorepeat(1000);


Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @x;
+------+
| @x |
+------+
| 1001 |
+------+
1 row in set (0.00 sec)
mysql>

You can redefine the delimiter to a string other than //, and the delimiter can
consist of a single character or multiple characters. You should avoid the
use of the backslash (\) character because that is the escape character for
MySQL.
The following is an example of a function that takes a parameter, performs
an operation using an SQL function, and returns the result. In this case, it is
unnecessary to use delimiter because the function definition contains no
internal; statement delimiters:

Using Stored Routines (Procedures and Functions)


Stored routines (procedures and functions) are supported in MySQL 5.1. A
stored routine is a set of SQL statements that can be stored in the server.
Sikkim Manipal University

Page No. 232

RDBMS with MySQL

Unit 10

Once this has been done, clients don't need to keep reissuing the individual
statements but can refer to the stored routine instead.
Stored routines require the proc table in the mysql database. This table is
created during the MySQL 5.1 installation procedure. If you are upgrading to
MySQL 5.1 from an earlier version, be sure to update your grant tables to
make sure that the proc table exists.
Stored routines can be particularly useful in certain situations:
When multiple client applications are written in different languages or
work on different platforms, but need to perform the same database
operations.
When security is paramount. Banks, for example, use stored procedures
and functions for all common operations. This provides a consistent and
secure environment, and routines can ensure that each operation is
properly logged. In such a setup, applications and users would have no
access to the database tables directly, but can only execute specific
stored routines.
Stored routines can provide improved performance because less
information needs to be sent between the server and the client. The tradeoff
is that this does increase the load on the database server because more of
the work is done on the server side and less is done on the client
(application) side. Consider this if many client machines (such as Web
servers) are serviced by only one or a few database servers.
Stored routines also allow you to have libraries of functions in the database
server. This is a feature shared by modern application languages that allow
such design internally (for example, by using classes). Using these client
application language features is beneficial for the programmer even outside
the scope of database use.
MySQL follows the SQL:2003 syntax for stored routines, which is also used
by IBM's DB2.
The MySQL implementation of stored routines is still in progress. All syntax
described here is supported and any limitations and extensions are
documented where appropriate.

Sikkim Manipal University

Page No. 233

RDBMS with MySQL

Unit 10

Self Assessment Questions


1. A ____________ actually refers collectively to standalone stored
functions, standalone procedures, packaged functions, and procedures.
2. A ____ is a named database object that is associated with a table and
that is activated when a particular event occurs for the table, such as an
insert or update.
3. To redefine the MySQL delimiter, use the ____ command.

10.3 Maintaining Stored Procedures


10.3.1 CREATE PROCEDURE and CREATE FUNCTION
CREATE
[DEFINER = {user | CURRENT_USER }]
PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
CREATE
[DEFINER = {user | CURRENT_USER }]
FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body
proc_parameter:
[ IN | OUT | INOUT ] param_name type

func_parameter:
param_name type
type:
Any valid MySQL data type
characteristic:
LANGUAGE SQL
| [NOT] DETERMINISTIC
| {CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY {DEFINER | INVOKER }
| COMMENT 'string'
routine_body:
Valid SQL procedure statement

Sikkim Manipal University

Page No. 234

RDBMS with MySQL

Unit 10

The CREATE FUNCTION statement is also used in MySQL to support


UDFs (user-defined functions). A UDF can be regarded as an external
stored function.
Note: The stored functions share their namespace with UDFs.
When the routine is invoked, an implicit USE db_name is performed (and
undone when the routine terminates). The causes the routine to have the
given default database while it executes. USE statements within stored
routines are disallowed.
When a stored function has been created, you invoke it by referring to it in
an expression. The function returns a value during expression evaluation.
When a stored procedure has been created, you invoke it by using the
CALL statement.
ALTER PROCEDURE and ALTER FUNCTION
ALTER {PROCEDURE | FUNCTION} sp_name [characteristic ...]
characteristic:
{ CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'

This statement can be used to change the characteristics of a stored


procedure or function. You must have the ALTER ROUTINE privilege for the
routine. (That privilege is granted automatically to the routine creator.) If
binary logging is enabled, the ALTER FUNCTION statement might also
require the SUPER privilege.
DROP PROCEDURE and DROP FUNCTION Syntax
DROP {PROCEDURE | FUNCTION} [IF EXISTS] sp_name

This statement is used to drop a stored procedure or function. That is, the
specified routine is removed from the server. You must have the ALTER
ROUTINE privilege for the routine. (That privilege is granted automatically to
the routine creator.)
The IF EXISTS clause is a MySQL extension. It prevents an error from
occurring if the procedure or function does not exist.
DROP FUNCTION is also used to drop user-defined functions.
Sikkim Manipal University

Page No. 235

RDBMS with MySQL

Unit 10

SHOW CREATE PROCEDURE and SHOW CREATE FUNCTION


SHOW CREATE PROCEDURE proc_name

This statement is a MySQL extension. It returns the exact string that can be
used to re-create the named stored procedure.
SHOW CREATE FUNCTION Syntax:
SHOW CREATE FUNCTION func_name

This function displays information about stored functions.


Both the above statements require that you be the owner of the routine or
have SELECT access to the mysql.proc table. If you do not have privileges
for the routine itself, the value displayed for the Create Procedure or
Create Function field will be NULL.
Self Assessment Questions
4. The________ statement is used to drop a stored procedure or function.

10.4 SHOW PROCEDURE STATUS and SHOW FUNCTION


STATUS
SHOW PROCEDURE STATUS

On execution of the above statement, the following fields or columns are


displayed:
1. Db
2. Name
3. Type
4. Definer
5. Modified
6. Created
7. Security_Type
8. Comment
9. character_set_client
10. collation_connection
11. Database Collation
SHOW FUNCTION STATUS

Sikkim Manipal University

Page No. 236

RDBMS with MySQL

Unit 10

On execution of the above statement, the same fields or columns as that of


SHOW PROCEDURE STATES are displayed.

10.5 CALL Statement


CALL sp_name([parameter[,...]])
CALL sp_name[()]

The CALL statement invokes a stored procedure that was defined


previously with CREATE PROCEDURE.
CALL can pass back values to its caller using parameters that are declared
as OUT or INOUT parameters. When the procedure returns, a client
program can also obtain the number of rows affected for the final statement
executed within the routine: At the SQL level, call the ROW_COUNT()
function; from C, call the mysql_affected_rows() C API function.
As of MySQL 5.1.13, stored procedures that take no arguments can be
invoked without parentheses. That is, CALL p() and CALL p are equivalent.
To get back a value from a procedure using an OUT or INOUT parameter,
pass the parameter by means of a user variable, and then check the value
of the variable after the procedure returns. (If you are calling the procedure
from within another stored procedure or function, you can also pass a
routine parameter or local routine variable as an IN or INOUT parameter.)
For an INOUT parameter, initialize its value before passing it to the
procedure. The following procedure has an OUT parameter that the
procedure sets to the current server version, and an INOUT value that the
procedure increments by one from its current value:
CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param
INT)
BEGIN
# Set value of OUT parameter
SELECT VERSION() INTO ver_param;
# Increment value of INOUT parameter
SET incr_param = incr_param + 1;
END;

Sikkim Manipal University

Page No. 237

RDBMS with MySQL

Unit 10

Before calling the procedure, initialize the variable to be passed as the


INOUT parameter. After calling the procedure, the values of the two
variables will have been set or modified:
mysql> SET @increment = 10;
mysql> CALL p(@version, @increment);
mysql> SELECT @version, @increment;
+-----------------+-----------------------+
| @version
| @increment |
+-----------------+-----------------------+
| 5.1.21-beta-log | 11 |
+-----------------+-----------------------+

For programs written in a language that provides a MySQL interface, there


is no native method for directly retrieving the results of OUT or INOUT
parameters from CALL statements. To get the parameter values, pass userdefined variables to the procedure in the CALL statement and then execute
a SELECT statement to produce a result set containing the variable values.
The following example illustrates the technique (without error checking) for a
stored procedure p1 that has two OUT parameters.
mysql_query(mysql, "CALL p1(@param1, @param2)");
mysql_query(mysql, "SELECT @param1, @param2");
result = mysql_store_result(mysql);
row = mysql_fetch_row(result);
mysql_free_result(result);

After the preceding code executes, row[0] and row[1] contain the values of
@param1 and @param2, respectively.
To handle INOUT parameters, execute a statement prior to the CALL that
sets the user variables to the values to be passed to the procedure.
Self Assessment Questions
5. The ___ statement can pass back values to its caller using parameters
that are declared as OUT or INOUT parameters.
6. To handle _____ parameters, execute a statement prior to the CALL that
sets the user variables to the values to be passed to the procedure.

Sikkim Manipal University

Page No. 238

RDBMS with MySQL

Unit 10

10.6 Summary
Stored procedures exist to perform data-intensive operations that cannot be
accomplished using just SQL, and they perform these operations inside the
database where network performance is a moot issue
1. Stored Procedure and Routines: A Stored Procedure actually refers
collectively to standalone stored functions, standalone procedures,
packaged functions, and procedures.
To call a stored procedure, you need to know the procedure name and
know about any parameters that will be passed to the procedure. In the
case of a function, you also need to know the return type.
2. Maintaining Stored Procedures: This topic deals with the discussion of
functions like Create, Alter and Drop statements which allow the user to
do the said operations on procedures and functions. It also discusses
the Show syntaxes of Procedures and Functions to create them.
3. Show Procedure Status and Show Function Status: These
statements cause the output to display the metadata regarding the
procedures and functions created.
4. CALL Statement: The CALL statement invokes a stored procedure that
was defined previously with CREATE PROCEDURE. CALL can pass
back values to its caller using parameters that are declared as OUT or
INOUT parameters.

10.7 Terminal Questions


1. Explain stored procedures and routines.
2. Give the syntax for the following:
a. Create Procedure and Create Function
b. Alter Procedure and Alter Function
c. Show Create Procedure and Show Create Function
3. Give the syntax of CALL statement.

10.8 Answers
Self Assessment Questions

1. Stored Procedure
2. trigger
3. delimiter
Sikkim Manipal University

Page No. 239

RDBMS with MySQL

Unit 10

4. DROP {PROCEDURE | FUNCTION} [IF EXISTS] sp_name


5. CALL
6. INOUT
Terminal Questions
1. A Stored Procedure actually refers collectively to standalone stored
functions, standalone procedures, packaged functions, and procedures.
To call a stored procedure, you need to know the procedure name and
know about any parameters that will be passed to the procedure. In the
case of a function, you also need to know the return type.
A stored function is used much like a built-in function. You invoke it in an
expression and it returns a value during expression evaluation. A stored
procedure is invoked using the CALL statement. A procedure does not
have a return value but can modify its parameters for later inspection by
the caller. It can also generate result sets to be returned to the client
program.
(Refer to Section 10.2)
2. (Refer to Section 10.3)
a. CREATE PROCEDURE <Procedure Name> (Parameter List)
BEGIN
Block of statements
END
<Return Type> CREATE FUNCTION <Function Name> (Parameter List)
BEGIN
Block of statements
END
b.
ALTER {PROCEDURE | FUNCTION} sp_name [characteristic ...]
characteristic:
{ CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL
DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'

Sikkim Manipal University

Page No. 240

RDBMS with MySQL

Unit 10

c.

SHOW CREATE PROCEDURE proc_name


SHOW CREATE FUNCTION func_name

3. (Refer to Section 10.5)


CALL sp_name([parameter[,...]])
CALL sp_name[()]

Sikkim Manipal University

Page No. 241

RDBMS with MySQL

Unit 11

Unit 11

Control Statements

Structure
11.1 Introduction
Objectives
11.2 BEGIN ... END Compound Statement
11.3 DECLARE Statement
11.4 Variables in Stored Procedures
11.5 Variable SET Statement
11.6 SELECT ... INTO Statement
11.7 DECLARE Conditions and Handlers
11.8 Flow Control Constructs
IF Statement
CASE Statement
LOOP Statement
LEAVE Statement
ITERATE Statement
REPEAT Statement
WHILE Statement
11.9 Cursors
Declaring Cursors
Cursor OPEN Statement
Cursor FETCH Statement
Cursor CLOSE Statement
11.10 Summary
11.11 Terminal Questions
11.12 Answers

11.1 Introduction
This unit introduces to the user various control statements used within
stored procedures or functions of MySQL server. It starts with a description
of the BeginEnd statements to be written inside a procedure to indicate
the begin and end of a block of statements. It describes the Declare
statement used to declare variables used in a Procedure or Function. It also
discusses the importance of the Variable SET statement. It explains th
eusageof SELECT.INTO statement used to fill in the data from a query
into the current procedure or function. It also discusses about the Declare
Sikkim Manipal University

Page No. 242

RDBMS with MySQL

Unit 11

conditions and Handlers. It describes various flow control constructs used


for iteration through a series of statements. It also discusses regarding the
Cursors and various statements used in Cursors with appropriate examples.
Objectives
After studying this unit, you should be able to:
discuss the importance and application of BEGINEND statement
discuss the role of DECLARE statement in declaring the variables
describe the declaration of variables in stored procedures
explain the importance of Variable SET statement
discuss the usage of SELECTINTO statement in stored procedures
discuss the concept of DECLARE conditions and handlers
explain various flow control constructs and apply them
describe the usage and application of cursors with suitable examples
11.2 BEGIN ... END Compound Statement
[begin_label:] BEGIN
statement(s)
END [end_label]

Stored routines may contain multiple statements, using a BEGIN ... END
compound statement. begin_label and end_label must be the same, if both
are specified. Please note that the optional [NOT] ATOMIC clause is not yet
supported. This means that no transactional savepoint is set at the start of
the instruction block and the BEGIN clause used in this context has no
effect on the current transaction.
Multiple statements requires that a client is able to send query strings
containing ;. This is handled in the mysql command-line client with the
delimiter command. Changing the ; end-of-query delimiter (for example, to
|) allows ; to be used in a routine body.
Self Assessment Questions
1. In a BEGINEND compound statement, the begin_label and end_label
must be the ____, if both are specified.

Sikkim Manipal University

Page No. 243

RDBMS with MySQL

Unit 11

11.3 DECLARE Statement


The DECLARE statement is used to define various items local to a routine:
local variables, , conditions and handlers, and cursors.
SIGNAL and RESIGNAL statements are not currently supported.
The DECLARE statement may only be used inside a BEGIN ... END
compound statement and must be at its start, before any other statements.
Self Assessment Questions
2. The ______ statement may only be used inside a BEGIN ... END
compound statement and must be at its start, before any other
statements.

11.4 Variables in Stored Procedures


You may declare and use variables within a routine.
DECLARE Local Variables
DECLARE var_name[,...] type [DEFAULT value]

This command is used to declare local variables. The scope of a variable is


within the BEGIN ... END block.

11.5 Variable SET Statement


SET variable = expression [,...]

The SET statement in stored procedures is an extended version of the


general SET command. Referenced variables may be ones declared inside
a routine, or global server variables. The SET statement in stored
procedures is implemented as part of the pre-existing SET syntax. This
allows an extended syntax of SET a=x, b=y, ... where different variable
types (locally declared variables, server variables, and global and session
server variables) can be mixed. This also allows combinations of local
variables and some options that only make sense for global/system
variables; in that case the options are accepted but ignored.
Self Assessment Questions
3. The _______ statement also allows combinations of local variables and
some options that only make sense for global/system variables; in that
case the options are accepted but ignored.
Sikkim Manipal University

Page No. 244

RDBMS with MySQL

Unit 11

11.6 SELECT ... INTO Statement


SELECT column[,...] INTO variable[,...] table_expression

This SELECT syntax stores selected columns directly into variables.


Therefore, only a single row may be retrieved. This statement is also
extremely useful when used in combination with cursors.
Example: SELECT id, data INTO x, y FROM test.t1 LIMIT 1;

11.7 DECLARE Conditions and Handlers


Certain conditions may require specific handling. These conditions can
relate to errors, as well as general flow control inside a routine.
DECLARE Conditions
DECLARE condition_name CONDITION FOR condition_value
condition_value:
SQLSTATE [VALUE] sqlstate_value
| mysql_error_code

This statement specifies conditions that will need specific handling. It


associates a name with a specified error condition. The name can
subsequently be used in a DECLARE HANDLER statement. In addition to
SQLSTATE values, MySQL error codes are also supported.
DECLARE Handlers
DECLARE
handler_type
HANDLER
sp_statement
handler_type:
CONTINUE
| EXIT
| UNDO
condition_value:
SQLSTATE [VALUE] sqlstate_value
| condition_name
| SQLWARNING
| NOT FOUND
| SQLEXCEPTION
| mysql_error_code

FOR

condition_value[,...]

This statement specifies handlers that each may deal with one or more
conditions. If one of these conditions occurs, the specified statement is
executed.
For a CONTINUE handler, execution of the current routine continues after
execution of the handler statement. For an EXIT handler, execution of the
Sikkim Manipal University

Page No. 245

RDBMS with MySQL

Unit 11

current BEGIN...END compound statement is terminated. The UNDO


handler_type is not yet supported.
SQLWARNING is shorthand for all SQLSTATE codes that begin with 01.
NOT FOUND is shorthand for all SQLSTATE codes that begin with 02.
SQLEXCEPTION is shorthand for all SQLSTATE codes not caught by
SQLWARNING or NOT FOUND.
In addition to SQLSTATE values, MySQL error codes are also supported.

Example:
mysql> CREATE TABLE test.t (s1 int,primary key (s1));
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter //
mysql> CREATE PROCEDURE handlerdemo ()
-> BEGIN
-> DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1;
-> set @x = 1;
-> INSERT INTO test.t VALUES (1);
-> set @x = 2;
-> INSERT INTO test.t VALUES (1);
-> SET @x = 3;
-> END;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> CALL handlerdemo()//
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @x//
+------+
| @x |
+------+
|3|
+------+
1 row in set (0.00 sec)

Notice that @x is 3, which shows that MySQL executed to the end of the
procedure. If the line DECLARE CONTINUE HANDLER FOR SQLSTATE
'23000' SET @x2 = 1; had not been present, MySQL would have taken the
default (EXIT) path after the second INSERT failed due to the PRIMARY
KEY constraint, and SELECT @x would have returned 2.

11.8 Flow Control Constructs


The IF, CASE, LOOP, WHILE, ITERATE, and LEAVE constructs are fully
implemented. These constructs may each contain either a single statement,

Sikkim Manipal University

Page No. 246

RDBMS with MySQL

Unit 11

or a block of statements using the BEGIN ... END compound statement.


Constructs may be nested.
FOR loops are not currently supported.
11.8.1 IF Statement
IF search_condition THEN statement(s)
[ELSEIF search_condition THEN statement(s)]
...
[ELSE statement(s)]
END IF

IF implements a basic conditional construct. If the search_condition


evaluates to true, the corresponding SQL statement is executed. If no
search_condition matches, the statement in the ELSE clause is executed.
11.8.2 CASE Statement
CASE implements a complex conditional construct. If a search_condition
evaluates to true, the corresponding SQL statement is executed. If no
search condition matches, the statement in the ELSE clause is executed.
CASE case_value
WHEN when_value THEN statement
[WHEN when_value THEN statement ...]
[ELSE statement]
END CASE
OR
CASE
WHEN search_condition THEN statement
[WHEN search_condition THEN statement ...]
[ELSE statement]
END CASE

Note: The syntax of a CASE statement inside a stored procedure differs


slightly from that of the SQL CASE expression. The CASE statement can
not have an ELSE NULL clause, and the construct is terminated with END
CASE instead of END.
11.8.3 LOOP Statement
[begin_label:] LOOP
statement(s)
END LOOP [end_label]

Sikkim Manipal University

Page No. 247

RDBMS with MySQL

Unit 11

LOOP implements a simple loop construct, enabling repeated execution of a


particular statement or group of statements. The statements within the loop
are repeated until the loop is exited, usually this is accomplished with a
LEAVE statement.
begin_label and end_label must be the same, if both are specified.
11.8.4 LEAVE Statement
LEAVE label

This statement is used to exit any flow control construct.


11.8.5 ITERATE Statement
ITERATE label

ITERATE can only appear within LOOP, REPEAT, and WHILE statements.
ITERATE means do the loop iteration again.
Example:
CREATE PROCEDURE doiterate(p1 INT)
BEGIN
label1: LOOP
SET p1 = p1 + 1;
IF p1 < 10 THEN ITERATE label1; END IF;
LEAVE label1;
END LOOP label1;
SET @x = p1;
END

11.8.6 REPEAT Statement


[begin_label:] REPEAT
statement(s)
UNTIL search_condition
END REPEAT [end_label]

The statements within a REPEAT statement are repeated until the


search_condition is true. begin_label and end_label must be the same, if
both are specified.

Sikkim Manipal University

Page No. 248

RDBMS with MySQL

Unit 11

Example:
mysql> delimiter //
mysql> CREATE PROCEDURE dorepeat(p1 INT)
-> BEGIN
-> SET @x = 0;
-> REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> CALL dorepeat(1000)//
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @x//
+------+
| @x |
+------+
| 1001 |
+------+
1 row in set (0.00 sec)

11.8.7 WHILE Statement


[begin_label:] WHILE search_condition DO
statement(s)
END WHILE [end_label]

The statements within a WHILE statement are repeated as long as the


search_condition is true. begin_label and end_label must be the same, if
both are specified.
Example:

CREATE PROCEDURE dowhile()


BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
...
SET v1 = v1 - 1;
END WHILE;
END

Self Assessment Questions


4. The ______ control construct implements a basic conditional construct.
5. The ____ syntax stores selected columns directly into variables.
Therefore, only a single row may be retrieved.
6. The _____ statement is used to exit any flow control construct.
Sikkim Manipal University

Page No. 249

RDBMS with MySQL

Unit 11

11.9 Cursors
Simple cursors are supported inside stored procedures and functions. The
syntax is as in embedded SQL. Cursors are currently asensitive, read-only,
and non-scrolling. Asensitive means that the server may or may not make a
copy of its result table.
Example:
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE a CHAR(16);
DECLARE b,c INT;
OPEN cur1;
OPEN cur2;
REPEAT
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF NOT done THEN
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
CLOSE cur2;
END

11.9.1 Declaring Cursors


DECLARE cursor_name CURSOR FOR sql_statement

Multiple cursors may be defined in a routine, but each must have a unique
name.
11.9.2 Cursor OPEN Statement
OPEN cursor_name

This statement opens a previously declared cursor.

Sikkim Manipal University

Page No. 250

RDBMS with MySQL

Unit 11

11.9.3 Cursor FETCH Statement


FETCH cursor_name

This statement fetches the next row (if a row exists) using the specified
open cursor, and advances the cursor pointer.
11.9.4 Cursor CLOSE Statement
CLOSE cursor_name

This statement closes a previously opened cursor.

11.10 Summary
This unit covers various control statements used within stored procedures or
functions of MySQL server.
1. BEGIN ... END Compound Statement: This topic discusses the
BeginEnd Compound statements used to mark the Begin and End of
a block structures within a stored procedure or function.
2. DECLARE Statement: This statement is used to declare the necessary
variables within a stored procedure or a function
3. Variables in Stored Procedures: This topic covers the declaration of
local variables within a stored procedure or a function whose scope is
within the BeginEnd Statement.
4. Variable SET Statement: This allows an extended syntax of SET a=x,
b=y, ... where different variable types (locally declared variables, server
variables, and global and session server variables) can be mixed.
5. SELECT ... INTO Statement: This statement is also extremely useful
when used in combination with cursors.
6. DECLARE Conditions and Handlers: Certain conditions may require
specific handling. These conditions can relate to errors, as well as
general flow control inside a routine.
7. Flow Control Constructs: The IF, CASE, LOOP, WHILE, ITERATE,
and LEAVE constructs are fully implemented. These constructs may
each contain either a single statement, or a block of statements using
the BEGIN ... END compound statement. Constructs may be nested.

Sikkim Manipal University

Page No. 251

RDBMS with MySQL

Unit 11

8. Cursors: Simple cursors are supported inside stored procedures and


functions. The syntax is as in embedded SQL. Suitable examples of
variants of cursors are given.

11.11 Terminal Questions


1. Give the syntax of BEGINEND compound statement.
2. Explain DECLARE statement.
3. Give the syntax for the following:
a. Cursor Declaration
b. Cursor OPEN statement
c. Cursor FETCH statement

11.12 Answers
Self Assessment Questions
1. same
2. DECLARE
3. SET
4. IF
5. SELECT
6. LEAVE
Terminal Questions
1. (Refer to Section 11.2)
[begin_label:] BEGIN
statement(s)
END [end_label]

2. The DECLARE statement is used to define various items local to a


routine: local variables, , conditions and handlers, and cursors. SIGNAL
and RESIGNAL statements are not currently supported. The DECLARE
statement may only be used inside a BEGIN ... END compound
statement and must be at its start, before any other statements. (Refer
to Section 11.3)
3.
a. DECLARE cursor_name CURSOR FOR sql_statement
b. OPEN cursor_name
c. FETCH cursor_name
Sikkim Manipal University

Page No. 252

RDBMS with MySQL

Unit 12

Unit 12

User Account Management

Structure
12.1 Introduction
Objectives
12.2 Adding New User Accounts to MySQL
12.3 MySQL Usernames and Passwords
12.4 Securing the Initial MySQL Accounts
12.5 Removing User Accounts from MySQL
12.6 Limiting Account Resources
12.7 Assigning Account Passwords
12.8 Keeping Your Password Secure
12.9 Account Management Statements
DROP USER Syntax
GRANT and REVOKE Syntax
SET PASSWORD Syntax
12.10 Summary
12.11 Terminal Questions
12.12 Answers

12.1 Introduction
This unit describes how to set up accounts for clients of your MySQL server.
It discusses the meaning of account names and passwords as used in
MySQL and how that compares to names and passwords used by your
operating system. It also deals with how to set up new accounts and remove
existing accounts and to change passwords. It contains guidelines for using
passwords securely and how to use secure connections with SSL.
Objectives
After studying this unit, you should be able to:
explain the importance and processes involved in Managing the user
accounts
discuss the methods of adding New user accounts in MySQL
describe the procedure followed in creation of individual user names and
passwords

Sikkim Manipal University

Page No. 253

RDBMS with MySQL

Unit 12

describe the procedure followed in maintaining the security of User


Accounts
discuss the procedure followed in removal of user accounts
explain how to limit the user account resources
discuss with suitable examples various user account management
statements

12.2 Adding New User Accounts to MySQL


You can create MySQL accounts in two ways:
By using statements intended for creating accounts, such as CREATE
USER or GRANT
By manipulating the MySQL grant tables directly with statements such
as INSERT, UPDATE, or DELETE
The preferred method is to use account-creation statements because they
are more concise and less error-prone.
Another option for creating accounts is to use one of several available thirdparty programs that offer capabilities for MySQL account administration.
phpMyAdmin is one such program.
The following examples show how to use the mysql client program to set up
new users. These examples assume that privileges are set up according to
the defaults.
This means that to make changes, you must connect to the MySQL server
as the MySQL root user, and the root account must have the INSERT
privilege for the mysql database and the RELOAD administrative privilege.
First, use the mysql program to connect to the server as the MySQL root
user:
shell> mysql user = root mysql

If you have assigned a password to the root account, you'll also need to
supply a password or -p option for this mysql command and also for those
later in this section.

Sikkim Manipal University

Page No. 254

RDBMS with MySQL

Unit 12

After connecting to the server as root, you can add new accounts. The
following statements use GRANT to set up four new accounts:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
-> IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
-> IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
mysql> GRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost';
mysql> GRANT USAGE ON *.* TO 'dummy'@'localhost';

The accounts created by these GRANT statements have the following


properties:
Two of the accounts have a username of monty and a password of
some_pass. Both accounts are superuser accounts with full privileges to
do anything. One account ('monty'@'localhost') can be used only when
connecting from the local host. The other ('monty'@'%') can be used to
connect from any other host. Note that it is necessary to have both
accounts for monty to be able to connect from anywhere as monty.
Without the localhost account, the anonymous-user account for
localhost that is created by mysql_install_db would take precedence
when monty connects from the local host. As a result, monty would be
treated as an anonymous user. The reason for this is that the
anonymous-user account has a more specific Host column value than
the 'monty'@'%' account and thus comes earlier in the user table sort
order.
One account has a username of admin and no password. This account
can be used only by connecting from the local host. It is granted the
RELOAD and PROCESS administrative privileges. These privileges
allow the admin user to execute the mysqladmin reload, mysqladmin
refresh, and mysqladmin flush-xxx commands, as well as
mysqladmin processlist . No privileges are granted for accessing any
databases. You could add such privileges later by issuing additional
GRANT statements.
One account has a username of dummy and no password. This account
can be used only by connecting from the local host. No privileges are
granted. The USAGE privilege in the GRANT statement enables you to
create an account without giving it any privileges. It has the effect of

Sikkim Manipal University

Page No. 255

RDBMS with MySQL

Unit 12

setting all the global privileges to 'N'. It is assumed that you will grant
specific privileges to the account later.
As an alternative to GRANT, you can create the same accounts directly by
issuing INSERT statements and then telling the server to reload the grant
tables using FLUSH PRIVILEGES:
shell> mysql --user=root mysql
mysql> INSERT INTO user
-> VALUES('localhost','monty',PASSWORD('some_pass'),
-> 'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
mysql> INSERT INTO user
-> VALUES('%','monty',PASSWORD('some_pass'),
-> 'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
mysql> INSERT INTO user SET Host='localhost',User='admin',
-> Reload_priv='Y', Process_priv='Y';
mysql> INSERT INTO user (Host,User,Password)
-> VALUES('localhost','dummy','');
mysql> FLUSH PRIVILEGES;

The reason for using FLUSH PRIVILEGES when you create accounts with
INSERT is to tell the server to re-read the grant tables. Otherwise, the
changes go unnoticed until you restart the server. With GRANT, FLUSH
PRIVILEGES is unnecessary.
The reason for using the PASSWORD() function with INSERT is to encrypt
the password. The GRANT statement encrypts the password for you, so
PASSWORD() is unnecessary.
The 'Y' values enable privileges for the accounts.
Depending on your MySQL version, you may have to use a different number
of 'Y' values in the first two INSERT statements. For the admin account, you
may also employ the more readable extended INSERT syntax using SET.
In the INSERT statement for the dummy account, only the Host, User, and
Password columns in the user table row are assigned values. None of the
privilege columns are set explicitly, so MySQL assigns them all the default
value of 'N'. This is equivalent to what GRANT USAGE does.
Note that to set up a superuser account, it is necessary only to create a user
table entry with the privilege columns set to 'Y'. user table privileges are
global, so no entries in any of the other grant tables are needed.

Sikkim Manipal University

Page No. 256

RDBMS with MySQL

Unit 12

The next examples create three accounts and give them access to specific
databases. Each of them has a username of custom and password of
obscure.
To create the accounts with GRANT, use the following statements:
shell> mysql --user=root mysql
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON bankaccount.*
-> TO 'custom'@'localhost'
-> IDENTIFIED BY 'obscure';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON expenses.*
-> TO 'custom'@'whitehouse.gov'
-> IDENTIFIED BY 'obscure';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON customer.*
-> TO 'custom'@'server.domain'
-> IDENTIFIED BY 'obscure';

The three accounts can be used as follows:


The first account can access the bankaccount database, but only from
the local host.
The second account can access the expenses database, but only from
the host whitehouse.gov.
The third account can access the customer database, but only from the
host server.domain.
To set up the custom accounts without GRANT, use INSERT statements as
follows to modify the grant tables directly:
shell> mysql --user=root mysql
mysql> INSERT INTO user (Host,User,Password)
-> VALUES('localhost','custom',PASSWORD('obscure'));
mysql> INSERT INTO user (Host,User,Password)
-> VALUES('whitehouse.gov','custom',PASSWORD('obscure'));
mysql> INSERT INTO user (Host,User,Password)
-> VALUES('server.domain','custom',PASSWORD('obscure'));
mysql> INSERT INTO db
-> (Host,Db,User,Select_priv,Insert_priv,
-> Update_priv,Delete_priv,Create_priv,Drop_priv)
-> VALUES('localhost','bankaccount','custom',
-> 'Y','Y','Y','Y','Y','Y');

Sikkim Manipal University

Page No. 257

RDBMS with MySQL

Unit 12

mysql> INSERT INTO db


-> (Host,Db,User,Select_priv,Insert_priv,
-> Update_priv,Delete_priv,Create_priv,Drop_priv)
-> VALUES('whitehouse.gov','expenses','custom',
-> 'Y','Y','Y','Y','Y','Y');
mysql> INSERT INTO db
-> (Host,Db,User,Select_priv,Insert_priv,
-> Update_priv,Delete_priv,Create_priv,Drop_priv)
-> VALUES('server.domain','customer','custom',
-> 'Y','Y','Y','Y','Y','Y');
mysql> FLUSH PRIVILEGES;

The first three INSERT statements add user table entries that allow the user
custom to connect from the various hosts with the given password, but grant
no global privileges (all privileges are set to the default value of 'N'). The
next three INSERT statements add db table entries that grant privileges to
custom for the bankaccount, expenses, and customer databases, but only
when accessed from the proper hosts. As usual when you modify the grant
tables directly, you must tell the server to reload them with FLUSH
PRIVILEGES so that the privilege changes take effect.
If you want to give a specific user access from all machines in a given
domain (for example, mydomain.com), you can issue a GRANT statement
that uses the % wildcard character in the host part of the account name:
mysql> GRANT ...
-> ON *.*
-> TO 'myname'@'%.mydomain.com'
-> IDENTIFIED BY 'mypass';

To do the same thing by modifying the grant tables directly, do this:


mysql> INSERT INTO user (Host,User,Password,...)
-> VALUES('%.mydomain.com','myname',PASSWORD('mypass'),...);
mysql> FLUSH PRIVILEGES;

Self Assessment Questions


1. The reason for using _____ PRIVILEGES when you create accounts
with INSERT is to tell the server to re-read the grant tables.
2. The MySQL program to connect to the server as the MySQL root user is
____________.

Sikkim Manipal University

Page No. 258

RDBMS with MySQL

Unit 12

12.3 MySQL Usernames and Passwords


A MySQL account is defined in terms of a username and the client host or
hosts from which the user can connect to the server. The account also has a
password. There are several distinctions between the way usernames and
passwords are used by MySQL and the way they are used by your
operating system:
Usernames, as used by MySQL for authentication purposes, have
nothing to do with usernames (login names) as used by Windows or
Unix.
MySQL usernames can be up to a maximum of 16 characters long. This
limit is hard-coded in the MySQL servers and clients, and trying to
circumvent it by modifying the definitions of the tables in the MySQL
database does not work.
MySQL passwords have nothing to do with passwords for logging in to
your operating system. There is no necessary connection between the
password you use to log in to a Windows or Unix machine and the
password you use to access the MySQL server on that machine.
MySQL encrypts passwords using its own algorithm. This encryption is
different from that used during the Unix login process. MySQL password
encryption is the same as that implemented by the PASSWORD() SQL
function. Unix password encryption is the same as that implemented by
the ENCRYPT() SQL function. From version 4.1 on, MySQL employs a
stronger authentication method that has better password protection
during the connection process than in earlier versions. It is secure even
if TCP/IP packets are sniffed or the mysql database is captured.
When you install MySQL, the grant tables are populated with an initial set of
accounts. Thereafter, you normally set up, modify, and remove MySQL
accounts using statements such as GRANT and REVOKE.
When you connect to a MySQL server with a command-line client, you
should specify the username and password for the account that you want to
use:
shell> mysql --user=monty --password=guess db_name

If you prefer short options, the command looks like this:


shell> mysql -u monty -pguess db_name

Sikkim Manipal University

Page No. 259

RDBMS with MySQL

Unit 12

There must be no space between the -p option and the following password
value.
The preceding commands include the password value on the command line,
which can be a security risk. To avoid this problem, specify the --password
or -p option without any following password value:
shell> mysql --user=monty --password db_name
shell> mysql -u monty -p db_name

When the password option has no password value, the client program prints
a prompt and waits for you to enter the password. (In these examples,
db_name is not interpreted as a password because it is separated from the
preceding password option by a space.)
On some systems, the library routine that MySQL uses to prompt for a
password automatically limits the password to eight characters. That is a
problem with the system library, not with MySQL. Internally, MySQL doesn't
have any limit for the length of the password. To work around the problem,
change your MySQL password to a value that is eight or fewer characters
long, or put your password in an option file.
Self Assessment Questions
3. The ______, as used by MySQL for authentication purposes, have
nothing to do with usernames (login names) as used by Windows or
Unix.
4. MySQL encrypts passwords using ____ algorithm.

12.4 Securing the Initial MySQL Accounts


Part of the MySQL installation process is to set up the MySQL database that
contains the grant tables:
Windows distributions contain pre-initialized grant tables that are
installed automatically.
On Unix, the grant tables are populated by the mysql_install_db
program. Some installation methods run this program for you. Others
require that you execute it manually.

Sikkim Manipal University

Page No. 260

RDBMS with MySQL

Unit 12

The grant tables define the initial MySQL user accounts and their access
privileges. These accounts are set up as follows:
Accounts with the username root are created. These are superuser
accounts that can do anything. The initial root account passwords are
empty, so anyone can connect to the MySQL server as root without a
password and be granted all privileges.
o On Windows, one root account is created; this account allows
connecting from the local host only. The Windows installer will
optionally create an account allowing for connections from any host
only if the user selects the Enable root access from remote
machines option during installation.
o On Unix, both root accounts are for connections from the local host.
Connections must be made from the local host by specifying a
hostname of localhost for one of the accounts, or the actual
hostname or IP number for the other.
Two anonymous-user accounts are created, each with an empty
username. The anonymous accounts have no password, so anyone can
use them to connect to the MySQL server.
o On Windows, one anonymous account is for connections from the
local host. It has all privileges, just like the root accounts. The other
is for connections from any host and has all privileges for the test
database and for other databases with names that start with test.
o On Unix, both anonymous accounts are for connections from the
local host. Connections must be made from the local host by
specifying a hostname of localhost for one of the accounts, or the
actual hostname or IP number for the other. These accounts have all
privileges for the test database and for other databases with names
that start with test_.
As noted, none of the initial accounts have passwords. This means that your
MySQL installation is unprotected until you do something about it:
If you want to prevent clients from connecting as anonymous users
without a password, you should either assign a password to each
anonymous account or else remove the accounts.
You should assign a password to each MySQL root account.
The following instructions describe how to set up passwords for the initial
MySQL accounts, first for the anonymous accounts and then for the root
Sikkim Manipal University

Page No. 261

RDBMS with MySQL

Unit 12

accounts. Replace newpwd in the examples with the actual password that
you want to use. The instructions also cover how to remove the anonymous
accounts, should you prefer not to allow anonymous access at all.
You might want to defer setting the passwords until later, so that you don't
need to specify them while you perform additional setup or testing.
However, be sure to set them before using your installation for production
purposes.
Anonymous Account Password Assignment
To assign passwords to the anonymous accounts, connect to the server as
root and then use either SET PASSWORD or UPDATE. In either case, be
sure to encrypt the password using the PASSWORD() function.
To use SET PASSWORD on Windows, do this:
shell> mysql -u root
mysql> SET PASSWORD FOR ''@'localhost' = PASSWORD('newpwd');
mysql> SET PASSWORD FOR ''@'%' = PASSWORD('newpwd');

To use SET PASSWORD on Unix, do this:


shell> mysql -u root
mysql> SET PASSWORD FOR ''@'localhost' = PASSWORD('newpwd');
mysql> SET PASSWORD FOR ''@'host_name' = PASSWORD('newpwd');

In the second SET PASSWORD statement, replace host_name with the


name of the server host. This is the name that is specified in the Host
column of the non-localhost record for root in the user table. If you don't
know what hostname this is, issue the following statement before using SET
PASSWORD:
mysql> SELECT Host, User FROM mysql.user;

Look for the record that has root in the User column and something other
than localhost in the Host column. Then use that Host value in the second
SET PASSWORD statement.
The other way to assign passwords to the anonymous accounts is by using
UPDATE to modify the user table directly. Connect to the server as root and
issue an UPDATE statement that assigns a value to the Password column
of the appropriate user table records. The procedure is the same for
Sikkim Manipal University

Page No. 262

RDBMS with MySQL

Unit 12

Windows and Unix. The following UPDATE statement assigns a password


to both anonymous accounts at once:
shell> mysql -u root
mysql> UPDATE mysql.user SET Password = PASSWORD('newpwd')
-> WHERE User = '';
mysql> FLUSH PRIVILEGES;

After you update the passwords in the user table directly using UPDATE,
you must tell the server to re-read the grant tables with FLUSH
PRIVILEGES. Otherwise, the change goes unnoticed until you restart the
server.
Anonymous Account Removal
If you prefer to remove the anonymous accounts instead, do so as follows:
shell> mysql -u root
mysql> DELETE FROM mysql.user WHERE User = '';
mysql> FLUSH PRIVILEGES;

The DELETE statement applies both to Windows and to Unix. On Windows,


if you want to remove only the anonymous account that has the same
privileges as root, do this instead:
shell> mysql -u root
mysql> DELETE FROM mysql.user WHERE Host='localhost' AND User='';
mysql> FLUSH PRIVILEGES;

That account allows anonymous access but has full privileges, so removing
it improves security.
Root Account Password Assignment
You can assign passwords to the root accounts in several ways. The
following discussion demonstrates three methods:
1. Use the SET PASSWORD statement
2. Use the mysqladmin command-line client program
3. Use the UPDATE statement
To assign passwords using SET PASSWORD, connect to the server as root
and issue two SET PASSWORD statements. Be sure to encrypt the
password using the PASSWORD() function.

Sikkim Manipal University

Page No. 263

RDBMS with MySQL

Unit 12

For Windows, do this:


shell> mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');
mysql> SET PASSWORD FOR 'root'@'%' = PASSWORD('newpwd');

For Unix, do this:


shell> mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');
mysql> SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpwd');

In the second SET PASSWORD statement, replace host_name with the


name of the server host. This is the same hostname that you used when
you assigned the anonymous account passwords.
To assign passwords to the root accounts using mysqladmin, execute the
following commands:
shell> mysqladmin -u root password "newpwd"
shell> mysqladmin -u root -h host_name password "newpwd"

These commands apply both to Windows and to Unix. In the second


command, replace host_name with the name of the server host. The double
quotes around the password are not always necessary, but you should use
them if the password contains spaces or other characters that are special to
your command interpreter.
You can also use UPDATE to modify the user table directly. The following
UPDATE statement assigns a password to both root accounts at once:
shell> mysql -u root
mysql> UPDATE mysql.user SET Password = PASSWORD('newpwd')
-> WHERE User = 'root';
mysql> FLUSH PRIVILEGES;

The UPDATE statement applies both to Windows and to Unix.


After the passwords have been set, you must supply the appropriate
password whenever you connect to the server. For example, if you want to
use mysqladmin to shut down the server, you can do so using this
command:
Sikkim Manipal University

Page No. 264

RDBMS with MySQL

Unit 12

shell> mysqladmin -u root -p shutdown


Enter password: (enter root password here)

Self Assessment Questions


5. Windows distributions contain _____ grant tables that are installed
automatically.
6. To assign passwords to ____ accounts, connect to the server as root
and then use either SET PASSWORD or UPDATE.

12.5 Removing User Accounts from MySQL


To remove an account, use the DROP USER statement.
Syntax:
DROP USER user [, user] ...

The DROP USER statement removes one or more MySQL accounts. To


use it, you must have the global CREATE USER privilege or the DELETE
privilege for the mysql database. Each account is named using the same
format as for the GRANT statement; for example, 'jeffrey'@'localhost'. The
user and host parts of the account name correspond to the User and Host
column values of the user table row for the account.
DROP USER as present in MySQL 5.0.0 removes only accounts that have
no privileges. In MySQL 5.0.2, it was modified to remove account privileges
as well. This means that the procedure for removing an account depends on
your version of MySQL.
As of MySQL 5.0.2, you can remove an account and its privileges as
follows:
DROP USER user;
The statement removes privilege rows for the account from all grant tables.
In MySQL 5.0.0 and 5.0.1, DROP USER deletes only MySQL accounts that
have no privileges. In these MySQL versions, it serves only to remove each
account record from the user table. To remove a MySQL account

Sikkim Manipal University

Page No. 265

RDBMS with MySQL

Unit 12

completely (including all of its privileges), you should use the following
procedure, performing these steps in the order shown:
1. Use SHOW GRANTS to determine what privileges the account has.
2. Use REVOKE to revoke the privileges displayed by SHOW GRANTS.
This removes rows for the account from all the grant tables except the
user table, and revokes any global privileges listed in the user table.
3. Delete the account by using DROP USER to remove the user table
record.
Note: DROP USER does not automatically close any open user sessions.
Rather, in the event that a user with an open session is dropped, the
statement does not take effect until that user's session is closed. Once the
session is closed, the user is dropped, and that user's next attempt to log in
will fail. This is by design.
Self Assessment Questions
7. To use it, you must have the global _____ privilege or the _____
privilege for the mysql database.

12.6 Limiting Account Resources


One means of limiting use of MySQL server resources is to set the
max_user_connections system variable to a non-zero value. However, this
method is strictly global, and does not allow for management of individual
accounts. In addition, it limits only the number of simultaneous connections
made using a single account, and not what a client can do once connected.
Both types of control are interest to many MySQL administrators, particularly
those working for Internet Service Providers.
In MySQL 5.0, you can limit the following server resources for individual
accounts:
The number of queries that an account can issue per hour
The number of updates that an account can issue per hour
The number of times an account can connect to the server per hour
Any statement that a client can issue counts against the query limit. Only
statements that modify databases or tables count against the update limit.
From MySQL 5.0.3 on, it is also possible to limit the number of simultaneous
connections to the server on a per-account basis.
Sikkim Manipal University

Page No. 266

RDBMS with MySQL

Unit 12

An account in this context is a single row in the user table. Each account is
uniquely identified by its User and Host column values.
As a prerequisite for using this feature, the user table in the mysql database
must contain the resource-related columns. Resource limits are stored in the
max_questions, max_updates, max_connections, and max_user_
connections columns. If your user table doesn't have these columns, it must
be upgraded.
To set resource limits with a GRANT statement, use a WITH clause that
names each resource to be limited and a per-hour count indicating the limit
value. For example, to create a new account that can access the customer
database, but only in a limited fashion, issue this statement:
mysql> GRANT ALL ON customer.* TO 'francis'@'localhost'
-> IDENTIFIED BY 'frank'
-> WITH MAX_QUERIES_PER_HOUR 20
->
MAX_UPDATES_PER_HOUR 10
->
MAX_CONNECTIONS_PER_HOUR 5
->
MAX_USER_CONNECTIONS 2;

The limit types need not all be named in the WITH clause, but those named
can be present in any order. The value for each per-hour limit should be an
integer representing a count per hour. If the GRANT statement has no WITH
clause, the limits are each set to the default value of zero (that is, no limit).
For MAX_USER_CONNECTIONS, the limit is an integer indicating the
maximum number of simultaneous connections the account can make at
any one time. If the limit is set to the default value of zero, the
max_user_connections system variable determines the number of
simultaneous connections for the account.
To set or change limits for an existing account, use a GRANT USAGE
statement at the global level (ON *.*). The following statement changes the
query limit for francis to 100:
mysql> GRANT USAGE ON *.* TO 'francis'@'localhost'
-> WITH MAX_QUERIES_PER_HOUR 100;

This statement leaves the account's existing privileges unchanged and


modifies only the limit values specified.

Sikkim Manipal University

Page No. 267

RDBMS with MySQL

Unit 12

To remove an existing limit, set its value to zero. For example, to remove
the limit on how many times per hour francis can connect, use this
statement:
mysql> GRANT USAGE ON *.* TO 'francis'@'localhost'
-> WITH MAX_CONNECTIONS_PER_HOUR 0;

Resource-use counting takes place when any account has a non-zero limit
placed on its use of any of the resources.
As the server runs, it counts the number of times each account uses
resources. If an account reaches its limit on number of connections within
the last hour, further connections for the account are rejected until that hour
is up. Similarly, if the account reaches its limit on the number of queries or
updates, further queries or updates are rejected until the hour is up. In all
such cases, an appropriate error message is issued.
Resource counting is done per account, not per client. For example, if your
account has a query limit of 50, you cannot increase your limit to 100 by
making two simultaneous client connections to the server. Queries issued
on both connections are counted together.
The current per-hour resource-use counts can be reset globally for all
accounts, or individually for a given account:
To reset the current counts to zero for all accounts, issue a FLUSH
USER_RESOURCES statement. The counts also can be reset by
reloading the grant tables (for example, with a FLUSH PRIVILEGES
statement or a mysqladmin reload command).
The counts for an individual account can be set to zero by re-granting it
any of its limits. To do this, use GRANT USAGE as described earlier and
specify a limit value equal to the value that the account currently has.
Counter resets do not affect the MAX_USER_CONNECTIONS limit.
All counts begin at zero when the server starts; counts are not carried over
through a restart.
Self Assessment Questions
8. The setting ___________ method is strictly global, and does not allow
for management of individual accounts.

Sikkim Manipal University

Page No. 268

RDBMS with MySQL

Unit 12

12.7 Assigning Account Passwords


Passwords may be assigned from the command line by using the
mysqladmin command:
shell> mysqladmin -u user_name -h host_name password "newpwd"

The account for which this command resets the password is the one with a
user table row that matches user_name in the User column and the client
host from which you connect in the Host column.
Another way to assign a password to an account is to issue a SET
PASSWORD statement:
mysql> SET PASSWORD FOR 'jeffrey'@'%' = PASSWORD('biscuit');

Only users such as root that have update access to the mysql database can
change the password for other users. If you are not connected as an
anonymous user, you can change your own password by omitting the FOR
clause:
mysql> SET PASSWORD = PASSWORD('biscuit');

You can also use a GRANT USAGE statement at the global level (ON *.*) to
assign a password to an account without affecting the account's current
privileges:
mysql> GRANT USAGE ON *.* TO 'jeffrey'@'%' IDENTIFIED BY 'biscuit';

Although it is generally preferable to assign passwords using one of the


preceding methods, you can also do so by modifying the user table directly:
To establish a password when creating a new account, provide a value
for the Password column:
shell> mysql -u root mysql
mysql> INSERT INTO user (Host,User,Password)
-> VALUES('%','jeffrey',PASSWORD('biscuit'));
mysql> FLUSH PRIVILEGES;

Sikkim Manipal University

Page No. 269

RDBMS with MySQL

Unit 12

To change the password for an existing account, use UPDATE to set the
Password column value:
shell> mysql -u root mysql
mysql> UPDATE user SET Password = PASSWORD('bagel')
-> WHERE Host = '%' AND User = 'francis';
mysql> FLUSH PRIVILEGES;

When you assign an account a non-empty password using SET


PASSWORD, INSERT, or UPDATE, you must use the PASSWORD()
function to encrypt it. PASSWORD() is necessary because the user table
stores passwords in encrypted form, not as plaintext. If you forget that fact,
you are likely to set passwords like this:
shell> mysql -u root mysql
mysql> INSERT INTO user (Host,User,Password)
-> VALUES('%','jeffrey','biscuit');
mysql> FLUSH PRIVILEGES;

The result is that the literal value 'biscuit' is stored as the password in the
user table, not the encrypted value. When jeffrey attempts to connect to the
server using this password, the value is encrypted and compared to the
value stored in the user table. However, the stored value is the literal string
'biscuit', so the comparison fails and the server rejects the connection:
shell> mysql -u jeffrey -pbiscuit test
Access denied

If you assign passwords using the GRANT ... IDENTIFIED BY statement or


the mysqladmin password command, they both take care of encrypting
the password for you. In these cases, using PASSWORD() function is
unnecessary.

12.8 Keeping Your Password Secure


On an administrative level, you should never grant access to the user grant
table to any non-administrative accounts.
When you run a client program to connect to the MySQL server, it is
inadvisable to specify your password in a way that exposes it to discovery
by other users. The methods you can use to specify your password when

Sikkim Manipal University

Page No. 270

RDBMS with MySQL

Unit 12

you run client programs are listed here, along with an assessment of the
risks of each method:
Use a -pyour_pass or password=your_pass option on the command
line. For example:
shell> mysql -u francis -pfrank db_name

This is convenient but insecure, because your password becomes


visible to system status programs such as ps that may be invoked by
other users to display command lines. MySQL clients typically overwrite
the command-line password argument with zeros during their
initialization sequence. However, there is still a brief interval during
which the value is visible. On some systems this strategy is ineffective,
anyway, and the password remains visible to ps. (SystemV Unix
systems and perhaps others are subject to this problem.)
Use the -p or password option with no password value specified. In this
case, the client program solicits the password from the terminal:
shell> mysql -u francis -p db_name
Enter password: ********

The * characters indicate where you enter your password. The


password is not displayed as you enter it.
It is more secure to enter your password this way than to specify it on
the command line because it is not visible to other users. However, this
method of entering a password is suitable only for programs that you run
interactively. If you want to invoke a client from a script that runs noninteractively, there is no opportunity to enter the password from the
terminal. On some systems, you may even find that the first line of your
script is read and interpreted (incorrectly) as your password.
Store your password in an option file. For example, on Unix you can list
your password in the [client] section of the .my.cnf file in your home
directory:
[client]
password=your_pass

Sikkim Manipal University

Page No. 271

RDBMS with MySQL

Unit 12

If you store your password in .my.cnf, the file should not be accessible to
anyone but yourself. To ensure this, set the file access mode to 400 or
600. For example:
shell> chmod 600 .my.cnf

Store your password in the MYSQL_PWD environment variable. This


method of specifying your MySQL password must be considered
extremely insecure and should not be used. Some versions of ps
include an option to display the environment of running processes. If you
set MYSQL_PWD, your password is exposed to any other user who
runs ps. Even on systems without such a version of ps, it is unwise to
assume that there are no other methods by which users can examine
process environments.
All in all, the safest methods are to have the client program prompt for the
password or to specify the password in a properly protected option file.
Self Assessment Questions
9. Passwords may be assigned from the command line by using the ____
command.

12.9 Account Management Statements


12.9.1 DROP USER Syntax
DROP USER user_name

The DROP USER statement deletes a MySQL account that doesnt have
any privileges. It serves to remove the account record from the user table.
The account is named using the same format as for GRANT or REVOKE;
for example, 'jeffrey'@'localhost'. The user and host parts of the account
name correspond to the User and Host column values of the user table
record for the account.
To remove a MySQL user account, you should use the following procedure,
performing the steps in the order shown:
1. Use SHOW GRANTS to determine what privileges the account has.

Sikkim Manipal University

Page No. 272

RDBMS with MySQL

Unit 12

2. Use REVOKE to revoke the privileges displayed by SHOW GRANTS.


This removes records for the account from all the grant tables except the
user table, and revokes any global privileges listed in the user table.
3. Delete the account by using DROP USER to remove the user table
record.
12.9.2 GRANT and REVOKE Syntax
GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ...
ON {tbl_name | * | *.* | db_name.*}
TO user_name [IDENTIFIED BY [PASSWORD] 'password']
[, user_name [IDENTIFIED BY [PASSWORD] 'password']] ...
[REQUIRE
NONE |
[{SSL| X509}]
[CIPHER cipher [AND]]
[ISSUER issuer [AND]]
[SUBJECT subject]]
[WITH [GRANT OPTION | MAX_QUERIES_PER_HOUR # |
MAX_UPDATES_PER_HOUR # |
MAX_CONNECTIONS_PER_HOUR #]]
REVOKE priv_type [(column_list)] [, priv_type [(column_list)]] ...
ON {tbl_name | * | *.* | db_name.*}
FROM user_name [, user_name] ...

REVOKE ALL PRIVILEGES,


[, user_name] ...

GRANT

OPTION

FROM

user_name

GRANT and REVOKE are implemented in MySQL 3.22.11 or later.


The GRANT and REVOKE statements allow system administrators to create
user accounts and to grant and revoke rights to MySQL accounts at four
privilege levels:
Global level
Global privileges apply to all databases on a given server. These privileges
are stored in the mysql.user table. GRANT ALL ON *.* and REVOKE ALL
ON *.* grant and revoke only global privileges.
Database level
Database privileges apply to all tables in a given database. These privileges
are stored in the mysql.db and mysql.host tables. GRANT ALL ON db.* and
REVOKE ALL ON db.* grant and revoke only database privileges.

Sikkim Manipal University

Page No. 273

RDBMS with MySQL

Unit 12

Table level
Table privileges apply to all columns in a given table. These privileges are
stored in the mysql.tables_priv table. GRANT ALL ON db.table and
REVOKE ALL ON db.table grant and revoke only table privileges.
Column level
Column privileges apply to single columns in a given table. These privileges
are stored in the mysql.columns_priv table. When using REVOKE you must
specify the same columns that were granted.
To make it easy to revoke all privileges for a user, MySQL 4.1.2 has added
the syntax:
REVOKE ALL PRIVILEGES,
[, user_name ...]

GRANT

OPTION

FROM

user_name

This drops all database, table, and column level privileges for the user.
For the GRANT and REVOKE statements, priv_type can be specified as
any of the following:
Table 12.1 Privilege Types and Descriptions

Privilege Type

Description

ALL [PRIVILEGES]

Sets all simple privileges except GRANT


OPTION

ALTER

Allows use of ALTER TABLE

CREATE

Allows use of CREATE TABLE

CREATE TEMPORARY TABLES

Allows use of CREATE TEMPORARY TABLE

DELETE

Allows use of DELETE

DROP

Allows use of DROP TABLE

EXECUTE

Allows the user to run stored procedures


(MySQL 5.0)

FILE

Allows use of SELECT ... INTO OUTFILE and


LOAD DATA INFILE

INDEX

Allows use of CREATE INDEX and DROP


INDEX

INSERT

Allows use of INSERT

LOCK TABLES

Allows use of LOCK TABLES on tables for


which you have the SELECT privilege

PROCESS

Allows use of SHOW FULL PROCESSLIST

Sikkim Manipal University

Page No. 274

RDBMS with MySQL

Unit 12

REFERENCES

Not yet implemented

RELOAD

Allows use of FLUSH

REPLICATION CLIENT

Gives the right to the user to ask where the


slave or master servers are

REPLICATION SLAVE

Needed for replication slaves (to read binary


log events from the master)

SELECT

Allows use of SELECT

SHOW DATABASES

SHOW DATABASES shows all databases

SHUTDOWN

Allows use of mysqladmin shutdown

SUPER

Allows use of CHANGE MASTER, KILL thread,


PURGE MASTER LOGS, and SET GLOBAL
statements, the mysqladmin debug command;
allows you to connect
(once) even if
max_connections is reached

UPDATE

Allows use of UPDATE

USAGE

Synonym for no privileges

GRANT OPTION

Allows privileges to be granted

USAGE can be used when you want to create a user that has no privileges.
The privileges CREATE TEMPORARY TABLES, EXECUTE, LOCK
TABLES, REPLICATION ..., SHOW DATABASES and SUPER are new for
in MySQL 4.0.2. To use these new privileges after upgrading to 4.0.2, you
must run the mysql_fix_privilege_tables script.
In older MySQL versions that do not have the SUPER privilege, the
PROCESS privilege can be used instead.
To revoke the GRANT OPTION privilege from a user, use a priv_type value
of GRANT OPTION:
mysql> REVOKE GRANT OPTION ON ... FROM ...;

The only priv_type values you can specify for a table are SELECT, INSERT,
UPDATE, DELETE, CREATE, DROP, GRANT OPTION, INDEX, and
ALTER.
The only priv_type values you can specify for a column (that is, when you
use a column_list clause) are SELECT, INSERT, and UPDATE.
Sikkim Manipal University

Page No. 275

RDBMS with MySQL

Unit 12

MySQL allows you to create database level privileges even if the database
doesnt exist, to make it easy to prepare for database use. However, MySQL
currently does not allow you to create table level grants if the table doesnt
exist.
MySQL will not automatically revoke any privileges even if you drop a table
or drop a database.
You can set global privileges by using ON *.* syntax. You can set database
privileges by using ON db_name.* syntax. If you specify ON * and you have
a current database, the privileges will be granted in that database. (Warning:
If you specify ON * and you dont have a current Database, the privileges
granted will be global!)
12.9.3 SET PASSWORD Syntax
SET PASSWORD = PASSWORD('some password')
SET PASSWORD FOR user = PASSWORD('some password')

The SET PASSWORD statement assigns a password to an existing MySQL


account.
The first syntax sets the password for the current user. Any client that has
connected to the server using a non-anonymous account can change the
password for that account.
The second syntax sets the password for a specific account on the current
server host. Only clients with access to the mysql database can do this. The
user value should be given in user_name@host_name format, where
user_name and host_name are exactly as they are listed in the User and
Host columns of the mysql.user table entry. For example, if you had an entry
with User and Host fields of 'bob' and '%.loc.gov', you would write the
statement like this:
mysql> SET PASSWORD FOR 'bob'@'%.loc.gov' =

PASSWORD('newpass');

That is equivalent to the following statements:


mysql> UPDATE mysql.user SET Password=PASSWORD('newpass')
-> WHERE User='bob' AND Host='%.loc.gov';
mysql> FLUSH PRIVILEGES;

Sikkim Manipal University

Page No. 276

RDBMS with MySQL

Unit 12

12.10 Summary
This unit covers the topics of setting user accounts and privileges for
individual accounts for clients of your MySQL server.
1. Adding New User Accounts to MySQL: You can create MySQL
accounts in two ways:
By using statements intended for creating accounts, such as
CREATE USER or GRANT
By manipulating the MySQL grant tables directly with statements
such as INSERT, UPDATE, or DELETE
The preferred method is to use account-creation statements because they
are more concise and less error-prone.
2. MySQL Usernames and Passwords: A MySQL account is defined in
terms of a username and the client host or hosts from which the user
can connect to the server. The account also has a password. There are
several distinctions between the way usernames and passwords are
used by MySQL and the way they are used by your operating system
3. Securing the Initial MySQL Accounts: The grant tables define the
initial MySQL user accounts and their access privileges. The stting up of
the accounts and corresponding passwords is discussed here.
4. Removing User Accounts from MySQL: The DROP USER statement
removes one or more MySQL accounts. To use it, you must have the
global CREATE USER privilege or the DELETE privilege for the mysql
database.
5. Limiting Account Resources: One means of limiting use of MySQL
server resources is to set the max_user_connections system variable to
a non-zero value. However, this method is strictly global, and does not
allow for management of individual accounts. In addition, it limits only
the number of simultaneous connections made using a single account,
and not what a client can do once connected. Both types of control are
interest to many MySQL administrators, particularly those working for
Internet Service Providers.
6. Assigning Account Passwords: Passwords may be assigned from the
command line by using the mysqladmin command. The syntax and
corresponding settings in this regard are discussed here.
7. Keeping Your Password Secure: When you run a client program to
connect to the MySQL server, it is inadvisable to specify your password
Sikkim Manipal University

Page No. 277

RDBMS with MySQL

Unit 12

in a way that exposes it to discovery by other users. The methods you


can use to specify your password when you run client programs are
listed here, along with an assessment of the risks of each method
8. Account Management Statements: This topic discusses the Account
Management statements like Drop User, Grant User privileges, and so
on.

12.11 Terminal Questions


1. Describe the methods of adding new user accounts in MySQL.
2. Describe the ways of Limiting Account Resources in MySQL.
3. Give the following syntaxes:
a. DROP USER
b. GRANT and REVOKE
c. SET PASSWORD

12.12 Answers
Self Assessment Questions
1. FLUSH
2. mysql>mysql user = root mysql
3. Usernames
4. its own
5. pre-initialized
6. Anonymous
7. CREATE USER or DELETE
8. setting of max_user_connections variable to a zero value
9. mysqladmin
Terminal Questions
1. You can create MySQL accounts in two ways:
By using statements intended for creating accounts, such as
CREATE USER or GRANT
By manipulating the MySQL grant tables directly with statements
such as INSERT, UPDATE, or DELETE
The preferred method is to use account-creation statements because
they are more concise and less error-prone.

Sikkim Manipal University

Page No. 278

RDBMS with MySQL

Unit 12

Another option for creating accounts is to use one of several available


third-party programs that offer capabilities for MySQL account
administration. phpMyAdmin is one such program.
(Refer to Section 12.2)
2. (Refer to Section 12.6)
One means of limiting use of MySQL server resources is to set the
max_user_connections system variable to a non-zero value. However,
this method is strictly global, and does not allow for management of
individual accounts. In addition, it limits only the number of simultaneous
connections made using a single account, and not what a client can do
once connected. Both types of control are interest to many MySQL
administrators, particularly those working for Internet Service Providers.
In MySQL 5.0, you can limit the following server resources for individual
accounts:
The number of queries that an account can issue per hour
The number of updates that an account can issue per hour
The number of times an account can connect to the server per hour
3. (Refer to Section 12.9)
a. DROP USER Syntax: DROP USER user_name
b. GRANT and REVOKE Syntax:
GRANT priv_type [(column_list)] [, priv_type
[(column_list)]] ...
ON {tbl_name | * | *.* | db_name.*}
TO user_name [IDENTIFIED BY [PASSWORD] 'password']
[, user_name [IDENTIFIED BY [PASSWORD] 'password']] ...
[REQUIRE
NONE |
[{SSL| X509}]
[CIPHER cipher [AND]]
[ISSUER issuer [AND]]
[SUBJECT subject]]
[WITH [GRANT OPTION | MAX_QUERIES_PER_HOUR # |
MAX_UPDATES_PER_HOUR # |
MAX_CONNECTIONS_PER_HOUR #]]
REVOKE priv_type [(column_list)] [, priv_type
[(column_list)]] ...
ON {tbl_name | * | *.* | db_name.*}
FROM user_name [, user_name] ...

Sikkim Manipal University

Page No. 279

RDBMS with MySQL

Unit 12

c. SET PASSWORD Syntax:


SET PASSWORD = PASSWORD('some password')
SET PASSWORD FOR user = PASSWORD('some
password')

Sikkim Manipal University

Page No. 280

RDBMS with MySQL

Unit 13

Unit 13

General Security Issues

Structure
13.1 Introduction
Objectives
13.2 General Security Guidelines
13.3 Making MySQL Secure Against Attackers
13.4 Startup Options for mysqld concerning Security
13.5 Security Issues with LOAD DATA LOCAL
13.6 Summary
13.7 Terminal Questions
13.8 Answers

13.1 Introduction
This unit speaks about the general security guidelines to be followed when
dealing with databases. It discusses the issues of possible attacks on the
databases and the strategies to be followed against the attackers. It
discusses about the mysqld option used during server startup for
maintaining the security. It also describes the security steps to be followed
when loading data from external sources into the database.

Objectives
After studying this unit, you should be able to:
discuss the importance and applications of general security guidelines
explain various procedures followed for making MySQL secure
describe the startup options for mysqld concerning security
discuss various aspects of security by using LOAD DATA LOCAL

13.2 General Security Guidelines


This section describes some general security issues to be aware of and
what you can do to make your MySQL installation more secure against
attack or misuse.
Anyone using MySQL on a computer connected to the Internet should read
this section to avoid the most common security mistakes. In discussing
security, we emphasize the necessity of fully protecting the entire server

Sikkim Manipal University

Page No. 281

RDBMS with MySQL

Unit 13

host (not just the MySQL server) against all types of applicable attacks:
eavesdropping, altering, playback, and denial of service.
We do not cover all aspects of availability and fault tolerance here.
MySQL uses security based on Access Control Lists (ACLs) for all
connections, queries, and other operations that users can attempt to
perform. There is also support for SSL-encrypted connections between
MySQL clients and servers. Many of the concepts discussed here are not
specific to MySQL at all; the same general ideas apply to almost all
applications.
When running MySQL, follow these guidelines whenever possible:
Do not ever give anyone (except MySQL root accounts) access to
the user table in the mysql database! This is critical.

Learn the MySQL access privilege system. The GRANT and


REVOKE statements are used for controlling access to MySQL. Do
not grant more privileges than necessary. Never grant privileges to
all hosts.
Checklist:
Try mysql -u root. If you are able to connect successfully to the
server without being asked for a password, anyone can connect to
your MySQL server as the MySQL root user with full privileges!
Review the MySQL installation instructions, paying particular
attention to the information about setting a root password.
Use the SHOW GRANTS statement to check which accounts have
access to what. Then use the REVOKE statement to remove those
privileges that are not necessary.
Do not store any plain-text passwords in your database. Instead, use
MD5(), SHA1(), or some other one-way hashing function and store
the hash value.
Do not choose passwords from dictionaries. Special programs exist
to break passwords. Even passwords like xfish98 are very bad.
Much better is duag98 which contains the same word fish but
typed one key to the left on a standard QWERTY keyboard. Another
method is to use a password that is taken from the first characters of
each word in a sentence (for example, Mary had a little lamb
results in a password of Mhall). The password is easy to remember
Sikkim Manipal University

Page No. 282

RDBMS with MySQL

Unit 13

and type, but difficult to guess for someone who does not know the
sentence.
Invest in a firewall. This protects you from at least 50% of all types of
exploits in any software. Put MySQL behind the firewall or in a
demilitarized zone (DMZ).
Checklist:
Try to scan your ports from the Internet using a tool such as nmap.
MySQL uses port 3306 by default. This port should not be accessible
from untrusted hosts. Another simple way to check whether or not
your MySQL port is open is to try the following command from some
remote machine, where server_host is the hostname or IP number of
the host on which your MySQL server runs:
shell> telnet server_host 3306
If you get a connection and some garbage characters, the port is
open, and should be closed on your firewall or router, unless you
really have a good reason to keep it open. If telnet hangs or the
connection is refused, the port is blocked, which is how you want it to
be.

Do not trust any data entered by users of your applications. They can try
to trick your code by entering special or escaped character sequences in
Web forms, URLs, or whatever application you have built. Be sure that
your application remains secure if a user enters something like ; DROP
DATABASE mysql;. This is an extreme example, but large security
leaks and data loss might occur as a result of hackers using similar
techniques, if you do not prepare for them.
A common mistake is to protect only string data values. Remember to
check numeric data as well. If an application generates a query such as
SELECT * FROM table WHERE ID=234 when a user enters the value
234, the user can enter the value 234 OR 1=1 to cause the application
to generate the query SELECT * FROM table WHERE ID=234 OR 1=1.
As a result, the server retrieves every row in the table. This exposes
every row and causes excessive server load. The simplest way to
protect from this type of attack is to use single quotes around the
numeric constants: SELECT * FROM table WHERE ID='234'. If the user
enters extra information, it all becomes part of the string. In a numeric
Sikkim Manipal University

Page No. 283

RDBMS with MySQL

Unit 13

context, MySQL automatically converts this string to a number and strips


any trailing non-numeric characters from it.
Sometimes people think that if a database contains only publicly
available data, it need not be protected. This is incorrect. Even if it is
allowable to display any row in the database, you should still protect
against denial of service attacks (for example, those that are based on
the technique in the preceding paragraph that causes the server to
waste resources). Otherwise, your server becomes unresponsive to
legitimate users.
Many application programming interfaces provide a means of
escaping special characters in data values. Properly used, this
prevents application users from entering values that cause the
application to generate statements that have a different effect than
you intend:
MySQL C API: Use the mysql_real_escape_string() API call.
MySQL++: Use the escape and quote modifiers for query
streams.
PHP: Use the mysql_real_escape_string() function (available as
of PHP 4.3.0, prior to that PHP version use mysql_
escape_string(), and prior to PHP 4.0.3, use addslashes() ). Note
that only mysql_real_escape_string() is character set-aware; the
other functions can be bypassed when using (invalid) multibyte
character sets. In PHP 5, you can use the mysqli extension,
which supports the improved MySQL authentication protocol and
passwords, as well as prepared statements with placeholders.
Perl DBI: Use placeholders or the quote() method.
Ruby DBI: Use placeholders or the quote() method.
Java JDBC: Use a PreparedStatement object and placeholders.
Other programming interfaces might have similar capabilities.
Do not transmit plain (unencrypted) data over the Internet. This
information is accessible to everyone who has the time and ability to
intercept it and use it for their own purposes. Instead, use an encrypted
protocol such as SSL or SSH. MySQL supports internal SSL
connections as of version 4.0. Another technique is to use SSH port-

Sikkim Manipal University

Page No. 284

RDBMS with MySQL

Unit 13

forwarding to create an encrypted (and compressed) tunnel for the


communication.
Learn to use the tcpdump and strings utilities. In most cases, you can
check whether MySQL data streams are unencrypted by issuing a
command like the following:
shell> tcpdump -l -i eth0 -w - src or dst port 3306 | strings
This works under Linux and should work with small modifications under
other systems.
Self Assessment Questions
1. MySQL uses security based on ______ for all connections, queries, and
other operations that users can attempt to perform.
2. MySQL uses port ____ by default.

13.3 Making MySQL Secure Against Attackers


When you connect to a MySQL server, you should use a password. The
password is not transmitted in clear text over the connection. Password
handling during the client connection sequence was upgraded in MySQL
4.1.1 to be very secure. If you are still using pre-4.1.1-style passwords, the
encryption algorithm is not as strong as the newer algorithm. With some
effort, a clever attacker who can sniff the traffic between the client and the
server can crack the password.
All other information is transferred as text, and can be read by anyone who
is able to watch the connection. If the connection between MySQL Server
Administration 473 the client and the server goes through an untrusted
network, and you are concerned about this, you can use the compressed
protocol to make traffic much more difficult to decipher. You can also use
MySQL's internal SSL support to make the connection even more secure.
Alternatively, use SSH to get an encrypted TCP/IP connection between a
MySQL server and a MySQL client. You can find an Open Source SSH
client at http://www.openssh.org/, and a commercial SSH client at
http://www.ssh.com/.
To make a MySQL system secure, you should strongly consider the
following suggestions:

Sikkim Manipal University

Page No. 285

RDBMS with MySQL

Unit 13

Require all MySQL accounts to have a password. A client program does


not necessarily know the identity of the person running it. It is common
for client/server applications that the user can specify any username to
the client program. For example, anyone can use the mysql program to
connect as any other person simply by invoking it as mysql -u
other_user db_name if other_user has no password. If all accounts have
a password, connecting using another user's account becomes much
more difficult.
Never run the MySQL server as the Unix root user. This is extremely
dangerous, because any user with the FILE privilege is able to cause
the server to create files as root (for example, ~root/.bashrc). To prevent
this, mysqld refuses to run as root unless that is specified explicitly using
the --user=root option.
mysqld can (and should) be run as an ordinary, unprivileged user
instead. You can create a separate Unix account named mysql to make
everything even more secure. Use this account only for administering
MySQL. To start mysqld as a different Unix user, add a user option that
specifies the username in the [mysqld] group of the my.cnf option file
where you specify server options. For example:
[mysqld]
user=mysql

This causes the server to start as the designated user whether you start
it manually or by using mysqld_safe or mysql.server.

Do not allow the use of symlinks to tables. (This capability can be


disabled with the --skip-symbolic-links option.) This is especially
important if you run mysqld as root, because anyone that has write
access to the server's data directory then could delete any file in the
system!
Make sure that the only Unix user account with read or write privileges in
the database directories is the account that is used for running mysqld.

Do not grant the PROCESS or SUPER privilege to non-administrative


users. The output of mysqladmin processlist and SHOW PROCESSLIST
shows the text of any statements currently being executed, so any user
who is allowed to see the server process list might be able to see

Sikkim Manipal University

Page No. 286

RDBMS with MySQL

Unit 13

statements issued by other users such as UPDATE user SET


password = PASSWORD('not_secure').
mysqld reserves an extra connection for users who have the SUPER
privilege, so that a MySQL root user can log in and check server activity
even if all normal connections are in use.

The SUPER privilege can be used to terminate client connections,


change server operation by changing the value of system variables, and
control replication servers.
Do not grant the FILE privilege to non-administrative users. Any user
that has this privilege can write a file anywhere in the filesystem with
the privileges of the mysqld daemon. To make this a bit safer, files
generated with SELECT ... INTO OUTFILE do not overwrite existing
files and are writable by everyone.
The FILE privilege may also be used to read any file that is worldreadable or accessible to the Unix user that the server runs as. With
this privilege, you can read any file into a database table. This could
be abused, for example, by using LOAD DATA to load / etc/passwd
into a table, which then can be displayed with SELECT.
If you do not trust your DNS, you should use IP numbers rather than
hostnames in the grant tables. In any case, you should be very careful
about creating grant table entries using hostname values that contain
wildcards.
If you want to restrict the number of connections allowed to a single
account, you can do so by setting the max_user_connections variable in
mysqld. The GRANT statement also supports resource control options
for limiting the extent of server use allowed to an account.

Self Assessment Questions


3. If the connection between the client and the server goes through an
untrusted network, and you are concerned about this, you can use the
______ protocol to make traffic much more difficult to decipher.
4. With the _____ privilege, you can read any file into a database table.

Sikkim Manipal University

Page No. 287

RDBMS with MySQL

Unit 13

13.4 Startup Options for mysqld concerning Security


The following mysqld options affect security:
Table 13.1: mysqld Security Option/Variable Summary

--allow-suspicious-udfs
This option controls whether user-defined functions that have only an
xxx symbol for the main function can be loaded. By default, the option is
off and only UDFs that have at least one auxiliary symbol can be loaded;
this prevents attempts at loading functions from shared object files other
than those containing legitimate UDFs.
--local-infile[={0|1}]
If you start the server with --local-infile=0, clients cannot use LOCAL in
LOAD DATA statements.
--old-passwords
Force the server to generate short (pre-4.1) password hashes for new
passwords. This is useful for compatibility when the server must support
older client programs.
--safe-show-database (OBSOLETE)
In previous versions of MySQL, this option caused the SHOW
DATABASES statement to display the names of only those databases

Sikkim Manipal University

Page No. 288

RDBMS with MySQL

Unit 13

for which the user had some kind of privilege. In MySQL 5.1, this option
is no longer available as this is now the default behavior, and there is a
SHOW DATABASES privilege that can be used to control access to
database names on a per-account basis.
--safe-user-create
If this option is enabled, a user cannot create new MySQL users by
using the GRANT statement unless the user has the INSERT privilege
for the mysql.user table or any column in the table. If you want a user to
have the ability to create new users that have those privileges that the
user has the right to grant, you should grant the user the following
privilege:
GRANT INSERT(user) ON mysql.user TO 'user_name'@'host_name';

This ensures that the user cannot change any privilege columns directly,
but has to use the GRANT statement to give privileges to other users.
--secure-auth
Disallow authentication for accounts that have old (pre-4.1) passwords.
The mysql client also has a --secure-auth option, which prevents
connections to a server if the server requires a password in old format
for the client account.
--secure-file-priv=path
This option limits the effect of the LOAD_FILE() function and the LOAD
DATA and SELECT ... INTO OUTFILE statements to work only with files
in the specified directory. This option was added in MySQL 5.1.17.
--skip-grant-tables
This option causes the server not to use the privilege system at all. This
gives anyone with access to the server unrestricted access to all
databases. You can cause a running server to start using the grant
tables again by executing mysqladmin flushprivileges or mysqladmin
reload command from a system shell, or by issuing a MySQL FLUSH
PRIVILEGES statement. This option also suppresses loading of plugins
and user-defined functions (UDFs).
--skip-merge
Disable the MERGE storage engine. This option was added in MySQL
5.1.12. It can be used if the following behavior is undesirable:

Sikkim Manipal University

Page No. 289

RDBMS with MySQL

Unit 13

If a user has access to MyISAM table t, that user can create a MERGE
table m that accesses t. However, if the user's privileges on t are
subsequently revoked, the user can continue to access t by doing so
through m.
--skip-name-resolve
Hostnames are not resolved. All Host column values in the grant tables
must be IP numbers or localhost.
--skip-networking
Do not allow TCP/IP connections over the network. All connections to
mysqld must be made via Unix socket files.
--skip-show-database
With this option, the SHOW DATABASES statement is allowed only to
users who have the SHOW DATABASES privilege, and the statement
displays all database names. Without this option, SHOW DATABASES
is allowed to all users, but displays each database name only if the user
has the SHOW DATABASES privilege or some privilege for the
database. Note that any global privilege is a privilege for the database.
--ssl*
Options that begin with --ssl specify whether to allow clients to connect
via SSL and indicate where to find SSL keys and certificates.

Self Assessment Questions


5. If the ______ option of mysqld is enabled, a user cannot create new
MySQL users by using the GRANT statement unless the user has the
INSERT privilege for the mysql.user table or any column in the table.

13.5 Security Issues with LOAD DATA LOCAL


The LOAD DATA statement can load a file that is located on the server host,
or it can load a file that is located on the client host when the LOCAL
keyword is specified.
There are two potential security issues with supporting the LOCAL version
of LOAD DATA statements:
The transfer of the file from the client host to the server host is initiated
by the MySQL server. In theory, a patched server could be built that
would tell the client program to transfer a file of the server's choosing
rather than the file named by the client in the LOAD DATA statement.
Sikkim Manipal University

Page No. 290

RDBMS with MySQL

Unit 13

Such a server could access any file on the client host to which the client
user has read access.

In a Web environment where the clients are connecting from a Web


server, a user could use LOAD DATA LOCAL to read any files that the
Web server process has read access to (assuming that a user could run
any command against the SQL server). In this environment, the client
with respect to the MySQL server actually is the Web server, not the
remote program being run by the user who connects to the Web server.
To deal with these problems, we changed how LOAD DATA LOCAL is
handled as of MySQL 3.23.49 and MySQL 4.0.2 (4.0.13 on Windows):

By default, all MySQL clients and libraries in binary distributions are


compiled with the --enable-local-infile option, to be compatible with
MySQL 3.23.48 and before.

If you build MySQL from source but do not invoke configure with the -enable-local-infile option, LOAD DATA LOCAL cannot be used by any
client
unless
it
is
written
explicitly
to
invoke
mysql_options(...MYSQL_OPT_LOCAL_INFILE, 0).

You can disable all LOAD DATA LOCAL commands from the server side
by starting mysqld with the --local-infile=0 option.

For the mysql command-line client, LOAD DATA LOCAL can be enabled
by specifying the --local-infile[=1] option, or disabled with the --localinfile=0 option. Similarly, for mysqlimport, the --local or -L option enables
local data file loading. In any case, successful use of a local loading
operation requires that the server is enabled to allow it.

If you use LOAD DATA LOCAL in Perl scripts or other programs that
read the [client] group from option files, you can add the local-infile=1
option to that group. However, to keep this from causing problems for
programs that do not understand local-infile, specify it using the looseprefix:
[client]
loose-local-infile=1

If LOAD DATA LOCAL INFILE is disabled, either in the server or the


client, a client that attempts to issue such a statement receives the
following error message:
ERROR 1148: The used command is not allowed with this MySQL version

Sikkim Manipal University

Page No. 291

RDBMS with MySQL

Unit 13

13.6 Summary
This unit speaks about the general security guidelines to be followed when
dealing with databases. It discusses the issues of possible attacks on the
databases and the strategies to be followed against the attackers.
1. General Security Guidelines: This section describes some general
security issues to be aware of and what you can do to make your
MySQL installation more secure against attack or misuse.
2. Making MySQL Secure Against Attackers: When you connect to a
MySQL server, you should use a password. The password is not
transmitted in clear text over the connection. The methods of
transmitting the text based passwords in encrypted format is discussed
here.
3. Startup Options for mysqld concerning Security: It describes various
security options and variables available with mysqld for secure
transmission of data.
4. Security Issues with LOAD DATA LOCAL: It covers the theory behind
the Security issues and their handling with the Load Data Local Option.

13.7 Terminal Questions


1. Describe some of the general security guidelines of MySQL server.
2. Describe the methods used to make MySQL Secure.

13.8 Answers
Self Assessment Questions
1. Access Control Lists (ACLs)
2. 3306
3. compressed
4. FILE
5. --safe-user-create
Terminal Questions
1. The following are the general security guidelines to be followed:
a. Do not ever give anyone (except MySQL root accounts) access to
the user table in the mysql database!
b. Learn the MySQL access privilege system.
c. Do not store any plain-text passwords in your database.
Sikkim Manipal University

Page No. 292

RDBMS with MySQL

Unit 13

d. Do not choose passwords from dictionaries.


e. Invest in a firewall.
(Refer to Section 13.2)
2. To make a MySQL system secure, you should strongly consider the
following suggestions:
a. Require all MySQL accounts to have a password.
b. Never run the MySQL server as the Unix root user.
c. mysqld can (and should) be run as an ordinary, unprivileged user.
d. Do not allow the use of symlinks to tables.
e. Do not grant the PROCESS or SUPER privilege to nonadministrative users.

Sikkim Manipal University

Page No. 293

RDBMS with MySQL

Unit 14

Unit 14

Log Files

Structure
14.1 Introduction
Objectives
14.2 Error Log
14.3 The General Query Log
14.4 The Binary Log
14.5 The Slow Query Log
14.6 Log File Maintenance
14.7 Summary
14.8 Terminal Questions
14.9 Answers
14.1 Introduction
This unit deals with administration of historic information using log files
regarding the transaction done with the database server. .MySQL has
several different logs that can help you find out what is going on inside
mysqld:
Table 14.1: Log Types
Log Type

Information Written to Log

The error log

Problems encountered starting, running, or


stopping mysqld

The general query log

Established client connections and statements


received from clients

The binary log

All statements that change data (also used for


replication)

The slow query log

All queries that took more than long_query_time


seconds to execute or didn't use indexes

By default, all log files are created in the mysqld data directory. You can
force mysqld to close and reopen the log files (or in some cases switch to a
new log) by flushing the logs. Log flushing occurs when you issue a FLUSH
LOGS statement or execute mysqladmin flush-logs or mysqladmin refresh.

Sikkim Manipal University

Page No. 294

RDBMS with MySQL

Unit 14

Objectives
After studying this unit, you should be able to:
Define and discuss the importance of using Log files in MySQL
Describe the importance and usage of Error logs
Describe the importance and usage of General Query and Binary logs
Explain the usage of Slow Query log
Describe the purpose and usage of Log File Maintenance

14.2 Error Log


The error log contains information indicating when mysqld was started and
stopped and also any critical errors that occur while the server is running. If
mysqld notices a table that needs to be automatically checked or repaired, it
writes a message to the error log.
On some operating systems, the error log contains a stack trace if mysqld
dies. The trace can be used to determine where mysqld died.
You can specify where mysqld writes the error log with the --logerror[=file_name] option. If no file_name value is given, mysqld uses the
name host_name.err by default and writes the file in the data directory. If
you execute FLUSH LOGS, the error log is renamed with the suffix -old and
mysqld creates a new empty log file. (No renaming occurs if the --log-error
option was not given to mysqld.)
If you do not specify --log-error, or (on Windows) if you use the --console
option, errors are written to stderr, the standard error output. Usually this is
your terminal.
On Windows, error output is always written to the .err file if --console is not
given.
The --log-warnings option or log_warnings system variable can be used to
control warning logging to the error log. The default value is enabled (1).
Warning logging can be disabled using a value of 0. If the value is greater
than 1, aborted connections are written to the error log.
If you use mysqld_safe to start mysqld, mysqld_safe arranges for mysqld to
write error messages to a log file or (as of MySQL 5.1.20) to syslog.
In 5.1.21 and up, the default with no logging options is --skip-syslog, which
is compatible with the default behavior of writing an error log file for releases
Sikkim Manipal University

Page No. 295

RDBMS with MySQL

Unit 14

prior to 5.1.20. To explicitly specify use of an error log file, specify --logerror=file_name to mysqld_safe, and mysqld_safe will arrange for mysqld to
write messages to a log file. To use syslog instead, specify the --syslog
option.
Self Assessment Questions
1. The _____ log is used to establish client connections and statements
received from clients.
2. The _____ option specifies the destination for log output, if logging is
enabled, but the option does not in itself enable the logs.
3. If you execute ____ LOGS, the error log is renamed with the suffix -old
and mysqld creates a new empty log file.

14.3 The General Query Log


The general query log is a general record of what mysqld is doing. The
server writes information to this log when clients connect or disconnect, and
it logs each SQL statement received from clients. The general query log can
be very useful when you suspect an error in a client and want to know
exactly what the client sent to mysqld.
mysqld writes statements to the query log in the order that it receives them,
which might differ from the order in which they are executed. This logging
order contrasts to the binary log, for which statements are written after they
are executed but before any locks are released. (Also, the query log
contains all statements, whereas the binary log does not contain statements
that only select data.)
Before 5.1.6, the general query log destination is always a file. To enable
the general query log file, use the --log[=file_name] or -l [file_name] option.
To enable the general query log as of MySQL 5.1.6, start mysqld with the
log [=file_name] or - l [file_name] option, and optionally use --log-output to
specify the log destination. As of MySQL 5.1.29, --log and -l are deprecated:
Use --general_log to enable the general query log, and optionally -general_log_file=file_name to specify a log filename. --general_log takes an
optional argument of 1 or 0 to enable or disable the log.

Sikkim Manipal University

Page No. 296

RDBMS with MySQL

Unit 14

If you specify no filename for the general query log, the default name is
host_name.log in the data directory. If you specify a filename that is not an
absolute pathname, the server writes the file in the data directory.
When --log or -l is specified, the --general_log option also may be given as
of MySQL 5.1.12 to specify the initial general query log state. With no
argument or an argument of 0, the option disables the log. If omitted or
given with an argument of 1, the option enables the log.
For runtime control of the general query log, use the global general_log and
general_log_file system variables. Set general_log to 0 (or OFF) to disable
the log or to 1 (or ON) to enable it. Set general_log_file to specify the name
of the log file. If a log file already is open, it is closed and the new file is
opened.
When the general query log is enabled, output is written to any destinations
specified by the --log-output option or log_output system variable. If you
enable the log, the server opens the log file and writes startup messages to
it. However, logging of queries to the file does not occur unless the FILE log
destination is selected. If the destination is NONE, no queries are written
even if the general log is enabled. Setting the log filename has no effect on
logging if the log destination value does not contain FILE.
Server restarts and log flushing do not cause a new general query log file to
be generated (although flushing closes and reopens it). On Unix, you can
rename the file and create a new one by using the following commands:
shell> mv host_name.log host_name-old.log
shell> mysqladmin flush-logs
shell> cp host_name-old.log backup-directory
shell> rm host_name-old.log

Before 5.1.3, you cannot rename a log file on Windows while the server has
it open. You must stop the server and rename the file, and then restart the
server to create a new log file. As of 5.1.3, this applies only to the error log.
However, a stop and restart can be avoided by using FLUSH LOGS, which
causes the server to rename the error log with an -old suffix and open a new
error log.

Sikkim Manipal University

Page No. 297

RDBMS with MySQL

Unit 14

As of MySQL 5.1.12, you can disable the general query log at runtime:
SET GLOBAL general_log = 'OFF';

With the log disabled, rename the log file externally; for example, from the
command line. Then enable the log again:
SET GLOBAL general_log = 'ON;

This method works on any platform and does not require a server restart.
The session sql_log_off variable can be set to ON or OFF to disable or
enable general query logging for the current connection.

Self Assessment Questions


4. If you specify no filename for the general query log, the default name is
_____ in the data directory.

14.4 The Binary Log


The binary log contains all statements that update data. It also contains
statements that potentially could have updated it (for example, a DELETE
which matched no rows), unless row-based logging is used. Statements are
stored in the form of events that describe the modifications. The binary log
also contains information about how long each statement took that updated
data. The binary log has two important purposes:
For replication, the binary log is used on master replication servers as a
record of the statements to be sent to slave servers. The master server
sends the events contained in its binary log to its slaves, which execute
those events to make the same data changes that were made on the
master.
Certain data recovery operations require use of the binary log. After a
backup file has been restored, the events in the binary log that were
recorded after the backup was made are re-executed. These events
bring databases up to date from the point of the backup.
The binary log is not used for statements such as SELECT or SHOW that
do not modify data. If you want to log all statements (for example, to identify
a problem query), use the general query log.

Sikkim Manipal University

Page No. 298

RDBMS with MySQL

Unit 14

The format of the events recorded in the binary log is dependent on the
binary logging format. Three format types are supported, rowbased logging,
statement-based logging and mixed-base logging. The binary logging format
used depends on the MySQL version.
Running the server with the binary log enabled makes performance about
1% slower. However, the benefits of the binary log for restore operations
and in allowing you to set up replication generally outweigh this minor
performance decrement.
When started with the --log-bin[=base_name] option, mysqld writes a log file
containing all SQL statements that update data (both DDL and DML
statements). If no base_name value is given, the default name is the value
of the pid-file option (which by default is the name of host machine) followed
by -bin. If the basename is given, but not as an absolute pathname, the
server writes the file in the data directory.
If you supply an extension in the log name (for example, --logbin=base_name.extension), the extension is silently removed and ignored.
mysqld appends a numeric extension to the binary log basename to
generate binary log filenames. The number increases each time the server
creates a new log file, thus creating an ordered series of files. The server
creates a new file in the series each time it starts or flushes the logs. The
server also creates a new binary log file automatically when the current log's
size reaches max_binlog_size.
A binary log file may become larger than max_binlog_size if you are using
large transactions because a transaction is written to the file in one piece,
never split between files.
To keep track of which binary log files have been used, mysqld also creates
a binary log index file that contains the names of all used binary log files. By
default, this has the same basename as the binary log file, with the
extension '.index'. You can change the name of the binary log index file with
the --log-bin-index[=file_name] option. You should not manually edit this file
while mysqld is running; doing so would confuse mysqld.
You can delete all binary log files with the RESET MASTER statement, or a
subset of them with PURGE BINARY LOGS.

Sikkim Manipal University

Page No. 299

RDBMS with MySQL

Unit 14

Writes to the binary log file and binary log index file are handled in the same
way as writes to MyISAM tables.
The binary log format has some known limitations that can affect recovery
from backups.
A replication slave server by default does not write to its own binary log any
data modifications that are received from the replication master.
To log these modifications, start the slave with the --log-slave-updates
option.
Evaluation of update selection options: The server evaluates the options
for logging or ignoring updates to the binary log according to the following
rules:
1. Are there --binlog-do-db or --binlog-ignore-db rules?
No: Write the statement to the binary log and exit.
Yes: Go to the next step.
2. There are some rules (--binlog-do-db, --binlog-ignore-db, or both). Is
there a default database (has any database been selected by USE?)?
No: Do not write the statement, and exit.
Yes: Go to the next step.
3. There is a default database. Are there some --binlog-do-db rules?
Yes: Does the default database match any of the --binlog-do-db rules?
Yes: Write the statement and exit.
No: Do not write the statement, and exit.
No: Go to the next step.
4. There are some --binlog-ignore-db rules. Does the default database
match any of the --binlog-ignore-db rules?
Yes: Do not write the statement, and exit.
No: Write the query and exit.
Important Note: An exception is made in the rules just given for the
CREATE DATABASE, ALTER DATABASE, and DROP DATABASE
statements. In those cases, the database being created, altered, or dropped
replaces the default database when determining whether to log or ignore
updates.
For example, a slave running with only --binlog-do-db=sales does not write
to the binary log any statement for which the default database is different
Sikkim Manipal University

Page No. 300

RDBMS with MySQL

Unit 14

from sales (in other words, --binlog-do-db can sometimes mean ignore
other databases).
If you are using replication, you should not delete old binary log files until
you are sure that no slave still needs to use them. For example, if your
slaves never run more than three days behind, once a day you can execute
mysqladmin flush-logs on the master and then remove any logs that are
more than three days old. You can remove the files manually, but it is
preferable to use PURGE BINARY LOGS, which also safely updates the
binary log index file for you (and which can take a date argument).
If you are using replication, you should not delete old binary log files until
you are sure that no slave still needs to use them. For example, if your
slaves never run more than three days behind, once a day you can execute
mysqladmin flush-logs on the master and then remove any logs that are
more than three days old. You can remove the files manually, but it is
preferable to use PURGE BINARY LOGS, which also safely updates the
binary log index file for you (and which can take a date argument).
A client that has the SUPER privilege can disable binary logging of its own
statements by using a SET sql_log_bin=0 statement.
You can display the contents of binary log files with the mysqlbinlog utility.
This can be useful when you want to reprocess statements in the log. For
example, you can update a MySQL server from the binary log as follows:
shell> mysqlbinlog log_file | mysql -h server_name

Binary logging is done immediately after a statement completes but before


any locks are released or any commit is done. This ensures that the log is
logged in execution order.
Updates to non-transactional tables are stored in the binary log immediately
after execution. In MySQL 5.1.22 and earlier versions of MySQL 5.1, an
UPDATE statement using a stored function that modified a nontransactional table was not logged if it failed, and an INSERT ... ON
DUPLICATE KEY UPDATE statement that encountered a duplicate key
constraint but which did not actually change any data was not logged.

Sikkim Manipal University

Page No. 301

RDBMS with MySQL

Unit 14

Beginning with MySQL 5.1.23, both of these statements are written to the
binary log. (Bug#23333)
Within an uncommitted transaction, all updates (UPDATE, DELETE, or
INSERT) that change transactional tables such as BDB or InnoDB tables
are cached until a COMMIT statement is received by the server. At that
point, mysqld writes the entire transaction to the binary log before the
COMMIT is executed. When the thread that handles the transaction starts, it
allocates a buffer of binlog_cache_size to buffer statements.
If a statement is bigger than this, the thread opens a temporary file to store
the transaction. The temporary file is deleted when the thread ends.
Modifications to non-transactional tables cannot be rolled back. If a
transaction that is rolled back includes modifications to non-transactional
tables, the entire transaction is logged with a ROLLBACK statement at the
end to ensure that the modifications to those tables are replicated.
The Binlog_cache_use status variable shows the number of transactions
that used this buffer (and possibly a temporary file) for storing statements.
The Binlog_cache_disk_use status variable shows how many of those
transactions actually had to use a temporary file. These two variables can
be used for tuning binlog_cache_size to a large enough value that avoids
the use of temporary files.
The max_binlog_cache_size system variable (default 4GB, which is also the
maximum) can be used to restrict the total size used to cache a multiplestatement transaction. If a transaction is larger than this many bytes, it fails
and rolls back. The minimum value is 4096.
If you are using the binary log and row based logging, concurrent inserts are
converted to normal inserts for CREATE ... SELECT or INSERT ... SELECT
statement. This is done to ensure that you can re-create an exact copy of
your tables by applying the log during a backup operation. If you are using
statement based logging then the original statement is written to the log.
By default, the binary log is not synchronized to disk at each write. So if the
operating system or machine (not only the MySQL server) crashes, there is
a chance that the last statements of the binary log are lost. To prevent this,

Sikkim Manipal University

Page No. 302

RDBMS with MySQL

Unit 14

you can make the binary log be synchronized to disk after every N writes to
the binary log, with the sync_binlog system variable.
1 is the safest value for sync_binlog, but also the slowest. Even with
sync_binlog set to 1, there is still the chance of an inconsistency between
the table content and binary log content in case of a crash. For example, if
you are using InnoDB tables and the MySQL server processes a COMMIT
statement, it writes the whole transaction to the binary log and then commits
this transaction into InnoDB.
If the server crashes between those two operations, the transaction is rolled
back by InnoDB at restart but still exists in the binary log.
To resolve this, you should set --innodb_support_xa to 1. Although this
option is related to the support of XA transactions in InnoDB, it also ensures
that the binary log and InnoDB data files are synchronized.
For this option to provide a greater degree of safety, the MySQL server
should also be configured to synchronize the binary log and the InnoDB logs
to disk at every transaction. The InnoDB logs are synchronized by default,
and sync_binlog=1 can be used to synchronize the binary log. The effect of
this option is that at restart after a crash, after doing a rollback of
transactions, the MySQL server cuts rolled back InnoDB transactions from
the binary log. This ensures that the binary log reflects the exact data of
InnoDB tables, and so, that the slave remains in synchrony with the master
(not receiving a statement which has been rolled back).
If the MySQL server discovers at crash recovery that the binary log is
shorter than it should have been, it lacks at least one successfully
committed InnoDB transaction. This should not happen if sync_binlog=1 and
the disk/filesystem do an actual sync when they are requested to (some
don't), so the server prints an error message The binary log <name> is
shorter than its expected size. In this case, this binary log is not correct and
replication should be restarted from a fresh snapshot of the master's data.
For MySQL 5.1.20 and later (and MySQL 5.0.46 and later for backward
compatibility), the session values of the following system variables are
written to the binary log and honored by the replication slave when parsing
the binary log:
sql_mode
Sikkim Manipal University

Page No. 303

RDBMS with MySQL

Unit 14

foreign_key_checks
unique_checks
character_set_client
collation_connection
collation_database
collation_server
sql_auto_is_null

Self Assessment Questions


5. For _____, the binary log is used on master replication servers as a
record of the statements to be sent to slave servers.

14.5 The Slow Query Log


The slow query log consists of all SQL statements that took more than
long_query_time seconds to execute and (as of MySQL5.1.21) required at
least min_examined_row_limit rows to be examined. The time to acquire the
initial table locks is not counted as execution time. mysqld writes a
statement to the slow query log after it has been executed and after all locks
have been released, so log order might be different from execution order.
The minimum and default values of long_query_time are 1 and 10,
respectively.
Prior to MySQL 5.1.21, the minimum value is 1, and the value for this
variable must be an integer. Beginning with MySQL 5.1.21, the minimum is
0, and a resolution of microseconds is supported when logging to a file.
However, the microseconds part is ignored and only integer values are
written when logging to tables.
Before 5.1.6, the slow query log destination is always a file. To enable the
slow query log file, use the --log-slow-queries[=file_name] option. To enable
the slow query log as of MySQL 5.1.6, start mysqld with the --log-slowqueries[=file_name] option, and optionally use --log-output to specify the log
destination.
As of MySQL 5.1.29, --log-slow-queries is deprecated: Use -slow_query_log to enable the slow query log, and optionally -slow_query_log_file=file_name to specify a log filename. --slow_query_log
takes an optional argument of 1 or 0 to enable or disable the log.
Sikkim Manipal University

Page No. 304

RDBMS with MySQL

Unit 14

If you specify no filename for the slow query log, the default name is
host_name-slow.log in the data directory. If you specify a filename that is
not an absolute pathname, the server writes the file in the data directory.
When --log-slow-queries is specified, --slow_query_log also may be given
as of MySQL 5.1.12 to specify the initial slow query log state. With no
argument or an argument of 0, the option disables the log. If omitted or
given with an argument of 1, the option enables the log.
For runtime control of the general query log, use the global slow_query_log
and slow_query_log_file system variables. Set slow_query_log to 0 (or OFF)
to disable the log or to 1 (or ON) to enable it. Set general_log_file to specify
the name of the log file. If a log file already is open, it is closed and the new
file is opened.
When the slow query log is enabled, output is written to any destinations
specified by the --log-output option or log_output system variable. If you
enable the log, the server opens the log file and writes startup messages to
it. However, logging of queries to the file does not occur unless the FILE log
destination is selected. If the destination is NONE, no queries are written
even if the slow query log is enabled. Setting the log filename has no effect
on logging if the log destination value does not contain FILE.
The slow query log can be used to find queries that take a long time to
execute and are therefore candidates for optimization. However, examining
a long slow query log can become a difficult task. To make this easier, you
can process the slow query log using the mysqldumpslow command to
summarize the queries that appear in the log. Use mysqldumpslow --help to
see the options that this command supports.
In MySQL 5.1, queries that do not use indexes are logged in the slow query
log if the --log-queries-not-using-indexes option is specified.
In MySQL 5.1, the --log-slow-admin-statements server option enables you to
request logging of slow administrative statements such as OPTIMIZE
TABLE, ANALYZE TABLE, and ALTER TABLE to the slow query log.
Queries handled by the query cache are not added to the slow query log,
nor are queries that would not benefit from the presence of an index
because the table has zero rows or one row.

Sikkim Manipal University

Page No. 305

RDBMS with MySQL

Unit 14

Replication slaves do not write replicated queries to the slow query log,
even if the same queries were written to the slow query log on the master.
This is a known issue which we intend to fix in a future version of MySQL.
(Bug#23300).
Self Assessment Questions
6. The minimum and default values of long_query_time are _____ and
____, respectively.

14.6 Log File Maintenance


MySQL Server can create a number of different log files that make it easy to
see what is going on. However, you must clean up these files regularly to
ensure that the logs do not take up too much disk space.
When using MySQL with logging enabled, you may want to back up and
remove old log files from time to time and tell MySQL to start logging to new
files.
On a Linux (Red Hat) installation, you can use the mysql-log-rotate script for
this. If you installed MySQL from an RPM distribution, this script should
have been installed automatically. You should be careful with this script if
you are using the binary log for replication.
You should not remove binary logs until you are certain that their contents
have been processed by all slaves.
On other systems, you must install a short script yourself that you start from
cron (or its equivalent) for handling log files.
For the binary log, you can set the expire_logs_days system variable to
expire binary log files automatically after a given number of days. If you are
using replication, you should set the variable no lower than the maximum
number of days your slaves might lag behind the master.
You can force MySQL to start using new log files by issuing a FLUSH LOGS
statement or executing mysqladmin flush-logs or mysqladmin refresh.
A log flushing operation does the following:
If general query logging or slow query logging to a log file is enabled, the
server closes and reopens the general query log file or slow query log
file.
Sikkim Manipal University

Page No. 306

RDBMS with MySQL

Unit 14

If binary logging is enabled, the server closes the current binary log file
and opens a new log file with the next sequence number.
If the server was given an error log filename with the --log-error option, it
renames the error log with the suffix -old and creates a new empty error
log file.

The server creates a new binary log file when you flush the logs. However, it
just closes and reopens the general and slow query log files. To cause new
files to be created on Unix, rename the current logs before flushing them. At
flush time, the server will open new logs with the original names. For
example, if the general and slow query logs are named mysql.log and
mysql-slow.log, you can use a series of commands like this:
shell> cd mysql-data-directory
shell> mv mysql.log mysql.old
shell> mv mysql-slow.log mysql-slow.old
shell> mysqladmin flush-logs

At this point, you can make a backup of mysql.old and mysql-slow.log and
then remove them from disk.
Before 5.1.3, you cannot rename a log file on Windows while the server has
it open. You must stop the server and rename the file, and then restart the
server to create a new log file. As of 5.1.3, this applies only to the error log.
However, a stop and restart can be avoided by using FLUSH LOGS, which
causes the server to rename the error log with an -old suffix and open a new
error log.
As of MySQL 5.1.2, you can disable the general query log or slow query log
at runtime:
SET GLOBAL general_log = 'OFF';
SET GLOBAL slow_query_log = 'OFF';

With the logs disabled, rename the log files externally; for example, from the
command line. Then enable the logs again:
SET GLOBAL general_log = 'ON';
SET GLOBAL slow_query_log = 'ON';

This method works on any platform and does not require a server restart.
Sikkim Manipal University

Page No. 307

RDBMS with MySQL

Unit 14

14.7 Summary
MySQL has several different logs that can help you find out what is going on
inside mysqld. By default, all log files are created in the mysqld data
directory. This unit discusses all the log files present in the mysqld option.
1. Error Log: The error log contains information indicating when mysqld
was started and stopped and also any critical errors that occur while the
server is running. If mysqld notices a table that needs to be
automatically checked or repaired, it writes a message to the error log.
2. The General Query Log: The general query log is a general record of
what mysqld is doing. The server writes information to this log when
clients connect or disconnect, and it logs each SQL statement received
from clients. The general query log can be very useful when you suspect
an error in a client and want to know exactly what the client sent to
mysqld.
3. The Binary Log: The binary log contains all statements that update
data. It also contains statements that potentially could have updated it
(for example, a DELETE which matched no rows), unless row-based
logging is used. Statements are stored in the form of events that
describe the modifications. The binary log also contains information
about how long each statement took that updated data.
4. The Slow Query Log: The slow query log consists of all SQL
statements that took more than long_query_time seconds to execute
and (as of MySQL5.1.21) required at least min_examined_row_limit
rows to be examined. The time to acquire the initial table locks is not
counted as execution time.
5. Log File Maintenance: MySQL Server can create a number of different
log files that make it easy to see what is going on. However, you must
clean up these files regularly to ensure that the logs do not take up too
much disk space. When using MySQL with logging enabled, you may
want to back up and remove old log files from time to time and tell
MySQL to start logging to new files.

14.8 Terminal Questions


1. Define various Log File Types available in MySQL.
2. Describe General Query Log
3. Describe Log File Maintenance.
Sikkim Manipal University

Page No. 308

RDBMS with MySQL

Unit 14

14.9 Answers
Self Assessment Questions
1. general query
2. --log-output
3. FLUSH
4. host_name.log
5. replication
6. 1 and 10
Terminal Questions
1. The following are various log files available in MySQL:
a. Error Log: Problems encountered starting, running, or stopping
mysqld.
b. General Query Log: Established client connections and statements
received from clients.
c. Binary Log: All statements that change data (also used for
replication).
d. Slow Query Log: All queries that took more than long_query_time
seconds to execute or didn't use indexes.
(Refer Section 14.1)
2. The general query log is a general record of what mysqld is doing. The
server writes information to this log when clients connect or disconnect,
and it logs each SQL statement received from clients. The general query
log can be very useful when you suspect an error in a client and want to
know exactly what the client sent to mysqld.
mysqld writes statements to the query log in the order that it receives
them, which might differ from the order in which they are executed. This
logging order contrasts to the binary log, for which statements are
written after they are executed but before any locks are released. (Also,
the query log contains all statements, whereas the binary log does not
contain statements that only select data.)
Before 5.1.6, the general query log destination is always a file. To
enable the general query log file, use the --log[=file_name] or -l
[file_name] option.

Sikkim Manipal University

Page No. 309

RDBMS with MySQL

Unit 14

If you specify no filename for the general query log, the default name is
host_name.log in the data directory. If you specify a filename that is not
an absolute pathname, the server writes the file in the data directory.
When --log or -l is specified, the --general_log option also may be given
as of MySQL 5.1.12 to specify the initial general query log state. With no
argument or an argument of 0, the option disables the log. If omitted or
given with an argument of 1, the option enables the log.
(Refer Section 14.3)
3. MySQL Server can create a number of different log files that make it
easy to see what is going on. However, you must clean up these files
regularly to ensure that the logs do not take up too much disk space.
When using MySQL with logging enabled, you may want to back up and
remove old log files from time to time and tell MySQL to start logging to
new files.
On a Linux (Red Hat) installation, you can use the mysql-log-rotate script
for this. If you installed MySQL from an RPM distribution, this script
should have been installed automatically. You should be careful with this
script if you are using the binary log for replication.
You should not remove binary logs until you are certain that their
contents have been processed by all slaves.
On other systems, you must install a short script yourself that you start
from cron (or its equivalent) for handling log files.
For the binary log, you can set the expire_logs_days system variable to
expire binary log files automatically after a given number of days. If you
are using replication, you should set the variable no lower than the
maximum number of days your slaves might lag behind the master.
You can force MySQL to start using new log files by issuing a FLUSH
LOGS statement or executing mysqladmin flush-logs or mysqladmin
refresh.
(Refer Section 14.6)

Sikkim Manipal University

Page No. 310

RDBMS with MySQL

Unit 14

Reference:
1. MySQL: The Complete Reference by Vikram Vaswani, Tata
McGraw-Hill Publishing Company Limited, Fifth Reprint 2006.
2. MySQL Stored Procedure Programming (Paperback) by Guy
Harrison (Author), Steven Feuerstein (Author); Oreilly Series.
3. MySQL Cookbook (Paperback) by Paul DuBois (Author); Oreilly Series
4. WebSite: http://www.mysql.com

Sikkim Manipal University

Page No. 311

Potrebbero piacerti anche