Home » Applications » ASP.NET » SQL Primary Key Constraint on alter table

SQL Primary Key Constraint on alter table

– Login to your server MS SQL Management Studio Express

– Expand the database; expand the tables folder, right click on the table “[your table name]”, click “Open Table.” Make sure the primary key column(s) have been declared to not contain NULL values (when the table was first created). With the open table click on “Show SQL Pane” (Management Studio Express toolbar). Type the following SQL statement in the SQL pane:

SQL Server / MS Access:
 ALTER TABLE MyTable ADD PRIMARY KEY (Id) 

 

– Click on exclamation sign to execute SQL statement, or right-click in the SQL pane and select “Execute SQL.”

If you did everything correct you will get the confirmation message that SQL command executed successful.
 

Solution for defining a primary key constraint on multiple columns, use the following SQL syntax:

SQL Server / MS Access:

ALTER TABLE MyTable
ADD CONSTRAINT pk_ProductID PRIMARY KEY (Id,ItemName)

 

In case you need to drop primary key here is syntax:

SQL Server / MS Access:

ALTER TABLE MyTable
DROP CONSTRAINT id