Puran Mehra
What is difference between UNIQUE and PRIMARY KEY constraints
By Puran Mehra in SQL Server on Aug 04 2006
  • Sanjib Barik
    Mar, 2015 30

    Primary key:- 1- Primary key uniquely identify a record in the table. 2- Primary Key can't accept null values. 3- We can have only one Primary key in a table. 4-It created clusted index by default. Unique key:- 1-Unique key also identify uniquely a record in the table. 2- it can accept multiple null value once. 3- We can have more than one unique key in a table. 4- It created nonclusted index by default.

    • 0
  • kavita singh
    Oct, 2012 10

    primary key generate clustered index by default whereas unique key generate non clustered index. table can have only one primary key and more than one unique key.

    • 0
  • vivek kumar pandey
    Jun, 2012 6

    unique key are use for unique integrity but primary key is use for idetity integrity

    • 0
  • deepak singh
    Oct, 2011 10

    Unique key- unique key accept only one null value accept.but unique key use many time in table.

    Primary key-Primary key does not accept null value and primary key use only one time in table.

    • 0
  • nagabhushan Hiremath
    Jul, 2011 6

    Unique: unique it allows null value only once.

    Primary key: It is unique but it does't allow null value.

    • 0
  • kanchan setia
    Feb, 2011 3

    in simple words...
    primary key is always unique and not null.
    but unique key is only unique it can be null.

    • 0
  • Automata
    Oct, 2010 12

    From Microsoft SQL server doc: http://msdn.microsoft.com/en-us/library/ms191166.aspx

    "UNIQUE constraints allow for the value NULL. However, as with any value participating in a UNIQUE constraint, only one null value is allowed per column."

    "Some of the fields can contain null values as long as the combination of values is unique."

    So, SQL Server allows only one null in unique contraint.
    Oracle allows multiple nulls in a unique contraint column.

    Since one null value is never considered equal to another null value oracle processes properly in this case.

    • 0
  • Automata
    Oct, 2010 12

    A column with Unique constraint can have hundreds of NULL's.

    Primary key-> Not Null + Unique

    A table/relation can have only one column with primary key, whereas all the columns can be set to Unique contraint.

    • 0
  • John Ranjith Bosco
    May, 2010 15

    Hi Friend,
    We can't insert more than 1 null value in the column as defined is UNIQUE.
    Please execute the following query one by one, we can find the result.

    create table temp1_john_test (fld1 Varchar(10)  primary key,
                    fld2 float unique)


    insert into temp1_john_test (fld1) values ('Fld1')
    insert into temp1_john_test (fld1,fld2) values ('Fld2', null)

    • 0
  • minhaj shunjaruff
    Apr, 2010 20

    You all are replying wrong.

    unique key always allow more than one null value if you want. In oracle one null value never equals to another null values.

    that is the difference between primary key and unique key.you cannot enter same as well as null values in primary key where as in unique key you cannot enter the same values but can enter more than one null value.

    just make a table with unique key and check for yourself.

    • 0
  • asma farrukh
    Aug, 2009 22

    A table must contain only one primary key.if a table contains more than one primary key,then these are called composite keys.a primary key must be unique.but a unique key can't be a primary key.a table contains more than one unidue keys but a table must be identified by it's primary key. 

    • 0
  • P Narasimha
    Jan, 2009 5

    Primary key:
    1)Primary key is nothing but it is uniqly identified each roe in Table.
    2)Primary key Does not Allowes Duplicate values and Null values.
    3)Primary key is default Clustered indexes
    4)One table can have only one Primary key.
    Unique Key:
    1)Unique Key is nothing but it is uniqly identified each roe in Table.
    2)Unique Key Does not Allowes Duplicate values but allowes only one Null value.
    3)Primary key is default Non- Clustered indexes

    Regards,

    Narasima

    • 0
  • Sanjeev Singh
    Aug, 2008 22

    The basic differences between Primary Key and Unique key are as follows. 1) By default Primary Key will generate Clustured Index whereas Unique Key will Generate Non-Clustured Index. 2) Primary Key is a combination of Unique and NOT NULL Constraints so it can’t have duplicate values or any NUll Whereas for Oracle UNIQUE Key can have any number of NULL whereas for SQL Server It can have only one NULL 3) A table can have only one PK but It can have any number of UNIQUE Key.

    • 0
  • manohar reddy
    Jun, 2008 10

    • PRIMARY KEY: A tabLe can aontain only one PRIMARY KEY. It Doesn't allow null values, it is used  as Foreign Key In Another Table(like Reference Key).

    • UNIQUE: A Table may Contain  one or more UNIQUE  constraints. Its aloow only one null value . Use this key We maintain unique vales in the table.

    • 0
  • Mohd Nasar
    Mar, 2008 12

    Both PRIMARY KEY constraint and UNIQUE constraint uses to enforce Entity integrity (defines a row as a unique entity for a particular table), but primary keys do not allow null values.

    • 0
  • madhukumar koppula
    Aug, 2006 17

    Both Primary and Unique Keys are enforce uniqueness of the column. But the differeces are given below:

    Primary Key:

    1) It creates clustered index by default

    2) It doesn't allow nulls

    Unique Key:

    1) It creates non-clustered index by default

    2) It allows only one null value

    Regards

    MadhuKumarKoppula

    • 0
  • Aug, 2006 4

    PRIMARY KEY Constraint

    Specifies the column that uniquely identifies a row in the table. The identified columns must be defined as NOT NULL.

    Note: If you attempt to add a primary key using ALTER TABLE and any of the columns included in the primary key contain null values, an error will be generated and the primary key will not be added. See ALTER TABLE statement for more information.

    UNIQUE KEY Constraint

    Specifies that values in the column must be unique. NULL values are not allowed.

    Thanks!
    Manoj [InfoAxon Technologies Ltd.]

    • 0
  • Puran Mehra
    Aug, 2006 4

    A table can have only one PRIMARY KEY whereas there can be any number of UNIQUE keys.

    The columns that compose PK are automatically define NOT NULL, whereas a column that compose a UNIQUE is not automatically defined to be mandatory must also specify the column is NOT NULL.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS