SQL Server, T-SQL, ASP.NET, Javascript, SAP, ABAP Programming

Kodyaz Development Resources

Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Vista, etc.
Welcome to Kodyaz Development Resources Sign in | Join | Help





How To Set a Column as AutoNumber Identity Column in SQL Server 2005

You may want to set a column as an autonumber column while you are creating the table in SQL Server 2005.

Especially if you are using the GUI for administring the SQL Server 2005 (Yukon), you may ask how to change the properties of a column in SQL Server Management Studio.





Let's assume you have created a table named AutoNumber by running the below script.

CREATE TABLE AutoNumber (
   id int,
   Name nvarchar(25)
)

Open the Microsoft SQL Server Management Studio, connect to Database Engine where you have created your table. Drill down till you find the table named AutoNumber. Click the Columns and you will see the list of table columns. If you want the id column to be the identity column of this table, just open the context menu by right clicking on the column named id. And select Modify from the context menu.

identity
When you click the Modify menu item, Column Properties dialog screen will open as a tab in the Management Studio shown as below.

identity

On the Column Properties box, set Identity Specification property to "Yes" and keep the Identity Increment and Identity Seed values as default ones.

After you saved your modifications on the AutoNumber table, run the below two sql insert statements.

INSERT INTO AutoNumber([Name]) VALUES (N'Kodyaz')
INSERT INTO AutoNumber([Name]) VALUES (N'MSDN')

And after you have created two records in your AutoNumber table, by running the below sql select statement you will see autonumber id field values.

SELECT * FROM AutoNumber

identity







Copyright © 2004 - 2010 Eralper Yilmaz. All rights reserved.
Powered by Community Server, by Telligent Systems