SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    .net install meltdown
    Join Date
    May 2001
    Location
    View Ridge
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Auto Increment primary key in MS SQL server

    How do you auto increment a primary key in SQL server?

    I've got a form which passes some fields into an SQL server database. I would like the primary key to be created automatically when a new record is inserted.

    I've done this in MySQL using auto_increment but can't figure out how to do it in SQL Server(2000).

    Any ideas?

  2. #2
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Set the table field to be an identity column.

    You can do this in Enterprise Manager in the Design Table mode or through SQL.

    MS example:
    Code:
    CREATE TABLE jobs
    (
       job_id  smallint IDENTITY(1,1)PRIMARY KEY CLUSTERED,
       job_desc varchar(50) NOT NULL DEFAULT
       min_lvl tinyint NOT NULL CHECK (min_lvl >= 10),
       max_lvl tinyint NOT NULL CHECK (max_lvl <= 250)
    )
    The 1's following the IDENTITY keyword indicate the SEED number (value for first record in table) and increment property (0 or 1).

  3. #3
    .net install meltdown
    Join Date
    May 2001
    Location
    View Ridge
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AHA! That worked!

    Thanks a lot shane, its easy once you know how but I would have floundered around for hours trying to figure that one out.

    You the man.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •