<bdo id='kK7Gw'></bdo><ul id='kK7Gw'></ul>
    <tfoot id='kK7Gw'></tfoot>
  • <legend id='kK7Gw'><style id='kK7Gw'><dir id='kK7Gw'><q id='kK7Gw'></q></dir></style></legend>

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

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

        SQL Server 向现有表添加自动增量主键

        SQL Server add auto increment primary key to existing table(SQL Server 向现有表添加自动增量主键)

        • <tfoot id='xyUSC'></tfoot>
            <tbody id='xyUSC'></tbody>
            <bdo id='xyUSC'></bdo><ul id='xyUSC'></ul>

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

              1. <i id='xyUSC'><tr id='xyUSC'><dt id='xyUSC'><q id='xyUSC'><span id='xyUSC'><b id='xyUSC'><form id='xyUSC'><ins id='xyUSC'></ins><ul id='xyUSC'></ul><sub id='xyUSC'></sub></form><legend id='xyUSC'></legend><bdo id='xyUSC'><pre id='xyUSC'><center id='xyUSC'></center></pre></bdo></b><th id='xyUSC'></th></span></q></dt></tr></i><div id='xyUSC'><tfoot id='xyUSC'></tfoot><dl id='xyUSC'><fieldset id='xyUSC'></fieldset></dl></div>
                <legend id='xyUSC'><style id='xyUSC'><dir id='xyUSC'><q id='xyUSC'></q></dir></style></legend>
                • 本文介绍了SQL Server 向现有表添加自动增量主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  作为标题,我有一个已经填充了 150000 条记录的现有表.我添加了一个 Id 列(当前为空).

                  As the title, I have an existing table which is already populated with 150000 records. I have added an Id column (which is currently null).

                  我假设我可以运行查询来用增量数字填充此列,然后设置为主键并打开自动增量.这是正确的方法吗?如果是这样,我该如何填写初始数字?

                  I'm assuming I can run a query to fill this column with incremental numbers, and then set as primary key and turn on auto increment. Is this the correct way to proceed? And if so, how do I fill the initial numbers?

                  推荐答案

                  不 - 你必须反过来做:从一开始就把它添加为 INT IDENTITY - 它会是执行此操作时填充标识值:

                  No - you have to do it the other way around: add it right from the get go as INT IDENTITY - it will be filled with identity values when you do this:

                  ALTER TABLE dbo.YourTable
                     ADD ID INT IDENTITY
                  

                  然后您可以将其设为主键:

                  and then you can make it the primary key:

                  ALTER TABLE dbo.YourTable
                     ADD CONSTRAINT PK_YourTable
                     PRIMARY KEY(ID)
                  

                  或者如果您更喜欢一步完成所有操作:

                  or if you prefer to do all in one step:

                  ALTER TABLE dbo.YourTable
                     ADD ID INT IDENTITY
                         CONSTRAINT PK_YourTable PRIMARY KEY CLUSTERED
                  

                  这篇关于SQL Server 向现有表添加自动增量主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  MySQL: When is Flush Privileges in MySQL really needed?(MySQL:何时真正需要 MySQL 中的 Flush Privileges?)
                  Composite Primary Key performance drawback in MySQL(MySQL 中的复合主键性能缺陷)
                  Altering a column: null to not null(改变列:null 到 not null)
                  Adding an identity to an existing column(向现有列添加标识)
                  How to write UPDATE SQL with Table alias in SQL Server 2008?(如何在 SQL Server 2008 中使用表别名编写 UPDATE SQL?)
                  Why can#39;t I use alias in a count(*) quot;columnquot; and reference it in a having clause?(为什么我不能在计数(*)“列中使用别名?并在具有条款中引用它?)

                          <tbody id='8iIuo'></tbody>

                        <small id='8iIuo'></small><noframes id='8iIuo'>

                        <tfoot id='8iIuo'></tfoot>
                        <legend id='8iIuo'><style id='8iIuo'><dir id='8iIuo'><q id='8iIuo'></q></dir></style></legend>

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