MICROSOFT SQL SERVER Reseeding identities in SQL Server

來源:互聯網
上載者:User
Baya Pavliashvili
07.31.2003
Rating: --- (out of 5)


Reseeding identities in SQL Server IDENTITY columns are commonly used as primary keys for SQL Server tables. If you're not familiar with IDENTITY columns, they are automatically incremented columns with numeric data types, such as INT, BIGINT, FLOAT, etc. When you add an IDENTITY column to a table you must specify two values:

  1. SEED: the starting value for this column
  2. INCREMENT: how much to add to the current identity value to derive the next value.

Both SEED and INCREMENT default to 1, however, you can customize these values to fit your needs. Keep in mind that many data types allow negative values, so you could safely set your identity seed to –1000.

In some cases you'll want to change the identity seed. For instance, suppose you created a database with a few tables and tested your application against it. Now you wish to remove some test records, but you want values in the IDENTITY column to remain sequential, without any gaps. For example, let's suppose that I have a table for storing various types of titles, created as follows:

CREATE TABLE title_type (            title_type_id INT IDENTITY(1, 1) NOT NULL,            type VARCHAR(50) NOT NULL)            

During initial testing this table had following values:

title_type_id  type            -------------  ----------------            1              business            2              mod_cook            3              popular_comp            4              psychology            5              trad_cook            6              UNDECIDED            

Now that I'm ready to deploy the application in production I no longer need the "UNDECIDED" type. If I delete this title type the next type will be inserted with the identity value of 7.

Of course with a tiny table like this I could easily copy all data to a temporary table and load it back up after truncating the test_type table. By the way, TRUNCATE TABLE command will reset the seed of the identity column to its initial value. However, if you have thousands and millions of records going through, such an exercise is painful.

A simple way to reseed an identity column is to open the table in the design view within Enterprise Manager and change the value of "SEED". However, if you have to reseed 150 tables it might not be such a great idea to click through all of them. Fortunately there is a better way. The DBCC CHECKIDENT command lets you check the current identity value for a particular column, and reseed it if needed.

The syntax for this DBCC command is fairly simple:

DBCC CHECKIDENT ( table_name, [reseed / noreseed], [new_seed_value])            

For instance, if I simply want to check the identity value on test_type table, I can execute the following:

DBCC CHECKIDENT ('dbo.test_type', NORESEED)            

The output will be similar to the following:

Checking identity information: current identity value '10', current column value '10'.            DBCC execution completed. If DBCC printed error messages, contact your system administrator.            

If I'd like to change the SEED value in the same table then I can execute the following:

DBCC CHECKIDENT ('dbo.test_type', RESEED, 100)            

Results:

Checking identity information: current identity value '10', current column value '100'.            DBCC execution completed. If DBCC printed error messages, contact your system administrator.            

One detail to be aware of, however, is that DBCC CHECKIDENT will let you change the identity seed to a value that already exists in the table. For example, if the employee table already has an employee_id of 2, DBCC CHECKIDENT will let you change the seed to 2, even though next time you try to add an employee record you will get the following error:

Server: Msg 2627, Level 14, State 1, Line 1            Violation of PRIMARY KEY constraint 'PK_Employees'. Cannot insert duplicate key in object 'Employees'.            The statement has been terminated.            

To avoid such pitfalls you should check the maximum value in the identity column prior to reseeding it.

About the Author

 

Baya Pavliashvili is a MCSE, MCSD and MCDBA with five years experience with SQL Server.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.