Skip to main content

Difference Between Primary Key and Unique Key In Sql Server

Both PRIMARY KEY and UNIQUE KEY enforces the Uniqueness of the values
 (i.e. avoids duplicate values) on the column[s] on which it is defined.
 Also these key’s can Uniquely identify each row in database table.
Below table lists out the major 
difference between PRIMARY KEY and UNIQUE KEY:
PRIMARY KEY
UNIQUE KEY
NULL
It doesn’t allow Null values.
Because of this we refer
PRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT
Allows Null value. But only one Null value.
INDEX
By default it adds a clustered index
By default it adds a UNIQUE non-clustered index
LIMIT
A table can have only one PRIMARY KEY Column[s]
A table can have more than one UNIQUE Key Column[s]
CREATE SYNTAX
Below is the sample example for defining a single column as a PRIMARY KEY column while creating a table:CREATE TABLE dbo.Customer
(
Id 
INT NOT NULL PRIMARY KEY,
FirstName 
VARCHAR(100),
LastName 
VARCHAR(100),
City 
VARCHAR(50)
)
Below is the Sample example for defining multiple columns as PRIMARY KEY. It also shows how we can give name for the PRIMARY KEY: CREATE TABLE dbo.Customer
(
Id 
INT NOT NULL,
FirstName 
VARCHAR(100) NOT NULL,
LastName 
VARCHAR(100),
City 
VARCHAR(50),
CONSTRAINT PK_CUSTOMER PRIMARY KEY(Id,FirstName)
)
Below is the sample example for defining a single column as a UNIQUE KEY column while creating a table:CREATE TABLE dbo.Customer
(
Id 
INT NOT NULL UNIQUE,
FirstName 
VARCHAR(100),
LastName 
VARCHAR(100),
City 
VARCHAR(50)
)
Below is the Sample example for defining multiple columns as UNIQUE KEY. It also shows how we can give name for the UNIQUE KEY:
CREATE TABLE dbo.Customer
(
Id 
INT NOT NULL,
FirstName 
VARCHAR(100) NOT NULL,
LastName 
VARCHAR(100),
City 
VARCHAR(50),
CONSTRAINT UK_CUSTOMER UNIQUE(Id,FirstName)
)
ALTER SYNTAX
Below is the Syntax for adding PRIMARY KEY CONSTRAINT on a column when the table is already created and doesn’t have any primary key:ALTER TABLE dbo.Customer
ADD CONSTRAINT PK_CUSTOMER PRIMARY KEY(Id)
Below is the Syntax for adding UNIQUE KEY CONSTRAINT on a column when the table is already created:ALTER TABLE dbo.Customer
ADD CONSTRAINT UK_CUSTOMERUNIQUE (Id)
DROP SYNTAX
Below is the Syntax for dropping a PRIMARY KEY:ALTER TABLE dbo.Customer
DROP CONSTRAINT PK_CUSTOMER
Below is the Syntax for dropping a UNIQUE KEY:ALTER TABLEdbo.Customer
DROP CONSTRAINT UK_CUSTOMER


Comments

Popular posts from this blog

October CMS E-Commerce Tutorial: GoT White Walkers Protection Store

As many of you probably know, Game of Thrones Season 6 is starting next April 24th. Now I recently introduced myself on this blog , but forgot to mention that I'm, like millions of others, a shameless GoT fan . When our content guy asked me to craft a post showing how the easy e-commerce integration we brag about would work with October CMS, I immediately picked GoT as a theme for the demo. ​ So in this post, I'm going to show you how to set up a store selling defense against the imminent White Walkers invasion. Because WINTER IS COMING big time, you know. ​ ​ More specifically, I'll provide a step-by-step e-commerce tutorial explaining how to integrate our shopping cart platform to an October CMS site. Let's get into it. ​ What is October CMS ​ We've been hearing about October CMS from developers here and there for a while now. This free, open-source CMS platform is the brainchild of fellow Canadian Alexey Bobkov and Australian Samuel Georges. It...

Creating a Custom Page in OpenCart

Creating a Custom Page in OpenCart by MarketInSG » Tue Apr 17, 2012 6:59 am I noticed a lot of users are searching for help on this topic, so here's a short tutorial for all of you. What you need: - Basic PHP knowledge - Basic HTML knowledge - Text Editor Required files to create a custom page: - Controller file - Template file Optional files: - Model file - Language file (I will be using it in this example here) Controller file Create a file name 'static.php' in /catalog/controller/information/ code inside the file (php) <?php class ControllerInformationStatic extends Controller {    private $error = array();            public function index() {       $this->language->load('information/static'); //Optional. This calls for your language file        $this->document->setTitle($this->language->get('heading_title')); //Optional. Set the title of your web page.   ...

Paypal In Pakistan Payoneer-Verified

Since there are a huge number of online organizations which just acknowledge Paypal as installment passage. Thusly I am going to demonstrate to you how you can make Paypal record in Pakistan and in what manner would you be able to check it ! It is an unlawful trap however you can utilize it for crisis purposes. Instructions to make and confirm Paypal record in Pakistan  Simply take after the regulated guide underneath to get your Verified Paypal Account. You will additionally get Virtual Bank Account in USA and  1 Master  Debit Card free of charge You will have the capacity to utilize that check card to cashout your trusts through Paypal from any ATM Machine. Go to  Paypal,  Sign up and Choose your nation as United States of America. Use  Fake USA Address,  But other all data ought to be 100% right. When you information exchange, confirm your email and login to your Paypal record. Presently you have your Paypal record, now is the rig...