SQL Server - I need to create Composite Key and one primary Key

Asked By bhanupratap singh on 28-Oct-13 09:36 AM
hello friends.

I need to create a table with Composite Key and one primary key . 

like below

ID Unuqueidentifier  Primery key

OfficeName varchar(100)          ----Compositekey
ConsignmentNo  varchar (10)  ----Compositekey


Composite key should be OfficeName+ConsignmentNo.

how to do it. 
I do wanna use composite key as primary key but ID should also be a primary key. I means I need uniqueness of both key
Mr Bobe reply pls.

thanks
Robbe Morris replied to bhanupratap singh on 28-Oct-13 09:43 AM
You use the Create Index functionality in Management Studio and add both columns.  There is a checkbox to define the index of both columns together as being unique.  And yes, you can do this along with a standard primary key as you've done here.

If you do not "need" a uniqueidentifier as your primary key (ie a bigint might be a better idea), I would suggest avoiding the use of them.  You'll find that your index can become more fragmented more often and it makes the index itself bigger.  I have apps that "need" it to be a unique identifier.  Just saying that if it isn't truly necessary, use a different data type.
bhanupratap singh replied to Robbe Morris on 29-Oct-13 07:05 AM
Thanks Robbe

I got some reference from internet that unique key allow null atleast one . 
My question is,  If  a unique key contain NOT NULL then. Despite of not null will it get null atleast one in the table ?.


Thanks
Robbe Morris replied to bhanupratap singh on 29-Oct-13 08:16 AM
That's only if you define the column as being nullable.
bhanupratap singh replied to Robbe Morris on 29-Oct-13 08:53 AM
Many many thanks Mr. Robe