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

      <small id='4nuQ5'></small><noframes id='4nuQ5'>

    1. <tfoot id='4nuQ5'></tfoot>

        MySQL InnoDB:自增非主键

        MySQL InnoDB: autoincrement non-primary key(MySQL InnoDB:自增非主键)
          <tfoot id='Iuyyc'></tfoot>

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

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

          • <legend id='Iuyyc'><style id='Iuyyc'><dir id='Iuyyc'><q id='Iuyyc'></q></dir></style></legend>

                  <tbody id='Iuyyc'></tbody>
                • 本文介绍了MySQL InnoDB:自增非主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以自动递增非主键?

                  表book_comments"

                  Table "book_comments"

                  book_id     medium_int
                  timestamp   medium_int
                  user_id     medium_int
                  vote_up     small_int
                  vote_down   small_int
                  comment     text
                  comment_id  medium_int
                  
                  Primary key -> (book_id, timestamp, user_id)
                  

                  此表上将没有其他索引.但是,我想让 comment_id 列自动递增,以便我可以轻松创建另一个表:

                  There will be no other indexes on this table. However, I would like to make the comment_id column autoincrement so that I can easily create another table:

                  表book_comments_votes"

                  Table "book_comments_votes"

                  comment_id  (medium_int)
                  user_id     (medium_int)
                  
                  Primary key -> (comment_id, user_id)
                  

                  用户只能为每条图书评论投票一次.此表通过主键强制执行此规则.

                  Users would be able to vote only once per book comment. This table enforces this rule by the primary key.

                  是否可以自动递增 非主 键 - 例如,自动递增表book_comments"中的 comment_id 列?

                  Is it possible to auto-increment a non-primary key - as in, auto-increment the comment_id column in table "book_comments"?

                  为了简单起见,我想这样做,如上所述.替代方案并不乐观.

                  I would like to do this for simplicity as explained above. The alternatives are not promising.

                  • 通过 book_id、timestamp、user_id 上的唯一索引制作 commnet_id PK 并强制执行完整性.在这种情况下,我会创建一个额外的索引.
                  • 保留 PK 并将 book_comments_votes 中的 comment_id 替换为整个 PK.这将使桌子的大小增加三倍以上.
                  • Make the commnet_id PK and enforce integrity through a unique index on book_id, timestamp, user_id. In this case, I would create an additional index.
                  • Keep the PK and replace the comment_id in the book_comments_votes with the entire PK. This would more than triple the size of the table.

                  建议?想法?

                  推荐答案

                  是的,你可以.您只需将该列设为索引即可.

                  Yes you can. You just need to make that column be an index.

                  CREATE TABLE `test` (
                    `testID` int(11) NOT NULL,
                    `string` varchar(45) DEFAULT NULL,
                    `testInc` int(11) NOT NULL AUTO_INCREMENT,
                    PRIMARY KEY (`testID`),
                    KEY `testInc` (`testInc`)
                  ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
                  
                  
                  insert into test(
                    testID,
                   string
                  )
                  values (
                  1,
                      'Hello'
                  );
                  
                  
                  insert into test( 
                  testID,
                   string
                  )
                  values (
                  2,
                      'world'
                  );
                  

                  将为testInc"插入具有自动递增值的行.然而,这是一件非常愚蠢的事情.

                  Will insert rows with auto-incrementing values for 'testInc'. However this is a really dumb thing to do.

                  你已经说过正确的方法了:

                  You already said the right way to do it:

                  通过 book_id、timestamp、user_id 上的唯一索引使 comment_id PK 并强制执行完整性."

                  "Make the comment_id PK and enforce integrity through a unique index on book_id, timestamp, user_id."

                  这正是你应该做的.它不仅为您提供将来查询所需的表的正确主键,还满足 最小惊讶原则.

                  That's exactly the way that you should be doing it. Not only does it provide you with a proper primary key key for the table which you will need for future queries, it also satisfies the principle of least astonishment.

                  这篇关于MySQL InnoDB:自增非主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bogus foreign key constraint fail(虚假外键约束失败)
                  how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                  Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                  How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                  Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                  ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)

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

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

                            <tbody id='CKo57'></tbody>
                          <tfoot id='CKo57'></tfoot>

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