Hi,
I am currently studying the database. I came across the concept of 'Keys'. I am very much confused in all of them. Can anyone help me by giving comparison between them along with some example?

batchmaster
batchid, batchname
1, batch1
2, batch2
3, batch3

studentmaster
studid,studname,batchid, emailid
1, abc, 1, ab@yahoo.com
2, pqr, 1, pr@yahoo.com
3, xyz, 1, xz@gmai.com
4, rms, 2, rms@gmail.com
5, opm, 2, pmo@gmail.com
6, abc, 2, bc@yahoo.com

In any table to avoid chaos or confusion we shold always select one primary key. primary key is used to identify each row distinctly, means when u say studid 6, you will find row with studid id 6 easily,
But if you same studname abc, then you will not able to decide which record to refer, 1 or 6.

Here In table batchmaster
we select batchid as primary key and in studentmaster we select studid as PK


Foreign key
It is used to refer details of other table to avoid duplicate entries. To mention batch for rms we only refer to its batch id and not whole batchname.

so batchid column in studentmaster table is know as foreign key as its details are found in some foreign table, we just store reference to it

UNique key
Some time in table you will need row values to be unique like emailid, userid like that. but you already have one primary key i n studentmaster so, we will set emailid to unique key.

Unique key help us to avoid entry of duplicate email ids in studentmaster

note primary keys are always unique keys
unique keys are NOT always PRIMARY KEYS

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.