You must Sign In to post a response.
  • Category: SQL Server

    How to create primay key after create table.

    how to create primay key after create table.please comment here
  • #751078
    Hi,

    There are several ways to set Primary Key.

    Procedure 1:
    1. Open table in Design Mode.
    2. Click row selector of database column you want to define as Primary Key. If you want to select multiple columns as Primary Key, hold down the CTRL key while you click the row selectors for the other columns.
    3. Right click the row selector of the column & select 'Set Primary Key'.

    Procedure 2:
    Syntax: ALTER TABLE <table_name> ADD PRIMARY KEY (column_name)
    Eg1: ALTER TABLE EmployeeInformation ADD PRIMARY KEY (EmpID) //For Single Primary Key
    Eg2: ALTER TABLE EmployeeInformation ADD PRIMARY KEY (EmpID,DeptName) //For Multiple Primary Key

    Hope this will help you.

    Thanks,
    Ram Prasad

  • #751080
    Hi,

    You can do this in 2 steps.

    Step 1:
    You can right client on the table name and select the "Design" Option.

    Step 2:
    Once you hit the design option you have to open the table design view. In that you should right client the column name where you want to set the primary key. If you decided the column you can hit the "Set Primary Key" option.

    Further you can view the attached image.

    -----------------------------------------------------------------------------
    Regards,
    Gopi A.
    +91 9894315571
    Skype:gopi.net
    http://asaigopi-dotnet.blogspot.in/

    Primary-Key.docx

    Delete Attachment

  • #751100
    Hi,

    If you want to add primary key on existing table then use Alter statement to alter the table structure.

    EX:


    ALTER TABLE TABLENAME ADD PRIMARY KEY(COL1,COL2...);


    Try something like above to achieve your goal.

    Hope this will help you to resolve the issue..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #751105
    Hi Anil,

    Just alter the table and add the column name which you want to include like below-

    ALTER TABLE tblname ADD PRIMARY KEY (yourcolumnname)


    That's all.

    Thanks,
    Ashutosh Jha
    http://tricksroad.com

  • #751106
    thanx all of you.I've got solution
    Syntax:
    1st Step:
    ALTER TABLE <table_name> ALTER COLUMN(column_name)datatype(provide size)not null

    2nd Step:
    ALTER TABLE table_name ADD PRIMARY KEY (column name)


  • Sign In to post your comments