MySQL: #1075 - Incorrect table definition; autoincrement vs another key?

前端 未结 6 864
旧时难觅i
旧时难觅i 2020-11-29 03:13

Here is a table in MySQL 5.3.X+ db:

CREATE TABLE members` (
  `id` int(11)  UNSIGNED NOT NULL AUTO_INCREMENT,
  `memberid` VARCHAR( 30 ) NOT NULL ,
  `Time`          


        
相关标签:
6条回答
  • 2020-11-29 03:50

    I think i understand what the reason of your error. First you click auto AUTO INCREMENT field then select it as a primary key.

    The Right way is First You have to select it as a primary key then you have to click auto AUTO INCREMENT field.

    Very easy. Thanks

    0 讨论(0)
  • 2020-11-29 03:51

    You can have an auto-Incrementing column that is not the PRIMARY KEY, as long as there is an index (key) on it:

    CREATE TABLE members ( 
      id int(11)  UNSIGNED NOT NULL AUTO_INCREMENT,
      memberid VARCHAR( 30 ) NOT NULL , 
      `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , 
      firstname VARCHAR( 50 ) NULL , 
      lastname VARCHAR( 50 ) NULL , 
      PRIMARY KEY (memberid) ,
      KEY (id)                          --- or:    UNIQUE KEY (id)
    ) ENGINE = MYISAM; 
    
    0 讨论(0)
  • 2020-11-29 03:51

    Identified this solution while reading this thread. Figured id post this for the next guy possibly.

    When dealing with Laravel migration file from a package, I Ran into this issue.

    My old value was

    $table->increments('id');
    

    My new

    $table->integer('id')->autoIncrement();
    
    0 讨论(0)
  • 2020-11-29 03:57

    You can make the id the primary key, and set member_id to NOT NULL UNIQUE. (Which you've done.) Columns that are NOT NULL UNIQUE can be the target of foreign key references, just like a primary key can. (I'm pretty sure that's true of all SQL platforms.)

    At the conceptual level, there's no difference between PRIMARY KEY and NOT NULL UNIQUE. At the physical level, this is a MySQL issue; other SQL platforms will let you use a sequence without making it the primary key.

    But if performance is really important, you should think twice about widening your table by four bytes per row for that tiny visual convenience. In addition, if you switch to INNODB in order to enforce foreign key constraints, MySQL will use your primary key in a clustered index. Since you're not using your primary key, I imagine that could hurt performance.

    0 讨论(0)
  • 2020-11-29 04:05

    For the above issue, first of all if suppose tables contains more than 1 primary key then first remove all those primary keys and add first AUTO INCREMENT field as primary key then add another required primary keys which is removed earlier. Set AUTO INCREMENT option for required field from the option area.

    0 讨论(0)
  • 2020-11-29 04:07

    First create table without auto_increment,

    CREATE TABLE `members`(
        `id` int(11) NOT NULL,
        `memberid` VARCHAR( 30 ) NOT NULL ,
        `Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
        `firstname` VARCHAR( 50 ) NULL ,
        `lastname` VARCHAR( 50 ) NULL
        PRIMARY KEY (memberid) 
    ) ENGINE = MYISAM;
    

    after set id as index,

    ALTER TABLE `members` ADD INDEX(`id`);
    

    after set id as auto_increment,

    ALTER TABLE `members` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT;
    

    Or

    CREATE TABLE IF NOT EXISTS `members` (
        `id` int(11) NOT NULL,
        `memberid` VARCHAR( 30 ) NOT NULL ,
        `Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
        `firstname` VARCHAR( 50 ) NULL ,
        `lastname` VARCHAR( 50 ) NULL,
          PRIMARY KEY (`memberid`),
          KEY `id` (`id`)
    ) ENGINE=MYISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
    
    0 讨论(0)
提交回复
热议问题