Working with MySQL Auto Incrementing Keys

If you’ve been working with Firebird, Oracle, or any number of other databases, the first thing you’ll want to understand is how to work with MySQL auto_increment keys. Anytime you work with keys that are auto incremented by the database engine, you will need a way to retrieve the key values for your insert statements. Relational database are built on joins and understanding how to assign your foreign keys is essential.

Using MySQL to manage your foreign key inserts you can use the LAST_INSERT_ID() function. This allows you to directly insert the value for your foreign keys without having to execute a query or some other mechanism. The LAST_INSERT_ID() MySQL function returns the value of the PRIMARY KEY previously inserted.

This works perfectly for simple designs where only a single foreign key exists and you want to link to the master table.

Example: Tables used

  1. CREATE TABLE web_template (
  2.         id bigint(20) NOT NULL AUTO_INCREMENT,
  3.         template_name varchar(30) NOT NULL,
  4.         template_location varchar(100) NOT NULL,
  5.  
  6.         UNIQUE KEY template_name (template_name),
  7.   PRIMARY KEY (id)
  8. ) TYPE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1000;
  9.  
  10.  
  11. CREATE TABLE web_config (
  12.         id bigint(20) NOT NULL AUTO_INCREMENT,
  13.         config_template_id bigint(20) NOT NULL,
  14.  
  15.   PRIMARY KEY (id),
  16.   FOREIGN KEY (config_template_id) REFERENCES web_template(id) ON DELETE CASCADE
  17. ) TYPE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1000;



Example: Insert statements

  1. INSERT INTO web_template (template_name, template_location) VALUES (‘Default Template’, ‘/template/Default/’);
  2.  
  3. INSERT INTO web_config (config_template_id) VALUES (LAST_INSERT_ID());



In PHP we have the mysql_insert_id() function, which allows us to retrieve the last key inserted. This function works pretty good, but it must be executed before you commit your transaction, otherwise you might get unexpected results. Additionally, it’s ideal to execute the statement directly after you submit your insert statement.

Using this function gives us more flexibility in key management in our applications, especially when working with tables that have multiple foreign keys.

Example: PHP statements

  1. <?php
  2. mysql_query("insert into web_template (template_name, template_location) values (‘Default Template’, ‘/template/Default/’");
  3. $web_template_id = mysql_insert_id();
  4. mysql_query("insert into web_config (config_template_id) values (".$web_template_id.")");
  5. ?>



Note: In a “data aware controls” environment, it’s not always easy to manage your auto incrementing keys (a lot of this is due to design, not just environment). In most cases where I have the decision making power I try to avoid data aware control development (especially on the web, now that Delphi for PHP is available). Read only grids are an exception, but most of the time you can design your applications to avoid “live data aware” entry/edit forms. It may seem like more work, but I believe in the end it’s cleaner and easier to debug.

Leave a Reply