19
Reply

What is difference between UNIQUE and PRIMARY KEY constraints

Puran Mehra

Puran Mehra

18y
166.1k
0
Reply

    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.

    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.

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

    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.

    Unique: unique it allows null value only once.

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

    Below is a nice video which demonstrates the difference between unique and primary key practically.

     

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

    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.

    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.

    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)

    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.


    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. 

    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

    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.

    • 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.

    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.

    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

    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.]

    18y
    0

    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.