Define Unique Key in SQL Server

How to define unique key in table? Is it possible to define one primary key and one unique key in a single table?

Questions by kathiravan_g

Showing Answers 1 - 14 of 14 Answers

cybersavvy

  • Feb 19th, 2009
 

Yes, its possible to have PRIMARY KEY and UNIQUE keys in the same table on different columns.

UNIQUE key constraint allows the column to have distinct & one NULL values.

  Was this answer useful?  Yes

pavan

  • Jul 27th, 2011
 

If a column allows the duplication once is known as a unique key.i.e; Unique Key allows duplication once.But, Primary Key will not allow duplication.

Code
  1. CREATE TABLE Employee

  2. (

  3.  EmployeeID CHAR(5) NOT NULL CONSTRAINT

  4.  pkID PRIMARY KEY,

  5.  EmployeeName VARCHAR(30) NOT NULL,

  6.  Title VARCHAR(30) NOT NULL,

  7.  Department VARCHAR(30) NOT NULL,

  8.  Phone CHAR(15) NOT NULL CONSTRAINT unqPhone UNIQUE

  9. )

  Was this answer useful?  Yes

erik

  • Jul 21st, 2012
 

SQL UNIQUE key...

The UNIQUE constraint uniquely identifies each record in a database table.

The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

SQL UNIQUE Constraint on CREATE TABLE

The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table is created:

SQL:


Code
  1. CREATE TABLE Persons

  2. (

  3. P_Id int NOT NULL UNIQUE,

  4. LastName varchar(255) NOT NULL,

  5. FirstName varchar(255),

  6. Address varchar(255),

  7. City varchar(255)

  8. )

  9.  

  10. ALTER:

  11.  

  12. ALTER TABLE Persons

  13. ADD UNIQUE (P_Id)

  14.  



SIMILARLY OTHERS

Code
  1. SQL UNIQUE Constraint ON CREATE TABLE

  2.  

  3. The following SQL creates a UNIQUE constraint ON the "P_Id" COLUMN when the "Persons" TABLE IS created:

  4.  

  5. SQL:

  6.  

  7. CREATE TABLE Persons

  8. (

  9. P_Id int NOT NULL UNIQUE,

  10. LastName varchar(255) NOT NULL,

  11. FirstName varchar(255),

  12. Address varchar(255),

  13. City varchar(255)

  14. )

  15.  

  16. ALTER:

  17.  

  18. ALTER TABLE Persons

  19. ADD UNIQUE (P_Id)

  20.  

  21. SIMILARLY OTHERS

  22.  

  23.  

  Was this answer useful?  Yes

asifeqbal

  • Mar 19th, 2015
 

UNIQUE key constraint enforce uniqueness of the column on which they are defined. It allows the column to have distinct & one NULL values. We can make one or more columns unique in a table by defining a UNIQUE NONCLUSTERED index on the column.
It is Possible to define Primary key and Unique key in the same table on different columns.

Code
  1. <Column Name> <DATA TYPE> NOT NULL CONSTRAINT <Constraint Name> UNIQUE

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions