Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining Composite Key with Auto Increment in MySQL

Scenario:

I have a table which references two foreign keys, and for each unique combination of these foreign keys, has its own auto_increment column. I need to implement a Composite Key that will help identify the row as unique using combination of these three (one foreign keys and one auto_increment column, and one other column with non-unique values)

Table:

CREATE  TABLE `issue_log` ( `sr_no` INT NOT NULL AUTO_INCREMENT ,   `app_id` INT NOT NULL ,   `test_id` INT NOT NULL ,   `issue_name` VARCHAR(255) NOT NULL , primary key (app_id, test_id,sr_no) ); 

Of course, there has to be something wrong with my query, because of which the error thrown is:

ERROR 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key

What I am trying to achieve:

I have an Application Table (with app_id as its primary key), each Application has a set of Issues to be resolved, and each Application has multiple number of tests (so the test_id col) The sr_no col should increment for unique app_id and test_id.

i.e. The data in table should look like:

enter image description here

The database engine is InnoDB. I want to achieve this with as much simplicity as possible (i.e. avoid triggers/procedures if possible - which was suggested for similar cases on other Questions).

like image 957
Nirav Zaveri Avatar asked Aug 08 '13 07:08

Nirav Zaveri


People also ask

How do you set a field as auto increment in MySQL?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name.

Can you create an auto increment on a unique 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.

How do I automatically generate numbers in MySQL?

If you want to avoid writing sql, you can also do it in MySQL Workbench by right clicking on the table, choose "Alter Table ..." in the menu. When the table structure view opens, go to tab "Options" (on the lower bottom of the view), and set "Auto Increment" field to the value of the next autoincrement number.

Can you have a composite key in 1NF?

1NF has nothing to do with the presence of composite keys. To satisfy 1NF a table must be an accurate representation of a relation and therefore must have at least one candidate key, which may or may not include composite keys.


1 Answers

You can't have MySQL do this for you automatically for InnoDB tables - you would need to use a trigger or procedure, or user another DB engine such as MyISAM. Auto incrementing can only be done for a single primary key.

Something like the following should work

DELIMITER $$  CREATE TRIGGER xxx BEFORE INSERT ON issue_log FOR EACH ROW BEGIN     SET NEW.sr_no = (        SELECT IFNULL(MAX(sr_no), 0) + 1        FROM issue_log        WHERE app_id  = NEW.app_id          AND test_id = NEW.test_id     ); END $$  DELIMITER ; 
like image 108
noz Avatar answered Sep 18 '22 11:09

noz