Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 CONSTRAINT in Primary Key

Author  Topic 

coolbudy
Starting Member

8 Posts

Posted - 2015-05-02 : 07:07:27
Hello friends,


I Have a confusion is that It is compulsory
to add CONSTRAINT at time of creation column with primary key
Means suppose i create a new table which take one filed as primary key
so at that time it is compulsory to add CONSTRAINT at that filed?

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-05-02 : 09:26:43
PRIMARY KEY *is* a constraint. if you define a table with a PK, you don't have to also define a constraint for it, e.g.


CREATE TABLE test(ID int Primary Key)


if you are using SSMS, try to run this code on a test database, then in SSMS, right-click on the Tables folder in Object Explorer and choose Refresh. Then, find table "test" created above and look in the constraints. YOu should see one created for the primary key

Gerald Britton
Toronto PASS Chapter
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2015-05-04 : 11:49:17
No. You can add the Primary Key Constraint later. For example:

CREATE TABLE test(Id int NOT NULL)

ALTER TABLE test ADD
CONSTRAINT test__PK
PRIMARY KEY CLUSTERED ( Id ) WITH ( FILLFACTOR = 99 ) ON [PRIMARY]

Go to Top of Page

coolbudy
Starting Member

8 Posts

Posted - 2015-05-05 : 01:30:30
Thanks both of you I clear my doubt
Go to Top of Page
   

- Advertisement -