UNIT-2pdf
UNIT-2pdf
The table name and column names are helpful to interpret the meaning of values in each row.
The data are represented as a set of relations. In the relational model, data are stored as tables.
However, the physical storage of the data is independent of the way the data are logically
organized.
.For example: In Student table, ID is used as a key because it is unique for each student. In
PERSON table, passport_number, license_number, SSN are keys since they are unique for each
person.
Types of key:
1. Primary key
o It is the first key which is used to identify one and only one instance of an entity
uniquely. An entity can contain multiple keys as we saw in PERSON table. The key
which is most suitable from those lists become a primary key.
o In the EMPLOYEE table, ID can be primary key since it is unique for each employee. In
the EMPLOYEE table, we can even select License_Number and Passport_Number as
primary key since they are also unique.
2. Super Key
Super key is a set of attribute which can uniquely identify a tuple. Super key is a superset of a
candidate key. A super key is a set of one of more columns (attributes) to uniquely identify rows
in a table. A Super key may have additional attributes that are not needed for unique
identification.
3. Candidate key
o A candidate key is an attribute or set of an attribute which can uniquely identify a tuple.
o A column or a set of columns can be called as candidate key if they identify each row of
a table uniquely. A table can have multiple candidate keys. One of them is specified as
Primary key and rest of them can be called as alternate key.
o A super key with no redundant attribute is known as candidate key
For example: In the EMPLOYEE table, id , SSN, Passport_Number, and License_Number, etc.
are considered as a candidate key.
Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to
uniquely identify the student record in the table.
4. Foreign key
o Foreign keys are the column of the table which is used to point to the primary key of
another table.
o In a company, every employee works in a specific department, and employee and
department are two different entities. So we can't store the information of the department
in the employee table. That's why we link these two tables through the primary key of
one table.
o We add the primary key of the DEPARTMENT table, Department_Id as a new attribute
in the EMPLOYEE table.
o Now in the EMPLOYEE table, Department_Id is the foreign key, and both the tables are
related.
Other Keys
5. Alternate key
All the keys which are not primary key are called as alternate key. It is a candidate key which is
currently not the primary key.
Example: In this table, StudID, Roll No, Email are qualified to become a primary key. But since
StudID is the primary key, Roll No, Email becomes the alternative key.
6. Composite key
A key which has multiple attributes to uniquely identify rows in a table is called a composite
key.
7. Surrogate Key
An artificial key which aims to uniquely identify each record is called a surrogate key. These
kind of key are unique because they are created when you don't have any natural primary key.
They do not lend any meaning to the data in the table. Surrogate key is usually an integer.
Above, given example, shown shift timings of the different employee. In this example, a
surrogate key is needed to uniquely identify each employee.
Summary
• The Relational database model represents the database as a collection of relations (tables)
• Attribute, Tables, Tuple, Relation Schema, Degree, Cardinality, Column, Relation
instance, are some important components of Relational Model
• Relational Integrity constraints are referred to conditions which must be present for a
valid relation
• Domain constraints can be violated if an attribute value is not appearing in the
corresponding domain or it is not of the appropriate data type
• Insert, Select, Modify and Delete are operations performed in Relational Model
• The relational database is only concerned with data and not with a structure which can
improve the performance of the model
• Advantages of relational model is simplicity, structural independence, ease of use, query
capability, data independence, scalability.
• Few relational databases have limits on field lengths which can't be exceeded.
Relational Algebra
Relational algebra is a procedural query language. It gives a step by step process to obtain the
result of the query. It uses operators to perform queries.
1. Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).
1. Notation: σ p(r)
Where:
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
o This operation shows the list of those attributes that we wish to appear in the result. Rest
of the attributes are eliminated from the table.
o It is denoted by ∏.
Where
Input:
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
1. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
1. ρ(STUDENT1, STUDENT)
SET OPERATIONS
2. Union Operation:
o Suppose there are two relations R and S. The union operation contains all the tuples that
are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
1. Notation: R ∪ S
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
1. ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
3. Set Intersection:
o Suppose there are two relations R and S. The set intersection operation contains all tuples
that are in both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
CUSTOMER_NAME
Smith
Jones
4. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in R but not in S.
o It is denoted by intersection minus (-).
1. Notation: R - S
Input:
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
5. Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the
other table. It is also known as a cross product.
o It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
Join Operations:
A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.
Inner Join:
• Theta join
• EQUI join
• Natural join
Outer join:
• Left Outer Join
• Right Outer Join
• Full Outer Join
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
INNER JOIN
1. Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are equal on their
common attribute names.
o It is denoted by ⋈.
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
Output:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
Notation R1 θ R2
Example:
Example, suppose we want to buy a mobile and a laptop, based on our budget we have thought
of buying both such that mobile price should be less than that of laptop. Look at the tables below,
Now, we have considered the condition as the cost of the mobile should be less than that of
laptop so our resulting table will have only those tuples that satisfy this condition.
AFTER JOINS
MOBILE LAPTOP (MOBILE.PRICE < LAPTOP.PRICE)
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn) such that the
attributes don’t have anything in common, that is R1 ∩ R2 = Φ. Theta join can use all kinds of
3. Equi join:
It is based on matched data as per the equality condition. The equi join uses the comparison
operator(=).When Theta join uses only equality comparison operator, it is said to be
equijoin. The above example corresponds to equijoin.
Student
101 Alex 10
102 Maria 11
Subjects
Class Subject
10 Math
10 English
11 Music
11 Sports
Student_Detail
STUDENT ⋈ SUBJECT (Student.Std = Subject.Class)
Student_detail
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with missing
information. An inner join includes only those tuples with matching attributes and the rest are
discarded in the resulting relation. Therefore, we need to use outer joins to include all the tuples
from the participating relations in the resulting relation.
Example:
EMPLOYEE
FACT_WORKERS
Input:
1. EMPLOYEE FACT_WORKERS
Input:
1. EMPLOYEE FACT_WORKERS
Output:
1. EMPLOYEE FACT_WORKERS
Output:
Operation Purpose
Inner Join Inner join, includes only those tuples that satisfy
the matching criteria.
Outer Join In an outer join, along with tuples that satisfy the
matching criteria.
Left Outer Join( ) In the left outer join, operation allows keeping
all tuple in the left relation.
Right Outer join( ) In the right outer join, operation allows keeping
all tuple in the right relation.
Full Outer Join( )++9 In a full outer join, all tuples from both relations
are included in the result irrespective of the
matching condition.