Click here to Skip to main content
15,886,807 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys ,
i am trying to know some basic thing that we have actually used a lot of time . i want to know is there any difference between Key and constraints in SQL . As we are frequently using terms like unique key , unique constraints or primary key , primary constraint . i have tried creating this and what i understand primary key or primary constraints are same and also unique key and unique constraints are also same . I googled also . but i am little doubt , is they really same or there is any difference between them .
if a am creating a primary key then i will use this -
alter table employees
Add  primary Key(employeeId)


and i can do also like this -
alter table employees
Add constraint Pk_Employee_IDD primary Key(employeeId)


in these two the only difference that comes is that now it will giving a meaning full name to this. but when i am creating a constraints as in second snippet then nothing is created in constraint folder . same happens in case of unique key or constraints ...can any one please give a help on this . i know it's basic but but i just want to clear this .

What I have tried:

i have tried this thing by using query -

alter table employees
Add  primary Key(employeeId)


and also this -

alter table employees
Add constraint Pk_Employee_IDD primary Key(employeeId)
Posted
Updated 9-Mar-17 6:19am

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)
);
 
Share this answer
 
Comments
Gaurav Dixit 9-Mar-17 12:19pm    
Hey bro , thanx for your response . this mean primary key mean combination of
[
1.) key(that generated in folder physically)+
2.) index(clustered or nonclustered that created)+
3.) Unique constraints(which is applied on index )] .

and primary constraint mean only that constraint which is point No. 3
Am i getting right .
you describe really in a good manner man . Thank You.
jgakenhe 9-Mar-17 12:49pm    
Sort of. The Primary Key is separate from the constraints, except for its own constraints. You can have a Primary Key that has multiple columns. Each primary key gets a constraint and index, though you can have other constraints and indexes on your table, for instance the example with the unique constraint for table Employee3.

Here is a nice article that might be able to explain it more thorough and differently than I: http://searchsqlserver.techtarget.com/tip/SQL-CONSTRAINT-clauses-PRIMARY-KEY-and-UNIQUE
Hi Gaurav

A primary key is a unique field on a table and can only be assigned one per table usually this is something like EmployeeID, CustomerID or whatever field you are trying to make your primary key. That means that other tables can use this field to create foreign key relationships to themselves.

for example look at the below tables:

-------------------------------
/* Create Supplier Table*/
-------------------------------
Create table Supplier(
SupplierID int not null primary key,
SupplierName varchar(50) null,
ContactName varchar(50) null,
Address varchar(50) null,
City varchar(50) null,
PostalCode varchar(50) null,
Country varchar(20) null,
Phone varchar(10) null,
)
---------------------------------
/* Create Products Table*/
---------------------------------
create table Products(
ProductID int not null primary key,
ProductName varchar(50) null,
SupplierID int not null,
CategoryID int not null,
Units int null,
Price int null,
foreign key (SupplierID) References Supplier (SupplierID)

you can see both tables have a primary key that is unique but the Products table has a foreign key that references supplier table. This creates a relationship between the two tables.

A unique constraint simply means that a particular field must be unique.

hope that helps
 
Share this answer
 
Comments
Gaurav Dixit 9-Mar-17 9:29am    
HI Surinder, thanx for your reply . But my question is not difference b/w primary key and unique key , actually -i want to know is there any difference between Key and constraints in SQL , for example is there any difference b/w primary and primary constraint . Or they are just the same things ?
Hey bro , thanx for your response . this mean primary key mean combination of
[
1.) key(that generated in folder physically)+
2.) index(clustered or nonclustered that created)+
3.) Unique constraints(which is applied on index )] .

and primary constraint mean only that constraint which is point No. 3
Am i getting right .
you describe really in a good manner man . Thank You.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900