Lec-9: What is Primary Key in DBMS | Primary Key with Examples in Hindi

Gate Smashers
14 Feb 202011:31

Summary

TLDRThis video from the Gate Smashers dives into the concept of primary keys in database management systems (DBMS). It explains the importance of keys in various exams and scenarios, including competitive exams and placements. The video uses the analogy of a lock and key to illustrate the uniqueness of primary keys in identifying database entries. It discusses candidate keys, their properties of being unique but potentially nullable, and contrasts them with primary keys, which are not only unique but also non-nullable and are assigned by the system. The video clarifies that a database can have only one primary key, emphasizing its critical role in ensuring data integrity and uniqueness.

Takeaways

  • 🔑 The primary key in a database is a unique identifier for each record, ensuring that no two entries are the same.
  • 📚 The concept of keys, including primary keys, is crucial in database management systems (DBMS) and is frequently tested in exams and interviews.
  • 📝 Candidate keys are attributes that can uniquely identify a record, but they may allow for null values and are not necessarily unique across all records.
  • 🚫 Primary keys must be unique and not null, meaning every record must have a value for the primary key, and no two records can share the same primary key value.
  • 👨‍🎓 Examples of primary keys in educational settings include student registration numbers and roll numbers, which are assigned by the institution and cannot be chosen by the student.
  • 🏢 In professional contexts, identifiers like passport numbers, Aadhaar card numbers, and license numbers serve as primary keys because they are issued by authorities and are both unique and mandatory.
  • 🚫 It is not possible to have more than one primary key in a database because the primary key's role is to uniquely identify each record, and this cannot be achieved with multiple attributes.
  • 🔍 The selection of a primary key depends on the context and the attributes available in the database, with factors like uniqueness and the necessity of having a value being key considerations.
  • 🛠️ The primary key is a fundamental aspect of database normalization and is used to enforce data integrity and support relationships between tables.
  • ❌ Attempting to assign a primary key that allows for null values or duplicates would lead to data anomalies and is not permitted in well-designed databases.

Q & A

  • What is the primary key in the context of a database?

    -The primary key is a unique identifier for each record in a database table, ensuring that each entry can be distinctly recognized and that no two records have the same key value.

  • Why is the concept of keys important in DBMS?

    -The concept of keys is crucial in DBMS because it helps in uniquely identifying records, ensuring data integrity, and is fundamental in database operations such as normalization and querying.

  • What is the difference between a primary key and a candidate key?

    -A candidate key is any column or set of columns that can uniquely identify a row in a table, while a primary key is the chosen candidate key that is used to uniquely identify each record in a table.

  • Can a table have more than one primary key?

    -No, a table cannot have more than one primary key because the primary key must uniquely identify each record, and having multiple would violate this uniqueness constraint.

  • What is the significance of the primary key being 'not null'?

    -The primary key being 'not null' ensures that every record in the database must have a value for the primary key, preventing the creation of records without an identifier.

  • Why is the primary key not chosen from user input?

    -The primary key is not chosen from user input because it needs to be unique and not null for every record. It is typically generated automatically by the system to maintain these constraints.

  • How does the concept of a primary key apply to real-world examples like student registration numbers or passport numbers?

    -In real-world examples, unique identifiers like student registration numbers or passport numbers serve as primary keys, ensuring that each individual's information is uniquely identifiable and distinct.

  • What happens if a candidate key is not unique or can be null?

    -If a candidate key is not unique or can be null, it cannot serve as a primary key because the primary key must guarantee both uniqueness and the presence of a value for every record.

  • Why are attributes like name, age, or date of birth not suitable as primary keys?

    -Attributes like name, age, or date of birth are not suitable as primary keys because they can have duplicate values, which violates the uniqueness requirement of a primary key.

  • How does the database ensure that the primary key remains unique and not null?

    -The database ensures the primary key remains unique and not null through constraints and system-generated values, preventing the insertion of duplicate or null values for the primary key.

Outlines

00:00

🔑 Introduction to Primary Keys

The video begins by emphasizing the importance of the concept of keys in database management systems (DBMS), particularly the primary key. The speaker uses the analogy of a physical key and lock to explain the uniqueness of primary keys in identifying records in a database. The discussion then shifts to the practical example of managing student records, highlighting the need for a unique identifier like a registration or roll number. The concept of candidate keys is introduced as potential unique identifiers, but the primary key is distinguished by its necessity and non-optional nature.

05:00

📚 Attributes of Primary Keys

This section delves deeper into the attributes that make a primary key. The speaker explains that while candidate keys can be unique, a primary key must also be non-nullable, meaning it cannot be left empty. Examples such as phone numbers and Aadhaar cards are discussed, noting that while they are unique, they might not be provided by every student, thus not fulfilling the non-nullable requirement. The speaker then contrasts this with registration or roll numbers, which are both unique and non-nullable, making them ideal for primary keys. The video also touches on the idea that primary keys are assigned by the system and not chosen by the users.

10:01

🚫 Limitations on Primary Keys

The final paragraph addresses the limitations regarding the number of primary keys in a database. The speaker makes it clear that a database can only have one primary key due to the stringent requirements of uniqueness and non-nullability. The analogy of hiring multiple people for a single job to illustrate the redundancy of having multiple primary keys is used. The speaker also mentions that while there can be multiple candidate keys, only one can be designated as the primary key, which is determined by the specific context and requirements of the database. The video concludes with a reiteration of the primary key's defining characteristics: it must be unique and not null.

Mindmap

Keywords

💡Primary Key

A primary key in the context of the video refers to a unique identifier for each record in a database table, ensuring that no two records have the same key value. It is a critical concept in database management systems (DBMS) as it allows for the efficient and accurate retrieval of data. The video emphasizes that a primary key must be unique and not null, meaning every record must have a value for the primary key and no two records can share the same value. Examples from the script include student registration numbers and roll numbers, which are used to uniquely identify students in a database.

💡Candidate Key

A candidate key is any column or set of columns in a table that can be used as a primary key. The video explains that while a candidate key must be unique, it does not necessarily have to be non-null, unlike a primary key. Candidate keys are potential choices for the primary key, and the selection of the primary key from among the candidate keys depends on the specific requirements and constraints of the database design. The script uses examples such as phone numbers and Aadhaar card numbers, which are unique for individuals but may not be available for all records or may be left null.

💡Unique

The term 'unique' in the video script is used to describe a property of keys in a database where no two records can have the same value for that key. This is a fundamental requirement for both candidate and primary keys to ensure that each record in the database can be distinctly identified. The video highlights that while many attributes can be unique, not all are suitable to be a primary key due to additional constraints like the non-null requirement.

💡Not Null

The 'not null' constraint, as discussed in the video, is a rule that ensures a field in a database cannot be left empty. It is a crucial aspect of the primary key because it guarantees that every record will have a value for the key, facilitating the reliable identification of records. The video contrasts this with candidate keys, which can be null, and uses examples like Aadhaar card numbers to illustrate that while they are unique, they might not be provided for every student, hence they cannot be primary keys without ensuring they are not null.

💡Database

A database, as mentioned in the video, is an organized collection of data stored and managed in a way that allows for easy access, retrieval, and management. The video focuses on the role of keys within databases, particularly how primary keys play a central role in uniquely identifying records within a database. The script uses the example of a database of student records to demonstrate the importance of primary keys in organizing and retrieving data.

💡DBMS

DBMS stands for Database Management System, which is software designed to manage and manipulate databases. The video script discusses how the concept of keys, including primary and candidate keys, is integral to the functioning of DBMS. It ensures data integrity and efficient data retrieval, as DBMS relies on keys to organize and access data within databases.

💡Normalization

Normalization is a process in database design aimed at organizing data to minimize redundancy and dependency. The video touches upon normalization when discussing the use of keys, indicating that understanding keys is essential for creating well-structured databases. Normalization involves defining tables and keys in a way that each piece of data is stored only once, which is facilitated by the use of primary keys.

💡Attributes

In the context of the video, attributes refer to the individual elements or fields in a database record that describe some aspect of the data. The script explains that attributes like student names, ages, and addresses can have duplicate values, which is why they are not suitable as primary keys. Instead, attributes that are both unique and not null, such as registration numbers, are chosen as primary keys.

💡Registration Number

The registration number, as used in the video, is an example of a primary key in the context of a student database. It is a unique identifier assigned to each student upon enrollment, ensuring that each student record can be distinctly identified. The video emphasizes that registration numbers are given by the institution and cannot be chosen by the student, reflecting the non-null property of primary keys.

💡Roll Number

A roll number, according to the video, is another example of a primary key, particularly in educational settings. It is a unique identifier assigned to students for administrative purposes, such as attendance and grading. The video script uses roll numbers to illustrate how primary keys are integral to managing and accessing student data in a database.

Highlights

The primary key in DBMS is crucial for uniquely identifying records.

Concepts of keys are frequently tested in competitive exams and academic assessments.

A primary key is essential in database normalization and SQL.

The primary key concept is introduced with an example of managing student records.

A key's uniqueness is compared to a lock and key mechanism.

Not all attributes like name or date of birth can guarantee uniqueness.

Attributes like phone number, Aadhaar card number, and roll number are typically unique.

Candidate keys are attributes that are unique but may allow null values.

Primary keys are selected from candidate keys based on their appropriateness and non-nullable nature.

Primary keys must be unique and not null, ensuring accurate identification of records.

The value of the primary key is assigned by the database, not provided by the user.

A database can only have one primary key, ensuring singularity in record identification.

The selection of a primary key depends on the specific requirements of the database scenario.

Examples of primary keys include registration numbers, roll numbers, Aadhaar card numbers, and passport numbers.

The primary key is a critical component in database design and management.

Transcripts

play00:00

Hello friends, welcome to the Gate Smashers

play00:02

We're going to discuss in this video.

play00:04

What's this primary key?

play00:06

In DBMS, key's concept is most important.

play00:10

Whether you talk about competitive exams.

play00:13

Whether you talk about college

play00:14

or university level exams

play00:15

Or even talk about placements,

play00:17

there also Concept of keys are

play00:20

Certainly asked.

play00:21

And that doesn't means that only one key,

play00:24

like what is primary key?

play00:25

what is candidate key?

play00:26

That's enough, No.

play00:27

If you talk about the normalization.

play00:29

Talk about the sequel,

play00:30

The concept of keys will be used everywhere.

play00:34

So in this video, I'm just focusing

play00:36

On the primary key.

play00:37

So What is the story of primary key?

play00:39

What is it, how do we talk?

play00:41

So I'm starting this story with a simple example

play00:44

Let say, we have 100 students in a class

play00:47

Now I have to store the data of 100 students.

play00:50

Now I will obviously not always

play00:52

call 100 students by face.

play00:53

If I have to take an attendance. There is an online system.

play00:56

So what would I do in the online system?

play00:57

I will save the details of all those students

play01:00

I'll make Excel file.

play01:02

In which I have registered the student's

play01:04

registration number, roll number,

play01:05

Father's name, mother's name,

play01:06

student's name, age, gender, marks

play01:09

Adderes, phone number, aadhaar card,

play01:11

make multiple value, multiple attributes.

play01:14

I'll make a table.

play01:16

Within which multiple attributes Will define.

play01:20

Then we will fill the details

play01:22

of all the students.

play01:24

In a way you can visualise,

play01:25

there is a table in which we have done

play01:27

all the entries.

play01:28

Now here comes the concept of key,

play01:30

first of all, what is the meaning of key?

play01:32

Like we have key in the lock.

play01:34

What is its property,

play01:36

that every lock has its unique key

play01:38

That key will open only that lock.

play01:42

I'm talking about the best case,

play01:43

I'm not talking about the normal case.

play01:45

A lot can happen in India.

play01:46

Maybe you can open multiple locks with the same key,

play01:48

but I'm not talking about that.

play01:50

Not even I am talking about the master key

play01:52

who is roaming around with thieves.

play01:54

What is the normal key's concept

play01:56

that there is only a key for a lock.

play01:58

From that, I can only open that lock,

play02:01

so the same thing we are doing here.

play02:03

Means you are uniquely identifying something.

play02:06

If two things are the same,

play02:08

So identify them that you are different

play02:10

and you are different

play02:11

How will you do? From the key.

play02:13

Now here's talking about the database,

play02:15

then in the database, How we will do?

play02:17

You have attributes in the database.

play02:19

Now If I say name

play02:20

If we take the student's name,

play02:22

then is the student's name guaranteed

play02:24

That will be different?

play02:25

No, you check yourself,

play02:27

you will have a lot of kids in your class,

play02:28

in college, university

play02:30

Whose name, even if you see

play02:31

the first name last name, will be same.

play02:35

You can't take guarantee.

play02:36

Age, date of birth,

play02:38

there are 365 days in 1 year.

play02:40

Or if in leap year 366 days, then obviously

play02:43

somewhere the date of birth will be same.

play02:46

Here if we talk,

play02:47

father's name can also be the same,

play02:48

mother's name can also be same.

play02:50

In case, If two brothers and sisters

play02:51

or two brothers are studying together,

play02:53

obviously their father mother will be same

play02:54

Similarly, the multiple attributes may have same values,

play02:58

but which are the attributes

play03:00

cannot have same value?

play03:01

That will be Unique key.

play03:03

Which could be?

play03:04

Let say, your phone number,

play03:05

It will comes in your mind that sir,

play03:07

The phone number will be different.

play03:08

Fine. The phone number will be always different.

play03:12

The Aadhaar card number.

play03:13

Yes, you can say that the adhar card we have,

play03:17

Everyone's numbers will be different.

play03:19

Pan card also, although

play03:20

I am talking about the students.

play03:22

So In the case of student,

play03:23

even if you do not take the PAN card, even if you take it,

play03:25

then also it will be unique

play03:26

In this way, even if you take the license number,

play03:28

it will be unique

play03:29

Take Registration number, Or roll number

play03:32

Because in many universities,

play03:34

there are registration numbers.

play03:35

In many colleges, Roll numbers are there,

play03:37

then it will also be unique.

play03:38

So in this way,

play03:39

you can count the multiple attributes somewhere that,

play03:41

sir all these will be unique.

play03:43

There are no duplicacy in it.

play03:46

Now let's talk about,

play03:47

what we all these attributes.

play03:50

We call all these attributes, Candidate key

play03:54

What are all these attributes being made?

play03:56

All of these are candidate keys.

play03:57

So what are the property of the candidate keys?

play04:00

It is unique.

play04:02

So Unique I Achieved

play04:04

But will unique alone work?

play04:07

Can't work, how?

play04:09

Let suppose, the phone number

play04:11

I'm assuming the phone number,

play04:13

If i want to find out a student

play04:16

then I'll fill in the phone number

play04:17

and the details will come.

play04:19

So, is there any guarantee that

play04:20

every student must have given the correct phone number

play04:22

or even given the phone number?

play04:24

This is also not guaranteed

play04:25

and there is also a possibility that,

play04:27

let suppose if two brothers and sisters are studying together,

play04:31

then both of them may have given

play04:32

the same number in the database.

play04:34

That's possible, isn't it?

play04:35

This case is possible.

play04:37

You can say that the student does not

play04:39

given the phone number.

play04:40

Or it is also possible that

play04:42

if there are two siblings,

play04:43

then somewhere they mentioned the same phone number.

play04:47

The two of them does not have different Phone Numbers.

play04:49

Next say, Aadhaar card.

play04:50

Now you can also say that it is not

play04:52

necessary that every single student has applied.

play04:55

If a student has applied,

play04:57

then obviously their Aadhaar card will be unique.

play04:59

But what is the guarantee

play05:00

that if we talk about your college or university,

play05:02

let suppose if there are , 10,000-20,000 students

play05:05

or 50,000 students.

play05:06

Can you give the guarantee that all

play05:08

students will have Aadhaar card?

play05:10

All students will have PAN card,

play05:11

license number.

play05:13

That is not a guarantee.

play05:14

And because of this, it can happen that

play05:17

if I am looking for a student,

play05:20

means if i entered his entry online,

play05:22

then there are two or three entries corresponding to it

play05:24

or no entries corresponding to it.

play05:27

Because you didn't have that in your database,

play05:30

So what is the second property that you have here?

play05:32

That's Null.

play05:34

It could also be possible

play05:36

that I had not given the phone number.

play05:39

I took admission in the university,

play05:40

in any college,

play05:41

He asked the phone number,

play05:43

I didn't give it there yet

play05:44

or gave a wrong number.

play05:45

So obviously somewhere I put this field,

play05:47

let suppose keep it null,

play05:49

Or with the same sibling case,

play05:51

if you give same phone number,

play05:53

it will pick the same phone number

play05:55

and what will happen because of it?

play05:57

Tomorrow when you will find the student,

play05:58

then there will not be one entry corresponding to it,

play06:00

there may be two or three entries.

play06:02

So it is not uniquely identified.

play06:04

So the concept of the database is that

play06:06

I can uniquely identify each one.

play06:08

I can uniquely identify every single tuple ,

play06:11

it will not be achieved,

play06:13

then when it will be achieve,

play06:14

that one it should be unique.

play06:16

Second it should not be null,

play06:19

Not null mean that

play06:21

you can't leave this field empty.

play06:24

you can't leave this field empty,

play06:26

You have to fill something in this field,

play06:29

Something has to be filled.

play06:30

Let suppose if I talk about the phone number

play06:32

or the Aadhaar card, the PAN card.

play06:34

So, let suppose, a student came to you

play06:36

get admission in your college.

play06:38

You'll definetly fill that field.

play06:40

Now if you fill that field.

play06:42

If Students say that I don't have

play06:43

an Aadhaar card number yet.

play06:45

So what will you do?

play06:46

Whether you fill any wrong value in that field? like $, #

play06:50

Just to fill it.

play06:52

Because you can't leave empty,

play06:53

It's a not null field,

play06:55

then what will happen, in that case,

play06:57

you are making the wrong entry in the database.

play06:59

What could be the second case?

play07:01

You will say, want to take admission,

play07:03

give me the aadhaar card number

play07:04

because aadhaar card number will have to be filled,

play07:06

it's a not null field.

play07:07

Null means empty, you can't leave null,

play07:10

what will you do then?

play07:11

You will say, you will not get admission now.

play07:14

Bring the Aadhaar card number first.

play07:15

So obviously, this case will not happen,

play07:17

then obviously other competitors

play07:20

college, university will pull that student.

play07:22

Similarly, of we talk about PAN card number,

play07:24

license number.

play07:26

You can't do this case a Mandatory field,

play07:29

To whom would you do the mandatory field

play07:31

in the case of a student?

play07:33

Registration number or roll number.

play07:35

What is the actual concept of primary key?

play07:38

Primary key is also unique Plus

play07:40

it is not null.

play07:41

So I got an advantage from the candidate,

play07:44

Unique.

play07:44

It will always be unique,

play07:46

but out of all candidates key

play07:48

I'm just one,

play07:50

The one who is the most appropriate.

play07:52

Appropriate means like I just told you the case.

play07:54

In the case of student

play07:55

the most appropriate will be either

play07:58

the registration number or roll number.

play08:00

If it is a small college,

play08:02

then you can also take the roll number.

play08:03

If there are universities,

play08:04

then what you will take, registration number

play08:06

or serial number as a primary key,

play08:09

because it is unique,

play08:10

because it is a candidate key so definitely it is unique

play08:13

Plus this is not null

play08:15

You can't leave it empty.

play08:17

So whenever you talk about this case,

play08:19

you can pick any database.

play08:21

In that database,

play08:23

Your primary key always unique plus not null

play08:25

and second,

play08:26

We never take the value of

play08:28

the primary key from the users.

play08:29

We give the value of primary key to the user.

play08:32

So let suppose if you go to a college

play08:33

or university to take admission

play08:35

So you won't say sir,

play08:37

I want this roll number

play08:39

or I want this registration number.

play08:40

that I want a VIP registration number,

play08:42

there is no such thing.

play08:44

They will give you automatically generated

play08:47

whether this is your registration number

play08:48

or roll number,

play08:49

because this field can't be leave empty.

play08:51

Similarly, Let suppose if you go to the passport office

play08:54

What will be the passport number

play08:55

at the passport office?

play08:57

It will be primary key

play08:58

because it is also unique,

play08:59

plus not null.

play09:01

Now in the passport office, data of the

play09:03

same student's sorry, same people's will be there, right?

play09:05

Those who have a passport

play09:07

means those who have applied for a passport.

play09:09

Now, do you ask for the passport number by yourself,

play09:11

No.

play09:11

They will give you the passport number,

play09:13

that is the primary key.

play09:14

Similarly in the license office,

play09:16

If you applying for the license,

play09:18

then the license office will have the data

play09:20

of those people whose license has been

play09:22

applied already or they have already have

play09:24

taken the proper or learning license.

play09:26

So the license number will also be

play09:28

provided you by the office.

play09:30

Because this is the very very tough field

play09:33

means it becomes tough somewhere.

play09:35

Because unique and not null both

play09:37

has to be achieved.

play09:38

Data should be unique and also not null

play09:41

means you can't even leave that field empty

play09:43

and even can't fill the duplicate here.

play09:46

So this is the main concept of the primary key.

play09:49

Always remember,

play09:50

the primary key is always unique plus not null.

play09:53

but the candidate key is only unique.

play09:55

You can leave it empty,

play09:57

you can skip it,

play09:58

but If you don't want skip it,

play10:01

then what will you made it, primary key.

play10:03

So the last point here is,

play10:05

in a database

play10:07

more than one means 2, 3, 4 primary key,

play10:09

can be possible?

play10:11

No,

play10:12

there must be only one primary

play10:14

because I told you first,

play10:15

to make a primary key is very hard

play10:17

You can neither leave it empty

play10:19

nor you can fill the duplicates in it.

play10:21

So if you try to make more than one

play10:23

attribute the primary key,

play10:26

then although at first your database will not be allowed.

play10:28

Any server, Any software you are using

play10:31

Whether you are using Oracle,

play10:32

Sequel server, IBM anyone will not allow you.

play10:35

Because why would I hire two person

play10:38

my work is getting done by one person?

play10:40

Same thing here also if my work is

play10:42

getting done by one attribute,

play10:43

then I will not mess with multiple attributes.

play10:46

So that's why your software doesn't even allow you.

play10:49

to make more than one primary key.

play10:51

But yes, more than one candidate key can be possible,

play10:54

because the unique multiple attribute can be possible.

play10:56

Alone not null also possible.

play10:58

You can also make more than one attribute, not null.

play11:00

But if we talk about combinedly,

play11:02

unique and not null,

play11:04

it will be just an attribute

play11:06

and we called it primary key.

play11:08

That will depends on the database

play11:10

that you are creating in which scenario.

play11:11

It will become in the student's case.

play11:13

In the case of Aadhaar card, Aadhaar card number will be made.

play11:16

In case of PAN, pan number will be made.

play11:18

In case of passport, passport number.

play11:20

In the case of license, license number.

play11:22

So whatever the database you will talk about,

play11:24

in that cases you will understand,

play11:26

what is the concept of primary key.

play11:28

Thank you.

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Database ManagementPrimary KeyData IntegrityUnique IdentifierCandidate KeyData NormalizationEducational ContentTechnical TutorialStudent RecordsIdentity Verification
هل تحتاج إلى تلخيص باللغة الإنجليزية؟