CHAPTER 3(DBMS)
CHAPTER 3(DBMS)
Relational Model
Definitions and Terminology
Relational model is today a primary data model in which database is represented as a
collection of Relation where each relation is represented by a two dimensional table.
The relational model was first proposed by E.F. Codd in 1970.
Because of its simplicity, the relational model is now the dominant model for commercial
data processing operation.
Consider a case we wish to store the name, the CGPA attained, and the roll number of all
the students of a particular class. This structured data can be easily stored in a table as
described below.
Relational database is a collection of organized set of tables related to each other, and
from which data can be accessed easily.
A Relational Database management System (RDBMS) is a database management system
based on the relational model .It is used to manage Relational database.
Examples of RDBMS are Oracle, MySQL, Microsoft SQL Server, PostgreSQL etc.
As discussed earlier, a relational database is based on the relational model. This database
consists of various components based on the relational model. These include:
Tuple: Each row of a table is known as record. It is also known as tuple. For example, the
following row is a record that we have taken from the above table.
1 anish 3.56
Attribute: Column of the relation, depicting properties that define the relation.
Eg. Roll_Number, Name, CGPA
Domain: A domain is a set of permitted values for an attribute in table. For example, a
domain of CGPA must be in the range of 0 to 4.
Relation Schema: A relation schema defines the structure of the relation and represents
the name of the relation with its attributes. e.g. student (Roll_Number, Name, CGPA) is the
relation schema for student. If a schema has more than 1 relation, it is called Relational
Schema.
Relational Instance: It is the collection of records present in the relation at a given time.
Above table shows the relation instance of student at a particular time. It can change
whenever there is an insertion, deletion, or update in the database.
Degree: It is the total number of attributes present in the relation.eg. The Student relation
defined above has degree 3.
Cardinality: It specifies the number of entities involved in the relation i.e., it is the total
number of rows present in the relation. The student relation defined above has cardinality
4.
1. Fundamental Operations
The fundamental operations selection, projection and rename on one relation so they
called unary operations.
Others operations union, set difference and Cartesian product operates on
pairs of relations and so called binary operations.
σdepartment=”computer”(employee)
output
σsalary>70000(employee)
output
Find all the employees with name sita and department is mechanical
σname= “sita”^department=”mechanical”(employee)
emp_id name department salary
4 sita mechanical 32000
Find all the employees whose department is either civil or computer and name is not nikita
∏name,salary(employee)
output
name salary
ramesh 68000
krishna 75000
rita 65000
sita 32000
juna 85000
nikita 60000
anish 55000
name salary
ramesh 68000
anish 55000
output
name department
sita mechanical
nikita computer
anish civil
customer_name account_no
ram A-101
sita A-105
hari A-205
nishant A-405
gita A-505
borrower
customer_name loan_no
aditya L-224
nishant L-185
mahesh L-150
gita L-174
ronish L-145
Output
customer_name
ram
sita
hari
nishant
gita
Aditya
Mahesh
ronish
Example:
Find name of all customer of the bank who have account but not loan
∏customer_name(depositor) - ∏customer_name(borrower)
customer_name
ram
sita
hari
Example:
Relation Employee
emp_id name
1 anish
2 sita
3 nitesh
Relation Department
dept_id dept_name
1 computer
2 civil
3 electrical
Employee X Department
emp_id name dept_id dept_name
1 anish 1 computer
1 anish 2 civil
1 anish 3 electrical
2 nitesh 1 computer
2 nitesh 2 civil
2 nitesh 3 electrical
3 sita 1 computer
3 sita 2 civil
3 sita 3 electrical
Relation borrower
customer_name loan_number
X L01
Y L02
Relation loan
Query: Find all customer who taken loan from branch “B1”.
Process:
borrower×loan
σ branch_name=”B1”(borrower×loan)
σ borrower.loan_number=loan.loan_number(σ branch_name=”B1”(borrower×loan))
customer_name
X
Example 2:
Rename the relation named customer
ρnewcustomer(customer)
Here relation named customer can be renamed as newcustomer.
Example 3:
Relation named can also be renamed as well their attributes also
ρc(cid,cname,ccity) (Customer)
Her relation name customer is renamed as c and their attributes
customer_id,customer_name,customer_city are renamed as cid,cname and ccity respectively.
customer_name
nishant
gita
2.2 Division
The division operator takes two relations and builds another relation consisting of values of
an attribute of one relation that matches all the values in another relation
Division operator r÷s or r/s can be applied if and only if, Attributes of s is proper subset of
Attributes of r.
The relation returned by division operator will have attributes = (All attributes of r – All
Attributes of s)
Example 1:
K X Y
1 A 2
1 B 4
2 A 2 X Y
3 B 4 A 2
4 B 4 B 4
3 A 2
Relation r Relation s
Example 2:
Consider the following relation
subject course
subject_name course_name course_name
DBMS CMP CMP
C++ ELX ELX
C++ CMP
OS CMP
subject_name
C++
Example: Find all customer who taken loan from bank as well as has bank account.
temp1←∏customer_name(borrower)
temp2←∏customer_name(depositor)
result← temp1 ∩ temp2
Course
R ⋈ <condition> S
Where R and S are the relations to be joined, and <condition> represents the equality condition on
the common attribute(S).
student ⋈ Student.stu_id = Course.stu_id course
The resulting relation includes only the tuples that have matching "stu_id" values in both the
"Student" and "Course" relations.
2. Natural Join
Natural join automatically matches and combines tuples from two relations based on the
common attribute(s) with the same name.
In example given below, the common attribute is "stu_id".
Natural join does not use any comparison operator
The name and type of the attribute must be same.
It eliminates duplicate attributes in the result.
It is denoted by ⋈
Student ⋈ course
The resulting relation includes only the tuples that have matching attribute values with the same
name ("stu_id" in this case).
The resulting relation includes all tuples from the left relation ("Student") and the matching tuples
from the right relation ("Course"). The NULL values in the "cname" and "fee" columns indicate that
there is no match for the corresponding tuples in the "Course" relation.
Student ⟗ Course
The resulting relation includes all tuples from both the left relation ("Student") and the right
relation ("Course"). The NULL values indicate that there is no match for the corresponding tuples
in either relation.
3.2 Aggregation
The aggregate operation permits the use of aggregate functions such as min ,average etc.
on set of values.
Aggregation function takes a collection of values and returns a single value as a result.
Some aggregate functions are
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
department salary
Civil 80000
Computer 70000
IT 52000
Deletion
A delete request is expressed similarly to a query, except instead of displaying tuples to the
user, the selected tuples are removed from the database.
Can delete only whole tuples; cannot delete values on only particular attributes
A deletion is expressed in relational algebra by:
rr–E
where r is a relation and E is a relational algebra query.
Let us consider the following relation
Employee(employee_id,name,department,salary)
Example 1: Delete information of all employee from civil department
Employee ← Employee- σ department=”civil” (Employee)
Example 2:
Delete all records of employee with salary in the range 25000 to 60000
Insertion
To insert data into relation we can either specify a tuple to be inserted or write a query
whose result is a set of tuples to be inserted.
In relational-algebra, an insertion is express by r←r∪ E
where r is a relation and E is a relational algebra expression
Example:
Insert the information of employee whose employee id is 10, named “rikesh” working in civil
department and having salary 50000
Updating
Updating allow to change a value in a tuple without changing all values in tuple. In
relational algebra, updating express by
r←∏ F1,F2, . . ,Fn(r)
(Employee-name=“ram”(Employee))
Note:
update operation will be in another form as well
Database schema
Database schema is a logical design of a database and the database instance is a snapshot of the
data in the database at a given instant in time.
The concept of a relation corresponds to the programming language notation of a variable
While the concept of a relation schema corresponds to the programming language notation
of type definition. In general a relation schema consists of list of attributes and their
corresponding domains.
The concept of a relation instance corresponds to the programming –language notation of a value
of a variable. The value of a given variable may change with time; similarly the contents of a
relation instance may change with time as the relation is updated. In contrast, the schema of a
relation does not generally change.
Each course in a university may be offered multiple times, across different semesters, or even within a
semester. We need a relation to describe each individual offering, or section, of the class.
The schema is section (course id, sec id, semester, year, building, room number, time slot id)
We need a relation to describe the association between instructors and the class sections that they teach.
The relation schema to describe this association is
As we can imagine, there are many more relations maintained in a real university database.
Now, all the database schemas for university database can be listed as
instructor(id,name,dept_name,salary)
course(course_id,title,dept_name,credits)
department (dept_name, building, budget)
section (course_id, sec_id, semester, year, building, room_number, time_slot_id)
teaches (ID, course_id, sec_id, semester, year)
student (ID, name, dept_name, tot_cred)
advisor (s_id, i_id)
prereq(course_id,prereq_id)
takes (ID, course_id, sec_id, semester, year, grade)
classroom (building, room_number, capacity)
time_slot (time_slot_id, day, start_time, end_time)
A database schema, along with primary key and foreign key dependencies, can be depicted by
schema diagrams. Figure given below shows the schema diagram for university organization. Each
relation appears as a box, with the relation name at the top in blue, and the attributes listed inside
the box. Primary key attributes are shown underlined. Foreign key dependencies appear as arrows
from the foreign key attributes of the referencing relation to the primary key of the referenced
relation.
Assignment
Consider the following relations for a database that keeps track of student enrollment in courses
and the books adopted for each course:
STUDENT(SSN,Name,Major,Bdate)
COURSE(Course#,Cname,Dept)
ENROLL(SSN,Course#,Quarter,Grade)
BOOK_ADOPTION(Course#,Quarter,Book_ISBN)
TEXT(Book_ISBN,Book_Title,Publisher,Author)
Draw a relational schema diagram specifying the foreign keys for this schema.
Employee(emp_id,emp_name,postion,salary,dept_id)
Department(dep_id,dept_name,location,budget)
Consider a person who needs to know information of employees with name, position and
department name but not salary as well as other information, then this person should see a
relation described, in the relational algebra, by
emp_name,postion,dept_name(Employee ⨝ Department)
But in such cases views can be created.
Views definition
A view is defined using the create view statement which has the form
create view v as <query expression>
where <query expression> is any legal relational algebra query expression.
The view name is represented by v.
Once a view is defined, the view name can be used to refer to the virtual relation that the
view generates.
View definition is not the same as creating a new relation by evaluating the query
expression
Rather, a view definition causes the saving of an expression; the expression is substituted
into queries using the view.
Consider the view (named all_employees) consisting of emp_name, position and department
Since we allow a view name to appear wherever a relation name is allowed, the person may write:
Assignment:
How relational algebra is different from relational calculus? Define Tuple Relational
Calculus and Domain Relational Calculus.
(Refer: Text Book “Database system concept” by Abraham Silberschatz )