21
Sep

Primary Key:
Primary Key enforces uniqueness of the column on which they are defined. Primary Key creates a clustered index on the column. Primary Key does not allow Nulls.

Create table with Primary Key:
CREATE TABLE Authors (
AuthorID INT NOT NULL PRIMARY KEY,
Name VARCHAR(100) NOT NULL
)
GO

Alter table with Primary Key:

ALTER TABLE Authors
ADD CONSTRAINT pk_authors PRIMARY KEY (AuthorID)
GO

Unique Key:
Unique Key enforces uniqueness of the column on which they are defined. Unique Key creates a non-clustered index on the column. Unique Key allows only one NULL Value.

Alter table to add unique constraint to column:
ALTER TABLE Authors ADD CONSTRAINT IX_Authors_Name UNIQUE(Name)
GO

Reference :http://blog.SQLAuthority.com

see this also to – http://blog.sqlauthority.com/2008/09/08/sql-server-%E2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/

VN:F [1.7.4_987]
Rating: 0.0/5 (0 votes cast)
VN:F [1.7.4_987]
Rating: 0 (from 0 votes)

Tags: , , ,

This entry was posted on Monday, September 21st, 2009 at 1:59 am and is filed under SQL Server. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or TrackBack URI from your own site.

One Response to "SQL SERVER–Primary Key Constraints and Unique Key Constraints"

  1. 1 Wits Square » Collection of Source Code
    October 21st, 2009 at 1:19 am  

    [...] SQL SERVER–Primary Key Constraints and Unique Key Constraints Primary Key: Primary Key enforces uniqueness of the column on which they are defined. Primary Key creates a clustered index on the column. Primary Key does not allow Nulls. Create table with Primary Key: CREATE TABLE Authors ( AuthorID INT NOT NULL PRIMARY KEY, Name VARCHAR(100) NOT NULL ) GO Alter table with Primary Key: ALTER TABLE Authors ADD CONSTRAINT … [...]

Leave a reply

Name (*)
Mail (*)
URI
Comment