Primary Key, Candidate Key, Alternate Key, Foreign Key, Composite Key
Primary Key, Candidate Key, Alternate Key, Foreign Key, Composite Key
Primary Key, Candidate Key, Alternate Key, Foreign Key, Composite Key
Prim ary Key, Can didat e Key, Alt ern at e Key, Foreign Key, Com posit e Key
Programming
Core Java
Data Structure
Brain Teasers
Interview Puz z le
Tech Interview
Keys in RDBMS
Search
We know that database uses tables to organiz e information. To maintain data integrity (that is
data should be correct and in well formed) we use concept of keys. There are five types of keys
in database which is as follows -
Connect with us
Candidate key
Primary Key
Foreign Key
Alternate Key
Composite Key
Example:
CrazyforCode
7,829likes
Like Page
Cont act Us
Bethefirstofyourfriendstolikethis
Quick Links
Foreign Key: COURSEID
ZS Associate Interview Puzzles
Alternate Key:FNAME+LAME
Composite Key:FNAME+LAME
Candidate Key
Candidate keys are those keys which is candidate for primary key of a table. In simple words we
can understand that such type of keys which full fill all the requirements of primary key which is
not null and have unique records is a candidate for primary key. So thus type of key is known as
candidate key.Every table must have at least one candidate key but at the same time can have
several.
Data Structures
Data Structure Interview Questions
Primary Key
Such type of candidate key which is chosen as a primary key for table is known as primary key.
Primary keys are used to identify tables. There is only one primary key per table. In SQL Server
when we create primary key to any table then a clustered index is automatically created to that
column.
Foreign Key
Foreign key are those keys which is used to define relationship between two tables. When we
want to implement relationship between two tables then we use concept of foreign key. It is also
known as referential integrity. We can create more than one foreign key per table.foreign key is
generally a primary key from one table that appears as a field in another where the first table has
a relationship to the second. In other words, if we had a table A with a primary key X that linked to
a table B where X was a field in B, then X would be a foreign key in B.
h t t p://www.craz yforcode.com /keys-rdbm s/
T opics
Algorithm
Array
Basic OOPs
1/7
5 /16/2016
Prim ary Key, Can didat e Key, Alt ern at e Key, Foreign Key, Com posit e Key
Alternate Key
If any table have more than one candidate key, then after choosing primary key from those
candidate key, rest of candidate keys are known as an alternate key of that table. Like here we
can take a very simple example to understand the concept of alternate key. Suppose we have a
table named Employee which has two columns EmpID and EmpMail, both have not null attributes
and unique value. So both columns are treated as candidate key. Now we make EmpID as a
primary key to that table then EmpMail is known as alternate key.
Composite Key
When we create keys on more than one column then that key is known as composite key. Like
here we can take an example to understand this feature. I have a table Student which has two
columns Sid and SrefNo and we make primary key on these two column. Then this key is known
as composite key.
C Programming
Graph
Interview Experiences
Linked List
Networking
Operating System
Puz z les
SQL
Strings
Tree
Web Technology
Whats going on ?
Deepali Agarwal on Check if Any Anagram of a
String is Palindrome or Not
Brian on Secret Mail Problem
kiran kumar patil on How Much Money He had
Initially ?
What should you ask in an interview? Mel
Reams on 10 identical bottles of pills
Netrapal Rajput on Keys in RDBMS
Vinay Kumar on Keys in RDBMS
Pahlaj on Design of a Tinyurl Service
fdgfd on Crossing the Bridge Puz z le
Contribute Us
Mail us your interview experience at
admin@craz yforcode.com
2/7
5 /16/2016
Prim ary Key, Can didat e Key, Alt ern at e Key, Foreign Key, Com posit e Key
4 0Comments
Sortby Olde st
Addacomment...
PavnishChauhanSoftwareEngineeratPixelConceptionT echnologiesPvtLtd
niceanswer
LikeReply
1Jun14,20149:39am
VenkateshNandamSCREngineeringCollege
YA..IT SREALLYNICEDESCRIPT ION.BUT IWANT GOODEXAMPLESFOR
CANDIDAT EKEYSANDALT ERNAT EKEYS.COULDYOUPLEASE........
LikeReply
3Jun30,201412:19am
SisayGesseseGondarUniversity
Justtoaddclearexample...Assumethatuhavethreepeoples(withnames
A,B,C.)runningforpresidencyinurcountry.Allofthemfullfillthecriteria
tobepresidentbutthemajorityvoteMr.B.SOuhaveMrBasprimary
candidateandMrAandMrCwerepotentialcandidatesbutmajoritychose
B.Ifutakeitindatabase,Mr.BisPrimaryKey,MrAandBarealternative
keys,ALLthreeofthemarecandidatekeys.ohhijustsawurcomment
wastwoyrsago...lateinfo.
LikeReply
1Jan26,20166:34pm
GauravAgarwalDehraDun,India
preciselydescribe
LikeReplyOct30,20141:58am
ShrutiVaswaniWorksatStudent
Veryniceanswer
LikeReplyJan7,20159:02pm
KamalKumarDesigneratSeniorDesigner
T hanks
LikeReplyJan26,20157:01am
SuvrajitKarUniversityofCalcutta
helpful...
LikeReplyFeb3,20155:34am
ImranMullaniDR.J.J.Magdumcollegeofengg.
T hanks
LikeReplyMar24,20154:58am
ImranMullaniDR.J.J.Magdumcollegeofengg.
thanks
LikeReplyMar24,20154:58am
MaryamMaghrebiAfghanInstituteofComputerT echnology
T hatwasveryhelpful.T hankssomuch
LikeReply
1Apr28,20156:25am
HemantGuptaBhadrak
Canyousuggesttrickstofindthem
LikeReplyJun16,20159:52am
Load10morecomments
FacebookCommentsPlugin
3 /7
5 /16/2016
Prim ary Key, Can didat e Key, Alt ern at e Key, Foreign Key, Com posit e Key
Chandra
on February 11, 2015 at 9:54 pm said:
Very useful. Clearly explained.
Reply
Vijay
on March 3, 2015 at 7:18 pm said:
Upto This time i am confused in the difference between keys but now its cleared with
this documenntation
Reply
unknow
on March 24, 2015 at 12:59 am said:
Hi
such a lovely way to represent a love with SQL Concept. I like it
Reply
nomesh
on April 2, 2015 at 1:02 pm said:
Thanks for this sweet and short answers
Reply
Maryam Maghrebi
on April 28, 2015 at 1:25 pm said:
That was very helpful. Thanks so much
Reply
greens
on June 9, 2015 at 12:04 am said:
hi very nice ,clean explanation. very easy and very quick to understand ! thanx budddy
:))
Reply
lakshmi nair
on July 31, 2015 at 12:30 pm said:
Good explanation.. Thanks, it helped me to prepare for my interview
Reply
4/7
5 /16/2016
Prim ary Key, Can didat e Key, Alt ern at e Key, Foreign Key, Com posit e Key
Naveen konda
on August 4, 2015 at 11:49 am said:
I have big confusion of keys before reading this website. nice document. thank u
craz yforcode.
Reply
shalini
on August 19, 2015 at 8:47 pm said:
can anyone explain what is superkey??
Reply
tanvir digra
on August 19, 2015 at 9:39 pm said:
U explained it in a very simple and easy to understand lang. Thnk u very much
Reply
rasna
on August 24, 2015 at 10:37 pm said:
well explained but plz update your types of keys bcoz their are 7 types of keys..
super key
unique key
Reply
Rishi
on September 8, 2015 at 3:50 pm said:
useful piece of knowledge
helped a lot to understand the difference between the keys..
Reply
shanthana bharathi
on November 12, 2015 at 11:55 am said:
im little bit confused and im not getting it
Reply
Dipankar
on November 18, 2015 at 9:57 am said:
Great work !!
Reply
5 /7
5 /16/2016
Prim ary Key, Can didat e Key, Alt ern at e Key, Foreign Key, Com posit e Key
m.z . khan
on November 26, 2015 at 7:42 pm said:
It is very use full for me to find about info for SQL
Reply
z ahid
on November 30, 2015 at 2:37 am said:
very informative it helped me alot,,,thank U
Reply
Mohd Riyaz
on December 11, 2015 at 2:44 pm said:
That was very helpful. Thanks so much
Reply
Sebastin
on February 4, 2016 at 7:38 pm said:
Thank You !!!
Very good Explanation
Now i understood that difference..
Reply
Pratiba Kanswal
on March 22, 2016 at 12:48 pm said:
Nice article,i really understand now this keys clearly.
Thanks a lot.
Reply
Thrifty
on April 14, 2016 at 8:06 pm said:
Tell me about Super Key !!!!!!! plz
Reply
Damanpreet Singh
on April 26, 2016 at 8:11 am said:
Thnks Its too easy language to learn
Reply
6/7
5 /16/2016
Prim ary Key, Can didat e Key, Alt ern at e Key, Foreign Key, Com posit e Key
Vinay Kumar
on May 6, 2016 at 10:02 am said:
Nice answer
Reply
Netrapal Rajput
on May 6, 2016 at 3:19 pm said:
Good,These article is very understandable ,
I was confused , composit key and candidate key , But now I have clear
Thanks
Reply
Leave a Reply
Your email address will not be published. Required fields are marked *
Name *
Email *
Website
Post Comment
Previous Post
Next Post
About Us
7/7