title: Difference Between Unique And Composite Primary key In sql server
body: <p>I want to know what is the difference between unique key and composite primary key in SQL Server.</p>
  
  <p>According to w3c school:</p>
  
  <blockquote>
    <p>The UNIQUE constraint uniquely identifies each record in a database table.</p>
    
    <p>The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.</p>
    
    <p>A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.</p>
    
    <p>Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.</p>
  </blockquote>
  
  <p>We can create composite primary key by using this:</p>
  
  <pre><code>CREATE TABLE Persons
  (
  P_Id int NOT NULL,
  LastName varchar(255) NOT NULL,
  FirstName varchar(255),
  Address varchar(255),
  City varchar(255),
  UNIQUE (P_Id)
  )
  </code></pre>
  
  <p>For composite primary key syntax:</p>
  
  <pre><code>CREATE TABLE Persons
  (
  P_Id int,
  C_Id int,
  LastName varchar(255) NOT NULL,
  FirstName varchar(255),
  Address varchar(255),
  City varchar(255),
  Primary Key (P_Id,C_Id)
  );
  </code></pre>