Professional Documents
Culture Documents
Printing
Artland Communications, Lahore. September 2006
Published by
Pakistan Software Export Board
Disclaimer
This toolkit is published by the PSEB for members of the IT industry and the public-at-large.
The toolkit’s compilers, or the editor, are not responsible, in any way possible, for the
errors/omissions of this toolkit. The OSRC does not accept any liability for any direct and
consequential use of this toolkit or its contents. The contents of this toolkit may be distributed
only subject to the terms and conditions set forth in the Open Publication License v 1.0 or
later. The latest version is presently available at http://opencontent.org/openpub/
i
TABLE OF CONTENT
INTRODUCTION...............................................................................................................................................1
RDBMS: AN INTRODUCTION............................................................................................................2
1. OVERVIEW................................................................................................................................................3
2. SQL- 92..................................................................................................................................................3
3. RELATIONAL DATABASES............................................................................................................................4
3.1. Relational vs. flat file databases....................................................................................................4
3.2. Database Normalization................................................................................................................4
MYSQL: AN INTRODUCTION............................................................................................................6
2. CONFORMANCE WITH SQL STANDARDS.........................................................................................................7
3. MYSQL CLIENT PROGRAMS.......................................................................................................................7
3.1. The mysql Client Program.............................................................................................................7
3.2. The mysqlimport Client Program .................................................................................................8
3.3. The mysqldump Client Program ...................................................................................................8
3.4. The mysqlcheck and myisamchk Client Program .........................................................................9
4. GENERAL DATABASE AND TABLE MANIPULATION ..........................................................................................9
5. STORAGE ENGINES AND TABLE TYPES........................................................................................................10
6. COLUMN TYPES.......................................................................................................................................10
6.1. Numerics......................................................................................................................................11
6.2. Strings..........................................................................................................................................12
6.3. Dates............................................................................................................................................13
6.4. Complex Types.............................................................................................................................14
7. COLUMN OPTIONS....................................................................................................................................15
8. OPERATORS.............................................................................................................................................15
8.1. Precedence Rule..........................................................................................................................15
8.2. Arithmetic Operators...................................................................................................................16
8.3. Comparison Operators................................................................................................................16
8.4. Logical Operators........................................................................................................................17
8.5. MySQL Conversions....................................................................................................................17
9. CONNECTING TO, AND DISCONNECTING FROM, THE SERVER............................................................................17
10. CREATING AND USING A DATABASE.........................................................................................................18
11. TABLE MANIPULATION............................................................................................................................19
11.1. Creating a Table........................................................................................................................19
11.2. Dropping a Table.......................................................................................................................19
11.3. Altering a Table.........................................................................................................................19
11.4. Renaming a table.......................................................................................................................20
12. INDEXES................................................................................................................................................20
12.1. Non-unique Indexes:..................................................................................................................21
12.2. Unique Indexes..........................................................................................................................21
12.3. Primary Key Indexes..................................................................................................................21
12.4. Dropping Indexes.......................................................................................................................22
13. MYSQL QUERIES.................................................................................................................................22
13.1. The SELECT Statements............................................................................................................22
13.1.1. Basic Data Retrieval....................................................................................................................22
13.1.2. Restricting a selection using WHERE Clause..............................................................................23
13.1.3. Sorting a selection using ORDER BY Clause..............................................................................23
13.1.4. Limiting a selection using LIMIT Clause.....................................................................................23
13.1.5. Grouping a selection using GROUP BY Clause..........................................................................24
13.2. The INSERT Statement..............................................................................................................25
13.2.1. Adding Multiple Records with a Single INSERT Statement........................................................26
13.3. REPLACE Statement.................................................................................................................27
13.4. UPDATE Statement...................................................................................................................27
13.5. DELETE Statement....................................................................................................................28
14. JOINS....................................................................................................................................................28
14.1. Cross-join..................................................................................................................................28
14.2. Inner Join...................................................................................................................................28
14.3. Outer Join..................................................................................................................................29
14.3.1. Left Join.......................................................................................................................................29
14.3.2. Right Join.....................................................................................................................................30
i
15. REFERENCE...........................................................................................................................................30
MYSQL: ADMINISTRATION............................................................................................................31
1. DOWNLOADING AND INSTALLING MYSQL..................................................................................................32
2. THE MYSQL DATA DIRECTORY...............................................................................................................33
2.1. Location of the Data Directory....................................................................................................33
2.2. Structure of Data Directory.........................................................................................................34
2.3. How the MySQL Server provides access to Data........................................................................34
2.4. Database Table Representation...................................................................................................34
3. MYSQL SERVER START-UP AND SHUTDOWN .............................................................................................35
3.1. The MySQL Server and Server Startup Scripts...........................................................................35
3.1.1. An Overview of the Server-Side Scripts........................................................................................35
3.1.2. Methods of Starting the Server......................................................................................................35
3.1.3. Regaining the control of the Server if you cannot connect:............................................................35
4. MYSQL USER ACCOUNT MANAGEMENT....................................................................................................36
4.1. MySQL Usernames and Passwords.............................................................................................36
4.2. Adding New User Accounts to MySQL........................................................................................37
4.3. Removing User Accounts from MySQL.......................................................................................39
4.4. Limiting Account Resources........................................................................................................40
4.5. Assigning Account Passwords.....................................................................................................41
5. DISASTER PREVENTION AND RECOVERY.......................................................................................................41
5.1. Database Back-ups .....................................................................................................................41
5.2. Back-up Principles.......................................................................................................................41
5.3. Back-up Methods.........................................................................................................................41
5.3.1. Direct copy method........................................................................................................................41
5.3.2. Mysqlhotcopy................................................................................................................................41
5.3.3. mysqldump....................................................................................................................................42
6. REPLICATION IN MYSQL.........................................................................................................................43
6.1. Set-up Database Replication in MySQL .....................................................................................43
6.1.1. Configure the Master.....................................................................................................................43
6.1.2. Configure the Slave.......................................................................................................................44
7. REFERENCE.............................................................................................................................................45
COMPARISON OF ORACLE, MYSQL AND POSTGRESQL DBMS..........................................46
1. ELEMENTARY FEATURES:............................................................................................................47
1.1. Conformity with SQL standards:.................................................................................................47
1.2. Binary and Character Large Objects:.........................................................................................47
1.3. User-defined data types:..............................................................................................................47
1.4. Object-relational extensions:.......................................................................................................47
1.5. Special data types (multimedia, etc.)...........................................................................................47
1.6. Sub-queries in SQL query:...........................................................................................................48
1.7. Primary and Unique keys:...........................................................................................................48
1.8. Foreign key:.................................................................................................................................48
1.9. Check:..........................................................................................................................................48
1.10. Views:........................................................................................................................................48
1.11. Updateable views: ....................................................................................................................48
1.12. Synonyms (an alias for any table, view or other object in database):......................................48
1.13. Auto Increment (counter) Columns:..........................................................................................49
1.14. Automatic conversion of code pages (e.g. between client and server):.....................................49
2. TRANSACTIONS...............................................................................................................................49
2.1. Support for transaction processing:............................................................................................49
2.2. Partial rollback of transaction:...................................................................................................49
2.3. Locking level (table, page, and row):..........................................................................................49
2.4. Deadlock Detection and Resolving:...........................................................................................49
3. PROGRAMMING IN DB ..................................................................................................................49
3.1. Languages for writing stored procedures:..................................................................................49
3.2. Triggers:......................................................................................................................................50
4. ADMINISTRATION ..........................................................................................................................50
4.1. User authorization:......................................................................................................................50
4.2. Encrypted Client - Server connection:........................................................................................50
4.3. Object access privileges (level)...................................................................................................50
4.4. Access privileges grouping:.........................................................................................................51
4.5. Incremental and on-line backups:...............................................................................................51
ii
4.6. Tools for data export:..................................................................................................................51
4.7. Tools for data import:..................................................................................................................51
5. PORTABILITY AND SCALABILITY .............................................................................................51
5.1. Hardware and system platforms supported:................................................................................51
5.2. Portability of data and code (e.g. stored procedures).................................................................51
5.3. Support for SMP systems (parallel query execution, etc.):.........................................................52
6. PERFORMANCE AND VLDB .........................................................................................................52
6.1. Known VLDB implementations:..................................................................................................52
6.2. Maximum number of rows and columns in a table:....................................................................52
6.3. Maximum number of tables and indexes:....................................................................................52
6.4. "Manual" tuning of the allocation:..............................................................................................52
6.5. Automatic partitioning of large tables/indexes and using partitions in query optimization:......52
6.6. Access to multiple databases in one session:..............................................................................53
6.7. Replication:..................................................................................................................................53
6.8. Gateways to other DBMSs:.........................................................................................................53
7. APPLICATION DEVELOPMENT AND INTERFACES .................................................................53
7.1. Standard interfaces ODBC and JDBC:.......................................................................................53
7.2. Application support: ...................................................................................................................54
7.3. Dedicated Web servers:...............................................................................................................54
7.4. XML support integrated in DBMS:..............................................................................................54
7.5. Support from CASE packages:....................................................................................................54
8. RELIABILITY ....................................................................................................................................54
8.1. Automatic recovery from failures:...............................................................................................54
9. COMMERCIAL ISSUES ...................................................................................................................55
9.1. License type:................................................................................................................................55
9.2. Technical support:.......................................................................................................................55
9.3. Market share: ..............................................................................................................................55
9.4. Specific market segments occupied:............................................................................................55
iii
Introduction
This open source toolkit has been developed by the Open Source Resource Center (OSRC),
a project of the Ministry of Information Technology (MoIT). This toolkit contains step-by-step
manuals related to open source applications for databases, application servers, desktop
applications, office productivity suites, Enterprise Resource Planning (ERP) and Customer
Relationship Management (CRM) software, and open source desktop applications for the
Microsoft Windows platform. A set of CDs, including some Linux distributions and other
applications, forms an integral part of this open source toolkit.
I would like to thank the OSRC team, including Mr. Abubakar Shoaib, Mr. Iftikhar Ahmad, Mr.
Muhammad Hammmad, Mr. Muazzam Ali, Mr. Sher Shah Farooq, and Mr. Qandeel Aslam,
who have compiled this toolkit; and Miss Seema Javed Amin, who has edited it. The OSRC
would especially wish to thank PSEB’s Director (Projects) Mr. Nasir Khan Afridi, Former
Project Manger(OSRC) Mr. Osman Haq and Ministry of Information Technology's Member
(IT) Mr. M. Tariq Badsha for their generous moral support, without which this toolkit would
never have been completed.
This is the first edition of this toolkit, and the OSRC hopes to continue to improve it with the
help of your feedback and comments.
Sufyan Kakakhel
Open Source Resource Center,
Pakistan Software Export Board,
2nd Floor, ETC, Agha Khan Road, F-5,
Islamabad, Pakistan.
Ph: +92-51-9208748
Fax: +92-51-9204075
Email: skakakhel@pseb.org.pk
http://www.osrc.org.pk
RDBMS : MySQL 2
1. Overview
SQL standard query language is referred to as a simple and basic language that allows you to
“communicate” with your database, and read, write, and extract useful information from it.
Non-procedural in nature, SQL is easier to work with than most other languages such as
PHP, PERL, and Java, etc., but can at times lead to some complicated code for people who
are new to this language.
The simplest SQL statement that allows a user to view or extract data from a table is the
SELECT statement. As the name implies, a user can select specific data from the table in
order to view it.
Assuming that your database table, known as “student”, looks like this:
2. SQL- 92
SQL is standardized, and the current version is referred to as SQL-92. Any SQL-compliant
database should conform to the SQL standards of the time. If not, it should state its flavor of
SQL (SQL-89, for example) so that you can quickly figure out which features are, and which
are not, available in it. The standardization of SQL makes it an excellent tool for use in
website design. Most web application development toolkits, most notably Allaire's Cold Fusion
and Macromedia Dreamweaver Ultradev, rely on SQL or SQL-like statements to connect to,
and extract information from, databases. For more details about SQL-92, please visit:
http://developer.mimer.com/documentation/html_82/Mimer_SQL_Reference_Manual/Intro_S
QL_Stds3.html
A relational database, on the other hand, incorporates multiple tables with methods for the
tables to work together. The relationships between table data can be collated, merged and
displayed in database forms. Most relational databases offer functionality to share data:
Across networks
Over the Internet
With laptops and other electronic devices, such as palm pilots
With other software systems
Designing flat file databases is simple, and requires little knowledge of design. Flat files can
be developed using just about any database engine. Flat files can be created in relational
database engines by not taking advantage of relational design concepts. Designing a
relational database takes more planning than flat file databases. With flat files, add
information as deemed necessary. With relational databases, store data in tables in such a
way that the relationships make sense. Building a relational database is dependant upon your
ability to establish a relational model. The model must fully describe how the data is
organized in terms of data structure, integrity, querying, manipulation and storage.
Relational databases allow users to define certain record fields as keys or indexes, to perform
search queries, join table records and establish integrity constraints. Search queries are
faster and more accurate when based on indexed values. Table records can be easily joined
by the indexed values. Integrity constraints can be established to ensure that table
relationships are valid. If you are able to establish a one-to-many relationship in your data
tables, you should use a relational database because a flat file is insufficient to handle all your
data processing needs.
Relational databases offer more robust reporting with report generators that filter and display
selected fields. Relational databases offer the capability of building your own reporting
modules. Most relational databases also offer the capability of importing and exporting data
from other software.
There are three primary relational database systems: proprietary, open source and
embedded. Proprietary relational databases often require the use of proprietary development
languages in order to complement SQL. Microsoft Access, for example, combines Visual
Basic with SQL. Open source databases, such as MySQL, are distributed freely in order to
encourage user development. Embedded and relational databases are packaged as parts of
other software packages, such as tax-preparation software packages. The vendor supplies
the database, and all its related manipulation tools, to control the database structure. These
databases are often accompanied by tools which provide audit trails of transactions.
RDBMS : MySQL 4
and applying them to your daily database design tasks really isn't all that complicated, and it
can drastically improve the performance of your DBMS.
Normalization is the process of efficiently organizing data in a database. There are two goals
of the normalization process:
• To eliminate redundant data (storing the same data in more than one table)
• To ensure that data dependencies make sense (only storing related data in a table)
Both these goals reduce the amount of space a database consumes, and ensure that data is
logically stored.
The database community has developed a series of guidelines to ensure that databases are
normalized. These are referred to as Normal Forms (NF) and are numbered from one (the
lowest form of normalization, referred to as First Normal Form or 1NF) through five (Fifth
Normal Form or 5NF). In practical applications, you will often see 1NF, 2NF, and 3NF along
with the occasional 4NF. The fifth normal form is very rarely seen. The First Normal Form
(1NF) sets the very basic rules for an organized database:
The second normal form (2NF) further addresses the concept of removing duplicative data:
The third normal form (3NF) goes one large step further:
Finally, the fourth normal form (4NF) has one additional requirement:
RDBMS : MySQL 6
1. Overview
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.
To connect to a server using a client program, the client must know which host the server is
running on. A connection may be locally established to a server running on the same host as
the client program, or remotely to a server running on a different host. In order to connect, you
must also identify yourself to the server with a username and password.
Two options indicate to the client where the server is running, as well as which type of
connection to establish.
--host=host_name or -h host_name
This option specifies the machine where the MySQL server is running. The value can be a
hostname or an IP number. The default host value is localhost.
This option indicates the port number to which to connect on the server host; it applies only to
TCP/IP connections. The default MySQL port number is 3306.
Two options provide identification information. These are the username and the password of
the account that you want to use in order to access the server:
--user=user_name or -u user_name
--password=pass_value or -ppass_value
This option specifies the password for your MySQL account. The following examples show
how to specify connection parameters:
• Connect to the server using the default hostname and username values with no
password:
shell> mysql
• Connect to the server on the local host with a username of myname, asking mysql to
prompt you for a password:
shell> mysql --host=localhost --password --user=myname
• Connect with the same options as the previous example, but using the corresponding
short option forms:
shell> mysql -h localhost -p -u myname
• Connect to the server at a specific IP address, with a username of myname and
password of mypass:
shell> mysql --host=192.168.1.33 --user=myname --password=mypass
• Connect to the server on the local host, using the default username and password
and compressing client/server traffic:
shell> mysql --host=localhost --compress
db_name names the database containing the table to be loaded, and input_file names the file
that contains the data to be loaded.
Each table to be loaded by mysqlimport must already exist, and each input file should contain
only data values. mysqlimport is not intended for processing files that consist of SQL
statements (such files can be created with the mysqldump program).
mysqldump Operation
By default, mysqldump interprets its first non-option argument as a database name, and
dumps all the tables into that database.
RDBMS : MySQL 8
• The following command dumps the contents of all the tables in the school database
into a file named school.sql:
shell> mysqldump school > school.sql
• The following command names just the student and teacher tables after the database
name, so mysqldump dumps just those tables to a file called student_teacher.sql:
shell> mysqldump school student teacher > student_teacher.sql
mysql can read from a pipe, so you can combine the use of mysqldump and mysql into a
single command. The preceding example can be written as one command:
mysqlcheck and myisamchk are similar in purpose, but they do have some differences. The
following is a comparison between the two programs:
Both programs can check, repair, and analyze MyISAM tables. mysqlcheck can also optimize
MyISAM tables, as well as check InnoDB tables and analyze BDB tables. There are certain
operations which myisamchk can perform that mysqlcheck cannot, such as disabling or
enabling indexes. These operations are beyond the scope of this tutorial.
• MySQL associates each database with a directory under the data directory. (This
means that the data directory is the parent of all database directories.) A database
directory has the same name as the database that it represents. For example, a
database named school corresponds to a directory named school under the data
directory. MySQL uses the database directory to manage the components of the
database - that is, its tables and indexes. A database may be empty, or have one or
more tables. Databases cannot be nested or have sub-databases; one database
cannot contain another.
• Each table in a database consists of rows and columns. A table can be empty (it can
have zero rows of data), but it must have at least one column. A table may also be
indexed in order to improve query performance. Every table is associated with a
format file in the database directory that contains the definition, or structure, of the
table. The format filename is the same as the table name, plus a .frm suffix. For
example, the format file for a table named student in the school database is named
student.frm and is located in the school directory under the server's data directory.
Depending on the table type, the storage engine for a table might create additional
files for the table. If student is a MyISAM table, the MyISAM storage engine creates
data and index files named student.MYD and student.MYI to store data rows and
indexes (respectively) for the table. If student is an InnoDB table, MySQL still creates
a student.frm format file in the database directory, but the InnoDB storage engine
stores the table data and index information elsewhere, in the InnoDB tablespace.
• The original storage engine was ISAM, which managed non-transactional tables. This
engine has been replaced by MyISAM and should no longer be used. It is deprecated
in MySQL 4.1, and will be removed in MySQL 5.0.
• The MyISAM storage engine was introduced in MySQL 3.23.0. MyISAM is an
improved replacement for ISAM. MyISAM manages non-transactional tables. It
provides high-speed storage and retrieval, as well as full-text searching capabilities.
MyISAM is supported in all MySQL configurations, and is its default storage engine,
unless you have configured MySQL to use a different one by default.
• The MEMORY storage engine provides in-memory tables. The MERGE storage
engine was added in MySQL 3.23.25. It allows a collection of identical MyISAM tables
to be handled as a single table. Like MyISAM, the MEMORY and MERGE storage
engines handle non-transactional tables, and both are also included in MySQL by
default.
Note: The MEMORY storage engine was formerly known as the HEAP engine.
• The InnoDB and BDB storage engines that handle transaction-safe tables were
introduced in later versions of MySQL 3.23. BDB is included in MySQL-Max binary
distributions on those operating systems that support it. InnoDB also is included in
MySQL-Max binary distributions for MySQL 3.23. Beginning with MySQL 4.0, InnoDB
is included by default in all MySQL binary distributions. In source distributions, you
can enable or disable either engine by configuring MySQL according to your own
preference.
• NDB Cluster is the storage engine used by MySQL Cluster to implement tables that
are partitioned over many computers. It is available in source code distributions as of
MySQL 4.1.2 and in binary distributions as of MySQL-Max 4.1.3. This storage engine
is currently supported on Linux, Solaris, and Mac OS X only. MySQL intends to add
support for this engine on other platforms, including Windows, and in future MySQL
releases.
• The EXAMPLE storage engine is a “stub” engine that does nothing. You can create
tables with this engine, but no data can be stored in them, or retrieved from them.
The purpose of this engine is to serve as an example in the MySQL source code
which illustrates how to begin writing new storage engines. As such, it is primarily of
interest to developers only.
• The ARCHIVE storage engine is used for storing large amounts of data without
indexes with a very small footprint.
• The CSV storage engine stores data in text files using a comma-separated-values
format.
• The BLACKHOLE storage engine accepts, but does not store, data, and retrievals
always return an empty set.
• The FEDERATED storage engine stores data in a remote database. In MySQL 5.1, it
works with MySQL only, using the MySQL C Client API. In future releases, the
intention is to enable it to connect to other data sources using other drivers or client
connection methods.
6. Column Types
For each data type, the syntax shown uses square brackets ([ ]) to indicate optional parts of
the syntax. The following example shows how BIGINT is explained in this chapter:
BIGINT[(display_size)]
In addition to the BIGINT type, many other MySQL data types support the specification of a
display size. Unless otherwise specified, this value must be an integer between 1 and 255.
In the following cases, MySQL silently changes the column type you specify in your table’s
creation to something else:
RDBMS : MySQL 10
When the specified VARCHAR column size is less than four characters, it is converted to
CHAR:
When a table has at least one column of a variable length, all CHAR columns greater than
three characters in length are converted to VARCHAR.
Display sizes for TIMESTAMP fields must be an even value between 2 and 14. A display size
of 0 or greater than 14 will convert the field to a display size of 14. An odd-valued display size
will be converted to the next-highest even value.
6.1. Numerics
MySQL supports all ANSI SQL 92 numeric data types. MySQL numeric types break down into
two groups: integers and floating points. Within each group, the types differ by the amount of
storage required for them.
When you insert a value into a column that requires more storage than the data type allows, it
will be clipped to the minimum (negative values) or maximum (positive values) value for that
data type. MySQL issues a warning when such clipping occurs during ALTER TABLE, LOAD
DATA INFILE, UPDATE, and multi-row INSERT statements.
The AUTO_INCREMENT attribute may be supplied for at most one column of an integer type
in a table. The UNSIGNED attribute may be used with any numeric type. An unsigned column
may contain only positive integers or floating-point values. The ZEROFILL attribute indicates
that the column should be left padded with zeros when displayed by MySQL. The number of
zeros padded is determined by the column's display width.
BIGINT
BIGINT[(display_size)] [AUTO_INCREMENT] [UNSIGNED] [ZEROFILL]
Storage: 8 bytes
Description:
Largest integer type, supporting range of whole numbers from
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (0 to
18,446,744,073,709,551,615 unsigned).
DECIMAL
DECIMAL[(precision, [scale])] [ZEROFILL]
Storage: precision + 2 bytes
Description:
Stores floating-point numbers where precision is critical, such as for monetary values.
DECIMAL types require you to specify the precision and scale. The precision is the
number of significant digits in the value. The scale is the number of those digits that
come after the decimal point. For example, a BALANCE column declared as
DECIMAL(9, 2) would store numbers with nine significant digits, two of which are to
the right of the decimal point. The range for this declaration would be -9,999,999.99
to 9,999,999.99. If you specify a number with more decimal points, it is rounded to fit
the proper scale. Values beyond the range of the DECIMAL are clipped to fit within
the range.
DOUBLE
DOUBLE[(display_size, digits)] [ZEROFILL]
Storage: 8 bytes
Description:
A double-precision floating-point number. This type stores large floating-point values.
DOUBLE columns store negative values from -1.7976931348623157E+308 to
-2.2250738585072014E-308, 0, and positive numbers from 2.2250738585072014E-
308 to 1.7976931348623157E+308.
FLOAT
FLOAT[(display_size, digits)] [ZEROFILL]
INT / INTEGER
INT[(display_size)] [AUTO_INCREMENT] [UNSIGNED] [ZEROFILL]
Storage: 4 bytes
Description:
A basic whole number with a range of -2,147,483,648 to 2,147,483,647 (0 to
4,294,967,295 unsigned).
MEDIUMINT
MEDIUMINT[(display_size)] [AUTO_INCREMENT] [UNSIGNED] [ZEROFILL]
Storage: 3 bytes
Description:
A basic whole number with a range of -8,388,608 to 8,388,607 (0 to 16,777,215
unsigned).
SMALLINT
SMALLINT[(display_size)] [AUTO_INCREMENT] [UNSIGNED] [ZEROFILL]
Storage: 2 bytes
Description:
A basic whole number with a range of -32,768 to 32,767 (0 to 65,535 unsigned).
TINYINT
TINYINT[(display_size)] [AUTO_INCREMENT] [UNSIGNED] [ZEROFILL]
Storage: 1 byte
Description:
A basic whole number with a range of -128 to 127 (0 to 255 unsigned).
6.2. Strings
String data types store various kinds of text data. There are several types to accommodate
data of different sizes. For each size, there is a type that sorts and compares entries in a
case-insensitive fashion, in accordance with the sorting rules for the default character set. A
corresponding binary type performs simple byte-by-byte sorts and comparisons. In other
words, binary values are case-sensitive. For CHAR and VARCHAR, the binary types are
declared using the BINARY attribute. The TEXT types, however, have corresponding BLOB
types as their binary counterparts.
BLOB
Binary form of TEXT.
CHAR
CHAR(size) [BINARY]
Size: Specified by the size value in a range of to 255 (1 to 255 prior to MySQL 3.23)
Storage: size bytes
Description:
A fixed-length text field. String values with fewer characters than the column's size
will be right padded with spaces. The right padding is removed on retrieval of the
value from the database.
LONGBLOB
Binary form of LONGTEXT.
LONGTEXT
LONGTEXT
Size: 0 to 4,294,967,295
Storage: Length of value + 4 bytes
Description:
RDBMS : MySQL 12
Storage for large text values. While the theoretical limit on the size of the text that can
be stored in a LONGTEXT column exceeds 4 GB, the practical limit is much less.
This is due to limitations of the MySQL communication protocol, and the amount of
memory available to both the client and the server ends of the communication.
MEDIUMBLOB
Binary form of MEDIUMTEXT.
MEDIUMTEXT
MEDIUMTEXT
Size: 0 to 16,777,215
Storage: Length of value + 3 bytes
Description:
Storage for medium-sized text values.
TEXT
TEXT
Size: 0 to 65,535
Storage: Length of value + 2 bytes
Description:
Storage for most text values.
TINYBLOB
Binary form of TINYTEXT.
TINYTEXT
TINYTEXT
Size: 0 to 255
Storage: Length of value + 1 byte
Description:
Storage for short text values.
VARCHAR
VARCHAR(size) [BINARY]
Size: Specified by the size value in a range of to 255 (1 to 255 prior to MySQL 3.23)
Storage: Length of value + 1 byte
Description:
Storage for variable-length text. Trailing spaces are removed from VARCHAR values.
6.3. Dates
MySQL date types are extremely flexible tools for storing date information. It is up to the
application, not to the database, to validate date values. MySQL only checks months that
range from 0-12, and dates that range from 0-31. February 31, 2006 is a legal MySQL date.
More useful, however, is the fact that February 0, 2006 is a legal date. In other words, you
can use 0 to signify dates in which you do not know a particular piece of the date.
MySQL automatically converts date and time values to integer values when used in an integer
context.
DATE
DATE
Format: YYYY-MM-DD (2006-01-01)
Storage: 3 bytes
Description:
Stores a date in the range of January 1, 1000 ('1000-01-01') to December 31, 9999
('9999-12-31') in the Gregorian calendar.
DATETIME
DATETIME
Format: YYYY-MM-DD hh:mm:ss (2006-01-01 01:00:00)
Storage: 8 bytes
Description:
TIME
TIME
Format: hh:mm:ss (06:00:00)
Storage: 3 bytes
Description:
Stores a time value in the range of midnight ('00:00:00') to one second before
midnight ('23:59:59').
TIMESTAMP
TIMESTAMP[(display_size)]
Format: YYYYMMDDhhmmss (20010101060000)
Storage: 4 bytes
Description:
A simple representation of a point-in-time down to the second in the range of midnight
on January 1, 1970, to one minute before midnight on December 31, 2037. Its
primary utility is keeping track of table modifications. When you insert a NULL value
into a TIMESTAMP column, the current date and time are inserted instead. When you
modify any value in a row with a TIMESTAMP column, the first TIMESTAMP column
will be automatically updated with the current date and time.
YEAR
YEAR[(size)]
Format:YYYY (2006)
Storage: 1 byte
Description:
Stores a year of the Gregorian calendar. The size parameter enables you to store
dates using two-digit years or four-digit years. The range for a YEAR(4) is 1900 to
2155; the range for a YEAR(2) is 1970-2069.The default size is YEAR(4).
ENUM
ENUM(value1, value2, ...)
Storage: 1-255 members: 1 byte / 256-65,535 members: 2 bytes
Description:
Stores one value of a pre-defined list of possible strings. When you create an ENUM
column, you provide a list of all possible values. Inserts and updates are allowed to
set the column to values only from that list. Any attempt to insert a value that is not
part of the enumeration will cause an empty string to be stored instead.
SET
SET(value1, value2, ...)
Storage:
1-8 members: 1 byte
9-16 members: 2 bytes
17-24 members: 3 bytes
25-32 members: 4 bytes
33-64 members: 8 bytes
Description:
A list of values taken from a pre-defined set of values. A field can contain any number
- including none - of the strings specified in the SET statement. A SET is basically an
ENUM that allows each field to contain more than one of the specified values. A SET,
however, is not stored according to index, but as a complex bitmap. Given a SET with
the members Orange, Apple, Pear, and Banana, each element is represented by an
"on" bit in a byte, as shown below:
RDBMS : MySQL 14
MySQL's Representation of SET Elements
Member Decimal Value Bit-wise Representation
Orange 1 0001
Apple 2 0010
Pear 4 0100
Banana 8 1000
In this example, the values Orange and Pear are stored in the database as 5 (0101).
You can store a maximum of 64 values in a SET column. Although you can assign
the same value multiple times in an SQL statement updating a SET column, only a
single value will actually be stored.
7. Column Options
In MySQL, at the time of table creation, or at a later stage, one can add, modify or change the
following column options. These column options restrict the data to be inserted according to
the option specified:
8. Operators
MySQL offers three kinds of operators: arithmetic, comparison, and logical.
Operators Description
Operators Description
<> or != Match rows if the two values are not equal
<= Match rows if the left value is less than or equal to the right
value
< Match rows if the left value is less than the right value
>= Match rows if the left value is greater than or equal to the right
value
> Match rows if the left value is greater than the right value
value BETWEEN value1 Match rows if value is between value1 and value2, or equal to
AND value2 one of them
Operators Description
NOT or ! Performs a logical not (returns "1" if the value is 0, NULL if it is
NULL, otherwise "0").
RDBMS : MySQL 16
OR or || Performs a logical or (returns "1" if any of the arguments are
non-zero and non-NULL, NULL if any are NULL; otherwise,
returns "0").
XOR Performs a logical exclusive or (returns "1" if one and only one
argument is non-zero and non-NULL, NULL if any are NULL;
otherwise. returns "0").
AND or && Performs a logical and (returns "0" if any of the arguments are
0, NULL if any are NULL; otherwise, returns "1").
• Out-of-range to In-range Values: MySQL clips or truncates the value to the highest or
lowest numbers possible.
• If you insert a negative into an unsigned column, it is converted to zero.
• When you reach the upper limit of AUTO_INCREMENT, any subsequent insert will
fail with a duplicate key error.
• If you try to store totally different data types for which no conversion is possible, the
DEFAULT is stored.
• String truncation: If you try to store a string value that is larger than the current
maximum allowed size, it is truncated.
• Date and Time: Calendar checks are not performed. If, for example, you try to enter
'2006-04-31' into a DATE field, it will accept it.
• Any attempt to enter invalid values, such as '24:00:00' in a TIME column will enter a
zero: '00:00:00'
• ENUM and SET: A wrong ENUM entry will enter a null string ' '. A wrong SET entry is
simply dropped, only legal ones taken.
• Assignment of NULL to NOT-NULL: For a single row insert the statement will fail. For
multiple row inserts the column is assigned the default value, generating a
WARNING.
• Conversion of fixed-point values: If you insert '0003' in a numeric column, it is
inserted as a 3. If you do the same with a DECIMAL column, it is stored as is,
truncating as many values as required. For example, storing '0003' into
DECIMAL(2,0) results in '03'.
Once you know the proper parameters, you should be able to connect like this:
shell> mysql
After you have connected successfully, you can disconnect any time by typing QUIT (or \q) at
the:
mysql> QUIT
The following table displays each of the prompts you can see, and summarizes what they
mean about the state that mysql is in:
Prompt Meaning
mysql> Ready for new command.
-> Waiting for next line of multiple-line
command.
'> Waiting for next line, collecting a string that
begins with a single quote
(‘'’).
"> Waiting for next line, collecting a string that
begins with a double-quote (‘"’).
`> Waiting for next line, collecting an identifier
that begins with a back-tick
(‘`’).
Multiple-line statements commonly occur by accident, when you intend to issue a command
on a single line, but forget the terminating semicolon.
Under UNIX, database names are case-sensitive (unlike SQL keywords), so you must always
refer to your database as school, not as School, SCHOOL, or some other variant. This is also
true for table names. This restriction does not apply under MS Windows.
Creating a database does not select it for use; you must do that explicitly. To make school the
current database, use this command:
Your database needs to be created only once, but you must select it for use each time you
begin a mysql session. You can do this by issuing a USE statement, as demonstrated in the
example. Alternatively, you can select the database on the command-line when you invoke
mysql. Specify its name after any connection parameters that you might need to provide. For
example:
RDBMS : MySQL 18
Note that school in the command just shown is not your password. If you want to supply your
password on the command-line after the -p option, you must do so with no intervening space
(for example, as -pmypassword, not as -p mypassword).
Once you have created a table, SHOW TABLES should produce some output:
To verify that your table was created according to your given specifications, use DESCRIBE
any time, for example, if you forget the names of the columns in your table, or what types they
have. Use a DESCRIBE statement:
ALTER TABLE works by making a temporary copy of the original table. The alteration is
performed on the copy, then the original table is deleted, and the new one is renamed. While
ALTER TABLE is executing, the original table is readable by other clients. Updates and writes
Use modify or change instead of add, which also depends on what you are altering, whether it
is a column option or a column type. Using modify in an alter table statement allows the user
to modify the column type, and change is similar to modify, except that a column name
change is also allowed.
Change multiple table names in one go by separating them with a comma (,):
Supply multiple alterations to a table in a single command by separating them with a comma
(,):
mysql> alter table candidates rename to student,
modify student_id int primary key auto_increment;
12. Indexes
As data in a table grows large, look-ups can become slow. Indexes allow column values to be
found more quickly, and retrievals based on indexes are relatively faster. In order to keep
your queries performing efficiently, it is essential to index your tables. Indexes can enforce
uniqueness to prevent duplicates.
Indexes can be defined when a table is being created. This includes the index definitions in
the CREATE TABLE statement, along with the column definitions. An index definition consists
of an appropriate keyword or keywords to indicate the index type, followed by a list in
parentheses that names the column or columns that comprise the index. Suppose that the
definition of a table teacher without any indexes looks like this:
RDBMS : MySQL 20
12.1. Non-unique Indexes:
To create the table with the same columns, but with a non-unique index on the column
teacher_joining, include an INDEX clause in the CREATE TABLE statement as:
The keyword KEY may be used instead of INDEX. To include multiple columns in an index
(that is, to create a composite index), list all the column names within the parentheses,
separated by commas. A composite index, for example, that includes both the teacher_fname
and teacher_lname columns can be defined by replacing teacher_fname and teacher_lname
separated by comma instead of teacher_joining.
Indexes can be added to a table, provided they do not cause a conflict. Addition is done by
using ALTER TABLE or CREATE INDEX statements:
This section describes the basic principles of entering commands, working with several
queries, from the basic to the most complex ones. The query illustrates several things about
mysql:
• When you issue a command, mysql sends it to the server for execution and
displays the results, then prints another mysql> prompt to indicate that it is ready
for another command.
• mysql displays query output in tabular form (rows and columns). The first row
contains labels for the columns. The following rows are the query results.
• Mysql shows how many rows were returned and how long the query took to
execute, which gives you a rough idea about server performance.
The simple SELECT statement for basic data retrieval would look like this:
RDBMS : MySQL 22
13.1.2. Restricting a selection using WHERE Clause
In the preceding examples, the result rows are displayed in no particular order. It is often
easier to examine query output when the rows are sorted in some meaningful way.
The default sort order is ascending, with the smallest values first. To sort in reverse
(descending) order, add the DESC keyword to the name of the column you are sorting by.
Limit clause allows you to limit the output of a query. The limit clause is useful, especially in
conjunction with ORDER BY. This is useful for retrieving records based on their position
within the set of selected rows.
LIMIT may be given with either one or two arguments, which must be integer constants:
LIMIT row_count
LIMIT skip_count, row_count
When followed by a single integer, row_count, LIMIT returns the first row_count rows from the
beginning of the result set. To select just the first two rows of student table, use the following
query:
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 super-aggregate) summary
operations. The GROUP BY clause may be added to generate a more fine-grained summary
that produces values for sub-groups within a set of selected rows.
Suppose we have three tables of a school database i.e. student, course and enrolment
tables. The scenario is that the students are enrolled in courses, the details of which are in
the enrolment table. So we would first describe all the three tables as:
In order to check which student is enrolled in how many courses, submit this query, and the
result will be grouped by GROUP BY clause as:
RDBMS : MySQL 24
This query will result as:
The first syntax for INSERT uses separate column and value lists, following the name of the
table into which you want to add the record. The number of columns and values must be the
same. The statement shown here uses this syntax to create a new record in the people. If the
column list is not mentioned in the statement, then provide the value list which should include
all values to be added in to the column, as described below:
The second INSERT syntax follows the table name by a SET clause that lists individual
column assignments separated by commas, and with this syntax we can add the values to the
desired columns only:
A single INSERT … VALUES statement can add multiple records to a table if you provide
multiple VALUES lists with parenthesis separating the lists by commas:
REPLACE returns an information string indicating how many rows it has affected. If the count
is one, the row was inserted without replacing an existing row. If, for example, the count is
two, a row was deleted before the new row was inserted.
RDBMS : MySQL 26
An advantage of using REPLACE instead of an equivalent DELETE (if needed) and INSERT
is that REPLACE is performed as a single atomic operation. There is no need to do any
explicit table locking as there might be if you were to issue separate DELETE and INSERT
statements.
In the student table, for example, there is a NULL value, and we want to make a correction in
that row:
After finding this NULL entry in the student table, correct the entry by querying the database
with the following query:
To remove only specific records in a table, issue a DELETE statement that includes a
WHERE clause that identifies which records to remove:
In this statement, because the WHERE clause is present, MySQL evaluates it for each row.
WHERE 1 is always true, so the effect of the statement is to produce a row-by-row table-
14. Joins
Basically, it is the combining of two rows based on the comparative values in selected
columns. This 'super-row' exists only for the duration of the query that creates it. Use joins to
temporarily create 'complete' records from a database which may split related data across
several tables (perhaps as a result of normalization).
14.1. Cross-join
A cross-join between two tables takes the data from each row in table 1 and joins it to the
data from each row in table 2. This can be done in two different syntaxes. One syntax lists the
tables to be joined separated by a comma, and the other uses the CROSS JOIN keywords.
Both display the same result:
RDBMS : MySQL 28
Two common forms of outer joins are left joins and right joins. These are written using the
LEFT JOIN or RIGHT JOIN keywords rather than the comma operator or the INNER JOIN
keywords. Left and right joins can answer the same kinds of questions, and differ only slightly
in their syntax. A left join can always be rewritten into an equivalent right join.
LEFT JOIN shows the missing and/or not matching values in the second table:
Left join is especially useful when we want to find only those rows in the left side table that do
not appear in the right side table. Add a WHERE clause that looks for rows in the right table
that have NULL values:
RIGHT JOIN shows the missing and/or not matching values in the first table:
15. Reference
• MySQl Manual at http://mysql.com
RDBMS : MySQL 30
1. Downloading and Installing MySQL
Step 1: Before installing you need to download the latest version of MySQL. In this example it
would be version 4.0.10, available from the MySQL site.
Note: Although it is possible to install MySQL from RPMs, the best method is to compile from
the source, as this gives greater flexibility, customization options, and speed.
Step 2: You will need to decide where you are going to store your downloads. This
installation guide assumes a location of /software/. If this directory is not already created,
create it and then change to it by using the following commands:
mkdir /software
cd /software
Now we will create the folder for the MySQL downloads. Type the following commands:
mkdir mysql
cd mysql
Note: This guide will install MySQL with support for Perl and OpenSSL.
Step 3: If you have an existing RPM installation, you should uninstall it before proceeding.
The first thing to do is to establish what MySQL RPMs are already installed, by running the
following command:
rpm -qa | grep -i mysql
If nothing is listed, then you do not have MySQL RPMs installed, and can safely proceed to
step 4. Otherwise, look carefully at the list, which will look something like this:
[root@server1 mysql]# rpm -qa | grep -i mysql
MySQL-client-3.23.55-1
MySQL-Max-3.23.55-1
MySQL-shared-3.23.55-1
MySQL-3.23.55-1
MySQL-devel-3.23.55-1
[root@server1 mysql]#
For each entry, use the command rpm -e packagename - where packagename is the name
part - i.e. the text right up until the version number - of the entry shown. Repeat this for each
of the entries in the list; so for the example output given, you would run the following
commands:
rpm -e MySQL-client
rpm -e MySQL-Max
rpm -e MySQL-shared
rpm -e MySQL
rpm -e MySQL-devel
Note: The RPM name will not always be obvious, or appear logical. However the general rule
of thumb is that the first hyphen (dash) after the text name but before the number is where the
name ends.
If in doubt, you can always simply use the whole entry (including the version number)
however you will obviously have to know the full entry first, which is best found out as
described above. Enter each command one by one to remove all the MySQL RPMs.
Important Note: The order you should follow is to remove MySQL last of all, with MySQL-
client immediately before that. Any other packages should be OK being removed first.
Using the example above, the packages should be removed in the following order:
rpm -e MySQL-Max
rpm -e MySQL-shared
rpm -e MySQL-devel
rpm -e MySQL-client
rpm -e MySQL
If you do run into errors, they are most likely to be in the form of broken dependencies, such
as shown here:
If that happens, look at the package that depends on the packages you are trying to remove,
and uninstall it first. If all else fails and you still get dependancy errors, remove the package
using the command rpm -e --nodeps packagename.
We should also stop MySQL if it is running. Type the following commands:
service mysql stop
pkill mysql
Step 4: The next step is to unpack, compile and install MySQL from the source file we just
downloaded. Type the following commands:
• tar zxvf *.tar.gz
• cd mysql-4.0.10-gamma
• ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var/lib/mysql
--mandir=/usr/share/man --infodir=/usr/share/info --with-mysqld-
user=mysql --with-openssl --with-tcp-port=3306 --with-unix-socket-
path=/var/lib/mysql/mysql.sock
• make
• make install
• cp support-files/mysql.server /etc/rc.d/init.d/mysqld
• chmod 755 /etc/rc.d/init.d/mysqld
• chkconfig --add mysqld
• cd ..
• rm -rf mysql-4.0.10-gamma
• cd ..
Step 5: Once the installation is complete, we need to set up MySQL by typing the following
commands:
• groupadd mysql
• useradd mysql -c "MySQL Server" -d /var/lib/mysql -g mysql
• scripts/mysql_install_db
• chown -R mysql:mysql /var/lib/mysql
• service mysqld start
• /usr/bin/mysqladmin -u root password 'new-password'
Step 6: If the mysql server responds failure to start then copy .cnf file into /etc/my.cnf
MySQL should start on it's own when the system boots.
/var/lib/mysql or /usr/local/mysql
The default data directory location can be explicitly specified during server start-up by using
the –datadir = / path option
RDBMS : MySQL 32
• Each database corresponds to a directory under the data directory
• Tables within a database correspond to files in the database directory
Data directory
/var/lib/mysql
Db 1 Db 2 Dbn
/var/lib/mysql/db1 /var/lib/mysql/db2 /var/lib/mysql/dbn
Tab 1
Tab 1
/var/lib/mysql/db1/table1.FRM
/var/lib/mysql/db2/table1.MYD
/var/lib/mysql/db3/table1.MYI
It is sometimes advisable to shutdown the server (if you are relocating a database, for
example, you do not want a server updating tables in that database). In order to deal with
such scenarios, you will be performing server start-up/shutdown so that you have the ability to
perform either function as and when deemed necessary.
All MySQL programs have many different options. Every MySQL program, however, provides
a --help option, which you can use in order to get a description of the program's options:
Override the default options for all standard programs by specifying options on the command-
line, or in an option file. The following table briefly describes the MySQL server, and its
server-related programs:
Restart the server manually by connecting to it and instructing it to terminate under the
following circumstances:
RDBMS : MySQL 34
• The root password can be forgotten
• Connections to the localhost are usually made through a UNIX domain socket file,
which is typically /tmp/mysql.sock
Root password forgotten
The root password might have been forgotten to a value you do not know. This can
happen when you change the password; if, for example, you accidentally type an invisible
code character when you enter the new password value. You might also have simply
forgotten the password.
In this case, when you cannot connect, you need to regain control of the server so that
you can reset the password again:
• The meaning of account names and passwords as used in MySQL, and how that
compares with names and passwords used by your operating system
• How to set up new accounts and remove existing accounts
• How to change passwords
• Guidelines for using passwords securely
• How to use secure connections with SSL
When you connect to a MySQL server with a command-line client, 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
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, 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
The client program will then print a prompt, and wait 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 call 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 does not set any limit for the length of the password. In order to
work around the problem, change your MySQL password to a value that is eight or fewer
characters long, or place your password in an option file.
The preferred method is to use GRANT statements, because they are more concise, and less
error-prone. GRANT is available as of MySQL 3.22.11; its syntax is described in the MySQL
Language Reference.
Another option is to use one of several available third-party 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. In
order to make changes, connect to the MySQL server as the MySQL root user. The root
account must have the INSERT privilege for the MySQL database, and the RELOAD
administrative privilege.
Use the MySQL program to connect to the server as the MySQL root user:
If you have assigned a password to the root account, you will also need to supply a
--password or -p option for this MySQL command, and also for those mentioned further on in
this section. After connecting to the server as root, you can add new accounts. The following
statements use GRANT to set up four new accounts:
RDBMS : MySQL 36
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
-> IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
The accounts created by these GRANT statements contain the following properties:
• Two of the accounts have a username of monty and a password of some_pass. Both
accounts are super-user 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. It is necessary to
have both accounts for monty in order 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 will take precedence when monty connects from the local
host. As a result, monty will 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 therefore 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 the mysqladmin processlist. No privileges are granted for accessing any
databases. You can 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 have been granted. The USAGE
privilege in the GRANT statement allows you to create an account without giving it
any privileges. It has the effect of setting all the global privileges to 'N'. It is assumed
that you will grant specific privileges to the account later.
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 will go unnoticed until you
restart the server. With GRANT, FLUSH PRIVILEGES is unnecessary.
The 'Y' values enable privileges for the accounts. Depending on your MySQL version, you
might have to use a different number of 'Y' values in the first two INSERT statements.
(Versions prior to 3.22.11 have fewer privilege columns, and versions from 4.0.2 on have
more). For the administrative account, the more readable extended INSERT syntax using
SET that is available starting with MySQL 3.22.11 is used.
In the INSERT statement for the dummy account, only the host, user, and password columns
in the user table record are assigned values. None of the privilege columns are explicitly set,
so MySQL assigns them all the default value of 'N'. This is equivalent to what GRANT
USAGE does.
In order to set-up a super-user account, it is only necessary 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.
The next examples create three accounts, and give them access to specific databases. Each
of them has a username, “custom”, and a password, “obscure”.
In order to create the accounts with GRANT, type the following statements:
• The first account can access the bank account 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
Delete the user table record and flush the GRANT tables:
RDBMS : MySQL 38
mysql> DELETE FROM mysql.user
-> WHERE User='user_name' and Host='host_name';
Starting from MySQL 4.0.2, however, you can limit the following server resources for
individual accounts:
Any statement that a client can issue counts against the query limit. Only statements that
modify databases or tables count against the update limit.
An account in this context is a single record in the user table. Each account is uniquely
identified by its user and host column values.
As a pre-requisite 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, and max_connections columns. If your user table does not have these
columns, it needs to 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. In order to create a new
account that can access the customer database, for example, but only in a limited fashion,
issue this statement:
The limit types need not all be named in the WITH clause, but those named can be presented
in any order. The value for each limit should be an integer representing a count per hour. If
the GRANT statement has no WITH clause, each of the limits is set to the default value of
zero, that is, no limit.
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:
This statement leaves the account's existing privileges unchanged, and modifies only the limit
values specified.
To remove an existing limit, set its value to zero. To remove the limit on how many times per
hour Francis can connect, for example, use this statement:
SAMP_DB.20000901
• Put your back-up files on a different file system than the one on which your databases
are.
• Back-up your back-up files using file system back-ups.
• MySQL stores database information under its sub-directory (in the data directory)
• Involves copying the data directory of the server
• Extremely fast (bit-wise copy of files)
• Must Lock tables for write access, or shutdown the server before copying the files
• Not very portable
5.3.2. Mysqlhotcopy
• Mysqlhotcopy is a PERL script that was originally written and contributed by Tim
Bunce. It uses LOCK TABLES, FLUSH TABLES, and CP or SCP to quickly make a
back-up of a database
• It is the fastest way to make a back-up of the database or single tables, but it can only
be run on the same machine on which the database directories are located
• Only works on MyISAM table types
RDBMS : MySQL 40
shell> mysqlhotcopy db_name [/path/to/new_directory]
shell> mysqlhotcopy db_name_1 ... db_name_n /path/to/new_directory
You can also copy all the table files (*.frm, *.MYD, and *.MYI files) as long as the server is
not updating anything. The mysqlhotcopy script uses this method.
This does not work if your database contains InnoDB tables. InnoDB does not store table
contents in database directories, and mysqlhotcopy works only on MyISAM and ISAM
tables.
5.3.3. mysqldump
Mysqldump options
Option Description
For compressed SQL output mysqldump backup | gzip > /tmp/backup.sql.gz
-no-create-db This option suppresses the CREATE DATABASE /*!32312 IF
NOT EXISTS*/ db_name statements that are otherwise
included in the output if the –databases or –all-databases
option is given.
Syntax:
mysqldump –no-create-db db-name > backup-file-name
--no-create-info Do not write CREATE TABLE statements that recreate each
dumped table.
Syntax:
mysqldump --no-create-info backup >backup.sql
--no-data Do not write any row information for the table. This is very
useful if you want to get a dump of structure for a table.
Syntax:
mysqldump –no-data db-name < backup-file-name
EXAMPLE
mysqldump -no-data backup < backup.sql
Database Restoration
• Create an empty database either from the command-line, or directly from the prompt:
Shell> mysqladmin create backup
• Go to the path where your back-up file resides, and issue the following command:
Shell> mysql backup < backup.sql
Both systems have MySQL installed, and the database exampledb with tables and data
already exists on the master, but not on the slave
Edit /etc/mysql/my.cnf. Enable networking for MySQL, and MySQL should listen on all IP
addresses. Comment out these lines, if they exist:
#skip-networking
#bind-address = 127.0.0.1
Tell MySQL which database it should write logs for (these logs are used by the slave to see
what has changed on the master); which log file it should use; and specify that this MySQL
server is the master. If you want to replicate the database exampledb, place the following
lines into /etc/mysql/my.cnf:
log-bin = /var/log/mysql/mysql-bin.log
binlog-do-db=exampledb
server-id=1
Restart MySQL:
/etc/init.d/mysql restart
Log into the MySQL database as root, and create a user with replication privileges:
mysql -u root –p
Enter password:
USE exampledb;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
RDBMS : MySQL 42
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)
Write down this information, you will need it later on the slave.
quit;
There are two possibilities of getting the existing tables and data from exampledb from the
master onto the slave. The first is to make a database dump; the second is to use the LOAD
DATA FROM MASTER; command on the slave. The latter carries the disadvantage that the
database on the master will be locked during this operation. If you have a large database on a
high-traffic production system, this is not what you want; follow the first method in this case.
The second method, however, is faster. Both will be described here.
This will create an SQL dump of exampledb in the file exampledb.sql. Transfer this file to your
slave server.
If you want to use the LOAD DATA FROM MASTER; method, do nothing for the moment.
mysql -u root -p
Enter password:
UNLOCK TABLES;
quit;
mysql -u root -p
Enter password:
CREATE DATABASE exampledb;
quit;
If you have made an SQL dump of exampledb on the master, and have transferred it to the
slave, import the SQL dump into the newly-created exampledb on the slave:
If you want to go the LOAD DATA FROM MASTER; method, do nothing for the moment.
Tell MySQL on the slave that it is the slave, that the master is 192.168.0.100, and that the
master database to watch is exampledb. Add the following lines to /etc/mysql/my.cnf:
server-id=2
master-host=192.168.0.100
master-user=slave_user
Restart MySQL:
/etc/init.d/mysql restart
If you have not imported the master exampledb with the help of an SQL dump, but want to
follow the LOAD DATA FROM MASTER; method, get the data from the master exampledb:
mysql -u root -p
Enter password:
LOAD DATA FROM MASTER;
quit;
If you have phpMyAdmin installed on the slave, check if all the tables and the data from the
master exampledb are also available on the slave exampledb.
Type:
mysql -u root -p
Enter password:
SLAVE STOP;
START SLAVE;
quit;
The replication process is now complete. Whenever exampledb is updated on the master, all
the changes will be replicated to exampledb on the slave as well.
7. Reference
• For a description of the syntax of administration-related SQL statements, see the MySQL
Language Reference.
• We suggest that you visit MySQL Web site at http://www.mysql.com often and read updates
RDBMS : MySQL 44
Comparison of Oracle, MySQL and
PostgreSQL DBMS
RDBMS : MySQL 46
1.6. Sub-queries in SQL query:
DBMS Description
MySQL Yes, starting from Version 4.1
Oracle Uncorrelated and correlated sub-queries allowed up to 255 levels
PostgreSQL SQL compliance with Oracle
1.9. Check:
DBMS Description
MySQL Yes, for MyISAM tables and views. No for InnoDB tables, where the CHECK
clause is allowed for compatibility only, and it has no effect on the
database’s operation
Oracle Yes
PostgreSQL Yes
1.10. Views:
DBMS Description
MySQL Yes, in Version 5 and higher
Oracle Yes
PostgreSQL Yes
1.12. Synonyms (an alias for any table, view or other object in database):
DBMS Description
MySQL No
Oracle Yes
PostgreSQL No
1.14. Automatic conversion of code pages (e.g. between client and server):
Automatic conversion of code pages means that if the code pages on the server and on the
client differ, conversion should be carried out.
DBMS Description
MySQL No
Oracle Yes
PostgreSQL No
2. TRANSACTIONS
3. PROGRAMMING IN DB
RDBMS : MySQL 48
extend MySQL with a new function that works like native MySQL functions
Oracle PL/SQL and Java
PostgreSQL PL/PGSQL, PL/TCL, PL/Perl, SQL, C, possibility of creating a new language
- CREATE LANGUAGE
3.2. Triggers:
DBMS Description
MySQL Rudimentary support for triggers is included as of Version 5.0.2
Oracle In PL/SQL. Possibility of writing triggers reacting on the events:
BEFORE/AFTER DELETE/UPDATE/INSERT. INSTEAD OF triggers
can be used to update data through views
PostgreSQL Declarative rules – an extension to SQL. The user can specify SELECT,
INSERT, DELETE or UPDATE as a rule event. INSTEAD OF rules can be
used to update data through views
Procedural triggers in PL/PGSQL, PL/TCL, PL/Perl, and C. CREATE
CONSTRAINT TRIGGER creates a trigger to support a constraint. The user
can specify BEFORE or AFTER on INSERT, DELETE or UPDATE as a
trigger event
4. ADMINISTRATION
RDBMS : MySQL 50
export/import utilities
PostgreSQL Yes
DBMS Description
MySQL A multi-threaded server that can use many processors. A separate thread is
created for each connection
Oracle Oracle can use multiprocessor SMP systems, e.g. for query paralleling
(Parallel Query Option)
PostgreSQL PostgreSQL is not threaded, but every connection gets its own process
DBMS Description
MySQL Handles large databases
Oracle Terabyte databases exist
PostgreSQL 60GB databases exist
6.7. Replication:
Replication is the process of copying and maintaining database objects in multiple databases
that make up a distributed database system. Replication can improve performance, and
protect the availability of applications because alternate data access options exist.
DBMS Description
MySQL Replication works well in the last stable Version (3.23.32). It is a master-
slave replication, using the binary log of operations on the server’s side. It is
possible to build star or chain type structures
Oracle Generally 3 Oracle tools/products cover the replication area. All of them are
part of the Oracle Server Enterprise Edition (not extra-cost options).
RDBMS : MySQL 52
architecture:
• JDBC OCI Client-side Driver - for client-server applications
• JDBC Server Driver - to create applications working on the server’s
side (servlets)
PostgreSQL PostgreSQL provides a type 4 JDBC Driver. Type 4 indicates that the driver
is written in Pure Java, and communicates in the database's own network
protocol. Because of this, the driver is platform-independent. Once compiled,
the driver can be used on any platform
8. RELIABILITY
RDBMS : MySQL 54