1. <tfoot id='Qpxrj'></tfoot>

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

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

      向现有列添加标识

      Adding an identity to an existing column(向现有列添加标识)
      <tfoot id='hzHW8'></tfoot>

      • <bdo id='hzHW8'></bdo><ul id='hzHW8'></ul>

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

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

                问题描述

                我需要将表的主键更改为标识列,并且表中已经有许多行.

                I need to change the primary key of a table to an identity column, and there's already a number of rows in table.

                我有一个脚本来清理 ID,以确保它们从 1 开始是连续的,在我的测试数据库上运行良好.

                I've got a script to clean up the IDs to ensure they're sequential starting at 1, works fine on my test database.

                更改列以具有标识属性的 SQL 命令是什么?

                What's the SQL command to alter the column to have an identity property?

                推荐答案

                您不能更改现有列的标识.

                You can't alter the existing columns for identity.

                你有两个选择,

                1. 创建一个具有身份的新表 &删除现有表

                1. Create a new table with identity & drop the existing table

                创建一个带有标识 & 的新列删除现有列

                Create a new column with identity & drop the existing column

                方法 1.(新表)在这里您可以保留新创建的标识列上的现有数据值.请注意,如果不满足if not exists",您将丢失所有数据,因此请确保您也将条件置于 drop 上!

                Approach 1. (New table) Here you can retain the existing data values on the newly created identity column. Note that you will lose all data if 'if not exists' is not satisfied, so make sure you put the condition on the drop as well!

                CREATE TABLE dbo.Tmp_Names
                    (
                      Id int NOT NULL
                             IDENTITY(1, 1),
                      Name varchar(50) NULL
                    )
                ON  [PRIMARY]
                go
                
                SET IDENTITY_INSERT dbo.Tmp_Names ON
                go
                
                IF EXISTS ( SELECT  *
                            FROM    dbo.Names ) 
                    INSERT  INTO dbo.Tmp_Names ( Id, Name )
                            SELECT  Id,
                                    Name
                            FROM    dbo.Names TABLOCKX
                go
                
                SET IDENTITY_INSERT dbo.Tmp_Names OFF
                go
                
                DROP TABLE dbo.Names
                go
                
                Exec sp_rename 'Tmp_Names', 'Names'
                

                方法二(New column)你不能在新创建的标识列上保留现有的数据值,标识列将保存数字的序列.

                Approach 2 (New column) You can’t retain the existing data values on the newly created identity column, The identity column will hold the sequence of number.

                Alter Table Names
                Add Id_new Int Identity(1, 1)
                Go
                
                Alter Table Names Drop Column ID
                Go
                
                Exec sp_rename 'Names.Id_new', 'ID', 'Column'
                

                有关详细信息,请参阅以下 Microsoft SQL Server 论坛帖子:

                See the following Microsoft SQL Server Forum post for more details:

                如何更改列以身份(1,1)

                这篇关于向现有列添加标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                SQL query to group by day(按天分组的 SQL 查询)
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

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

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

                          <tbody id='TMcZM'></tbody>