Join Type and Calculas

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

Database Language

Database Language
• A DBMS has appropriate languages and
interfaces to express database queries and
updates.
• Database languages can be used to read, store
and update the data in the database.
Types of Database Language
Data Definition Language (DDL)
• DDL is used for specifying the database schema. It is used for
creating tables, schema, indexes, constraints etc. in database. Lets
see the operations that we can perform on database using DDL:
• To create the database instance – CREATE
• To alter the structure of database – ALTER
• To drop database instances – DROP
• To delete tables in a database instance – TRUNCATE
• To rename database instances – RENAME
• To drop objects from database such as tables – DROP
• To Comment – Comment
• All of these commands either defines or update the database
schema that’s why they come under Data Definition language.
Data Manipulation Language (DML)
• DML is used for accessing and manipulating
data in a database. The following operations
on database comes under DML:
• To read records from table(s) – SELECT
• To insert record(s) into the table(s) – INSERT
• Update the data in table(s) – UPDATE
• Delete all the records from the table – DELETE
Data Control language (DCL)
• DCL is used for granting and revoking user
access on a database –
• To grant access to user – GRANT
• To revoke access from user – REVOKE
Transaction Control Language(TCL)
• The changes in the database that we made
using DML commands are either performed or
rollbacked using TCL.
• To persist the changes made by DML
commands in database – COMMIT
• To rollback the changes made to the database
– ROLLBACK
CREATE DATABASE Statement
• Syntax:
We use CREATE DATABASE statement in order
to create a database. This is how it is used:
• CREATE DATABASE DBName;Here DBName can
be any string that would represent the
database name.
• Example – The below statement would create
a database named employee
Query Languages
• Language in which user requests information from the
database.
• Categories of languages
– procedural
– non-procedural
• “Pure” languages:
– Relational Algebra
– Tuple Relational Calculus
– Domain Relational Calculus
• Pure languages form underlying basis of query languages
that people use.
Relational Algebra
• Procedural language
• Relational operators are of two types:
• Traditional set Special operators
– Union - Selection
– Intersection - Projection
– set difference - Join
– Cartesian product - Division
• The operators take one or more relations as
inputs and give a new relation as a result.
Traditional Operators
• Union: in mathematical set theory union of
two sets is the set of elements belonging to
both sets. It is denoted by U.
S1={1,2,3,4}
S2={5,6,7,8} the union of these two would be
S3={1,2,3,4,5,6,7,8} Cust_nam Cust_stat
e us
Cust_name Cust_Status Cust_name Cust_Status Rahul Good
Rahul Good Raman Average
Sumit Excellent
Sumit Excellent Rahul Good
Raman Average
Traditional Operators
• Intersection: in mathematics intersection of
two sets produces a set containing common
elements of both sets

Cust_name Cust_Status Cust_name Cust_Status Cust_name Cust_status


Rahul Good Raman Average Rahul Good
Sumit Excellent Rahul Good
Traditional Operators
Cust_name Cust_Status Cust_name Cust_Status
Rahul Good Raman Average
Sumit Excellent Rahul Good

A B
A-B= Cust_name Cust_status
Sumit Excellent

Cust_name Cust_status
B-A= Raman Average
Cartesian-Product Operation-Example
Select Operation – Example
Select Operation ()
• Notation:  p(r)
• p is called the selection predicate
• Defined as:
p(r) = {t | t  r and p(t)}
Where p is a formula in propositional calculus consisting of
terms connected by :  (and),  (or),  (not)
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, , >, . <. 
• Example of selection:
 branch-name=“Perryridge”(account)
Project Operation – Example
Project Operation
• Notation:

A1, A2, …, Ak (r)


where A1, A2 are attribute names and r is a relation name.
• The result is defined as the relation of k columns obtained
by erasing the columns that are not listed
• Duplicate rows removed from result, since relations are sets
• E.g. To eliminate the branch-name attribute of account
account-number, balance (account)
Joins
• Join in DBMS is a binary operation which
allows you to combine join product and
selection in one single statement. The goal of
creating a join condition is that it helps you to
combine the data from two or more DBMS
tables. The tables in DBMS are associated
using the primary key and foreign keys.
EMP_CODE EMP_NAME
⋈ EMP_CODE SALARY

101 Stephan 101 50000

102 Jack 102 30000

103 25000
103 Harry

Employee Salary
Operation: (EMPLOYEE ⋈ SALARY)
EMP_CODE EMP_NAME SALARY

101 Stephan 50000

102 Jack 30000

103 Harry 25000


Types of join
Natural Join
• A natural join is the set of tuples of all combinations in R and S
that are equal on their common attribute names.
• It is denoted by ⋈.
• Example: Let's use the above EMPLOYEE table and SALARY
table:
• ∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
∏EMP_NAME, SALARY
(EMPLOYEE ⋈ SALARY)
EMP_CODE SALARY
EMP_CODE EMP_NAME
101 50000
101 Stephan
102 30000
102 Jack
103 25000
103 Harry

Employee Salary

EMP_NAME SALARY

Stephan 50000
Jack 30000
Harry 25000
Outer Join
• The outer join operation is an extension of the
join operation. It is used to deal with missing
information.
Types of outer join
• Left outer join
• Right outer join
• Full outer join
(EMPLOYEE ⋈ FACT_WORKERS)
Employee
EMP_NAME STREET CITY

Ram Civil line Mumbai

Shyam Park street Kolkata

Ravi M.G. Street Delhi

Hari Nehru nagar Hyderabad

EMP_NAME BRANCH SALARY

Ram Infosys 10000


Shyam Wipro 20000
Kuber HCL 30000
Hari TCS 50000 Output
EMP_NAME STREET CITY BRANCH SALARY
Fact_workers
Ram Civil line Mumbai Infosys 10000
Shyam Park street Kolkata Wipro 20000
Hari Nehru nagar Hyderabad TCS 50000
Left outer join
• Left outer join contains the set of tuples of all combinations in
R and S that are equal on their common attribute names.
• In the left outer join, tuples in R have no matching tuples in S.
• It is denoted by ⟕.
• 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


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

EMP_NAME BRANCH SALARY STREET CITY

Ram Infosys 10000 Civil line Mumbai


Shyam Wipro 20000 Park street Kolkata
Hari TCS 50000 Nehru Hyderabad
street
Kuber HCL 30000 NULL NULL
Full outer join
• Full outer join is like a left or right join except that it contains all rows from
both tables.
• 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.
• It is denoted by ⟗.
• 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
Kuber NULL NULL HCL 30000
Equi join
• It is also known as an inner join. It is the most common join. It
is based on matched data as per the equality condition. The
equi join uses the comparison operator(=).
CLASS_ID NAME PRODUCT_ID CITY

1 John 1 Delhi
2 Harry 2 Mumbai
3 Jackson 3 Noida

CLASS_ID NAME PRODUCT_ID CITY

1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida

CUSTOMER ⋈ PRODUCT
Relational Calculus
• Relational calculus is a non-procedural query
language. In the non-procedural query language, the
user is concerned with the details of how to obtain
the end results.
• The relational calculus tells what to do but never
explains how to do.
Types of Relational calculus
Tuple Relational Calculus (TRC)
• The tuple relational calculus is specified to select the
tuples in a relation. In TRC, filtering variable uses the
tuples of a relation.
• The result of the relation can have one or more
tuples.
• Notation
• {T | P (T)} or {T | Condition (T)}
Tuple Relational Calculus (TRC)
• Query to display the last name of those students where age is
greater than 30
• { t.Last_Name | Student(t) AND t.age > 30 }

• Query to display all the details of students where Last name is


‘Singh’
• { t | Student(t) AND t.Last_Name = 'Singh' }
Domain Relational Calculus (DRC)
• The second form of relation is known as Domain relational
calculus. In domain relational calculus, filtering variable uses
the domain of attributes.
• Domain relational calculus uses the same operators as tuple
calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓
(not).
• It uses Existential (∃) and Universal Quantifiers (∀) to bind the
variable.
Domain Relational Calculus (DRC)
• {< First_Name, Age > | ∈ Student ∧ Age > 27}

• Output
Difference between relational algebra and
calculus
S.NO Relational Algebra Relational Calculus
While Relational Calculus is
1. It is a Procedural language.
Declarative language.

Relational Algebra means how to While Relational Calculus means what


2.
obtain the result. result we have to obtain.

In Relational Algebra, The order is


While in Relational Calculus, The order
3. specified in which the operations
is not specified.
have to be performed.

Relational Algebra is independent on While Relation Calculus can be a


4.
domain. domain dependent.

Relational Algebra is nearer to a While Relational Calculus is not nearer


5.
programming language. to programming language.

You might also like