Lec-10: Foreign Key in DBMS | Full Concept with examples | DBMS in Hindi

Gate Smashers
3 Aug 201808:43

Summary

TLDRIn this educational video titled 'Gate Smashers,' the presenter delves into the concept of foreign keys in databases. The video is split into two segments: the first explains what a foreign key is, emphasizing its role as an attribute that references a primary key within the same or another table. The second part promises to explore why foreign keys are essential, particularly in maintaining referential integrity. Using the example of 'student' and 'course' tables, the presenter illustrates how foreign keys ensure data consistency across related tables. The video also touches on how to create and apply foreign keys in Oracle, highlighting the importance of matching data types and referencing the correct primary keys.

Takeaways

  • 🔑 A foreign key is an attribute or a set of attributes in a table that refers to the primary key of the same or another table.
  • 🔍 The primary key can be simple, consisting of one attribute, or composite, made up of multiple attributes.
  • 📚 Referential Integrity is maintained by foreign keys, ensuring that the data in the database remains consistent and accurate.
  • 🎓 The foreign key must have the same data type as the primary key it references to ensure data consistency.
  • 🏫 In the provided example, the 'student' table has a primary key 'roll number', and the 'course' table has a foreign key 'roll number' referencing the 'student' table.
  • 🚫 You cannot insert a value into a foreign key column unless that value exists as a primary key in the referenced table.
  • 🔗 A table can have more than one foreign key, but it can only have one primary key.
  • 🛠️ Foreign keys are created during table creation using the 'CREATE TABLE' statement or can be added to an existing table using the 'ALTER TABLE' statement.
  • 📝 The script explains how to define a foreign key in Oracle SQL, specifying the data type, the 'REFERENCES' keyword, and the primary key of the referenced table.
  • 🔄 The concept of a 'referencing table' and a 'referenced table' is introduced, with the former containing the foreign key and the latter containing the primary key.

Q & A

  • What is a foreign key in a database?

    -A foreign key is an attribute or a set of attributes in a table that references the primary key of another table or the same table.

  • Can a primary key be composite, and how does it affect the foreign key?

    -Yes, a primary key can be composite, meaning it is made up of more than one attribute. A foreign key can reference a composite primary key as well.

  • How does a foreign key maintain referential integrity in a database?

    -A foreign key maintains referential integrity by ensuring that a value in the foreign key column must exist as a primary key value in the referenced table. This prevents invalid references.

  • What are the two types of tables involved in a foreign key relationship?

    -The two types of tables are the 'referenced table,' which contains the primary key, and the 'referencing table,' which contains the foreign key.

  • Can a table have more than one foreign key? What about primary keys?

    -Yes, a table can have more than one foreign key, but it can only have one primary key.

  • What is the importance of having a common attribute between two tables in a database?

    -A common attribute, like a foreign key, is crucial because it allows the two tables to be related or joined together, making it possible to query related data from both tables.

  • How do you define a foreign key while creating a table in Oracle SQL?

    -To define a foreign key while creating a table, you use the 'REFERENCES' keyword. For example: `CREATE TABLE Course (Roll_number INT REFERENCES Student(Roll_number));`.

  • What is the query to add a foreign key to an already created table?

    -To add a foreign key to an existing table, you use the `ALTER TABLE` command. For example: `ALTER TABLE Course ADD CONSTRAINT fk_roll_number FOREIGN KEY (Roll_number) REFERENCES Student(Roll_number);`.

  • Does the name of the foreign key attribute need to match the primary key attribute?

    -No, the names of the foreign key and primary key attributes do not need to match, as long as they reference the correct values.

  • Why do we make the primary key unique and not null?

    -A primary key is made unique and not null to ensure that each row in a table can be uniquely identified, and no empty values are allowed in the primary key column.

Outlines

00:00

🔗 Introduction to Foreign Keys in Databases

This paragraph introduces the topic of foreign keys in databases. The speaker begins by welcoming viewers to the 'Gate Smashers' channel and requests them to subscribe and enable notifications for updates. The video is divided into two parts: the first part defines what a foreign key is, and the second part discusses its purpose in databases. A foreign key is described as an attribute or a set of attributes that references the primary key of the same or another table. The concept of primary keys being either simple or composite is explained, emphasizing that foreign keys must reference primary keys, which can be found in the same table or in a different one. The paragraph also touches on the importance of referential integrity and how foreign keys play a role in maintaining it, a topic that will be further explored in the second part of the video. Two example tables, 'student' and 'course', are introduced to illustrate the relationship between tables using foreign keys.

05:00

📚 Implementing Foreign Keys and Referential Integrity

In this paragraph, the speaker delves into the implementation of foreign keys and the concept of referential integrity. It explains that the 'student' table is the base table with a primary key 'roll number', and the 'course' table includes a 'roll number' column that acts as a foreign key referencing the 'student' table. The importance of matching data types between the foreign key and the primary key is highlighted, as well as the syntax for creating a foreign key in Oracle. The speaker also discusses how to add a foreign key constraint to an existing table using the 'ALTER TABLE' command. The paragraph concludes by clarifying that the names of the columns used as foreign keys do not have to match the primary key column names, and that a table can have multiple foreign keys, unlike primary keys, which are unique to a table. The speaker promises to cover the maintenance of referential integrity in both referenced and referencing tables in the next part of the video.

Mindmap

Keywords

💡Foreign Key

A foreign key is an attribute in a database table that refers to the primary key in the same or another table. It is used to establish a link between two tables, ensuring data integrity and relational connections. In the script, the foreign key is exemplified by the 'roll number' in the 'course' table, which references the 'roll number' primary key in the 'student' table. This relationship allows for the association of courses with students, maintaining referential integrity.

💡Primary Key

A primary key is a unique identifier for each record in a database table. It cannot contain null values and ensures that each entry is distinct. In the context of the video, the 'roll number' is established as the primary key in the 'student' table, serving as a unique identifier for each student. This is crucial for the foreign key in the 'course' table to reference, ensuring that only valid student roll numbers can be associated with courses.

💡Referential Integrity

Referential integrity is a database constraint that ensures that all foreign key values must match values in the referenced primary key table. It is maintained by foreign keys and prevents data anomalies such as orphaned records. The script discusses how foreign keys, like the 'roll number' in the 'course' table, maintain referential integrity by ensuring that any value in the foreign key column must exist in the primary key column of the referenced table.

💡Composite Key

A composite key is a primary key composed of two or more attributes. It is used when a single attribute is not sufficient to uniquely identify a record. The script mentions that a primary key can be simple, consisting of one attribute, or composite, consisting of multiple attributes. This concept is important for understanding the flexibility of primary key design and how it can be referenced by foreign keys.

💡Referencing Table

The referencing table is the table that contains a foreign key. It is the table that relies on another table's primary key for establishing a relationship. In the script, the 'course' table is the referencing table because it contains the 'roll number' foreign key that references the 'student' table's primary key. This term helps to clarify the direction of the relationship between tables.

💡Referenced Table

The referenced table is the one that contains the primary key being referenced by a foreign key in another table. It is the source of the unique identifier for the relationship. In the video script, the 'student' table is the referenced table because its 'roll number' primary key is referenced by the foreign key in the 'course' table.

💡Data Type

Data type refers to the classification of data items, such as integer, varchar, etc., which determines the type of values that can be stored in a column. The script emphasizes that the data type of a foreign key must match the data type of the primary key it references, ensuring consistency and compatibility in data storage and retrieval.

💡Constraint

A constraint in database design is a rule that defines the limitations on data in a table. Foreign keys are a type of constraint that enforces referential integrity. The script describes how to define a foreign key constraint in SQL, using 'ALTER TABLE' to add a constraint after the table has been created, which is essential for maintaining the accuracy and consistency of the database.

💡Simple Key

A simple key is a primary key that consists of a single attribute. It is used when one attribute is sufficient to uniquely identify each record in a table. The script contrasts simple keys with composite keys, noting that the 'roll number' in the 'student' table is a simple key because it uniquely identifies each student without the need for additional attributes.

💡Null Value

A null value in a database represents the absence of a value or a lack of information. The script explains that primary keys cannot contain null values, which is crucial for ensuring that each record can be uniquely identified. This concept is important for understanding data integrity and the role of primary keys in database design.

Highlights

Introduction to the concept of foreign keys in databases.

Definition of a foreign key as an attribute that references a primary key within the same or another table.

Explanation of primary keys being simple or composite.

Discussion on the importance of primary keys for unique identification in databases.

The role of foreign keys in maintaining referential integrity.

How foreign keys reference primary keys either in the same table or a different one.

Illustration of foreign key usage with an example of 'student' and 'course' tables.

Clarification that the roll number in the 'course' table is a foreign key referencing the 'student' table.

The requirement for the data type of a foreign key to match the data type of the referenced primary key.

Instructions on how to create a foreign key in Oracle using SQL commands.

The difference between referencing and referenced tables in the context of foreign keys.

Demonstration of altering an existing table to add a foreign key constraint.

Highlight that the name of the foreign key column does not have to match the primary key column.

Revelation that a table can have multiple foreign keys, unlike the single primary key constraint.

Anticipation of the second part of the video, which will cover referential integrity in more depth.

Transcripts

play00:00

Hello Friends

play00:01

Welcome to Gate Smashers

play00:03

The topic is foreign key in database.

play00:05

So, in this video we are going to discuss about foreign key

play00:09

Where what is the foreign key?

play00:10

And why it is actually used in the database?

play00:13

So before starting this video,

play00:14

I want to request from my viewers,

play00:16

Please subscribe to my channel.

play00:19

And please press the bell button,

play00:20

so that you get the latest notifications.

play00:23

So here actually I have divided the video in two parts,

play00:26

In part one we are going to discuss

play00:28

What is a foreign key?

play00:29

In Part Two, why it is actually used in database?

play00:33

So, what is foreign key,

play00:35

It is an attribute of set of attributes

play00:39

that references to Primary key of same table.

play00:42

or another table. Table means the relation.

play00:46

Means first of all, you focus on this point

play00:48

"it's attribute of set of attributes".

play00:51

Means to say.

play00:52

that primary key

play00:53

Can be simple or it can be composite.

play00:56

Means if there is a primary key in a table.

play00:59

So that primary key made up of the only one attribute.

play01:02

Or a primary key consisting of

play01:05

two or three attributes.

play01:07

So it actually means

play01:09

that primary key is a simple or whether it is a composite

play01:12

But the Main Point is

play01:13

That the primary key always reference,

play01:16

It take references from the primary key of

play01:19

Same table

play01:21

or another table, means

play01:22

Whenever Foreign key take a reference,

play01:25

whenever takes their data,

play01:27

From where it takes the reference,

play01:28

from the primary key

play01:29

and in which table the primary will be?

play01:31

Either in the same table.

play01:33

Or it is existing in another table

play01:38

So here the important point is this.

play01:41

Many times in the interviews also and often comes in the exams.

play01:45

That what is the Referential Integrity

play01:48

and which keys used to maintain the referential Integrity,

play01:52

It is foreign key.

play01:53

What is the concept of this referential integrity?

play01:56

And how Foreign key maintain it?

play01:59

We'll see this in Part Two.

play02:01

Here we are going to see first,

play02:03

what is actually a foreign key?

play02:04

So here i have taken two tables.

play02:06

There's a table here, student.

play02:08

And the second table I took here.

play02:10

course.

play02:12

So in the student table

play02:14

Let's say it is a base table.

play02:16

It is the base table.

play02:18

Suppose, it is a base table of any university or college

play02:22

where the Data of all student are present,

play02:25

where student's roll number

play02:27

Student's name

play02:28

Student's address are present.

play02:30

There's a lot of entries and rows.

play02:32

And here I have made roll number

play02:35

as primary key.

play02:37

Why do we make the primary key?

play02:39

It is unique as well as not null,

play02:41

means neither I can leave it empty

play02:44

nor can I repeat any value

play02:47

and it's benefit I have already explained.

play02:50

Ans its benefit is

play02:52

that in any university,

play02:54

if the value of any number of rows same

play02:56

If I have to uniquely identify them.

play02:59

Whose help do we always take?

play03:01

Primary key.

play03:02

Means the value of all the columns can be same

play03:05

But there will be a column in which

play03:07

there is a difference between every two.

play03:10

Why that?

play03:11

Because one is the unique value in it.

play03:13

& empty means you can't even leave it null.

play03:16

I already explain the concept of primary key

play03:20

Here we talk about, the second table is the course.

play03:23

In the course, we took the course ID,

play03:25

took the course name and took a attribute here, Roll number.

play03:28

Now here why have I taken the roll number because

play03:32

The student & course are related to each other

play03:35

Whenever you feel that

play03:36

these two tables are related to each other,

play03:38

how do we denote them?

play03:40

There will be aleast one attribute must be

play03:42

common between them.

play03:44

so that in future, I can join these two tables.

play03:48

what do I have in common in these two tables?

play03:50

Roll number.

play03:51

You can also keep the course ID here,

play03:53

that's not a problem.

play03:54

But I've kept the roll number here.

play03:57

And this roll number

play03:58

working here as a Foreign key.

play04:03

And this foreign key,

play04:05

where it is taking reference from?

play04:07

It's taking a reference from

play04:08

this primary key.

play04:10

What does it mean to take a reference?

play04:12

What is the meaning of the reference

play04:14

that here in this table

play04:17

Like new students are coming

play04:19

New students are taking admission here,

play04:21

and I'm putting their data.

play04:23

New courses are coming.

play04:24

Their course name like course 1, course 2, course 3, course 4.

play04:27

There is a lot of data, that too, I'm entering it

play04:29

But whenever I have to put data in this column.

play04:32

I have to put very carefully,

play04:35

because where is the value of this column being referenced from?

play04:39

From here on,

play04:40

if I ask a simple question, Can I write

play04:43

the Roll number 10?

play04:46

No,

play04:47

Because roll number 10 should be in this table first.

play04:51

If it exist in this table.

play04:53

then obviously roll numbers 10 can be in this table as well.

play04:56

because where are this tables taking the referencing from?

play04:59

From this table,

play05:00

where are this foreign table takimg reference from?.

play05:02

From the primary key of this table,

play05:05

so we called this table,

play05:07

Referencing in tables. Referencing.

play05:16

And we called this table

play05:17

Referenced.

play05:25

Referenced Table,

play05:27

means the table in which primary key,

play05:29

We call it the referenced table or the base table.

play05:32

and in the table,

play05:33

in which foreign key exist,

play05:35

we call ot referencing table,

play05:37

who is taking the reference in the table.

play05:39

And I have denoted here that

play05:41

how we make foreign key in the oracle.

play05:44

If in the oracle or

play05:45

normally in a sequel, if I'm writing a command.

play05:48

And I'm using my platform of oracle.

play05:50

Create Table Course.

play05:53

We are making this tables

play05:54

in which we took the course ID, let say varchar

play05:57

We also took the course name Varchar

play05:59

The main point here is the roll number,

play06:00

I have to make the roll number as a foreign key

play06:02

So roll number, integer,

play06:04

this data type should be equal to

play06:06

the data type of the primary key,

play06:09

The data type of the primary key is used here,

play06:11

the same data type is to be used here,

play06:13

then write references.

play06:16

After references, table of primary key,

play06:19

that table name where primary key exist.

play06:22

Which table is that, student.

play06:24

And in the bracket put primary key,

play06:26

which primary key is in that table?

play06:28

The roll number.

play06:29

So we used this thing here.

play06:32

I represented it this case, because in case,

play06:34

If there is a coding, let say like

play06:36

if a query is written,

play06:37

then you can also note it down like this.

play06:40

And if already table is made

play06:42

This is when we are making the table,

play06:45

during the creation.

play06:46

How can we apply foreign key?

play06:48

If after the table created

play06:49

means the table has already made

play06:51

& now I have to put the foreign key.

play06:53

So the query for that is Alter

play06:57

Table.

play06:58

Let say, take course

play07:00

Alter, Table, Table Name, Course.

play07:02

Add

play07:04

Constraints

play07:07

Given the Name to Constraint, let's say FK

play07:11

Name anything,

play07:12

Actually foreign key is a constraints.

play07:14

there is a rule.

play07:15

Add, Constraint, foreign key, FK

play07:18

Which constraint is this?

play07:19

Foreign key,

play07:22

Foreign key,

play07:23

And who are you creating the foreign key?

play07:25

Roll number of that table

play07:28

Who are you making of that table?

play07:30

Roll number

play07:31

and where will it take the reference from?

play07:34

It will take the reference.

play07:36

Reference.

play07:38

References.

play07:39

What is the name of the primary table, student.

play07:42

And in that student, which is the primary key?

play07:45

Roll number. So this is how,

play07:47

we write a query.

play07:49

After the table is created. Alter, Table,

play07:51

Table Name, Add, Constraint, constraint name, foreign key,

play07:54

Which you have to make roll number,

play07:57

references- student, the table of primary key.

play08:00

And what is the primary key in that table.

play08:04

Now let me tell you a small point here,

play08:06

that in both these tables I took the roll number, roll number.

play08:09

It doesn't mean that their name should be same.

play08:12

you take some other names here.

play08:14

That is not a problem & the second point is

play08:17

Can a table have more than one foreign key?

play08:20

Yes, a table have more than one foreign key.

play08:23

but primary key is always one in a table.

play08:26

So this is all about the part one,

play08:28

where what is foreign key,

play08:29

we have discussed.

play08:30

In part two, we will talk about

play08:32

the referential integrity,

play08:34

that how foreign key maintain

play08:36

Referential integrity

play08:38

in the referenced table as well as

play08:39

in the referencing table?

play08:41

Thank you.

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Database ManagementForeign KeyReferential IntegrityData RelationshipsSQL TutorialOracle SQLData IntegrityPrimary KeyData ModelingDatabase Design
¿Necesitas un resumen en inglés?