Copyright © 2006 Pakistan Software Export Board (G) Limited Ministry of Information Technology Government of Pakistan Printing Artland Communications

, Lahore. September 2006 Published by Pakistan Software Export Board The Funding Agency This open source toolkit is funded by the Open Source Resource Center (OSRC) project of the Pakistan Software Export Board (PSEB). PSEB is the entity within Government charged with the task of enhancing exports of software and IT enabled services (ITES) from Pakistan. PSEB is a guarantee limited company totally owned and funded by the Government of Pakistan. Any questions or comments about this toolkit may be directed to PSEB Islamabad at 92-51-111-333-666 or through e-mail at osrc@pseb.org.pk. 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.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
13.2.1. Adding Multiple Records with a Single INSERT Statement........................................................26

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.1.1. Configure the Master.....................................................................................................................43 6.1.2. Configure the Slave.......................................................................................................................44

6. REPLICATION IN MYSQL.........................................................................................................................43 6.1. Set-up Database Replication in MySQL .....................................................................................43 7. REFERENCE.............................................................................................................................................45 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

COMPARISON OF ORACLE, MYSQL AND POSTGRESQL DBMS..........................................46

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

Open Source Software Training Toolkit

1

RDBMS: An Introduction

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:

You would run a query like this: mysql> select * from student;

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

Open Source Software Training Toolkit

3

3. Relational Databases
3.1. Relational vs. flat file databases
A flat file database is a database designed around a single table. The flat file design puts all data into one table, or list, with fields that represent all parameters. A flat file may contain many fields, often with duplicate data that is prone to data corruption. If you decide to merge data between two flat files, copy and paste relevant information from one file to the other. There is no automation between flat files. If you have two or more flat files that contain client addresses, for example, and a client has moved, you will have to manually modify the address parameters in each file that contains that client’s information. Changing information in one file has no bearing on other files. Flat files offer the functionality to store information, manipulate fields, print or display formatted information, and exchange information with others through e-mail and over the Internet. Some flat files may be attached to external files, such as text editors, in order to extend functionality and manage related information. 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.

3.2. Database Normalization
If you have been working with databases for a while, then you must be familiar with the term, “normalization”. Perhaps someone has asked you, "Is that database normalized?" or, "Is that in BCNF?" All too often, the reply is, "Yes". Knowing the principles of normalization, however,

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: • • Eliminate duplicative columns from the same table Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key)

The second normal form (2NF) further addresses the concept of removing duplicative data: • • • Meet all the requirements of the first normal form Remove sub-sets of data that apply to multiple rows of a table and place them in separate tables Create relationships between these new tables and their predecessors through the use of foreign keys

The third normal form (3NF) goes one large step further: • • Meet all the requirements of the second normal form Remove columns that are not dependent upon the primary key

Finally, the fourth normal form (4NF) has one additional requirement: • • Meet all the requirements of the third normal form A relation is in 4NF if it has no multi-valued dependencies

Remember, these normalization guidelines are cumulative. For a database to be in 2NF, it must first fulfill all the criteria of a 1NF database.

Open Source Software Training Toolkit

5

MySQL: An Introduction

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. Following are the major features of MySQL: • MySQL is a relational database management system. • MySQL software is Open Source. • The MySQL Database Server is very fast, reliable, and easy to use. • MySQL Server works in client/server or embedded systems.

2. Conformance with SQL standards
MySQL supports entry-level SQL-92. Entry-level contains the set of features that define basic SQL-92 compliance. Oracle conforms to SQL-89, which is a sub-set of SQL-92 types plus specific types. Some SQL-92 types are mapped into Oracle types. PostgreSQL uses an extended sub-set of the SQL92-99 and SQL 3 languages. SQL-92 data types’ syntax is mapped directly into native PostgreSQL types.

3. MySQL Client Programs
MySQL client programs can be invoked from the command-line; from a Windows console prompt, or from a UNIX shell prompt, for example. When you invoke a client program, you can specify options to control its behavior. Some options tell the client how to connect to the MySQL server. Other options tell the program which actions to perform.

3.1. The mysql Client Program
The mysql client program enables you to send queries to the MySQL server, and to receive their results. To determine the options supported by any mysql program, invoke the program with the --help option. For example, to find out how to use mysql, type this command: shell> mysql –help;

To determine the version of a program, use the --version option: shell> mysql –version;

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.

Open Source Software Training Toolkit

7

--port=port_number or -P port_number 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 This option specifies the username for your MySQL account. --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

3.2. The mysqlimport Client Program
The mysqlimport client program loads datafiles into tables. It provides a command-line interface to the LOAD DATA INFILE statement. This means that mysqlimport examines the options given on the command line and builds a LOAD DATA INFILE statement that corresponds to the actions specified by those options. It then connects to the server and, for each input file, issues the LOAD DATA INFILE statement that correctly loads the file into the appropriate table. Invoke mysqlimport from the command line as follows: shell> mysqlimport options db_name input_file 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).

3.3. The mysqldump Client Program
The mysqldump client program dumps table contents onto files. The program is useful for making database backups, or for transferring database contents to another server. mysqldump can produce SQL-format dump files that contain CREATE TABLE and INSERT statements for recreating the dumped files, or it can produce tab-delimited datafiles. 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

Reloading mysqldump Output To reload an SQL-format dump file produced by mysqldump, process it with mysql. In order to make a copy of the student table from the school database in the college database, for example, you can issue the following commands: shell> mysqldump --opt school student > student.sql shell> mysql college < student.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: shell> mysqldump --opt school student | mysql college

3.4. The mysqlcheck and myisamchk Client Program
The mysqlcheck and myisamchk programs are used to check and repair tables (primarily MyISAM tables). They can help to keep your tables free from problems, or repair them if problems do occur - if, for example, a MyISAM table suffers damaged as a result of a server crash. 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.

4. General Database and Table Manipulation
Every MySQL server has a data directory under which it manages the contents of databases and tables. The server represents this as follows:

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.

Open Source Software Training Toolkit

9

5. Storage Engines and Table Types
MySQL supports several storage engines that act as handlers for different table types. MySQL storage engines include both those that handle transaction-safe tables and those that handle non-transaction-safe tables: • • 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: VARCHAR -> CHAR

RDBMS : MySQL

10

When the specified VARCHAR column size is less than four characters, it is converted to CHAR: CHAR -> VARCHAR 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 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (0 18,446,744,073,709,551,615 unsigned).

from to

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.2250738585072014E308 to 1.7976931348623157E+308. FLOAT FLOAT[(display_size, digits)] [ZEROFILL]

Open Source Software Training Toolkit

11

Storage: 4 bytes Description: A single-precision floating-point number, this type is used to store small floating-point numbers. FLOAT columns can store negative values between -3.402823466E+38 and -1.175494351E-38, 0, and positive values between 1.175494351E-38 and 3.402823466E+38. 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:

Open Source Software Training Toolkit

13

Stores a specific time in the range of 12:00:00 AM, January 1, 1000 (‘1000-01-01 00:00:00’) to 11:59:59 P.M., December 31, 9999 (‘9999-12-31 23:59:59’) in the Gregorian calendar. 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).

6.4. Complex Types
MySQL's complex data types ENUM and SET are special string types. They are listed separately because they are conceptually more complex, and represent a lead into the SQL 3 data types that MySQL may support in the future. 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 Orange Apple Pear Banana Decimal Value 1 2 4 8 Bit-wise Representation 0001 0010 0100 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: • • • • • • • UNSIGNED makes the column not have negative values. ZEROFILL pads zeros to the display width. AUTO_INCREMENT applies to integer columns. Used to generate sequences of unique values. Only one such column per table. BINARY applies to CHAR and VARCHAR. NULL and NOT NULL apply to all. DEFAULT value provides a value in case no value was given. PRIMARY KEY and UNIQUE.

8. Operators
MySQL offers three kinds of operators: arithmetic, comparison, and logical.

8.1. Precedence Rule
When SQL contains complex expressions, the sub-expressions are evaluated based on MySQL's rules of precedence. You can always override MySQL's rules of precedence by enclosing an expression in parentheses: Precedence priority 1 2 3 4 5 6 7 8 9 10 11 12 13 Operators BINARY NOT ^ - (unary minus) */% +<< >> & | <= > >= = <=> <> IN IS LIKE REGEXP BETWEEN CASE AND OR XOR

8.2. Arithmetic Operators
Arithmetic operators perform basic arithmetic on two values: Operators Description

Open Source Software Training Toolkit

15

+ * / % | ^ & << >>

Adds two numerical values Subtracts two numerical values Multiplies two numerical values Divides two numerical values Gives the modulus of two numerical values Performs a bit-wise OR on two integer values Performs a bit-wise exclusive OR on two integer values Performs a bit-wise AND on two integer values Performs a bit-wise left shift on an integer value Performs a bit-wise right shift on an integer value

8.3. Comparison Operators
Comparison operators compare values and return 1 if the comparison is true, and 0 if otherwise. Except for the <=> operator, NULL values cause a comparison operator to evaluate to NULL: Operators <> or != <= < >= > value BETWEEN AND value2 Description 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 Match rows if value is between value1 and value2, or equal to one of them Match rows if value is among the values listed Match rows if value is not among the values listed Compares value1 to value2 and matches the rows if they match. The right hand value can contain the wildcard '%', which matches any number of characters (including 0), and '_', which matches exactly one character. Its most common use is comparing a field value with a literal containing a wildcard (e.g., SELECT name FROM people WHERE name LIKE 'B%'). Compares value1 to value2 and matches the rows if they differ. This is identical to NOT (value1 LIKE value2). Compares value1 to value2 using the extended regular expression syntax and matches the rows if the two values match. The right hand value can contain full UNIX regular expression wildcards and constructs (e.g., SELECT name FROM people WHERE name RLIKE '^B.*'). Compares value1 to value2 using the extended regular expression syntax and matches the rows if they differ. This is identical to NOT (value1 REXEXP value2).

value1

value IN ( value1,value2,...) value NOT IN ( value1, value2,...) value1 LIKE value2

value1 NOT LIKE value2 value1 value2 REGEXP/RLIKE

value1 value2

NOT

REGEXP

8.4. Logical Operators
Logical operators check the truth value of one or more expressions. In SQL terms, a logical operator checks whether its operands are 0, nonzero, or NULL. A 0 value means false, nonzero means true and NULL means no value: Operators NOT or ! Description Performs a logical not (returns "1" if the value is 0, NULL if it is NULL, otherwise "0").

RDBMS : MySQL

16

OR or || XOR AND 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"). 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"). Performs a logical and (returns "0" if any of the arguments are 0, NULL if any are NULL; otherwise, returns "1").

8.5. MySQL Conversions
MySQL is very forgiving if the given value doesn’t match the data type of a column. You MUST validate the data on the client side before putting it in. Sometimes the effects of automatic conversion can be very surprising. Never rely on automatic conversion unless you are sure of what you are doing. The following conversions are performed by MySQL:


• • •

• •

• • •

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'.

9. 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 the mysql client program 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 which connection parameters you should use to connect (that is, what host, user name, and password to use). For details regarding the MySQL client program, view a previous section. Once you know the proper parameters, you should be able to connect like this: shell> mysql -h host -u user –p

Open Source Software Training Toolkit

17

The mysql> prompt tells you that mysql is ready for you to enter commands. Some MySQL installations allow you to connect as an 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> 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 mysql> -> '> "> `> Meaning 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.

10. Creating and Using a Database
If an administrator creates your database for you when setting up your permissions, you can begin using it. Otherwise, you need to create it yourself: mysql> CREATE DATABASE school; 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: mysql> USE school Database changed 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: shell> mysql -h host -u user -p school Enter password: ********

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).

11. Table Manipulation
Tables can be created and modified as and when required. With a default database chosen, a table can be created as follows:

11.1. Creating a Table
mysql>CREATE TABLE tblname (col def 1,col def 2....) type=tabletype

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:

11.2. Dropping a Table
Alter and drop tables after creating them: shell> drop table teacher; shell> drop table if exists teacher;

11.3. Altering a Table
ALTER to: • • • • • TABLE allows you to change the structure of an existing table. You can alter a table Add or drop columns Change the name or definition of a column Add or drop indexes Apply a different sorting to the rows Rename the table

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

Open Source Software Training Toolkit

19

to the table are stalled until the new table is ready, and then are automatically redirected to the new table without any failed updates. Use the same teacher table for alteration as:

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.

11.4. Renaming a table
Rename changes the name of the table, and also works in an alter table statement: mysql> alter table teacher rename to lecturers; mysql> rename table teacher to lecturers; Change multiple table names in one go by separating them with a comma (,): mysql> rename table teacher to lecturers, students to candidates; 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. MySQL supports four types of indexes: • • • • Non-unique simple indexes UNIQUE index, every key value is different from others PRIMARY KEY is a UNIQUE key that disallows NULL values FULLTEXT is optimized for text searching

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: mysql> create table teacher ( teacher_id int(4) NOT NULL auto_increment, teacher_fname char(25), teacher_lname char(25), teacher_address char(250), teacher_phone char(25), teacher_joining date NOT NULL, Index (teacher_joining) ) TYPE=MyISAM; 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.

12.2. Unique Indexes
Unique enforces the uniqueness of the column and prohibits duplicates. It is especially useful when assigning lookup values, for example. ID's mysql> create table teacher ( teacher_id int(4) NOT NULL auto_increment, teacher_fname char(25), teacher_lname char(25), teacher_address char(250), teacher_phone char(25), teacher_joining date NOT NULL, Unique (teacher_id) ) TYPE=MyISAM;

12.3. Primary Key Indexes
There is only one primary key per table. Primary key is a UNIQUE index which does not permit a NULL value. mysql> create table teacher ( teacher_id int(4) NOT NULL auto_increment, teacher_fname char(25), teacher_lname char(25), teacher_address char(250), teacher_phone char(25), teacher_joining date NOT NULL, Primary Key (teacher_id) ) TYPE=MyISAM; 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: mysql> ALTER TABLE teacher ADD PRIMARY KEY (teacher_id);

12.4. Dropping Indexes
An index may be dropped with Drop Index or Alter Table statements, such as: mysql> DROP INDEX teacher_joining on teacher; mysql > ALTER TABLE teacher DROP INDEX teacher_joining; mysql > ALTER TABLE teacher DROP PRIMARY KEY;

Open Source Software Training Toolkit

21

13. MySQL Queries
MySQL fully supports ANSI SQL-92, entry-level. A SQL reference for MySQL is largely a general SQL reference. SQL is a kind of controlled English language consisting of verb phrases. Each of these verb phrases begins with an SQL command followed by other SQL keywords, literals, identifiers, or punctuation. 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.

13.1. The SELECT Statements
It is no use putting records in a database unless you eventually intend to retrieve them and do something with them; that is the main purpose of a SELECT statement. The basic syntax of the SELECT statement looks like this: SELECT selection-list FROM table-list WHERE primary-constrain GROUP BY group-cols ORDER BY sorting-col HAVING secondary-constraint LIMIT count selection-list table-list primary-constrain group-cols sorting-col secondary-constraint count 13.1.1. Basic Data Retrieval The simple SELECT statement for basic data retrieval would look like this: what to select where to select rows from what conditions rows must satisfy how to group results how to sort results secondary conditions rows must satisfy limit on results

For selected records it would look like this:

RDBMS : MySQL

22

13.1.2. Restricting a selection using WHERE Clause You can select only particular rows from your table:

13.1.3. Sorting a selection using ORDER BY 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. To sort a result, use an ORDER BY clause:

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. 13.1.4. Limiting a selection using LIMIT Clause 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:

Open Source Software Training Toolkit

23

When followed by two integers, skip_count and row_count, LIMIT skips the first skip_count rows from the beginning of the result set, and then returns the next row_count rows. To skip the first two rows and then return the next two rows, type:

13.1.5. Grouping a selection using GROUP BY Clause 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:

13.2. The INSERT Statement
The INSERT statement adds new records to a table. It has two basic formats, one of which allows for the insertion of multiple rows using a single statement: INSERT INTO table_name (column_list) VALUES (value_list); INSERT INTO table_name SET column_name1 = value1; 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:

Open Source Software Training Toolkit

25

13.2.1. Adding Multiple Records with a Single INSERT Statement 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:

13.3. REPLACE Statement
REPLACE statement syntax is similar to that of INSERT. Each of the following are valid forms of REPLACE: REPLACE INTO table_name (column_list) VALUES (value_list); REPLACE INTO table_name SET column_name1 = value1; 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.

13.4. UPDATE Statement
The UPDATE statement modifies the contents of existing records. To use it, name the table you want to update, provide a SET clause that lists one or more column value assignments, and optionally specify a WHERE clause that identifies which records to update: UPDATE table_name SET column_name1 = value1, column_name2 = value2,… WHERE ... ; 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:

13.5. DELETE Statement
To remove records from tables, use a DELETE statement. The DELETE statement allows a WHERE clause that identifies which records to remove; it can, therefore, be more precise in its effect. To empty a table entirely by deleting all its records, type the following statement: DELETE FROM table_name; To remove only specific records in a table, issue a DELETE statement that includes a WHERE clause that identifies which records to remove: DELETE FROM table_name WHERE ...; If a table contains an AUTO_INCREMENT column, emptying it completely with a DELETE statement that includes no WHERE clause might have the side-effect of resetting the sequence. In this case, the next record inserted into the table is assigned an AUTO_INCREMENT value of 1. If this side-effect is unwelcome, empty the table using a WHERE clause that always evaluates to true: DELETE FROM table_name WHERE 1; 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-

Open Source Software Training Toolkit

27

emptying operation. Although this form of DELETE avoids the side-effect of resetting the AUTO_INCREMENT sequence when performing a complete-table deletion, the cost is that the statement will execute much more slowly than a DELETE with no WHERE.

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: mysql> select course_name, student_fname from course, student; mysql> select course_name, student_fname from course CROSS JOIN student;

14.2. Inner Join
In an inner join, the comparison being made between two columns is that they match the same value. Use this method to select certain fields from both tables, and only the correct rows will be joined together. Inner joins may be written using two different syntaxes. One syntax lists the tables to be joined separated by a comma, and the other uses the INNER JOIN keywords. Both display the same result: mysql> select student_fname, course_name from course, student, enrolment where student.student_id = enrolment.student_id and enrolment.course_id = course.course_id; mysql> select student_fname, course_name from enrolment INNER JOIN student, course on student.student_id = enrolment.student_id and enrolment.course_id = course.course_id;

14.3. Outer Join
Use an outer join to write a join that provides information about mismatches or missing records. An outer join can find matches (just like an inner join), but can also identify mismatches. Furthermore, with an appropriate WHERE clause, an outer join can filter out matches to display only the mismatches.

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. 14.3.1. Left Join LEFT JOIN shows the missing and/or not matching values in the second table: mysql> select student_fname, enrolment_id from student LEFT JOIN enrolment on student.student_id = enrolment.student_id; 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: mysql> select student_fname, enrolment_id from student left join enrolment on student.student_id=enrolment.student_id where enrolment.student_id is NULL;

14.3.2. Right Join RIGHT JOIN shows the missing and/or not matching values in the first table: mysql> select student_fname, enrolment_id from student RIGHT JOIN enrolment on student.student_id = enrolment.student_id;

15. Reference
• MySQl Manual at http://mysql.com

Open Source Software Training Toolkit

29

MySQL: Administration

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 MySQLclient 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:

Open Source Software Training Toolkit

31

[root@server1 mysql]# rpm -e MySQL error: removing these packages would break dependencies: MySQL = 3.23.55 is needed by MySQL-Max-3.23.55-1 [root@server1 mysql]# 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-mysqlduser=mysql --with-openssl --with-tcp-port=3306 --with-unix-socketpath=/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.

2. The MySQL Data Directory
The data directory is where the server stores its databases and status files, and provides information about the server's operation. It is important to understand the structure and the contents of the data directory, so that you know how the server uses the file system to represent databases and tables, and where the logs are located. By default, all the data managed by the MySQL server mysqld is stored under the MySQL data directory.

2.1. Location of the Data Directory
A default data directory is compiled into the server: /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

2.2. Structure of Data Directory
The MySQL data directory contains all the databases and tables managed by the server. These are organized into a tree structure, that is implemented by taking advantage of the hierarchical structure of the UNIX or MS Windows file systems:

RDBMS : MySQL

32

• •

Each database corresponds to a directory under the data directory Tables within a database correspond to files in the database directory

2.3. How the MySQL Server provides access to Data
Client 1 Client 2 Client 3

UNIX – socket

TCP/IP – port

NT – pipe

MySQL server (mysqld)

Data directory /var/lib/mysql

Db 1 /var/lib/mysql/db1 Tab 1 Tab 2 Tab 3

Db 2 /var/lib/mysql/db2

Dbn /var/lib/mysql/dbn

Tab 1

Tab 2

Tab 3

Tab 1

Tab 2

Tab 3

Where Tab 1 or any other table in the previous figure is: Tab 1

Tab 1 /var/lib/mysql/db1/table1.FRM /var/lib/mysql/db2/table1.MYD /var/lib/mysql/db3/table1.MYI

2.4. Database Table Representation
Each table within a database exists in the form of three files in the database directory: a form (description) file, a data file and an index file. The base name is the table name, and the file extensions indicate the file type.

Open Source Software Training Toolkit

33

3. MySQL Server Start-up and Shutdown
A MySQL administrator’s goal is to ensure that the server is running, and that the clients can access it. 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.

3.1. The MySQL Server and Server Startup Scripts
The MySQL server, mysqld, is the main program that does most of the work in a MySQL installation. The server is accompanied by several related scripts that perform setup operations when you install MySQL, or are helper programs which assist you in starting and stopping the server. 3.1.1. An Overview of the Server-Side Scripts 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: shell> mysqld --help. Override the default options for all standard programs by specifying options on the commandline, or in an option file. The following table briefly describes the MySQL server, and its server-related programs: Server-side Script mysqld mysqld-max mysqld_safe mysql.server Description The SQL daemon, that is, the MySQL server. This program must be running because clients gain access to databases by connecting to this server in order to use client programs. A version of the server that includes additional features. A server start-up script. mysqld_safe attempts to start mysqldmax if it exists, and mysqld otherwise. A server start-up script. This script is used on systems that use run directories containing scripts that start system services for particular run levels. It invokes mysqld_safe to start the MySQL server. A server start-up script that can start or stop multiple servers installed on the system. This script creates the MySQL grant tables with default privileges. It is usually executed only once, when initially installing MySQL on a system. This script is used after an upgrade installation operation in order to update the grant tables with any changes that have been made in newer versions of MySQL.

mysqld_multi mysql_install_db mysql_fix_privilege_tables

3.1.2. Methods of Starting the Server INVOKE mysqld directly. This is probably the least common method. INVOKE the safe_mysqld or mysqld_safe script. safe_mysqld attempts to determine the location of the server program and the data directory and then invokes the server with options reflecting those values. 3.1.3. Regaining the control of the Server if you cannot connect: 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: • Bring down the server o If you can log in as root on the server host, you can terminate the server using the kill command. o Note: You can find out the server’s process ID via the ps command. • RESTART the server with the –skip-grant-tables option o This tells the server not to use the grant tables to verify the connections. That allows you to connect as root with no password. After you are connected, change the root password as required. • After changing the password, use the command flush privileges. • Restart the server. When the Socket File is Removed If you cannot connect because you have lost the socket file, you can get it back by restarting the server, because the server recreates it at start-up time. The trick here is that you cannot use the socket to establish a connection because it is gone. You must establish a TCP/IP connection instead: mysqladmin -uroot -p'pass' -h'ip-addr/hostname' shutdown Restart the server again after this.

4. MySQL User Account Management
This section describes how to set up accounts for your MySQL server’s clients. It discusses the following topics: • • • • • 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

4.1. 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 MS Windows or UNIX. On UNIX, most MySQL clients try by default to log in using the current UNIX username as the MySQL username, but that is for the sake of convenience only. The default can be overridden easily, because client programs allow any username to be specified with a -u or --user option. Since this means that anyone can attempt to connect to the server using any username, you cannot make a database secure in any way unless all MySQL accounts have passwords. Anyone who specifies a username for an account that has no password will be able to connect successfully to the server. MySQL usernames can be up to sixteen characters long. Operating system usernames might have a different maximum length. Usernames in UNIX, for example, are typically limited to eight characters.

Open Source Software Training Toolkit

35

MySQL passwords have nothing to do with passwords for logging into your operating system. There is no connection between the password you use to log in to a MS Windows or UNIX machine, and the password you use to access the MySQL server on that machine. MySQL encrypts passwords using its own algorithms. This encryption is different from that which is 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 onwards, MySQL employs a stronger authentication method which features better password protection during the connection process than in earlier versions. It is secure even if TCP/IP packets are sniffed, or the database mysql is captured. (In earlier versions, even though passwords were stored in an encrypted form in the user table, knowledge of the encrypted password value could be used to connect to the MySQL server).

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.

4.2. Adding New User Accounts to MySQL
Create MySQL accounts in two ways: • • By using GRANT statements By manipulating the MySQL grant tables directly

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: shell> mysql --user=root mysql 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; 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 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.

As an alternative to GRANT, create the same accounts directly by issuing INSERT statements, and then telling the server to reload the GRANT tables: 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 will go unnoticed until you restart the server. With GRANT, FLUSH PRIVILEGES is unnecessary.

Open Source Software Training Toolkit

37

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 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: 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 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

4.3. Removing User Accounts from MySQL
To remove an account, use the DROP USER statement, which has been added in MySQL 4.1.1. For older MySQL versions, use DELETE instead. To remove a MySQL user account before 4.1.1, follow the procedure outlined below: • • • Use SHOW GRANTS to determine what privileges the account has. 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. Delete the account by using DROP USER to remove the user table record.

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'; mysql> FLUSH PRIVILEGES;

4.4. Limiting Account Resources
Before MySQL 4.0.2, the only available method for limiting the use of MySQL server resources was to set the max_user_connections system variable to a non-zero value. That method, however, is strictly global. It does not allow for the management of individual accounts. It also limits only the number of simultaneous connections made using a single account, not what a client can do once connected. Both types of control are of interest to many MySQL administrators, particularly those of interest to Internet Service Providers (ISPs). Starting from MySQL 4.0.2, however, 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. 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: 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; 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: 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. 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: mysql> GRANT USAGE ON *.* TO 'francis'@'localhost'

Open Source Software Training Toolkit

39

-> WITH MAX_CONNECTIONS_PER_HOUR 0;

4.5. Assigning Account Passwords
Passwords may be assigned from the command-line by using the mysqladmin command.

5. Disaster Prevention and Recovery
This section discusses how to make database back-ups and how to perform table maintenance.

5.1. Database Back-ups
• • • • If a system crash occurs, you want to be able to restore your tables to the state they were in at the time of the crash, with as little data loss as possible. A user might accidentally issue a DROP or DELETE statement, and then want you to restore the data. An administrator might try to edit table files with normal editors, thereby usually corrupting the table. A sudden hard-disk failure is common, and having a recent back-up of the databases can ease the recovery process.

5.2. Back-up Principles
• • • • Perform regular back-ups. Set up a schedule and follow it strictly. Instruct the server to perform update logging. Update logs represent incremental dumps, and are needed to restore the databases to their state at the time of crash. Use FLUSH LOGS to ensure that new updated logs are synchronized with back-up files. Use a consistent and an understandable back-up file-naming scheme by including the date in the filename, for example: 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.

5.3. Back-up Methods
There are three ways to perform the back-up: • Direct copy method • Mysqldump • Mysqlhotcopy 5.3.1. Direct copy method • • • • • 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

Consider the following example:

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 • • • • • • Regenerates the table data and/or schema using SQL statements Extremely portable between different versions of MySQL Allows portability to other databases as well Very slow in comparison to hot copy Back-up files are very large (SQL text) but can be compressed with bzip/gzip Client utility Syntax: mysqldump [options] db_name [tables]> backup-file-name mysqldump [options] –databases > backup-file-name mysqldump [options] –all-databases > backup-file-name Assuming the database being used for the table previously is ‘backup’, we issue: shell> mysqldump backup > backup.sql Description mysqldump backup | gzip > /tmp/backup.sql.gz 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 Do not write CREATE TABLE statements that recreate each dumped table. Syntax: mysqldump --no-create-info backup >backup.sql 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

Mysqldump options Option For compressed SQL output -no-create-db

--no-create-info

--no-data

Dumping multiple databases Syntax: mysqldump –databases db1 db2 > backup-file-name Dumping all databases Syntax: mysqldump –all-databases > backup-file-name 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

Open Source Software Training Toolkit

41

6. Replication in MySQL
Replication capabilities, allowing the databases on one MySQL server to be duplicated on another, were introduced in MySQL 3.23.15. This section describes the various replication features provided by MySQL. It introduces replication concepts, shows how to set up replication servers, and serves as a reference guide to the replication options currently available.

6.1. Set-up Database Replication in MySQL
MySQL replication requires the user to have an exact copy of a database from a master server on another server (slave), and all updates to the database on the master server are immediately replicated to the database on the slave server, so that both databases remain synchronized. This is not a back-up policy, because an accidentally issued DELETE command will also be carried out on the slave; but replication can help protect against hardware failure. In this tutorial, you will learn how to replicate the database exampledb from the master with the IP address 192.168.0.100 to a slave. Both systems (master and slave) are running Fedora Core 3; the configuration, however, should apply to almost all distributions, and among all major platforms, with little, or no, modification. Both systems have MySQL installed, and the database exampledb with tables and data already exists on the master, but not on the slave 6.1.1. Configure the Master 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: You are now on the MySQL shell: GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY '<some_password>'; (Replace <some_password> with a real password!) FLUSH PRIVILEGES; Still on the MySQL shell, type: USE exampledb; FLUSH TABLES WITH READ LOCK; SHOW MASTER STATUS; The last command will display something like this: +---------------+----------+--------------+------------------+ | File | Position | Binlog_do_db | Binlog_ignore_db | +---------------+----------+--------------+------------------+ | mysql-bin.006 | 183 | exampledb | |

RDBMS : MySQL

42

+---------------+----------+--------------+------------------+ 1 row in set (0.00 sec) Write down this information, you will need it later on the slave. Leave the MySQL shell: 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. If you want to follow the first method, type: mysqldump -u root -p<password> --opt exampledb > exampledb.sql (Replace <password> with the real password for the MySQL user root. Important: There is no space between -p and <password>. 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. Unlock the tables in exampledb: mysql -u root -p Enter password: UNLOCK TABLES; quit; The configuration on the master is now finished. 6.1.2. Configure the Slave Create the database exampledb on the slave: 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: mysql -u root -p<password> exampledb < /path/to/exampledb.sql (Replace <password> with the real password for the MySQL user root. Important: There is no space between -p and <password> 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

Open Source Software Training Toolkit

43

master-password=secret master-connect-retry=60 replicate-do-db=exampledb 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; Still on the MySQL shell, replace the values appropriately: CHANGE MASTER TO MASTER_HOST='192.168.0.100', MASTER_USER='slave_user', MASTER_PASSWORD='<some_password>', MASTER_LOG_FILE='mysql-bin.006', MASTER_LOG_POS=183; MASTER_HOST is the IP address or hostname of the master (in this example it is 192.168.0.100). MASTER_USER is the user we granted replication privileges on the master. MASTER_PASSWORD is the password of MASTER_USER on the master. MASTER_LOG_FILE is the file MySQL gave back when you ran SHOW MASTER STATUS; on the master. MASTER_LOG_POS is the position MySQL gave back when you ran SHOW MASTER STATUS; on the master. Now all that is left to do is start the slave. Still on the MySQL shell we run 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

Open Source Software Training Toolkit

45

1. ELEMENTARY FEATURES:
1.1. Conformity with SQL standards:
DBMS MySQL Oracle PostgreSQL Description Entry-level SQL’92 Conformity with SQL'89, subset of SQL'92 types plus specific types. Some SQL'92 types are mapped into Oracle types. No Boolean type or equivalent Extended subset of the SQL’92/’99 and SQL3 languages. SQL'92 data types syntax are mapped directly into native PostgreSQL types

1.2. Binary and Character Large Objects:
DBMS MySQL Description Binary Character TINYBLOB, BLOB, MEDIUMBLOB, The four TEXT types TINYTEXT, and LONGBLOB, differ only in the TEXT, MEDIUMTEXT, and maximum length of the values; they LONGTEXT correspond to the four can hold up to 2^32(4GB) bytes BLOB types and have the same maximum length and storage requirements LONG RAW (2 GB), BLOB (4 GB) LONG (2 GB) and CLOB (4 GB) data and BFILE (external storage, up to 4 types GB) data types Must be defined by the user in TEXT type (size limited by a CREATE TYPE; size is limited by a maximum row size) maximum row size. BLOBs are saved in files outside tables; only the OID of the BLOB is stored in the PostgreSQL table

Oracle PostgreSQL

1.3. User-defined data types:
DBMS MySQL Oracle PostgreSQL Description None The user can define new complex data types The user may add new types to PostgreSQL using the CREATE TYPE command

1.4. Object-relational extensions:
DBMS MySQL Oracle PostgreSQL Description None Yes The table structure can be inherited

1.5. Special data types (multimedia, etc.)
DBMS MySQL Oracle PostgreSQL Description None Yes Geometric types that represent two-dimensional spatial objects

RDBMS : MySQL

46

1.6. Sub-queries in SQL query:
DBMS MySQL Oracle PostgreSQL Description Yes, starting from Version 4.1 Uncorrelated and correlated sub-queries allowed up to 255 levels SQL compliance with Oracle

1.7. Primary and Unique keys:
DBMS MySQL Oracle PostgreSQL Description Yes Yes Yes

1.8. Foreign key:
DBMS MySQL Description Yes, for InnoDB table types where a relational or multi-table delete can be performed For MyISAM table type FOREIGN KEY clause is allowed for compatibility only, and it has no effect on the database’s operation Yes, ON DELETE CASCADE supported Yes, ON DELETE CASCADE and ON UPDATE CASCADE supported

Oracle PostgreSQL

1.9. Check:
DBMS MySQL Oracle PostgreSQL Description 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 Yes Yes

1.10. Views:
DBMS MySQL Oracle PostgreSQL Description Yes, in Version 5 and higher Yes Yes

1.11. Updateable views:
DBMS MySQL Oracle PostgreSQL Description Updateable VIEWs based on a single table or other updateable VIEWs available from MySQL 5.0.1 release Yes Yes - rights to the view are pushed through to the underlying base tables

1.12. Synonyms (an alias for any table, view or other object in database):
DBMS MySQL Oracle PostgreSQL Description No Yes No

Open Source Software Training Toolkit

47

1.13. Auto Increment (counter) Columns:
DBMS MySQL Oracle PostgreSQL Description Yes. There can be only one auto increment column in a table, which must be indexed No Yes. SERIAL data type

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 MySQL Oracle PostgreSQL Description No Yes No

2. TRANSACTIONS
2.1. Support for transaction processing:
DBMS MySQL Oracle PostgreSQL Description Yes, for InnoDB table types Yes Yes, it offers the read committed and serializable isolation levels

2.2. Partial rollback of transaction:
DBMS MySQL Oracle PostgreSQL Description Starting from 4.0.14, InnoDB supports SAVEPOINT and ROLLBACK TO SAVEPOINT Rollback to save-point. There is also a possibility of starting a new, independent transaction from the current transaction Starting from 8.0.1 supports ROLLBACK, ROLLBACK TO SAVEPOINT

2.3. Locking level (table, page, and row):
DBMS MySQL Oracle PostgreSQL Description Table locking for ISAM/MyISAM and HEAP tables, page level locking for BDB tables InnoDB tables provide locking individual rows Row level and table level Row level and table level

2.4. Deadlock Detection and Resolving:
DBMS MySQL Oracle PostgreSQL Description Yes Yes Yes

3. PROGRAMMING IN DB
3.1. Languages for writing stored procedures:
DBMS MySQL Description Yes, as of version 5. Previously a UDF (written in C or C++) was used to

RDBMS : MySQL

48

Oracle PostgreSQL

extend MySQL with a new function that works like native MySQL functions PL/SQL and Java PL/PGSQL, PL/TCL, PL/Perl, SQL, C, possibility of creating a new language - CREATE LANGUAGE

3.2. Triggers:
DBMS MySQL Oracle PostgreSQL Description Rudimentary support for triggers is included as of Version 5.0.2 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 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
4.1. User authorization:
DBMS MySQL Description The user is identified on the base of the login, password, and hostname (from which the user can connect) Authorization is made on the server’s side, but there is the possibility of using secure connections between the client and the server using SSL The user is identified on the base of the login and password; there is also the possibility of using the operating system level authorization The following per-database authentication options are available in PostgreSQL: • Trust authentication • Password authentication • Kerberos authentication • Ident-based authentication • PAM - Pluggable Authentication Modules based authentication method

Oracle PostgreSQL

4.2. Encrypted Client - Server connection:
DBMS MySQL Oracle PostgreSQL Description SSL 4.0 Unknown SSL or SSH. Kerberos authentication

4.3. Object access privileges (level)
DBMS MySQL Oracle PostgreSQL Description Table level. UPDATE and INSERT rights can also be limited for selected columns Access rights on the table level. UPDATE, INSERT and REFERENCES rights on selected columns Table level

4.4. Access privileges grouping:
DBMS Description

Open Source Software Training Toolkit

49

MySQL Oracle PostgreSQL

No Privileges can be grouped into roles. The roles can be granted to users or to other roles Groups of users can be created and privileges can be granted to these groups

4.5. Incremental and on-line backups:
DBMS MySQL Oracle PostgreSQL Description InnoDB on-line backup, also supports incremental backups Yes May be added soon; is expected after the release of 7.5

4.6. Tools for data export:
DBMS MySQL Description Set of special utilities - mysqldump, mysqlhotcopy, mysqlsnapshot, innodb2myisam Selected tables or the whole database dumped to SQL statements ready to insert to another database BLOB values are saved inside normal tables, so there is no problem regarding dumping such values Only spooling of SQL query results pg_dump data dumping into a script file containing DML commands pg_dumpall is a utility for dumping out all PostgreSQL databases into one file COPY moves data between PostgreSQL tables and standard file-system files

Oracle PostgreSQL

4.7. Tools for data import:
DBMS MySQL Oracle PostgreSQL Description Import from text files, HTML, DBF High-speed and very flexible data loader COPY moves data between PostgreSQL tables and standard file-system files COPY instructs the PostgreSQL backend to directly read from, or write to, a file

5. PORTABILITY AND SCALABILITY
5.1. Hardware and system platforms supported:
DBMS MySQL Oracle PostgreSQL Description Many UNIX, Windows and OS/2 platforms. Packaged and available with most Linux distributions Over 200 platforms, including UNIX, Windows, mid-range and mainframe platforms Many UNIX and Windows platforms, QNX - real time system for x86

5.2. Portability of data and code (e.g. stored procedures)
DBMS MySQL Description Copy MySQL files (.FRM, .MYI, .MYD) between platforms with different architecture, but using the same floating-point format. Data and index files in ISAM databases are independent of hardware architecture and in some cases of systems, such as the "mysqldump" program Data and code can be ported between platforms without any changes using

Oracle

RDBMS : MySQL

50

PostgreSQL

export/import utilities Yes

5.3. Support for SMP systems (parallel query execution, etc.):
Parallel processing in single memory systems - also known as Symmetric Multi Processing (SMP) hardware, in which multiple processors use one memory resource. DBMS MySQL Oracle PostgreSQL Description A multi-threaded server that can use many processors. A separate thread is created for each connection Oracle can use multiprocessor SMP systems, e.g. for query paralleling (Parallel Query Option) PostgreSQL is not threaded, but every connection gets its own process

6. PERFORMANCE AND VLDB
6.1. Known VLDB implementations:
Very Large Databases (VLDB) mean multiple gigabytes or single terabytes. DBMS MySQL Oracle PostgreSQL Description Handles large databases Terabyte databases exist 60GB databases exist

6.2. Maximum number of rows and columns in a table:
DBMS MySQL Oracle PostgreSQL Description Maximum number of columns in one table - 3398; size of a table row - 65534 (BLOB and TEXT not included) Maximum number of columns in one table - 1000. Up to 32 columns in index key Unlimited, columns - 1600; size of a table row - 1.6TB

6.3. Maximum number of tables and indexes:
DBMS MySQL Oracle PostgreSQL Description Number of tables - unlimited, up to 32 indexes per table with 256 bytes maximum key length Unlimited Unlimited

6.4. "Manual" tuning of the allocation:
DBMS MySQL Oracle PostgreSQL Description Some parameters exist for InnoDB tables as table space, data files location, auto-extending, etc. Yes, many parameters of the allocation can be tuned on a system, table space or object level Data file location and a table space implementation as of Version 8

6.5. Automatic partitioning of large tables/indexes and using partitions in query optimization:
Partitioning addresses the problem of supporting very large tables and indexes by allowing users to decompose them into smaller and more manageable pieces called “partitions”.

Open Source Software Training Toolkit

51

DBMS MySQL Oracle PostgreSQL

Description Yes for InnoDB tables Yes Tables and indexes are automatically partitioned, but there are no specific enhancements for query optimization

6.6. Access to multiple databases in one session:
DBMS MySQL Oracle PostgreSQL Description Only switching between databases is possible. In SELECT, data from different databases can be taken Each session can use data from many instances, using transparent distributed SQL access Only switching between databases is possible. No support exists for selecting data from different databases

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 MySQL Oracle Description Replication works well in the last stable Version (3.23.32). It is a masterslave replication, using the binary log of operations on the server’s side. It is possible to build star or chain type structures Generally 3 Oracle tools/products cover the replication area. All of them are part of the Oracle Server Enterprise Edition (not extra-cost options). Oracle replication methods: Advanced Replication – traditional, trigger/snapshot-log based change data capture can be synchronous or asynchronous. Oracle Streams – Oracle Streams provide asynchronous, redo log based data replication. Oracle Streams capture process mines active redo logs. Oracle Data Guard – redo log based change data capture. Oracle Data Guard uses archived redo logs to capture the data changes. Oracle Data Guard standby databases can be either Physical standby or Logical standby. Physical replica/standby cannot be opened during replication. Logical replica can be opened during replication (useful for near real time reporting, replicated objects are in read-only mode) Several replication systems are available (both commercial and noncommercial) for master-slave, 2 way asynchrons, and cascading slave solutions

PostgreSQL

6.8. Gateways to other DBMSs:
DBMS MySQL Oracle PostgreSQL Description None Yes, gateways to other DBMSs are available None

7. APPLICATION DEVELOPMENT AND INTERFACES
7.1. Standard interfaces ODBC and JDBC:
DBMS MySQL Oracle Description ODBC (myODBC code) and 2 types of JDBC drivers are supported JDBC Thin Driver - for client-side applications (applets) in a three-layer

RDBMS : MySQL

52

PostgreSQL

architecture: • JDBC OCI Client-side Driver - for client-server applications • JDBC Server Driver - to create applications working on the server’s side (servlets) 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

7.2. Application support:
DBMS MySQL Oracle PostgreSQL Description C/C++, Java, Perl, Python, PHP, Ruby Oracle OCI - low level access to DBMS; Perl, Python C/C++, JAVA/JDBC, PHP, Perl, Ruby, TCK/TK, Python, Object Pascal and internal procedural languages like PL/pgSQL comparable to Oracle PL/SQL

7.3. Dedicated Web servers:
DBMS MySQL Oracle Description None Several specialized Oracle Web products exist, e.g.: • Oracle WebDB - for simple Web applications • Oracle Application Server (OAS) - for professional, scalable Web applications • Oracle Internet Server (IAS) – a specialized version of the Apache HTTP server None

PostgreSQL

7.4. XML support integrated in DBMS:
DBMS MySQL Oracle PostgreSQL Description None Many XML tools do integrate with DBMS (parsers, processors, XSLT, etc.) Provides an optional XML handling package distributed with the core server

7.5. Support from CASE packages:
DBMS MySQL Oracle PostgreSQL Description PLONE, Ruby on Rails Oracle Designer, JDeveloper Unknown

8. RELIABILITY
8.1. Automatic recovery from failures:
DBMS MySQL Oracle PostgreSQL Description Only InnoDB tables have automatic crash recovery. Hot backup tool is available for InnoDB to make backups of a running database in the background, without setting any locks or disturbing database operation Recovery from soft failures is automatic and transparent. Recovery from media failures requires a backup copy. With redo logs, it is possible to recover all committed transactions, or to recover upto a specific point in time Write Ahead Logging (WAL)

Open Source Software Training Toolkit

53

9. COMMERCIAL ISSUES
9.1. License type:
DBMS MySQL Oracle PostgreSQL Description GPL (GNU General Public License) or Commercial Negotiated with Oracle on a site-by-site basis BSD (Berkeley Software Distribution) open source

9.2. Technical support:
DBMS MySQL Oracle PostgreSQL Description Mailing lists and website. Paid technical support services are also available via telephone High quality technical support services are available on several levels Mailing lists and website. Commercial support is now becoming available from several smaller dedicated PostgreSQL companies (Command Prompt, PGSQL Inc.). It is also available in some geographical areas from major companies like Fujitsu, SRA, and Pervasive

9.3. Market share:
According to SD Times magazine, dated July 1, 2004, the Top Deployed Databases poll shows the following databases to be in popular use: • • • • SQL Server: 78% Oracle: 55% MySQL: 33% PostgreSQL - 8%

9.4. Specific market segments occupied:
DBMS MySQL Oracle PostgreSQL Description Web servers. Common on Linux-based systems Not applicable Education, Telephony, DNS Services, Web services

RDBMS : MySQL

54