Quantcast
Channel: How to use 2 auto increment columns in MySQL phpmyadmin - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 5

Answer by RolandoMySQLDBA for How to use 2 auto increment columns in MySQL phpmyadmin

$
0
0

I have good news and bad news

GOOD NEWS

You can multiple auto_increment values

BAD NEWS

Two things:

  1. It is only available for the MyISAM Storage Engine
  2. Only one numeric auto_increment value is allowed. Each auto_increment must be have associated column to define uniqueness from other auto_increment values within the same table.

I have discussed this before

Here is a sample table

USE testDROP TABLE IF EXISTS stores;CREATE TABLE stores(    store_type int not null,    id int not null auto_increment,    store_name varchar(128) not null,    PRIMARY KEY (store_type,id)) ENGINE=MyISAM;

Here is sample data

INSERT INTO stores (store_type,store_name) VALUES(1,'Red Lobster'),(1,'Olive Garden'),(2,'ShopRite'),(2,'PathMark'),(2,'Wegman''s'),(3,'McDonald''s'),(3,'Wendy''s'),(3,'Burger King'),(1,'Ruby Tuesdays'),(1,'TGI Fridays'),(4,'BJs'),(4,'Costco'),(1,'Bennigan''s');

Let's load it

mysql> USE testDatabase changedmysql> DROP TABLE IF EXISTS stores;Query OK, 0 rows affected (0.00 sec)mysql> CREATE TABLE stores    -> (    ->     store_type int not null,    ->     id int not null auto_increment,    ->     store_name varchar(128) not null,    ->     PRIMARY KEY (store_type,id)    -> ) ENGINE=MyISAM;Query OK, 0 rows affected (0.08 sec)mysql> INSERT INTO stores (store_type,store_name) VALUES    -> (1,'Red Lobster'),(1,'Olive Garden'),    -> (2,'ShopRite'),(2,'PathMark'),(2,'Wegman''s'),    -> (3,'McDonald''s'),(3,'Wendy''s'),(3,'Burger King'),    -> (1,'Ruby Tuesdays'),(1,'TGI Fridays'),    -> (4,'BJs'),(4,'Costco'),(1,'Bennigan''s');Query OK, 13 rows affected (0.00 sec)Records: 13  Duplicates: 0  Warnings: 0mysql>

Now, look at the data:

mysql> SELECT * FROM stores;+------------+----+---------------+| store_type | id | store_name    |+------------+----+---------------+|          1 |  1 | Red Lobster   ||          1 |  2 | Olive Garden  ||          2 |  1 | ShopRite      ||          2 |  2 | PathMark      ||          2 |  3 | Wegman's      ||          3 |  1 | McDonald's    ||          3 |  2 | Wendy's       ||          3 |  3 | Burger King   ||          1 |  3 | Ruby Tuesdays ||          1 |  4 | TGI Fridays   ||          4 |  1 | BJs           ||          4 |  2 | Costco        ||          1 |  5 | Bennigan's    |+------------+----+---------------+13 rows in set (0.00 sec)mysql>

Now, look at the data ordered by the PRIMARY KEY columns

mysql> SELECT * FROM stores ORDER BY store_type,id;+------------+----+---------------+| store_type | id | store_name    |+------------+----+---------------+|          1 |  1 | Red Lobster   ||          1 |  2 | Olive Garden  ||          1 |  3 | Ruby Tuesdays ||          1 |  4 | TGI Fridays   ||          1 |  5 | Bennigan's    ||          2 |  1 | ShopRite      ||          2 |  2 | PathMark      ||          2 |  3 | Wegman's      ||          3 |  1 | McDonald's    ||          3 |  2 | Wendy's       ||          3 |  3 | Burger King   ||          4 |  1 | BJs           ||          4 |  2 | Costco        |+------------+----+---------------+13 rows in set (0.00 sec)mysql>

Give it a Try !!!

UPDATE 2013-02-26 12:00 EST

Let's augment the SuperMarkets (store_type 2) by add 4000400 to the id values

mysql> UPDATE stores SET id = id + 4000400 WHERE store_type = 2;Query OK, 3 rows affected (0.02 sec)Rows matched: 3  Changed: 3  Warnings: 0mysql> SELECT * FROM stores ORDER BY store_type,id;+------------+---------+---------------+| store_type | id      | store_name    |+------------+---------+---------------+|          1 |       1 | Red Lobster   ||          1 |       2 | Olive Garden  ||          1 |       3 | Ruby Tuesdays ||          1 |       4 | TGI Fridays   ||          1 |       5 | Bennigan's    ||          2 | 4000401 | ShopRite      ||          2 | 4000402 | PathMark      ||          2 | 4000403 | Wegman's      ||          3 |       1 | McDonald's    ||          3 |       2 | Wendy's       ||          3 |       3 | Burger King   ||          4 |       1 | BJs           ||          4 |       2 | Costco        |+------------+---------+---------------+13 rows in set (0.00 sec)mysql>

Let's add A & P as another SuperMarket

mysql> INSERT INTO stores (store_type,store_name) VALUES (2,'A & P');Query OK, 1 row affected (0.00 sec)mysql> SELECT * FROM stores ORDER BY store_type,id;+------------+---------+---------------+| store_type | id      | store_name    |+------------+---------+---------------+|          1 |       1 | Red Lobster   ||          1 |       2 | Olive Garden  ||          1 |       3 | Ruby Tuesdays ||          1 |       4 | TGI Fridays   ||          1 |       5 | Bennigan's    ||          2 | 4000401 | ShopRite      ||          2 | 4000402 | PathMark      ||          2 | 4000403 | Wegman's      ||          2 | 4000404 | A & P         ||          3 |       1 | McDonald's    ||          3 |       2 | Wendy's       ||          3 |       3 | Burger King   ||          4 |       1 | BJs           ||          4 |       2 | Costco        |+------------+---------+---------------+14 rows in set (0.00 sec)mysql>

Looks like it can work for you !!!


Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>