UNIT-2
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
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.
[Link]: It is nothing but a single row of a table, which contains a single record.
[Link] 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.
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of
information.
o 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.
o Thus, integrity constraint is used to guard against accidental damage to the
database.
Types of Integrity Constraint
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of
valuesfor an attribute.
o 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.
2. Entity integrity constraints
o The entity integrity constraint states that primary key value can't be null.
o 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.
o A table can contain a null value other than the primary key field.
Example:
3. Referential Integrity Constraints
o A referential integrity constraint is specified between two tables.
o Referential integrity constraints is base on the concept of Foreign Keys. A foreign key
is an important attribute of a relation which should be referred to in other
relationships.
o Referential integrity constraint state happens where relation refers to a key attribute
of a different or same relation. However, that key element must exist in the table.
Example:
In the above example, we have 2 relations, Customer and Billing.
Tuple for CustomerID =1 is referenced twice in the relation Billing. So we know Customer
Name=Google has billing amount $300
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 Algebra
Relational Algebra is a procedural query language used to query the database tables to
access data in different ways. It consists of a set of operations that take one or two relations
as input and produce a new relation as their result.
In relational algebra, input is a relationand output is also a relation.
Fundamental relational algebra operations:
The primary operations that we can perform using relational algebra are:
1. Select
2. Project
3. Union
4. Set Different
5. Cartesian product
6. Rename
STUDENT STUDENT_SPORTS
ROLL_NO SPORTS
ROLL_NO NAME GENDER AGE 1 Badminton
1 RAM MALE 19
2 Cricket
2 RAMESH MALE 18
2 Badminton
3 NISHA FEMALE 20
4 Badminton
4 DIYA FEMALE 17
EMPLOYEE
EMP_NO NAME GENDER AGE
1 RAM MALE 18
5 RAMESH MALE 22
6 SWETHA FEMALE 21
4 DIYA FEMALE 18
1. Select Operation (σ)
This is used to fetch rows/tuples from table/relation which satisfies a given condition.
Syntax: σp(r)
Where, σ represents the Select Predicate, r is the name of relation, and p is the predicate,
where we specify the conditions that must be satisfied by the data. In predicate, one can
use unary and binary operators like =, <, > etc, to specify the conditions.
To fetch data for students with age more than 18.
σage > 18 (Student)
This will fetch the tuples from table Student, for which age will be greater than 18.
The result is and,or operators etc can also be used to specify two conditions. for example,
ROLL_NO NAME GENDER AGE
1 RAM MALE 19
3 NISHA FEMALE 20
σage > 18 and gender = 'Male' (Student)
This will return tuples(rows) from table Student with information of male students, of age
more than 18.
ROLL_NO NAME GENDER AGE
1 RAM MALE 19
2. Project Operation (∏)
Project operation is a unary operation which is used to project only a certain set of
attributes of a relation. It removes duplicate data from the columns. Projection is denoted
by the uppercase Greek letter pi (∏).
Syntax: ∏A1, A2...(r)
where A1, A2 etc are attribute names(column names).
For example,
∏Name, Age(Student)
Above statement will show us only the Name and Age columns for all the rows of data
in Student table.
The result is
NAME AGE
RAM 19
RAMESH 18
NISHA 20
DIYA 17
3. Union Operation (U)
The Union operation is used to fetch data from two relations. For this operation to work, the
relations specified should have same number of attributes(columns) and same attribute
domain. Also the duplicate tuples are automatically eliminated from the result. The U
symbol is used to represent this operation.
Syntax: A U B
where A and B are relations.
For example, to display the name of both the students and employees from STUDENT and
EMPLOYEE relation, then
∏NAME(STUDENT) U ∏NAME(EMPLOYEE)
The above eliminates repeating names.
The result is
NAME
RAM
RAMESH
NISHA
DIYA
SWETHA
4. Set Difference (-)
This operation is used to find data present in first relation and not in the second relation.
The – symbol is used to represent this operation. This operation is also applicable on two
relations, just like Union operation. For this operation to work, the relations specified should
have same number of attributes(columns) and same attribute domain.
Syntax: A - B
where A and B are relations.
For example, to find the name of students who are not employees, we can use the below
operation:
∏ NAME (STUDENT) - ∏NAME(EMPLOYEE)
The result is
NAME
NISHA
5. Cartesian Product (X)
This is used to combine data from two different relations into one and fetch data from the
combined relation. The cross (X) symbol is used to represent this operation.
For every row of Relation1, each row of Relation2 is concatenated. If Relation1 has m tuples
and and Relation2 has n tuples, cross product of Relation1 and Relation2 will have m X n
tuples.
Syntax: A X B
For example, if we want to find the information for Regular Class and Extra Class which are
conducted during morning, then, we can use the following operation:
Example:
EMPLOYEE DEPARTMENT
EMP_ID EMP_NAME EMP_DEPT
DEPT_NO DEPT_NAME
1 Smith A
A Marketing
2 Harry C
B Sales
Input: EMPLOYEE X DEPARTMENT
The result is
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
1 Smith A A Marketing
1 Smith A B Sales
2 Harry C A Marketing
2 Harry C B Sales
6. Rename Operation (ρ)
The rename operation allows us to rename the output relation. 'rename' operation is
denoted with small Greek letter rho ρ.
Syntax: ρ (NewRelation, OldRelation)
ρ x (E)
Where the result of expression E is saved with name of x.
ρ (STUDENT_NAMES, ∏name (STUDENT))
The result is
NAME
RAM
RAMESH
NISHA
DIYA
Additional Relational Algebra Opearations
1. Set Intersection Operator (∩)
Intersection operator is denoted by ∩ symbol and it is used to select common rows from
two relations.
Let us say we have two relations R1 and R2 both have same columns and we want to select
all those rows that are present in both the relations, then in that case we can apply
intersection operation on these two relations R1 ∩ R2.
Syntax: R ∩ S
R and S are relations
Example:
To display the names of those who appear in both STUDENT relation and EMPLOYEE relation
∏ NAME (STUDENT) ∩ ∏NAME(EMPLOYEE)
The result is
NAME
RAM
RAMESH
DIYA
2. The Natural-Join operation:
The natural join is a binary operation that allows us to combine certain selections and a
Cartesian product into one operation. It is denoted by the join symbol ⋈
The name and type of the attribute must be same.
Example:
Consider the following two tables:
C
D
Num Square
Num Cube
2 4 2 8
3 9
3 27
C⋈D
Num Square Cube
2 4 8
3 9 27
3. Assignment Operation:
It is similar to assignment operator in programming languages. The assignment operation is
denoted by ←. The result of the expression to the right of the ← is assigned to the relation
variable on the left of the←.
temp1 ← R × S
Extended additional algebra operations:
1. Generalized Projection:
Generalized projection extends the projection operation by allowing operations such as
arithmetic and string functions to be used in the projection list. The generalized-projection
operation has the form:
∏F1,F2,...,Fn (E)
where E is any relational-algebra expression, and each of F1, F2, . . . , Fn is an arithmetic
expression involving constants and attributes.
For example, the expression:
∏ID,name,dept name,salary÷12(instructor)
gives the ID, name, dept name, and the monthly salary of each instructor.
2. Aggregate function:
Aggregate functions take a collection of values and return a single value as a result. For
example, the aggregate function sum takes a collection of values and returns the sum of the
values. The relational-algebra operation signifies that aggregation is to be applied, and its
subscript specifies the aggregate operation to be applied.
To find out the sum of salaries of all employees, the query is:
sum(salary)(emp )
The following query finds the average salary in each department.
dept name average(salary) (emp)
the resultant relation will have tuples in the emp relation grouped by the deptname
attribute.
Join in DBMS:
• A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
• 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.
3. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with missing
information.
Types of SQL JOIN
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
Table name: EMPLOYEE
EMP_ID EMP_NAME CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36
6 Marry Canada 600000 48
PROJECT
PROJECT_NO EMP_ID DEPARTMENT
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables as long as
the condition is satisfied.
It returns the combination of all rows from both the tables where the condition satisfies.
Syntax
SELECT [Link], [Link]
FROM table1 INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME, [Link] FROM EMPLOYEE
INNER JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table and the matching values from the right
table. If there is no matching join value, it will return NULL.
Syntax
SELECT table1.column1, table1.column2
FROM table1 LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME,
[Link] FROM EMPLOYEE LEFT
JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of right table and the
matched values from the left table. If there is no matching in both tables, it will return NULL.
Syntax
Syntax
SELECT table1.column1,
table1.column2 FROM
table1 RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME,
[Link] FROM EMPLOYEE RIGHT
JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right
outer join. Join tables have all the records from both tables. It puts NULL
on the place of matches not found.
Student_Name Course
Robert Databases
Robert Programming Languages
David Databases
David Operating Systems
Hannah Programming Languages
Hannah Machine Learning
Tom Operating Systems
Syntax
SELECT table1.column1,
table1.column2 FROM
table1 FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME,
[Link] FROM EMPLOYEE
FULL JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
4. NULL values:
Null values present special problems in relational operations. The result of an arithmetic
expression is null if any of the input values is null. Comparing anything to null really doesn’t
have much meaning, so we create a new “boolean value” in this case — unknown, meaning
that we really can’t say whether a
comparison to null is true or false.
Formal Definition of the Relational Algebra:
A general expression in the relational algebra is constructed out of smaller sub expressions.
Let E1 and E2 be relational-algebra expressions. Then, the following are all relational-
algebra expressions:
• E1 U E2
• E1 − E2
• E1 × E2
• σP(E1), where P is a predicate on attributes in E1
• ∏ S(E1), where S is a list consisting of some of the attributes in E1
• ρ x (E1), where x is the new name for the result of E1
Modification of the database
Deletion:
A delete request is expressed in much the same way as a query. We can delete only whole
tuples; we cannot delete values on only particular attributes. In relation algebra, a deletion
is expressed by
r r – E, where r is a relation and E is a relational-algebra expression.
Example:
To delete all of student id 100’s records
student student – σ sno=100(student)
To delete all loans with amount in the range 0 to 1000
loan loan - σ amount≥0 and amount≤1000(loan)
Insertion:
To insert data into a relation, we either specify a tuple to be inserted or write a querywhose
result is a set of tuples to be [Link], the attribute values for inserted tuples
must be members of the corresponding attribute’s domain.
Similarly, tuples inserted must have the correct number of attributes.
Insertion is expressed by
r r U E, where r is a relation and E isa relational-algebra expression.
Example;
To insert a record in to the account table
Account Account U {104,”Pondy”, 20000}
Updation:
In certain situations, we may wish to change a value in a tuple without changing all values in
the tuple. For this purpose, the update statement can be used.
Here, updation is represented by
r ∏F1,F2.....,Fn(r)
where r is relation, Fi is ith attribute to be updated.
Example:
To add interest rate 5% to the balance in a bank account
Account Account ∏accnum,bname,balance * 1.05(Account)
Relational Calculus
Relational Calculus in non-procedural query language and has no description about how the
query will work or the data will be fetched. It only focuses on what to do, and not on how to
do it.
Relational Calculus exists in two forms:
1. Tuple Relational Calculus
2. Domain Relational Calculus
Tuple Relational Calculus (TRC) in DBMS
Tuple Relational Calculus (TRC) is a non-procedural query language used in relational
database management systems (RDBMS) to retrieve data from tables. TRC is based on the
concept of tuples, which are ordered sets of attribute values that represent a single row or
record in a database table.
TRC is a declarative language, meaning that it specifies what data is required from the
database, rather than how to retrieve it. TRC queries are expressed as logical formulas that
describe the desired tuples.
Syntax: The basic syntax of TRC is as follows:
{ t | P(t) }
where t = resulting tuples,
P(t) = known as Predicate and these are the conditions that are used to fetch t. Thus, it
generates a set of all tuples t, such that Predicate P(t) is true for t.
P(t) may have various conditions logically combined with OR (∨), AND (∧), NOT(¬).
It also uses quantifiers:
∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that predicate Q(t) is true.
∀ t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.
For example, let’s say we have a table called “Employees” with the following attributes:
Employee ID
Name
Salary
Department ID
To retrieve the names of all employees who earn more than $50,000 per year, we can use
the following TRC query:
{ t | Employees(t) ∧ [Link] > 50000 }
In this query, the “Employees(t)” expression specifies that the tuple variable t represents a
row in the “Employees” table. The “∧” symbol is the logical AND operator, which is used to
combine the condition “[Link] > 50000” with the table selection.
The result of this query will be a set of tuples, where each tuple contains the Name attribute
of an employee who earns more than $50,000 per year.
TRC can also be used to perform more complex queries, such as joins and nested queries, by
using additional logical operators and expressions.
While TRC is a powerful query language, it can be more difficult to write and understand
than other SQL-based query languages, such as Structured Query Language (SQL). However,
it is useful in certain applications, such as in the formal verification of database schemas and
in academic research.
Tuple Relational Calculus Examples
Table Customer
Customer name Street City
Saurabh A7 Patiala
Mehak B6 Jalandhar
Sumiti D9 Ludhiana
Ria A5 Patiala
Table Branch Table Account
Branch name Branch City Account Branch
number name Balance
ABC Patiala
1111 ABC 50000
DEF Ludhiana
1112 DEF 10000
GHI Jalandhar
1113 GHI 9000
1114 ABC 7000
Table Loan
Loan Branch
number name Amount
L33 ABC 10000
L35 DEF 15000
GHI 9000
L49
L98 DEF 65000
Table Borrower Table Depositor
Customer name Loan number
Customer name Account number
Saurabh L33
Saurabh 1111
Mehak L49
Mehak 1113
Ria L98
Suniti 1114
Table Depositor
Example 1: Find the loan number, branch, and amount of loans greater than or equal to
10000 amount.
{t| t ∈ loan ∧ t[amount]>=10000}
Resulting relation:
Loan number Branch name Amount
L33 ABC 10000
L35 DEF 15000
L98 DEF 65000
In the above query, t[amount] is known as a tuple variable.
Example 2: Find the loan number for each loan of an amount greater or equal to 10000.
{t| ∃ s ∈ loan(t[loan number] = s[loan number]
∧ s[amount]>=10000)}
Resulting relation:
Loan number
L33
L35
L98
Example 3: Find the names of all customers who have a loan and an account at the bank.
{t | ∃ s ∈ borrower( t[customer-name] = s[customer-name])
∧ ∃ u ∈ depositor( t[customer-name] = u[customer-name])}
Resulting relation:
Customer name
Saurabh
Mehak
Example 4: Find the names of all customers having a loan at the “ABC” branch.
{t | ∃ s ∈ borrower(t[customer-name] = s[customer-name]
∧ ∃ u ∈ loan(u[branch-name] = “ABC” ∧ u[loan-number] = s[loan-number]))}
Resulting relation:
Customer name
Saurabh
Formal definition of tuple relational calculus:
A tuple-relational-calculus expression is of the form:
{t|P(t)}
where P is a formula. Several tuple variables may appear in a formula. A tuple variable is
said to be a free variable unless it is quantified by a ∃ or ∀.Thus, in
t ϵ loan ∧ ∃s ϵ customer(t[bname] = s[bname])
t is a free variable and s is said to be a bound variable.
A tuple-relational-calculus formula is built up out of atoms. An atom has one of the
following forms:
• s ∈ r, where s is a tuple variable and r is a relation
• s[x] О u[y], where s and u are tuple variables, x is an attribute on which sis defined,
y is an attribute on which u is defined, and О is a comparison operator (<, ≤, =, _=, >,
≥); we require that attributes x and y have domains whose members can be
compared by О
• s[x] О c, where s is a tuple variable, x is an attribute on which s is defined, О is a
comparison operator, and c is a constant in the domain of attribute x.
We build up formulae from atoms by using the following rules:
• An atom is a formula.
• If P1 is a formula, then so are ¬P1 and (P1).
• If P1 and P2 are formulae, then so are P1 ∨ P2, P1 ∧ P2, and P1 ⇒ P2.
• If P1(s) is a formula containing a free tuple variable s, and r is a relation, then ∃ s ∈ r
(P1(s)) and ∀ s ∈ r (P1(s)) are also formulae.
Domain Relational Calculus:
Domain relational calculus, uses domain variables that take on values from an attributes
domain, rather than values for an entire tuple. In domain relational calculus, filtering is
done based on the domain of the attributes and not based on the tuple values.
Syntax: { <x1, x2, x3, ..., xn | F(x1, x2, x3, ... ,xn)}
where, x1, x2... etc represents domain of attributes(columns) and F defines the formula
including the condition for fetching the data.
Example:
To find loan number, branch name and amount for loans of over Rs. 1200
{<l,b,a>| <l,b,a> ∈ loan ∧ a > 1200}
To find all loan numbers for loans with an amount greater than Rs. 1200
{<l>|∃ b,a ( <l,b,a> ∈ loan ∧ a > 1200)}
Formal Definition:
An expression in the domain relational calculus is of the form
{< x1, x2, . . . , xn > | P(x1, x2, . . . , xn)}
where x1, x2, . . . , xn represent domain variables. P represents a formula composed of
atoms.
An atom in the domain relational calculus has one of the following forms:
• < x1, x2, . . . , xn > ∈ r, where r is a relation on n attributes and x1, x2, . . . , xn are
domain variables or domain constants.
• x О y, where x and y are domain variables and О is a comparison operator (<, ≤, =, _=,
>, ≥).
• x О c, where x is a domain variable, О is a comparison operator, and c is a constant in
the domain of the attribute for which x is a domain variable.
We build up formulae from atoms by using the following rules:
• An atom is a formula.
• If P1 is a formula, then so are ¬P1 and (P1).
• If P1 and P2 are formulae, then so are P1 ∨ P2, P1 ∧ P2, and P1 ⇒ P2.
• If P1(x) is a formula in x, where x is a free domain variable, then ∃ x (P1(x)) and ∀ x
(P1(x)) are also formulae.