The two are similar, but not quite the same. Neither will create constraints, on their own in the constraints folder. When you use a primary key on a table in SQL Server, you can either add the constraint clause or not. If you do not, SQL Server will automatically create the constraint; consider it an internal short-cut. SQL Server will manage this short-cut for you by using a clustered index on the primary key; it will automatically put a unique constraint on the primary key, and not allow the primary key to be null.
CREATE TABLE Employee
(
EmployeeID INT PRIMARY KEY,
SSN varchar(9) NOT NULL
);
If you add the constraint clause, you have more power to control the primary key; such as having a multi-column primary key or even having a different type of index on the primary key. Now if you add a constraint with an index, clustered or non-clustered, then the constraint goes into the keys folder. If you create a constraint that does not use an index, then the constraint goes into the constraints folder.
CREATE TABLE Employee
(
EmployeeID INT NOT NULL,
SSN varchar(9) NOT NULL,
CONSTRAINT pk_EmployeeID_SSN
PRIMARY KEY CLUSTERED (EmployeeID, SSN)
);
If you look in SQL Server, you now have a display for the pk_EmployeeID_SSN primary key, under the keys folder.
So now you wonder why nothing is showing in the constraints folder? The reason is SQL Server couples indexed constraints into the keys folder. To provide an example of a constraint that has no key. Please see below.
CREATE TABLE Employee
(
EmployeeID INT NOT NULL,
SSN varchar(9) NOT NULL,
AddDate datetime NOT NULL
);
ALTER TABLE [dbo].[Employee] ADD CONSTRAINT [DF_Employee_AddDate] DEFAULT (getdate()) FOR [AddDate];
insert into employee(employeeid, ssn) values(1, '111111111');
insert into employee(employeeid, ssn) values(2, '222222222');
If you run this, you'll see the DF_Employee_AddDate has been added to the constraints folder. This constraint will automatically add current datetime to any new record that goes into the table.
I have put together a few more examples below, if you are interested on how they work.
CREATE TABLE Employee1
(
EmployeeID1 INT PRIMARY KEY,
SSN varchar(9) NOT NULL
);
CREATE TABLE Employee2
(
EmployeeID2 INT NOT NULL,
SSN varchar(9) NOT NULL,
CONSTRAINT pk_EmployeeID2_SSN
PRIMARY KEY CLUSTERED (EmployeeID2, SSN)
);
CREATE TABLE Employee3
(
EmployeeID3 INT PRIMARY KEY,
SSN varchar(9) NOT NULL,
CONSTRAINT unq_SSN
UNIQUE NONCLUSTERED (SSN)
);