Forum Discussion

elitesmitpatel's avatar
elitesmitpatel
Copper Contributor
Aug 16, 2024

update a created column to a primary key column

I have created a table mentioned below, now i want to convert "PersonID" into Primary key . Please provide me the code?
create table Persons(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

Thanks

1 Reply

  • Arshad440's avatar
    Arshad440
    Copper Contributor

    Hi,
    You Should Open object Explorer in SSMS and Find your table and expand the table where you can see the columns of your table,
    Right Click the Column you want and click 'Set Primary Key'


    or

     

    You should use this Query


    ALTER TABLE Persons
    ALTER COLUMN PersonID INT NOT NULL;

     

    ALTER TABLE Persons
    ADD CONSTRAINT PK_Employees PRIMARY KEY (PersonID);



    regards
    Arshad

Resources