How to Add a Primary Key   MS-sql Server 2008

Posted on 2011-10-30
Last Modified: 2012-05-12
How to add Primary key to th existing column in the MS-sql Server 2008
Question by:nrajasekhar7
    LVL 50

    Accepted Solution

    does the table already have a primary key?

    what is the clustering index for the table?
    are there additional indexes on the table?


    alter table youtablename
    drop primary key


    alter table yourtablename
    add primary key (column1,column2,...)

    LVL 23

    Expert Comment

    - the following link provide a further explanation on creating primary key on existing table. the syntax is as follows:

    ALTER TABLE tablename ADD CONSTRAINT primarykeyname_PK PRIMARY KEY (column1);

    LVL 3

    Expert Comment

    alter table TableName add constraint New_primary_key primary key (NewCol)
    LVL 23

    Expert Comment

    by:Racim BOUDJAKDJI
    Post the script for the table you want to set a primary key onto.  

    Author Closing Comment

    Thanks you

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
    Via a live example, show how to shrink a transaction log file down to a reasonable size.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now