Adding a primary key using ALTER Command:
Alter command is generally used to modify table . It can be used to add columns or to remove columns. Using Alter command we can also add or remove a key from a table.
Syntax:
ALTER TABLE tablename ADD CONSTRAINT constraintname PRIMARY KEY (columnname);
Example:
ALTER TABLE Employee
ADD CONSTRAINT EmployeePK PRIMARY KEY (EmpID);
Above query will make EmpID a primary key for Employee table.
ALTER TABLE Employee
ADD CONSTRAINT EmployeePK PRIMARY KEY (EmpID, Name);
Above query will make EmpID and Name a primary key for Employee table. As combining EmpID and Name it work as a primary key for Employee table.
0 Comment(s)