how to add a primary key to an existing table code example

Example 1: add primary key to existing table sql

alter table Persion add primary key (persionId,Pname,PMID)

Example 2: sql server add primary key to existing table with data

ALTER TABLE Production.TransactionHistoryArchive
   ADD CONSTRAINT PK_TransactionHistoryArchive_TransactionID PRIMARY KEY CLUSTERED (TransactionID);

Example 3: MySQL Primary Key

Typically, you define the primary key for a table in the CREATE TABLE statement.

If the primary key has one column, you can use the PRIMARY KEY constraint as a column constraint:

CREATE TABLE table_name(
    primary_key_column datatype PRIMARY KEY,
    ...
);

Tags:

Sql Example