Relational Model & Relational Algebra
Relational Model & Relational Algebra
Semester – III
● Relational key: In the relational key, each row has one or more
attributes. It can identify the row in the relation uniquely.
Relational Databases
DOB
• Like if there is no EmployeeID or SSN columns,
then you can make EmployeeName +
DateOfBirth (DOB) as Composite Primary Key.
DeptID
DOB
EmailID
DOB
Integrity Rules
Integrity constraints are used to ensure accuracy and consistency of the data in
a relational database.
Database integrity refers to the validity and consistency of stored data.
Integrity constraints are a set of rules.
It is used to maintain the quality of information.
○ 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.
Integrity Constraints
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.
Integrity Constraints
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.
Integrity Constraints
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.
Relational Algebra
• The relational algebra is a procedural query language
• It consists of a set of operations that take one or two relations as
input and produce a new relation as their result.
• It uses operators to perform queries.
39
Selection Condition
<condition> OR <condition>
NOT <condition>
40
Selection Condition - Examples
σ Id>3000 Or Hobby=‘hiking’ (Person)
σ Hobby≠‘hiking’ (Person)
41
Selection Condition - Examples
• Select the EMPLOYEE tuples whose department number is 4:
σ DNO = 4 (EMPLOYEE)
• Select the employee tuples whose salary is greater than
$30,000:
σ (EMPLOYEE)
SALARY > 30,000
Example:
Person ΠName,Hobby(Person)
47
Example
• “Find the name of all instructors in the Physics
department.
π name (σ dept name =“Physics”(instructor))
RENAME
Rename operator returns an existing relation under a new name.
• Union
• Intersection
• Set Difference / Minus
UNION
• It is a Binary operation, denoted by ∪
52
UNION
Example
Consider the following relations
▪Borrower (customer-name, loan-number)
▪Depositor (customer-name, account-number)
▪Customer (customer-name, street-number, customer-city)
Q. List all the customers who have both a loan and an account.
Q. Find the names of all customers who have an account but not a
loan.
■ r x s:
DIVISION OPERATOR
SYNTAX: (Query1) ÷ (Query2)
R1 ÷ R2
DIVISION OPERATOR Example
Aggregate Functions
Aggregation function takes a collection of values and returns a single value as
a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in relational algebra
G1, G2, …, Gn
g F1( A1), F2( A2),…, Fn( An) (E)
E is any relational-algebra expression
G1, G2 …, Gn is a list of attributes on which to group (can be empty)
a a 7
a b 7
b b 3
b b 10
sum-C
g sum(c) (r)
27
Aggregate Operation – Example
branch-name
g sum(balance)
(account)
branch-name balance
Perryridge 1300
Brighton 1500
Redwood 700
JOIN OPERATOR
JOIN is used to combine related tuples from two relations
•In its simplest form the JOIN operator is just the cross product of the two relations.
•As the join becomes more complex, tuples are removed within the cross product to
make the result of the join more meaningful.
•JOIN allows you to evaluate a join condition between the attributes of the relations o
which the join is undertaken
Notation
R1 ⋈θ R2
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 = Φ
● Computes the join and then adds tuples form one relation that do not
match tuples in the other relation to the result of the join.
● Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Outer Join – Example 2
Inner Join
loan borrower
inprotected.com