MySQL
  Home arrow MySQL arrow Page 4 - Using Transactions In MySQL (Part 1)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Using Transactions In MySQL (Part 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 55
    2003-11-03

    Table of Contents:
  • Using Transactions In MySQL (Part 1)
  • Money, Money, Money...
  • The Acid Test
  • Turning the Tables
  • A Question of Commitment
  • Rules of the Game
  • Artificial Intelligence
  • Time Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Transactions In MySQL (Part 1) - Turning the Tables


    (Page 4 of 8 )

    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 '',
    -> PRIMARY KEY (id)
    -> ) 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


       · Thank you for this article. I wish I could look at Part 2 - but I cannot find it. ...
       · http://www.devshed.com/c/a/MySQL/Using-Transactions-In-MySQL-Part-2Aruna...
     

       

    MYSQL ARTICLES

    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT