Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql two column primary key with auto-increment

Tags:

mysql

I have multiple databases with the same structure in which data is sometimes copied across. In order to maintain data integrity I am using two columns as the primary key. One is a database id, which links to a table with info about each database. The other is a table key. It is not unique because it may have multiple rows with this value being the same, but different values in the database_id column.

I am planning on making the two columns into a joint primary key. However I also want to set the table key to auto increment - but based on the database_id column.

EG, With this data:

table_id   database_id     other_columns 1          1 2          1 3          1 1          2 2          2 

If I am adding data that includes the dabase_id of 1 then I want table_id to be automatically set to 4. If the dabase_id is entered as 2 then I want table_id to be automatically set to 3. etc.

What is the best way of achieving this in MySql.

like image 901
SystemicPlural Avatar asked Mar 24 '11 08:03

SystemicPlural


People also ask

Can I auto increment two columns MySQL?

You can't have two auto-increment columns.

Does MySQL auto increment primary key?

One of the important tasks while creating a table is setting the Primary Key. The Auto Increment feature allows you to set the MySQL Auto Increment Primary Key field. This automatically generates a sequence of unique numbers whenever a new row of data is inserted into the table.

Can auto increment be a primary key?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

Can there be 2 columns of primary key?

The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).


1 Answers

if you are using myisam

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

CREATE TABLE animals (     grp ENUM('fish','mammal','bird') NOT NULL,     id MEDIUMINT NOT NULL AUTO_INCREMENT,     name CHAR(30) NOT NULL,     PRIMARY KEY (grp,id) ) ENGINE=MyISAM;  INSERT INTO animals (grp,name) VALUES     ('mammal','dog'),('mammal','cat'),     ('bird','penguin'),('fish','lax'),('mammal','whale'),     ('bird','ostrich');  SELECT * FROM animals ORDER BY grp,id;  Which returns:  +--------+----+---------+ | grp    | id | name    | +--------+----+---------+ | fish   |  1 | lax     | | mammal |  1 | dog     | | mammal |  2 | cat     | | mammal |  3 | whale   | | bird   |  1 | penguin | | bird   |  2 | ostrich | +--------+----+---------+ 

For your example:

mysql> CREATE TABLE mytable (     ->     table_id MEDIUMINT NOT NULL AUTO_INCREMENT,     ->     database_id MEDIUMINT NOT NULL,     ->     other_column CHAR(30) NOT NULL,     ->     PRIMARY KEY (database_id,table_id)     -> ) ENGINE=MyISAM; Query OK, 0 rows affected (0.03 sec)  mysql> INSERT INTO mytable (database_id, other_column) VALUES     ->     (1,'Foo'),(1,'Bar'),(2,'Baz'),(1,'Bam'),(2,'Zam'),(3,'Zoo'); Query OK, 6 rows affected (0.00 sec) Records: 6  Duplicates: 0  Warnings: 0  mysql> SELECT * FROM mytable ORDER BY database_id,table_id; +----------+-------------+--------------+ | table_id | database_id | other_column | +----------+-------------+--------------+ |        1 |           1 | Foo          | |        2 |           1 | Bar          | |        3 |           1 | Bam          | |        1 |           2 | Baz          | |        2 |           2 | Zam          | |        1 |           3 | Zoo          | +----------+-------------+--------------+ 6 rows in set (0.00 sec) 
like image 173
dting Avatar answered Sep 25 '22 21:09

dting