Defining Composite Key with Auto Increment in MySQL

后端 未结 5 1441
心在旅途
心在旅途 2020-12-01 04:01

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

相关标签:
5条回答
  • 2020-12-01 04:13

    You can do this with myISAM and BDB engines. InnoDB does not support this. Quote from MySQL 5.0 Reference Manual.

    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.

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

    0 讨论(0)
  • 2020-12-01 04:18

    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 ;
    
    0 讨论(0)
  • 2020-12-01 04:18

    I don't fully understand your increment requirement on the test_id column, but if you want an ~autoincrement sequence that restarts on every unique combination of (app_id, test_id), you can do an INSERT ... SELECT FROM the same table, like so:

    mysql> INSERT INTO `issue_log` (`sr_no`, `app_id`, `test_id`, `issue_name`) SELECT
               IFNULL(MAX(`sr_no`), 0) + 1 /* next sequence number */,
               3 /* desired app_id */,
               1 /* desired test_id */,
               'Name of new row'
               FROM `issue_log` /* specify the table name as well */
           WHERE `app_id` = 3 AND `test_id` = 1 /* same values as in inserted columns */
    

    This assumes a table definition with no declared AUTO_INCREMENT column. You're essentially emulating autoincrement behavior with the IFNULL(MAX()) + 1 clause, but the manual emulation works on arbitrary columns, unlike the built-in autoincrement.

    Note that the INSERT ... SELECT being a single query ensures atomicity of the operation. InnoDB will gap-lock the appropriate index, and many concurrent processes can execute this kind of query while still producing non-conflicting sequences.

    0 讨论(0)
  • 2020-12-01 04:22

    Just add key(sr_no) on auto-increment column:

    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),
      key (`sr_no`)
    );
    
    0 讨论(0)
  • 2020-12-01 04:27

    You can use a unique composite key for sr_no,app_id & test_id. You cannot use incremental in sr_no as this is not unique.

    CREATE TABLE IF NOT EXISTS `issue_log` (
      `sr_no` int(11) NOT NULL,
      `app_id` int(11) NOT NULL,
      `test_id` int(11) NOT NULL,
      `issue_name` varchar(255) NOT NULL,
      UNIQUE KEY `app_id` (`app_id`,`test_id`,`sr_no`)
    ) ENGINE=InnoDB ;
    

    I have commented out unique constraint violation in sql fiddle to demonstrate (remove # in line 22 of schema and rebuild schema )

    0 讨论(0)
提交回复
热议问题