<tfoot id='I8XwQ'></tfoot>

  • <i id='I8XwQ'><tr id='I8XwQ'><dt id='I8XwQ'><q id='I8XwQ'><span id='I8XwQ'><b id='I8XwQ'><form id='I8XwQ'><ins id='I8XwQ'></ins><ul id='I8XwQ'></ul><sub id='I8XwQ'></sub></form><legend id='I8XwQ'></legend><bdo id='I8XwQ'><pre id='I8XwQ'><center id='I8XwQ'></center></pre></bdo></b><th id='I8XwQ'></th></span></q></dt></tr></i><div id='I8XwQ'><tfoot id='I8XwQ'></tfoot><dl id='I8XwQ'><fieldset id='I8XwQ'></fieldset></dl></div>

      <bdo id='I8XwQ'></bdo><ul id='I8XwQ'></ul>
    1. <legend id='I8XwQ'><style id='I8XwQ'><dir id='I8XwQ'><q id='I8XwQ'></q></dir></style></legend>

      1. <small id='I8XwQ'></small><noframes id='I8XwQ'>

      2. Mysql - 将 auto_increment 添加到主键

        时间:2023-07-18

          <tbody id='RMxM4'></tbody>

        <legend id='RMxM4'><style id='RMxM4'><dir id='RMxM4'><q id='RMxM4'></q></dir></style></legend>

        1. <i id='RMxM4'><tr id='RMxM4'><dt id='RMxM4'><q id='RMxM4'><span id='RMxM4'><b id='RMxM4'><form id='RMxM4'><ins id='RMxM4'></ins><ul id='RMxM4'></ul><sub id='RMxM4'></sub></form><legend id='RMxM4'></legend><bdo id='RMxM4'><pre id='RMxM4'><center id='RMxM4'></center></pre></bdo></b><th id='RMxM4'></th></span></q></dt></tr></i><div id='RMxM4'><tfoot id='RMxM4'></tfoot><dl id='RMxM4'><fieldset id='RMxM4'></fieldset></dl></div>
                <bdo id='RMxM4'></bdo><ul id='RMxM4'></ul>

                  <small id='RMxM4'></small><noframes id='RMxM4'>

                  <tfoot id='RMxM4'></tfoot>
                1. 本文介绍了Mysql - 将 auto_increment 添加到主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的 mysql 有一个奇怪的问题.

                  I have a strange problem with mysql.

                  我正在尝试更改表的列,该列是主键并在其上定义了 auto_increment 约束.这也是多个其他表的外键引用.我需要在父项和所有子项中更改此列的长度.

                  I am trying to alter a table's column which is a primary key and has an auto_increment constraint defined on it. This is also a foreign key reference for multiple other tables. I need to change the length of this column in both , parent and all children.

                  set foreign_key_checks=0;
                  alter table Parent  modify Identifier smallint(10) unsigned;
                  alter table Child_1 modify FK_Identifier smallint(10) unsigned;
                  alter table Child_2 modify FK_Identifier smallint(10) unsigned;
                  alter table Child_3 modify FK_Identifier  smallint(10) unsigned;
                  alter table Child_4 modify FK_Identifier smallint(10) unsigned;
                  alter table Child_5 modify FK_Identifier smallint(10) unsigned;
                  set foreign_key_checks=1;
                  

                  这会删除父表上的自动增量.添加约束的最佳方法是什么?

                  This removes the auto increment on the parent table. What would be the best way to add the constraint back ?

                  以下似乎失败了.

                  mysql> ALTER TABLE Parent MODIFY Identifier smallint(10) PRIMARY KEY AUTO_INCREMENT;
                  ERROR 1068 (42000): Multiple primary key defined
                  
                  
                  ALTER TABLE Parent MODIFY Identifier smallint(10) AUTO_INCREMENT;
                  ------------------------
                  LATEST FOREIGN KEY ERROR
                  ------------------------
                  110125 15:49:08 Error in foreign key constraint of table db/Child_1:
                  there is no index in referenced table which would contain
                  the columns as the first columns, or the data types in the
                  referenced table do not match to the ones in table. Constraint:
                  ,
                    CONSTRAINT Child_1_ibfk_1 FOREIGN KEY (FK_Identifier) REFERENCES RoomProfile (Identifier) ON DELETE CASCADE ON UPDATE CASCADE
                  The index in the foreign key in table is PRIMARY
                  

                  有没有更好的方法来实现这一目标?

                  Is there a better way to achieve this ?

                  显示创建是(更改后):

                  Edit : Show create is (after alter) :

                    CREATE TABLE `Parent` (
                    `Identifier` smallint(10) unsigned NOT NULL default '0',
                    `Name` varchar(20) default NULL,
                    `Description` varchar(100) default NULL,
                     PRIMARY KEY  (`Identifier`),
                     UNIQUE KEY `Name` (`Name`),
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | 
                  

                  修改前

                  `Identifier` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
                  

                  谢谢!

                  推荐答案

                  你不需要在MODIFY语句中指定PRIMARY KEY:

                  You don't need to specify PRIMARY KEY in the MODIFY statement:

                  ALTER TABLE Parent MODIFY Identifier smallint(10) AUTO_INCREMENT;
                  

                  这篇关于Mysql - 将 auto_increment 添加到主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用另一个表中的 MAX 值重置 MySQL AutoIncrement? 下一篇:如何在不指定列名的情况下使用 AUTO_INCREMENT 列将新行插入到数据库中?

                  相关文章

                  1. <tfoot id='8TrlZ'></tfoot>
                  2. <small id='8TrlZ'></small><noframes id='8TrlZ'>

                      <bdo id='8TrlZ'></bdo><ul id='8TrlZ'></ul>
                  3. <legend id='8TrlZ'><style id='8TrlZ'><dir id='8TrlZ'><q id='8TrlZ'></q></dir></style></legend>
                  4. <i id='8TrlZ'><tr id='8TrlZ'><dt id='8TrlZ'><q id='8TrlZ'><span id='8TrlZ'><b id='8TrlZ'><form id='8TrlZ'><ins id='8TrlZ'></ins><ul id='8TrlZ'></ul><sub id='8TrlZ'></sub></form><legend id='8TrlZ'></legend><bdo id='8TrlZ'><pre id='8TrlZ'><center id='8TrlZ'></center></pre></bdo></b><th id='8TrlZ'></th></span></q></dt></tr></i><div id='8TrlZ'><tfoot id='8TrlZ'></tfoot><dl id='8TrlZ'><fieldset id='8TrlZ'></fieldset></dl></div>