UNIT-II Relational Model in DBMS
UNIT-II
Relational Model in DBMS
Relational model can represent as a table with columns
and rows. Each row is known as a tuple. Each table of the column has a name or
attribute.
Domain: It contains a set of atomic values that an
attribute can take.
Attribute: It contains the name of a column in a particular
table. Each attribute Ai must have a domain, dom(Ai)
Relational instance: In the relational database system, the
relational instance is represented by a finite set of tuples. Relation
instances do not have duplicate tuples.
| 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 | 
Relational schema: A relational schema contains the name of the
relation and name of all columns or attributes.
Relational key: In the relational key, each row has one or more
attributes. It can identify the row in the relation uniquely.
Example: STUDENT Relation
| NAME | ROLL_NO | PHONE_NO | ADDRESS | AGE | 
| Ram | 14795 | 7305758992 | Noida | 24 | 
| Shyam | 12839 | 9026288936 | Delhi | 35 | 
| Laxman | 33289 | 8583287182 | Gurugram | 20 | 
| Mahesh | 27857 | 7086819134 | Ghaziabad | 27 | 
| Ganesh | 17282 | 9028
  9i3988 | Delhi | 40 | 
- In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are
     the attributes.
- The instance of schema STUDENT has 5 tuples.
- t3 = <Laxman, 33289, 8583287182, Gurugram, 20>
Properties of Relations
- Name of the relation is distinct from all other relations.
- Each relation cell contains exactly one atomic (single) value
- Each attribute contains a distinct name
- Attribute domain has no significance
- tuple has no duplicate value
- Order of tuple can have a different sequence
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

1. Select Operation:
- The
     select operation selects tuples that satisfy a given predicate.
- 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
Backward
Skip 10sPlay VideoForward Skip 10s
Input:
1.     
σ BRANCH_NAME="perryride" (LOAN)  
Output:
| BRANCH_NAME | LOAN_NO | AMOUNT | 
| Perryride | L-15 | 1500 | 
| Perryride | L-16 | 1300 | 
2. Project Operation:
- 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.
- 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 | 
3. Union Operation:
- Suppose
     there are two tuples R and S. The union operation contains all the tuples
     that are either in R or S or both in R & S.
- It
     eliminates the duplicate tuples. It is denoted by ∪.
1.     
Notation: R ∪ S   
A union operation must hold the following condition:
- R
     and S must have the attribute of the same number.
- 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 | 
4. Set Intersection:
- Suppose
     there are two tuples R and S. The set intersection operation contains all
     tuples that are in both R & S.
- 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 | 
5. Set Difference:
- Suppose
     there are two tuples R and S. The set intersection operation contains all
     tuples that are in R but not in S.
- 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 | 
6. Cartesian product
- 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.
- 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 | 
7. 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)  
oin Operations:
A Join operation combines related tuples from different
relations, if and only if a given join condition is satisfied. It is denoted by
⋈.
Example:
EMPLOYEE
| EMP_CODE | EMP_NAME | 
| 101 | Stephan | 
| 102 | Jack | 
| 103 | Harry | 
SALARY
| EMP_CODE | SALARY | 
| 101 | 50000 | 
| 102 | 30000 | 
| 103 | 25000 | 
1.     
Operation: (EMPLOYEE ⋈ SALARY)   
Result:
Backward
Skip 10sPlay VideoForward Skip 10s
| EMP_CODE | EMP_NAME | SALARY | 
| 101 | Stephan | 50000 | 
| 102 | Jack | 30000 | 
| 103 | Harry | 25000 | 
Types of Join operations:

1. 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:
Input:
1.     
∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)  
Output:
| EMP_NAME | SALARY | 
| Stephan | 50000 | 
| Jack | 30000 | 
| Harry | 25000 | 
2. Outer Join:
The outer join operation is an extension of the join
operation. It is used to deal with missing information.
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 | 
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 nagar | Hyderabad | TCS | 50000 | 
An outer join is basically of three types:
- Left
     outer join
- Right
     outer join
- Full
     outer join
a. 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 ⟕.
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:
- 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 ⟖.
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:
- 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 ⟗.
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 | 
3. 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(=).
Example:
CUSTOMER RELATION
| CLASS_ID | NAME | 
| 1 | John | 
| 2 | Harry | 
| 3 | Jackson | 
PRODUCT
| PRODUCT_ID | CITY | 
| 1 | Delhi | 
| 2 | Mumbai | 
| 3 | Noida | 
Input:
1.     
CUSTOMER ⋈ PRODUCT    
Output:
| CLASS_ID | NAME | PRODUCT_ID | CITY | 
| 1 | John | 1 | Delhi | 
| 2 | Harry | 2 | Mumbai | 
| 3 | Harry | 3 | Noida | 
Integrity Constraints
- Integrity
     constraints are a set of rules. It is used to maintain the quality of
     information.
- Integrity
     constraints ensure that the data insertion, updating, and other processes
     have to be performed in such a way that data integrity is not affected.
- Thus,
     integrity constraint is used to guard against accidental damage to the
     database.
Types of Integrity
Constraint

1. Domain constraints
- Domain
     constraints can be defined as the definition of a valid set of values for
     an attribute.
- The
     data type of domain includes string, character, integer, time, date,
     currency, etc. The value of the attribute must be available in the
     corresponding domain.
Example:

2. Entity integrity
constraints
- The
     entity integrity constraint states that primary key value can't be null.
- This
     is because the primary key value is used to identify individual rows in
     relation and if the primary key has a null value, then we can't identify
     those rows.
- A
     table can contain a null value other than the primary key field.
Example:

3. Referential Integrity
Constraints
- A
     referential integrity constraint is specified between two tables.
- In
     the Referential integrity constraints, if a foreign key in Table 1 refers
     to the Primary Key of Table 2, then every value of the Foreign Key in
     Table 1 must be null or be available in Table 2.
Example:

4. Key constraints
- Keys
     are the entity set that is used to identify an entity within its entity
     set uniquely.
- An
     entity set can have multiple keys, but out of which one key will be the
     primary key. A primary key can contain a unique and null value in the
     relational table.
Example:

Relational Calculus
There is an alternate way of formulating queries known as
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. Most commercial relational languages are based on
aspects of relational calculus including SQL-QBE and QUEL.
Why it is called
Relational Calculus?
It is based on Predicate calculus, a name derived from
branch of symbolic language. A predicate is a truth-valued function with
arguments. On substituting values for the arguments, the function result in an expression
called a proposition. It can be either true or false. It is a tailored version
of a subset of the Predicate Calculus to communicate with the relational
database.
Many of the calculus expressions involves the
use of Quantifiers. There are two types of quantifiers:
- Universal
     Quantifiers: The
     universal quantifier denoted by ∀ is read as for
     all which means that in a given set of tuples exactly all tuples satisfy a
     given condition.
- Existential
     Quantifiers: The
     existential quantifier denoted by ∃
     is read as for all which means that in a given set of tuples there is at
     least one occurrences whose value satisfy a given condition.
Before using the concept of quantifiers in formulas, we
need to know the concept of Free and Bound Variables.
ADVERTISEMENT
A tuple variable t is bound if it is quantified which
means that if it appears in any occurrences a variable that is not bound is
said to be free.
Free and bound variables may be compared with global and
local variable of programming languages.
Types of Relational
calculus:

1. Tuple Relational
Calculus (TRC)
It is a non-procedural query language which is based on
finding a number of tuple variables also known as range variable for which
predicate holds true. It describes the desired information without giving a
specific procedure for obtaining that information. 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:
A Query in the tuple relational calculus is expressed as
following notation
1.     
{T | P (T)}   or {T | Condition (T)}     
Where
T is the resulting
tuples
P(T) is the
condition used to fetch T.
For example:
1.     
{ T.name | Author(T) AND T.article = 'database' }    
Output: This
query selects the tuples from the AUTHOR relation. It returns a tuple with
'name' from Author who has written an article on 'database'.
TRC (tuple relation calculus) can be quantified. In TRC,
we can use Existential (∃)
and Universal Quantifiers (∀).
For example:
1.     
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}  
Output: This
query will yield the same result as the previous one.
2. 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. The
QBE or Query by example is a query language related to domain relational
calculus.
Notation:
1.     
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}  
Where
a1, a2 are attributes
P stands for formula built by inner attributes
For example:
1.     
{< article, page, subject > |  ∈ javatpoint ∧ subject = 'database'}  
Output: This
query will yield the article, page, and subject from the relational javatpoint,
where the subject is a database.
 
 
Comments
Post a Comment