FAQ: MS SQL Server - How to generate a number automatically for the Primary Key when inserting a record?

When inserting a record into a table, I want SQL Server to generate a number automatically for the Primary Key.  (e.g. OrderID is 1, 2, 3 and so on) How to do it?
(This behavior is similar to the "Auto-number" in Microsoft Access)

 Essential steps:

  1. Go to MS SQL Server > SQL Server Management Studio Express
  2. Right Click the table (e.g. Orders) > Design table.
  3. The primary key column (e.g. OrderID) must be defined using the decimal, int, numeric, smallint, bigint, or tinyint data type.
  4. Select the primary key and set the "Identify specification > IsIdentity" to yes.
Refer to the following snapshot. Click the snapshot to enlarge it.

Keywords:
Database Server
Microsoft SQL Server FAQ, MS SQL Server FAQ
Information Technology, SQL

No comments: