1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Primary key VS Unique key in SQL Server

Discussion in 'SQL Server' started by venkatesanj@hcl.in, Mar 12, 2008.

?

Which key will actively participate in referential integrity

Poll closed Mar 17, 2008.
  1. Unique key

    50.0%
  2. Primary key

    100.0%
  3. Candidate key

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. venkatesanj@hcl.in

    venkatesanj@hcl.in New Member

    Joined:
    Oct 19, 2007
    Messages:
    24
    Likes Received:
    1
    Trophy Points:
    0
    Unique Key constraints:

    Unique key constraint will provide you a constraint like the column values should retain uniqueness.
    It will allow null value in the column.
    It will create non-clustered index by default
    Any number of unique constraints can be added to a table.


    Code:
    CREATE TABLE EMPLOYEETABLE
    	(EMPID INT ,
             IDVAL INT NOT NULL,  
    	 FIRSTNAME VARCHAR(30) ,
    	 LASTNAME VARCHAR(30) ,
    	 CITY VARCHAR(30),
    	 JOININGDATE DATETIME)
    I have created a table EmployeeTable and i am trying add a unique constriant on the column IDVAL.

    Code:
     ALTER TABLE EMPLOYEETABLE
     ADD CONSTRAINT UNIQUE_CONSTRAINT 
     UNIQUE (IDVAL)

    Primary Key:
    Primary key will create column data uniqueness in the table.
    Primary key will create clustered index by default
    Only one Primay key can be created for a table
    Multiple columns can be consolidated to form a single primary key
    It wont allow null values.


    Code:
     ALTER TABLE EMPLOYEETABLE
     ADD CONSTRAINT KEY_CONSTRAINT 
     PRIMARY KEY (IDVAL)
    Please provide me your valuable feedback regarding this article.

    Regards,
    Venkatesan Prabu . J
     
    Last edited: Mar 12, 2008

Share This Page