0% found this document useful (0 votes)
12 views

UNIT-2pdf

The document provides an overview of the Relational Data Model in Database Management Systems (DBMS), explaining key concepts such as attributes, tables, tuples, and various types of keys including primary, foreign, and candidate keys. It discusses the advantages and disadvantages of using the relational model, as well as the operations performed in relational algebra, including selection, projection, and set operations. Additionally, it outlines join operations and their types, emphasizing the procedural nature of relational algebra for querying databases.

Uploaded by

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

UNIT-2pdf

The document provides an overview of the Relational Data Model in Database Management Systems (DBMS), explaining key concepts such as attributes, tables, tuples, and various types of keys including primary, foreign, and candidate keys. It discusses the advantages and disadvantages of using the relational model, as well as the operations performed in relational algebra, including selection, projection, and set operations. Additionally, it outlines join operations and their types, emphasizing the procedural nature of relational algebra for querying databases.

Uploaded by

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

Relational Data Model in DBMS – UNIT - II

What is Relational Model?


The relational model represents the database as a collection of relations. A relation is nothing but
a table of values. Every row in the table represents a collection of related data values. These rows
in the table denote a real-world entity or relationship.

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.

Some popular Relational Database management systems are:

• DB2 and Informix Dynamic Server - IBM


• Oracle and RDB – Oracle
• SQL Server and Access - Microsoft

Relational Model Concepts(Structure)


1. Attribute: Each column in a Table. Attributes are the properties which define a relation.
e.g., Student_Rollno, NAME, etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is stored
along with its entities. A table has two properties rows and columns. Rows represent
records and columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its
attributes.
5. Degree: The total number of attributes which in the relation is called the degree of the
relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the RDBMS system.
Relation instances never have duplicate tuples.
9. Relation key - Every row has one, two or multiple attributes, which is called relation
key.
10. Attribute domain – Every attribute has some pre-defined value and scope which is
known as attribute domain
Keys in DBMS
A DBMS key is an attribute or set of an attribute which helps you to identify a row(tuple) in a
relation(table). They allow you to find the relation between two tables. Keys help you uniquely
identify a row in a table by a combination of one or more columns in that table.

.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.

Rules for defining Primary key:


• Two rows can't have the same primary key value
• The primary key field cannot be null.
• The value in a primary key column can never be modified or updated if any foreign key
refers to that primary key.

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.

For example: In the above EMPLOYEE table, for(EMPLOEE_ID, EMPLOYEE_NAME) the


name of two employees can be the same, but their EMPLYEE_ID can't be the same. Hence, this
combination can also be a key.

The super key would be EMPLOYEE-ID, (EMPLOYEE_ID, EMPLOYEE-NAME), etc.

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.

StudID Roll No First Name LastName Email

1 11 Tom Price [email protected]

2 12 Nick Wright [email protected]

3 13 Dana Natan [email protected]

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.

Fname Lastname Start Time End Time

Anne Smith 09:00 18:00

Jack Francis 08:00 17:00

Anna McLean 11:00 20:00

Shown Willam 14:00 23:00

Above, given example, shown shift timings of the different employee. In this example, a
surrogate key is needed to uniquely identify each employee.

Surrogate keys are allowed when

• No property has the parameter of the primary key.


• In the table when the primary key is too big or complicated.

Advantages of using Relational model


• Simplicity: A relational data model is simpler than the hierarchical and network model.
• Structural Independence: The relational database is only concerned with data and not
with a structure. This can improve the performance of the model.
• Easy to use: The relational model is easy as tables consisting of rows and columns is
quite natural and simple to understand
• Query capability: It makes possible for a high-level query language like SQL to avoid
complex database navigation.
• Data independence: The structure of a database can be changed without having to
change any application.
• Scalable: Regarding a number of records, or rows, and the number of fields, a database
should be enlarged to enhance its usability.
Disadvantages of using Relational model
• Few relational databases have limits on field lengths which can't be exceeded.
• Relational databases can sometimes become complex as the amount of data grows, and
the relations between pieces of data become more complicated.
• Complex relational database systems may lead to isolated databases where the
information cannot be shared from one system to another.

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.

Types of Relational operation


What is Relational Algebra?
Relational algebra is a procedural query processing language that provides the base of
relational databases and SQL. Relational algebra has various operators like select, project,
rename, cartesian product, union, intersection, set difference, joins, etc. These operators
are used to form queries in relational algebra.
What are Unary Operations in Relational Algebra?
The operations that operate on only one relation are called unary operations in relational
algebra. The three unary operations in relational algebra are:
• Selection
• Projection
• Rename

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:

σ is used for selection prediction


r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR and NOT.
These relational can use as relational operators like =, ≠, ≥, <, >, ≤.

For example: LOAN Relation

BRANCH_NAME LOAN_NO AMOUNT

Downtown L-17 1000

Redwood L-23 2000

Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500


Roundhill L-11 900

Perryride L-16 1300

Input:

1. σ BRANCH_NAME="perryride" (LOAN)

Output:

BRANCH_NAME LOAN_NO AMOUNT

Perryride L-15 1500

Perryride L-16 1300

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 ∏.

1. Notation: ∏ A1, A2, An (r)

Where

A1, A2, A3 is used as an attribute name of relation r.

Example: CUSTOMER RELATION

NAME STREET CITY

Jones Main Harrison

Smith North Rye


Hays Main Harrison

Curry North Rye

Johnson Alma Brooklyn

Brooks Senator Brooklyn

Input:

1. ∏ NAME, CITY (CUSTOMER)

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

A union operation must hold the following condition:

o R and S must have the attribute of the same number.


o Duplicate tuples are eliminated automatically.
Example:

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

Example: Using the above DEPOSITOR table and BORROW table

Input:

1. ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)


Output:

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

Example: Using the above DEPOSITOR table and BORROW table

Input:

1. ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)

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

EMP_ID EMP_NAME EMP_DEPT

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:

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME

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 ⋈.

Types of Join operations:

Various forms of join operation are:

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:

1. ∏ EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)

Output:

EMP_NAME SALARY

Stephan 50000
Jack 30000

Harry 25000

2. Theta (θ) Join


Theta join combines tuples from different relations provided they satisfy the theta condition.
here the comparison operators (≤, ≥, ˂, ˃, =, ̚ )come into picture.
The join condition is denoted by the symbol θ.

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

SID Name Std

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

SID Name Std Class Subject

101 Alex 10 10 Math

101 Alex 10 10 English

102 Maria 11 11 Music

102 Maria 11 11 Sports

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

EMP_NAME STREET CITY

Ram Civil line Mumbai


Shyam Park street Kolkata

Ravi M.G. Street Delhi

Hari Nehru nagar Hyderabad

FACT_WORKERS

EMP_NAME BRANCH SALARY

Ram Infosys 10000

Shyam Wipro 20000

Kuber HCL 30000

Hari TCS 50000

An outer join is basically of three types:

a. Left outer join


b. Right outer join
c. Full outer join

a. Left outer join:


o Left outer join contains the set of tuples of all combinations in R and S that are equal on
their common attribute names.
o In the left outer join, tuples in R have no matching tuples in S.
o It is denoted by .

Example: Using the above EMPLOYEE table and FACT_WORKERS table

Input:

1. EMPLOYEE FACT_WORKERS

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000


Shyam Park street Kolkata Wipro 20000

Hari Nehru street Hyderabad TCS 50000

Ravi M.G. Street Delhi NULL NULL

b. Right outer join:


o Right outer join contains the set of tuples of all combinations in R and S that are equal on
their common attribute names.
o In right outer join, tuples in S have no matching tuples in R.
o It is denoted by .

Example: Using the above EMPLOYEE table and FACT_WORKERS Relation

Input:

1. EMPLOYEE FACT_WORKERS

Output:

EMP_NAME BRANCH SALARY STREET CITY

Ram Infosys 10000 Civil line Mumbai

Shyam Wipro 20000 Park street Kolkata

Hari TCS 50000 Nehru street Hyderabad

Kuber HCL 30000 NULL NULL

c. Full outer join:


o Full outer join is like a left or right join except that it contains all rows from both tables.
o In full outer join, tuples in R that have no matching tuples in S and tuples in S that have
no matching tuples in R in their common attribute name.
o It is denoted by .

Example: Using the above EMPLOYEE table and FACT_WORKERS table


Input:

1. EMPLOYEE FACT_WORKERS

Output:

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru street Hyderabad TCS 50000

Ravi M.G. Street Delhi NULL NULL

Kuber NULL NULL HCL 30000

Operation Purpose

Select(σ) The SELECT operation is used for selecting a


subset of the tuples according to a given
selection condition

Projection(π) The projection eliminates all attributes of the


input relation but those mentioned in the
projection list.

Union Operation(∪) UNION is symbolized by symbol. It includes all


tuples that are in tables A or in B.

Set Difference(-) - Symbol denotes it. The result of A - B, is a


relation which includes all tuples that are in A
but not in B.

Intersection(∩) Intersection defines a relation consisting of a set


of all tuple that are in both A and B.

Cartesian Product(X) Cartesian operation is helpful to merge columns


from two relations.

Inner Join Inner join, includes only those tuples that satisfy
the matching criteria.

Theta Join(θ) The general case of JOIN operation is called a


Theta join. It is denoted by symbol θ.

EQUI Join When a theta join uses only equivalence


condition, it becomes a equi join.

Natural Natural join can only be performed if there is a


common attribute (column) between the
relations.

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.

You might also like