Dbms
Dbms
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.
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.
1. Equi Join
2. Natural Join
Now let us learn about these inner joins one-by-one.
1. Equi Join
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.
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 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:
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.
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.
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.