Adding Primary Key to Table in SQL Server
Hello to everyone,
In this article, I will provide information on how to add a primary key to the table in SQL Server.
In SQL Server, primary key fields are mostly used in tables. You can also do this using the Designer side. I will do this using code.
You can do this easily using the code below.
--Create a sample table CREATE TABLE ExampleTableSample( ID int not null , Name nvarchar(20) ) --Adding a Primary Key to the Table ALTER TABLE ExampleTableSample ADD CONSTRAINT Example_PK PRIMARY KEY (ID);
The point to note here is that the field with the primary key is not NULL. Otherwise, you will get an error.
When you run the code, you will get a result like the one below.
As you can see, a primary key field has been added to the table.
Good luck to everyone in business and life.