Register
Sign In
Skip over navigation
Home
Add New Question
Jobs
Products
SQL Server primary and unique keys
Question posted
in
Computer Software
on
06 2010
Rate question difficulty level
1
1 Votes
what are the difference between the two? what are they used for?
Add an answer
Share
|
9 Answers
Gadi Berqowitz
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.
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.
06/24/2010
Guest
1)You can create only one PK on the table
2) By default PK, creates Clustered Unique index on the table , UNIQUE constraint creates inique NCI...
3) With UNIQUE constraint on the table you can have only one NULL value...
06/27/2010
Guest
A UNIQUE KEY can uniquely identify each row in a table. A UNIQUE KEY comprises a single column or a set of columns. No two distinct rows in a table can have the same value (or combination of values) in those columns if NULL values are not used. Depending on its design, a table may have arbitrarily many UNIQUE KEYS BUT at most one PRIMARY KEY.
A PRIMARY KEY is a special case of UNIQUE KEYS. The major difference is that for UNIQUE KEYS the implicit NOT NULL constraint is not automatically enforced, while for PRIMARY KEYS it is enforced. Thus, the values in UNIQUE KEY columns may or may not be NULL. Another difference is that PRIMARY KEYS must be defined using another syntax.
DEFINING PRIMARY KEYS
ALTER TABLE
ADD [ CONSTRAINT
]
PRIMARY KEY (
{,
}... )
CREATE TABLE table_name (
id_col INT,
col2 CHARACTER VARYING(20),
...
CONSTRAINT tab_pk PRIMARY KEY(id_col),
...
)
CREATE TABLE table_name (
id_col INT PRIMARY KEY,
col2 CHARACTER VARYING(20),
...
)
DEFINING UNIQUE KEYS
ALTER TABLE
ADD [ CONSTRAINT
]
UNIQUE (
{,
}... )
CREATE TABLE table_name (
id_col INT,
col2 CHARACTER VARYING(20),
key_col SMALLINT,
...
CONSTRAINT key_unique UNIQUE(key_col),
...
)
CREATE TABLE table_name (
id_col INT PRIMARY KEY,
col2 CHARACTER VARYING(20),
...
key_col SMALLINT UNIQUE,
...
)
06/27/2010
Guest
Both the keys enforces uniqueness in terms of values on which they are applied. However there is (are) a (some) difference(s):
PK does not accept null and creates clustered index and UK accepts one null value and creates non clustered indexes.
Kind Regards
PRatik Shah
06/28/2010
Guest
A primary key can be created using either a clustered or nonclustered index. In Microsoft SQL Server, clustered is the default, but this can, and often should, be changed.
06/28/2010
Joselito
Whoa!You made certain fine poitns there. Used to looking about them and located a number of persons will consent with the blog. This site is one of the best Ive witnessed in a little while. Your post offers excellent material. We've previously been browsing in many different places for info for this sort of stuff. I looked everywhere, I looked in Bing, and that i missed your posting right until today. Honestly, you truly present great articles, really it is actually informative. We're returning here in the forseeable future. You'll want to maintain website updated, it's superb. Go take a look at my website and obtain a Nook
04/30/2012
tnqsfn
EoEq4R
dsigklzlugbz
05/02/2012
Bear
. Your preferred cause appeared to be at the internet the easiest aspect to consider of. I say to you, I certainly obtain irked even as folks consider issues that they just do not comprehend about. A person were able to hit the toe nail on the very best as well as described the entire factor without necessity side effect , folks might take an indication. Will likely be once again to obtain more. Thank you
auto insurance quotes
auto insurance rates
05/12/2012
lala
Coach Outlet
is an American leather goods company famous for its ladies handbags and purses.
Coach Factory
Brand may be creating Coach Bags styles which are absolutely multicolored and fun. Here, we talk about
Coach Outlet Online
comes in different colors and sizes, which means that you can go in for
Coach Outlet Online
one that suits you the most.
05/17/2012
Add an answer*
Your name
Email
Enter the code shown above:
please wait..
Company:
TGS
Location:
Israel
Tags:
SQL
,
primary key
,
unique key
Occupations:
Software\System Architecture
,
Software\Web Development
add a question
Now hiring!
IT Sourcing Procurement Analyst Software Service Infrastructure
CVS Caremark
Woonsocket, RI
Strategy, including: Procurement Execution ?Maintain IT procurement process in collaboration with IT stakeholders ... legal risk mitigation strategy are addressed ?Support the development of executive briefings summarizing contract...
---------------------------
Software Development Analyst/ .NET Developer- Health IT
Northrop Grumman
Atlanta, GA
Sharp programming skills, eagerness for challenging development opportunities, and the ability to work with a ... of information security concepts and principles, as it pertains to application/service development and...
---------------------------
(IT Applications Analyst Developer Lead
Halliburton
Houston, TX
Houston, TX DUTIES: Responsible for the development, acquisition, maintenance and support of applications in assigned ... plus 7 years of progressive experience in Systems Development and Application Support, including analysis,...
---------------------------
Mgr Software Development 2-Health IT
Northrop Grumman
Little Rock, AR
and software developers. Works with members of the development team to ensure that all legacy data is ... years of relevant experience - Experience with IT Project Development Lifecycle - Experience converting data for...
---------------------------
IT Specialist (Applications Development)
Parsons
Houston, TX
in software complexity. Makes a recommendation to senior IT management and the end user. Uses charts, diagrams and ... guidance to lower-level IT staff assigned to an application development project. * Develops functional specifications to...
---------------------------
Software Development Engineer (IT Security)
Amazon.com
Seattle, WA
Role Description Amazon.com is looking for expert Software Development Engineers to develop tools supporting the IT ... for you. You will participate in every level of software development from requirements gathering to operational...
---------------------------
Software Development Engineer - Kindle with Special Offers, AmazonLocal
Amazon.com
Seattle, WA
Software Developement Engineer - Kindle Application Development, AmazonLocal AmazonLocal is looking for exceptional, ... or related field * 2+ years professional experience in software development * Fundamentals in object-oriented...
---------------------------
Manager Software Development 2-Health IT
Northrop Grumman
Little Rock, AR
and software developers. Works with members of the development team to ensure that all legacy data is ... years of relevant experience - Experience with IT Project Development Lifecycle - Experience converting data for...
Jobs
by
Simply
Hired
View all jobs