0% found this document useful (0 votes)
50 views19 pages

Dbms

Inner join combines records from two tables based on a common field between them. There are two types of joins - inner join and outer join. Inner join returns records that match across both tables, while outer join also returns non-matching records. Some types of joins include equi join, natural join, left outer join, right outer join, and full outer join.

Uploaded by

Prabhat Katariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views19 pages

Dbms

Inner join combines records from two tables based on a common field between them. There are two types of joins - inner join and outer join. Inner join returns records that match across both tables, while outer join also returns non-matching records. Some types of joins include equi join, natural join, left outer join, right outer join, and full outer join.

Uploaded by

Prabhat Katariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

What is Join in DBMS and what

are its types?

Join
In DBMS, a join statement is mainly used to combine two
tables based on a specified common field between them. If
we talk in terms of Relational algebra, it is the cartesian product of
two tables followed by the selection operation. Thus, we can execute
the product and selection process on two tables using a single join
statement. We can use either 'on' or 'using' clause in MySQL to apply
predicates to the join queries.

A Join can be broadly divided into two types:

1. Inner Join
2. Outer Join
For all the examples, we will consider the below-mentioned employee
and department table.
Inner Join
Inner Join is a join that can be used to return all the values
that have matching values in both the tables. Inner Join can be
depicted using the below diagram.

The inner join can be further divided into the following


types:

1. Equi Join
2. Natural Join
Now let us learn about these inner joins one-by-one.
1. Equi Join

Equi Join is an inner join that uses the equivalence


condition for fetching the values of two tables.

The MySQL query for equi join can be as follows:

Select employee.empId, employee.empName, department.deptName from employee


Inner Join department on employee.deptId = department.deptId;
The returned values for the above query is as follows:

2. Natural Join
Natural Join is an inner join that returns the values of the
two tables on the basis of a common attribute that has the
same name and domain. It does not use any comparison
operator. It also removes the duplicate attribute from the results.

The MySQL query for natural join can be as follows:

Select * from employee Natural Join department;


The above query will return the values of tables removing the
duplicates. If we want to specify the attribute names, the query will
be as follows:

Select employee.empId, employee.empName, department.deptId,


department.deptName from employee Natural Join department;
The returned values for the above two queries are as follows:
Outer Join
Outer Join is a join that can be used to return the records in
both the tables whether it has matching records in both the
tables or not.

The outer join can be further divided into three types:

1. Left-Outer Join
2. Right-Outer Join
3. Full-Outer Join
we'll learn about these outer joins one-by-one.
1. Left-Outer Join:

The Left-Outer Join is an outer join that returns all the


values of the left table, and the values of the right table that
has matching values in the left table. If there is no matching
result in the right table, it will return null values in that field. The
Left-Outer Join can be depicted using the below diagram.

The MySQL query for left-outer join can be as follows:

Select employee.empId, employee.empName, department.deptName from employee


Left Outer Join department on employee.deptId = department.deptId;
The returned values for the above query is as follows:
2. Right-Outer Join:

The Right-Outer Join is an outer join that returns all the


values of the right table, and the values of the left table that
has matching values in the right table. The Right-Outer Join
can be depicted using the below diagram.
The MySQL query for right-outer join can be as follows:

Select employee.empId, employee.empName, department.deptName from employee


Right Outer Join department on employee.deptId = department.deptId;
The returned values for the above query is as follows:
3. Full-Outer Join:

The Full-Outer join contains all the values of both the tables
whether they have matching values in them or not. The Full-
Outer Join can be depicted using the below diagram.
The MySQL query for full-outer join can be as follows:

Select * from employee Full Join department;


The returned values for the above query is as follows:
This is all about join in DBMS. Hope you learned something new
today. That's it for this blog.
What are Super key, Primary
key, Candidate key, and
Foreign keys?

Key
A key is an attribute or set of attributes which helps us in uniquely
identifying the rows of a table. It also helps in establishing
relationship among tables. We will now see how this is done with the
help of examples.

Example: If we have the details of students of a classroom stored in


Student table as follows:

Now, from this classroom, if we want to call a student whose name is


‘Andrew’ we don't know which ‘Andrew’ to call as there are two
students with the same name. Also, we if know the age of student
‘Andrew’ we can’t distinguish between both the students because
both are having the same age. So, there must some value through
which we can distinguish and uniquely identify the students. The
‘Roll_no’ attribute will help us in uniquely identifying the rows in a
table. We can say that ‘Roll_no’ is the key here. Now, if we know the
‘Roll_no’ of the student then there will be no confusion and we can
easily select the student from here.

Key can be of the following types:

 Super Key
 Candidate Key
 Primary Key
 Alternate Key
 Foreign Key

Super Key
A super key or simply key is a combination of all possible attribute
which can uniquely identify the rows(tuples) in a table. This means
that a superkey may have some extra attribute which isn't necessary
for uniquely identifying the rows in the table.

Example: In the given Student Table we can have the following keys
as the super key.
1. {Roll_no}
2. {Registration_no}
3. {Roll_no, Registration_no},
4. {Roll_no, Name}
5. {Name, Registration_no}
6. {Roll_no, Name, Registration_no}
All the above keys are able to uniquely identify each row. So, each of
these keys is super key. Here you can see that by using Roll_no only,
we can uniquely identify the rows but if you are making a super key,
then you will try to find all the possible cases of keys that can be used
to identify data uniquely.

Candidate Key
A candidate key is a minimal super key or a super key with no
redundant attribute. It is called a minimal superkey because we select
a candidate key from a set of super key such that selected candidate
key is the minimum attribute required to uniquely identify the table.
It is selected from the set of the super key which means that all
candidate keys are super key. Candidate Keys are not allowed to have
NULL values.
If the subset of the candidate key is a super key, then that
candidate key is not a valid candidate key.

Example: In the above example, we had 6 super keys but all of them
cannot become a candidate key. Only those super keys would become
a candidate key which have no redundant attributes.

1. {Roll_no}: This key doesn't have any redundant or repeating


attribute. So, it can be considered as a candidate key.
2. {Registration_no}: This key also doesn't have any repeating
attribute. So, it can be considered as a candidate key.
3. {Roll_no, Registration_no}: This key cannot be considered
as a candidate key because when we take the subset of this key
we get two attributes i.e Roll_no or Registration_no. Each of
these attributes is the candidate key. So, it is not a minimal
super key. Hence, this key is not a candidate key.
4. {Roll_no, Name}: This key cannot be considered as a
candidate key because when we take the subset of this key we
get two attributes i.e. Roll_no or Name. Roll_no is a candidate
key. So, it is not a minimal super key. Hence, this key is not a
candidate key.
5. {Name, Registration_no}: This key cannot be considered as
a candidate key because when we take the subset of this key we
get two attributes i.e Registration_no or Name. Registration_no
is a candidate key. So, it is not a minimal super key. Hence, this
key is not a candidate key.
6. {Roll_no, Name, Registration_no}: This key cannot be
considered as a candidate key because when we take the subset
of this key we get three attributes i.e Roll_no, Registration_no
and Name. Two of these attributes i.e Roll_no and
Registration_no are the candidate key. So, it is not a minimal
superkey. Hence, this key is not a candidate key.
So, from the above discussion, we conclude that we can have only 2
out of above 6 super keys as the candidate key. i.e. (Roll_no)
and(Registration_no).

Primary Key
The primary key is the minimal set of attributes which uniquely
identifies any row of a table. It is selected from a set of candidate
keys. Any candidate key can become a primary key . It
depends upon the requirements and is done by the Database
Administrator (DBA). The primary key cannot have a NULL value. It
cannot have a duplicate value.

Example: In the above example, we saw that we have two candidate


keys i.e (Roll_no) and (Registration_no). From this set, we can select
any key as the primary key for our table. It depends upon our
requirement. Here, if we are talking about class then selecting
‘Roll_no’ as the primary key is more logical instead of
‘Registrartion_no’.
Alternate Key
All the candidate key which are not a primary key are called an
alternate key.

Example: In the above example, since we have made ‘Roll_no’ as


the Primary Key our Alternate Key would be ‘Registration_no’.
Foreign Key
The foreign key of a table is the attribute which establishes the
relationship among tables. The foreign key is the attribute which
points to the primary key of another table.

Example: If we have two tables of Student and Course then we can


establish a relationship between these two tables using a foreign key.
The ‘Course_id’ in the Student table is the foreign key as it
establishes the link between the Student and Course Table. So, if we
need to find the information about any course opted by any student
then we can go the Course table using the foreign key.
One thing that is to be noted here is that the foreign key of one table
may or may not be the primary key. But it should be the primary key
of another table. In the above example, Course_id is not a primary
key in the Student table but it is a primary key in the Course table.

You might also like