If we forget to create the primary key while table creation,we also have the option to add the primary key for the existing table.
We can add the primary for the existing table using ALTER TABLE keyword.
Syntax :
ALTER TABLE [TABLE NAME] ADD CONSTRAINT [PRIMARY KEY NAME] PRIMARY KEY ([COLUMN NAME] ASC)
Example :
ALTER TABLE Student ADD CONSTRAINT PK_Student PRIMARY KEY ([StudentID] ASC)
Explanation:
PK_Student --> Refers to primary key name.
StudentID -- > Refers to the primary key column name.
Now the primary key is created successfully in the existing table using T-SQL query.
No comments:
Post a Comment