Defining Composite Key with Auto Increment in MySQL

后端 未结 5 1474
心在旅途
心在旅途 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: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 )

提交回复
热议问题