Home arrow MySQL arrow Page 4 - Using Transactions In MySQL (Part 1)

Turning the Tables - MySQL

mysqlOne of the most-requested MySQL features - transactions - is finally available in MySQL 4.0. In this first segment of a two-part article, learn about the theory behind the transactional model, find out how it can make your SQL applications more robust, and find out how to implement a transactional environment with MySQL's InnoDB table handler.

  1. Using Transactions In MySQL (Part 1)
  2. Money, Money, Money...
  3. The Acid Test
  4. Turning the Tables
  5. A Question of Commitment
  6. Rules of the Game
  7. Artificial Intelligence
  8. Time Out
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 74
November 03, 2003

print this article


Now that you know the basics, let's take a little break from the theory and dive into the gritty reality of transactions. The first step is to create some InnoDB tables to use as a base for development. Before you can do this, though, you need to check if your MySQL build supports InnoDb tables. You can verify this by checking the "have_innodb" variable on a running MySQL server, as below:

mysql> SHOW VARIABLES LIKE 'have_innodb';
| Variable_name | Value |
| have_innodb | YES |
1 row in set (0.00 sec)

For BDB support, look for the "have_bdb" variable:

mysql> SHOW VARIABLES LIKE 'have_bdb';
| Variable_name | Value |
| have_bdb | YES |
1 row in set (0.00 sec)

Most recent MySQL binary distributions should support InnoDB out of the box. In case yours doesn't, or if you custom-built your MySQL server, you'll need to recompile it after adding the "--with-innodb" parameter to the configure script.

Now, even if InnoDB support is enabled, it doesn't mean that MySQL will create new tables using that format automatically. By default, when MySQL creates a new table, it does so using the MyISAM table format. As noted on the previous page, this table type does not support transactions. In order to tell MySQL to create an InnoDB table, therefore, it becomes necessary to add the optional TYPE clause to your CREATE TABLE command.

The following example illustrates, by creating an InnoDB table to store user names and passwords:

mysql> CREATE TABLE users (
-> id int(8) NOT NULL auto_increment,
-> name varchar(255) NOT NULL default '',
-> pass varchar(255) NOT NULL default '',
-> ) TYPE=InnoDB;
Query OK, 0 rows affected (0.16 sec)

In a similar manner, create two other tables, one for group memberships and the other for mailbox configuration.

mysql> CREATE TABLE groups (
-> uid int(8) NOT NULL default '0',
-> grp varchar(255) NOT NULL default ''
-> ) TYPE=InnoDB;
Query OK, 0 rows affected (0.09 sec)

mysql> CREATE TABLE mailboxes (
-> uid int(8) NOT NULL default '0',
-> host varchar(255) NOT NULL default '',
-> mboxname varchar(255) NOT NULL default '',
-> mboxpass varchar(255) NOT NULL default ''
->) TYPE=InnoDB;
Query OK, 0 rows affected (0.10 sec)

In case you already have MyISAM tables that you need to convert to InnoDB format for transactional usage, you might think that the only way is to manually back up the table data, drop the table, recreate it in InnoDB format and reinsert the records. You'd be wrong - MySQL allows you to alter the table type on the fly using the very cool ALTER TABLE command, as below:

mysql> ALTER TABLE mailboxes TYPE=INNODB;
Query OK, 1 row affected (0.13 sec)
Records: 1 Duplicates: 0 Warnings: 0

Obviously, you can also do this with the BDB (or any other) table type.

With the InnoDB tables created, let's move on to an actual transaction.

>>> More MySQL Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: