locked
Adding a unique primary key field in SQL Server? RRS feed

  • Question

  • I need to add a column with a unique primary key (using SQL Server 2008). I wanted to BULK load a great amount of data, but I found the repetition in field I was going to use.

    I am asking for some hints and directions on the best choice of setting this up so that the key field is populated during the BULK load.
    Friday, July 26, 2013 2:24 PM

Answers

All replies

  • Simplest: ID INT IDENTITY(1,1) PRIMARY KEY


    Kalman Toth Database & OLAP Architect sqlusa.com
    New Book / Kindle: Pass SQL Exam 70-461 & Job Interview: Programming SQL Server 2012


    • Proposed as answer by Heidi-Duan Tuesday, July 30, 2013 3:12 PM
    • Marked as answer by Allen Li - MSFT Friday, August 2, 2013 3:42 AM
    Friday, July 26, 2013 2:26 PM
  • I suggest you try this query:

     

    alter table database.schema.table_name

        add ID_column int identity(1,1)

        primary key (ID_column)

     

    In case you’ve set the primary key on the table, you’ll need to delete it before running this query

    Friday, July 26, 2013 2:37 PM
  • Hi,

    You may add identity column and remove the primary key from your field that you used.

    Or you may bulk the data on another temp table and then clear it and check you field again or use any other candidate key.

    I hope this is helpful.


    Please Mark it as Answered if it answered your question
    OR mark it as Helpful if it help you to solve your problem
    Elmozamil Elamir Hamid

    MCSE Data Platform
    MCITP: SQL Server 2008 Administration/Development
    MCSA SQL Server 2012
    MCTS: SQL Server Administration/Development

    MyBlog

    Friday, July 26, 2013 5:34 PM