SQL Server auto-increment primary key


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: SQL Server auto-increment primary key

  1. #1
    Join Date
    Dec 2005
    Posts
    51

    SQL Server auto-increment primary key

    Can someone teach me how to auto increment the primary key? I am currenlty using ms sql server 2000. I had tried

    create table TABLE1
    (id int not null auto_increment primary key
    );

    but it did not work. Please help
    Last edited by Phil Weber; 03-18-2006 at 01:38 AM.

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    The property you want is IDENTITY: http://msdn.microsoft.com/library/en...ia-iz_3iex.asp
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Dec 2005
    Posts
    51
    Thank you.

  4. #4
    Join Date
    Nov 2010
    Posts
    1

    Auto Increment Field or Identity Field In MS SQL Server

    Creating a Auto Increment field in SQL Server allows a unique number to be generated when a new record is inserted into a table.

    Syntax for creating an Auto Increment field in SQL Server.

    CREATE TABLE [dbo].[Company](
    [CompanyId] [bigint] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](200) NOT NULL,
    [Address] [nvarchar](max) NOT NULL,
    [Phone] [nvarchar](max) NULL,
    [autoBackup] [bit] NOT NULL,
    [applycreditlimit] [bit] NULL,
    [EmailId] [nvarchar](max) NULL,
    [salesTaxPercentage] [numeric](18, 2) NULL)
    Here, CompanyId has been set as an Auto Increment Id.
    A column which has datatype int, big int, tiny int or small int can only be set as an Auto Increment field.
    Suppose, you would like to set the AutoIncrement field (CompanyId) at the remote databases to only use the next number free AFTER the 100.

    Here, CompanyId has been set as an Auto Increment Id.
    A column which has datatype int, big int, tiny int or small int can only be set as an Auto Increment field.
    Suppose, you would like to set the AutoIncrement field (CompanyId) at the remote databases to only use the next number free AFTER the 100.

    There’s an “Identity Seed” parameter that you can specify (either through SQL or through the table designer in Enterprise Manager) that will let you set the base value for an Identity/AutoNumber field to the value you want. Any new Identity values will be incremented from this base (using the “Identity Increment” value which you can specify in the same place to determine how much to grow the value by).
    Last edited by Hack; 03-04-2011 at 07:49 AM.

Similar Threads

  1. Connect SQL Server Box to Oracle Box
    By Mark in forum Database
    Replies: 5
    Last Post: 07-15-2002, 10:24 AM
  2. Replies: 2
    Last Post: 08-15-2001, 03:03 AM
  3. Can Sql Server do this?
    By DavidR in forum Database
    Replies: 5
    Last Post: 02-14-2001, 11:46 PM
  4. Replies: 0
    Last Post: 11-20-2000, 05:32 AM
  5. Remove exchange from a site
    By andriano in forum Enterprise
    Replies: 1
    Last Post: 10-02-2000, 03:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center