DBMS Class Notes
DBMS Class Notes
LECTURER NOTES
ON
1 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
TOPIC ONE: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS
Data
In simple words data can be facts related to any object in consideration, e.g. your name, age,
height, weight, etc. are some data related to you. A picture, image, file, pdf etc. can also be
considered data.
Database
Database is a collection of related data. Databases support storage and manipulation of data.
.
Database Management System (DBMS)
It is a software system that allows users to define, create maintain a database and provides
controlled access to the data. DBMS is basically a collection of programs that enables users
to store, modify and extract information from a database as per the requirements.
COMPONENTS OF DBMS
A database management system (DBMS) consists of several components. Each component
plays very important role in the database management system environment. The major
components of database management system are:
• Software
• Hardware
• Data
• Procedures
• Users
Hardware: The hardware is the actual computer system used for keeping and accessing the
database. It consists of a set of physical electronic devices such as computers (together with
associated I/O devices like disk drives), storage devices, I/O channels, electromechanical
devices that make interface between computers and the real-world systems etc.
Software: The main component of a DBMS is the software. It is the set of programs used to
handle the database and to control and manage the overall computerized database. DBMS
software itself, is the most important software component in the overall system.
Data: The main purpose of DBMS is to process the data. In DBMS, databases are defined,
constructed and then data is stored, updated and retrieved to and from the databases.
Procedures: Procedures refer to the instructions and rules that help to design the database
and to use the DBMS. The users that operate and manage the DBMS require documented
procedures on how use or run the database management system.
2 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Users: The users are the people who manage the databases and perform different operations
on the databases in the database system e.g. Application Programmers, Data Administrators
Database Administrators, End-Users etc.
TYPES OF DBMS
The major types of DBMS are:
Hierarchical - this type of DBMS employs the "parent-child" relationship of storing data.
This type of DBMS is rarely used nowadays. Its structure is like a tree with nodes
representing records and branches representing fields
Network DBMS - this type of DBMS supports many-to many relations. This usually results
in complex database structures. RDBM Server is an example of a database management
system that implements the network model.
Relational DBMS - this type of DBMS defines database relationships in form of tables, also
known as relations. Unlike network DBMS, RDBMS does not support many to many
relationships. Relational DBMS usually have pre-defined data types that they can support.
This is the most popular DBMS type in the market. Examples of relational database
management systems include MySQL, Oracle, and Microsoft SQL Server database.
Object Oriented Relation DBMS - this type supports storage of new data types. The data to
be stored is in form of objects. The objects to be stored in the database have attributes (i.e.
gender, age) and methods that define what to do with the data. PostgreSQL is an example of
an object-oriented relational DBMS.
1990’s to date: Object Oriented model and other Modern Database Management
Systems are being researched
3 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
File-based System
File-based systems were an early attempt to computerize the manual filing system. File-
based system is a collection of application programs that perform services for the end-users.
Each program defines and manages its data.
Disadvantages
1. Separation and isolation of data
When data is isolated in separate files, it is more difficult for us to access data that should be
available. The application programmer is required to synchronize (harmonize) the processing
of two or more files to ensure the correct data is extracted.
2. Duplication of data
When employing the decentralized file-based approach, the uncontrolled duplication of data
can easily occur.
Database Approach:
In order to overcome the limitations of the file-based approach, the concept of database and
the Database Management System (DBMS) emerged in 60s.
Advantages
A number of advantages of applying database approach in application system are obtained
including:
2. Sharing of data
The data held in the database can be shared by different users. Different users can access
the same data in the database for their own processing applications. This eliminates the
need to duplicate data.
4. Data consistency
By eliminating or controlling redundancy, the database approach reduces the risk of
inconsistencies occurring. It ensures all copies of the data are kept consistent.
5. Improved security
4 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Database approach provides a protection of the data from the unauthorized users. It may take
the term of user names and passwords to identify user type and their access right in the
operation including retrieval, insertion, updating and deletion.
6. Improved maintenance
Database approach provides a data independence. As a change of data structure in the
database will not affect the application program, therefore simplifies database application
maintenance.
Disadvantages
In split of a large number of advantages can be found in the database approach, it is not
without any challenge. The following disadvantages can be found including:
1. Complexity
Database management system is an extremely complex piece of software. All memory as
well as a large number amount of disk space in order to make it run parties must be familiar
with its functionality and take full advantage of it. Therefore, training for the administrators,
designers and users is required.
2. Size
The database management system consumes a substantial amount of main memory
efficiently.
3. Cost of DBMS
A multi-user database management system may be very expensive. Even after the
installation, there is a high recurrent annual maintenance cost on the software.
Database systems:
A database system consists of a database, DBMS and a data model. To build a database
system we must use the facilities of some DBMS to define the data structures (scheme) of the
database. We must use the facilities of some DBMS to define integrity constraints and then
populate the database with data.
5 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
DBMS Architecture:
There are three level or layers of DBMS architecture:
1. External level
2. Conceptual level
3. Internal level
1. External level: This is a highest level of abstraction as seen by user. This level of
abstraction describes only the part of entire database. It is based on the conceptual model, is
the end user view of data environment. Each external view described by means of a schema
called an external schema or subschema.
2. Conceptual level: At this level of database abstraction all the database entities and the
relationships among them are included. One conceptual view represents the entire database.
The conceptual schema defines the theoretic view.
3. Internal (physical) level: This lowest level of abstraction. It closest to physical storage
device. It describes how data are actually stored on the storage medium.
Data Independence:
The ability to modify a schema definition in one level without affecting a schema definition
in a higher level is called data independence.
6 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
▪ The ability to modify the physical schema without causing application
programs to be rewritten
▪ Modifications at this level are usually to improve performance
2. Logical data independence
▪ The ability to modify the conceptual schema without causing
application programs to be rewritten
▪ Usually done when logical structure of database is altered
Logical data independence is harder to achieve as the application programs are usually
heavily dependent on the logical structure of the data. An analogy is made to abstract data
types in programming languages.
1. Database Administrators (DBA): The DBA is responsible for authorizing access to the
database, for coordinating and monitoring its use and for acquiring software and hardware
resources as needed. These are the people, who maintain and design the database daily.
2. Database Designers: are responsible for identifying the data to be stored in the database
and for choosing appropriate structures to represent and store this data.
3. End Users: are the people whose jobs require access to the database for querying,
updating and generating reports.
a) Casual End users: These people occasionally access the database, but they may need
different information each time.
b) Naive or Parametric End Users: Their job function revolves around constantly
querying and updating the database using standard types of queries and updates.
c) Sophisticated End Users: These include Engineers, Scientists, Business analyst and
others familiarize to implement their applications to meet their complex requirements.
7 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
d) Stand-alone End users: These people maintain personal databases by using ready-
made program packages that provide easy to use menu-based interfaces.
4. System Analyst:
These people determine the requirements of end users and develop specifications for
transactions.
2. Tool Developers: Include persons who design and implement tools consisting the
packages for design, performance monitoring, and prototyping and test data generation.
Centralized Database
A database that all data is located at a single computer (or site) and multiple users can access
the database i s known as centralized database. A centralized database provides an efficient
way to access and update data. These databases are usually used in computer network
environments. The examples of centralized databases are:
• Personal Computer Databases
• Client/Server Databases
• Central Computer Databases
The client/server architecture is designed for the distribution of work on a computer network
in which many clients may share the data (or services). Here is an example of client/server
database
8 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
(iii) Central Computer Databases
The central computer databases are commonly used in central computers in large
organizations. The central computer may be a mainframe or minicomputer. These databases
are accessed by a large number of users. The users at remote locations can also access the
database using remote terminals and data communication links.
Distributed Database
Many organizations/departments have sub-offices in different cities and countries. In such
cases, the distributed databases are used instead of centralized databases. A distributed
database is a single logical database, which is spread physically across computers in multiple
locations (such as cities or countries).
Homogeneous Databases
The homogeneous database means that the database technology is the same at each of the
locations (or sites) and that the data at various locations are also compatible. In a
homogeneous system, all nodes use the same hardware and software for the database system.
The following conditions must be satisfied for homogeneous database.
9 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
1. The operating system used at each location must be same or compatible.
2. The data structures used at each location must be same or compatible.
3. The database application (or DBMS) used at each location must be same or
compatible.
Heterogeneous Databases
The heterogeneous database systems are opposite to homogeneous database systems. In a
heterogeneous system, different nodes may have different hardware and software and data
structures at various nodes or locations are also compatible
10 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
TOPIC THREE: PRINCIPLES AND TECHNIQUES OF DATABASE DESIGN
Database development is just one part of the much wider field of software engineering, the
process of developing and maintaining software. A core aspect of software engineering is the
subdivision of the development process into a series of phases, or steps, each of which
focuses on one aspect of the development. The collection of these steps is sometimes referred
to as a development life cycle.
Waterfall model: It shows the process as a strict sequence of steps where the output of one
step is the input to the next and all of one step has to be completed before moving onto the
next. However, in reality there is usually some degree of refinement and feedback as the
product proceeds through the development.
We can use the above figure as a means of identifying the tasks that are required, together
with the input and output for each activity. What is important is the scope of the activities,
which can be summarized as follows:
11 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
• Implementation is the construction of a computer system according to a given design
document and taking account of the environment in which, the system will be
operating (for example specific hardware or software available for the development).
Implementation may be staged, usually with an initial system than can be validated
and tested before a final system is released for use.
• Testing compares the implemented system against the design documents and
requirements specification and produces an acceptance report or, more usually, a list
of errors and bugs that require a review of the analysis, design and implementation
processes to correct (testing is usually the task that leads to the waterfall model
iterating through the life cycle).
• Maintenance involves dealing with changes in the requirements, or the
implementation environment, bug fixing or porting of the system to new
environments (for example migrating a system from a standalone PC to a UNIX
workstation or a networked environment). Since maintenance involves the analysis of
the changes required, design of a solution, implementation and testing of that solution
over the lifetime of a maintained software system, the waterfall life cycle will be
repeatedly revisited.
12 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
TOPIC FOUR: RELATIONAL DATABASE SYSTEM
DBMS Concepts
A Relational Database Management System (RDBMS) is a database management system
based on the relational model introduced by E.F Codd. In relational model, data is stored in
relations (tables) and is represented in form of tuples (rows).
RDBMS is used to manage Relational database which is the most commonly used database
these days. Relational database is a collection of organized set of tables related to each
other, and from which data can be accessed easily.
Table
In Relational database model, a table is a collection of data elements organized in terms of
rows and columns. A table is also considered as a convenient representation of relations.
Table is the simplest form of data storage. Below is an example of an Employee table.
Tuple
A single entry in a table is called a Tuple or Record or Row. A tuple in a table represents a
set of related data. For example, the above Employee table has 4 tuples/records/rows.
Attribute
A table consists of several records (row), each record can be broken down into several
smaller parts of data known as Attributes. The above Employee table consist of four
attributes, ID, Name, Age and Salary.
Attribute Domain
When an attribute is defined in a relation (table), it is defined to hold only a certain type of
values, which is known as Attribute Domain. Hence, the attribute Name will hold the name
of employee for every tuple.
Degree
The number of fields is called as ‘degree’. This is also called as ‘arity’.
Cardinality
The cardinality of a relation instance is the number of tuples in it.
Relation Schema
A relation schema describes the structure of the relation, with the name of the relation (name
of table), its attributes and their names and type.
Relation Key
13 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
A relation key is an attribute which can uniquely identify a particular tuple (row) in a relation
(table).
Key Constraints
We store data in tables, to later access it whenever required. In every table one or more than
one attributes together are used to fetch data from tables. The Key Constraint specifies that
there should be such an attribute (column) in a relation (table), which can be used to fetch
data for any tuple (row). The Key attribute should never be NULL or same for two different
rows of data. For example, in the Employee table we can use the attribute ID to fetch data for
each of the employee. No value of ID is null and it is unique for every row, hence it can be
our Key attribute.
Domain Constraint
Domain constraints refer to the rules defined for the values that can be stored for a certain
attribute. Like we explained above, we cannot store Address of employee in the column for
Name. Similarly, a mobile number cannot exceed 10 digits.
RELATIONAL ALGEBRA
Relational Algebra operators are mathematical functions used to retrieve queries by
describing a sequence operation on tables or even database (schema) involved. With
relational algebra operators a query is always composed of a number of operators, which
each in turn are composed of relations as variables and return an individual abstraction as the
end product.
The following are the main relational algebra operators as applied to SQL:
1. UNARY OPERATORS
(i) The SELECT Operator (σ)
The SELECT Operator is used to choose a sub set of the tuples (rows) from a relation that
satisfies a selection condition, acting as a filter to return only tuples that fulfills a qualifying
requirement.
14 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
The SELECT Operator is denoted by the symbol (σ) (Sigma)
The syntax for the SELECT statement is the written as follows:
Ex
Reseach_Student
Name University GPA Age E-mail
Joe Harvard 75 23 joe@myuniv.com
Tim Stanford 80 27 tim@myuniv.com
Janie TU 63 26 janie@myuniv.com
Rob Virginia 77 22 rob@myuniv.com
Required:
(i) Write an algebraic expression that would display student(s) who have graduated
from Harvard University and give the output relation
Soln.
(i) (σ)University=’Harvard’(Reseach_Student)
Output
Name University GPA Age E-mail
Joe Harvard 75 23 joe@myuniv.com
(ii) Write an algebraic expression that would display student(s) who have GPA>=75 and
their Age<=25 and give the output relation
This operator is used to re order, select and gets rid of attributes from a table. At some point
we might want only certain attributes in a relation and eliminate others from our query result.
Therefore, the PROJECT Operator would be used in such operations.
15 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Π <attribute list>(R)
Π would represent the PROJECT
< attribute list> would represent the attributes (columns) we want from the relational
(R) Would represent the relation or table we want to choose the attributes from.
Soln
Π Name, E-mail (Reseach_Student)
Output
Name E-mail
Joe joe@myuniv.com
Tim tim@myuniv.com
Janie janie@myuniv.com
Rob rob@myuniv.com
Required: Write an algebraic expression to find Name and E-mail of students having
GPA>75 in graduation and give out the output relation
Sol:
(i) Gender =’Female’ AND Marks>70 (Student)
(ii) Π Course_Id (Student)
16 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
2. SET OPERATORS
The UNION, INTERSECTION and DIFFERENCE Operators (Set Operations)
Ex.
Project_Mgr
EmpID Name E-mail
123 Joe joe@...
256 Janie janie@...
742 Tim tim@...
Engineer_Mgr
EmpID Name E-mail
256 Janie janie@...
742 Tim tim@...
341 Rob rob@...
Required: Write an algebraic expression to display Name and Email of people who are
either Engineer OR Project_Mgr or give the output relation.
Sol.
Π Name, E-mail (Project_Mgr)) U Π (Name, E-mail (Engineer))
Name E-mail
Joe joe@...
Janie janie@...
Tim tim@...
Rob rob@...
Ex.
Required: Write an algebraic expression to display Name, Email of persons who are both
Engineers and Project_Mgr and give the output result.
Soln.
Π Name, E-mail (Project_Mgr)) ∩ Π (Name, E-mail (Engineer))
17 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Name E-mail
Janie janie@...
Tim tim@...
DIFFERENCE: the DIFFERENCE (MINUS) operations includes tuples from one relation
that is not in other relation. Let the relation be A and B, the DIFFRENCE operation A
MINUS B is denoted by A-B, that results in tuples that are A and not in B.
Ex.
3 1 6
2 7 A U B= {3, 4, 5, 1, 2, 6, 7, 8}
A 4 B
5 8 A∩ = {1, 2}
A-B = {3, 4, 5}
B-A = {6, 7, 8}
Required: Write an algebraic expression that would display Name and Email of persons who
are Project_Mgr but Not Engineers and give the output result.
Soln.
Π Name, E-mail (Project_Mgr)) − Π (Name, E-mail (Engineer))
Name E-mail
Joe joe@...
Ex:
Use the following tables to answer the question that follows.
Table X
ID NAME RATING AGE
22 Paul 7 45
31 Linn 8 20
58 Mike 10 35
Table Y
ID NAME RATING AGE
28 James 9 35
31 Linn 8 20
44 Sussan 5 35
58 Mike 10 35
18 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
(ii) X∩Y
Ex1.
Write an algebraic expression using Cartesian product operator on Emp. and Dept. relations
given below
Employee
Department
DeptName Manager
Finance Rob
Marketing Sean
Output
Ex2.
Using the following relations, write the algebraic expression to display Name and Grade of
students having Grade A
Student
RollNo Name
1 Joan
19 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
2 Crisp
3 Teddy
Grade
RollNo Grade
1 B
2 A
3 C
Soln.
πName, Grade Grade=’A’ (Student ×Grade)
4. DIVISION Operator
Division operator takes two relations as input and produces one relation as output. One of the
input relations must be a binary relation. The other input relation must be a unary relation.
The unary relation must be defined on the same domain as one of the attributes in the binary
relation.
It takes the values of the unary relation and checks them off against the associated attribute in
the binary relation. If all the values in the unary relation match with the same value in the
binary relation then it outputs a value to the output relation.
Ex.
Using the following relations, write an algebraic expression that would display names of
students who passed all the required subjects in diploma module 2. Give the output result.
Student
Name Subject
A DBMS
B DBMS
C OOP
C SAD
A OOP
C APPL2
A SAD
C DBMS
B VB
B QT
A QT
A VB
A APPL2
C QT
B SAD
C VB
Subject
20 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Subject
DBMS
OOP
SAD
QT
VB
APPL2
Soln.
ΠName (Student÷Subject)
Name
A
C
Ex2.
Using the following relations, write an algebraic expression that would display name of
players who have goals in all the venues. Give output result.
R1
Player name Venue
Joe XYZ
Joe PQR
Jim LPM
Tom ABC
Joe LPM
Joe ABC
Ric ABC
R2
Venue
XYZ
PRQ
LPM
ABC
Soln.
ΠPlayer_name (R1÷R2)
Player name
Joe
5. JOIN Operator
The JOIN operator is used to combine related tuples from two relations into single tuple. It is
denoted by ⋈ symbol. It is similar to cross/Cartesian product.
21 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
the filtering/matching condition from the relation
TYPES OF JOIN
1. INNER JOINS: Contains only matching tuples
(i) THETA JOIN
Comparative operators like =, <, >, >=, <=
Laptop
Model Price
Dell 30000
Acer 20000
Asus 10000
Required: Write an algebraic expression that would enable the purchase of both mobile and
laptop, but mobile price should be less than laptop.
Soln.
Mobile ⋈ Laptop
(Mobile. Price < laptop. Price)
Ex.
Using the above example write an algebraic expression that would enable the purchase of
both mobile and laptop but mobile and laptop price should be the same.
Soln.
Mobile ⋈ laptop
(Mobile. Price = laptop. Price)
Ex
Using the following relations, write algebraic expression that would perform natural join
22 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Employee
EmpID Name Dept.
1 A Finance
2 B Sales
3 C Sales
Department
Dept. Manager
Finance M1
Sales M2
Soln
Employee ⋈ Department
Output
EmpID Name Dept. Manager
1 A Finance M1
2 B Sales M2
3 C Sales M2
Ex
Using the following relations, write algebraic expression that would perform natural join and
give the output relation.
R
A1 A3 A2
A x10 12
B x20 13
C x30 17
S
A2 A4 A5
17 C X40
12 D X70
13 E X90
Sol:
R⋈S
Output
A1 A3 A2 A4 A5
A x10 12 D x70
B x20 13 E x90
C x30 17 C x40
Ex:
Use the following tables C and D to answer the questions that follow
23 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Sid Sname
001 English
002 Kiswahili
003 Science
C
2. OUTER JOIN: It used when we want to keep all the tuples in either or both the relation in
the result of the JOIN regardless of whether or not they have matching tuples in other
relation.
(i) LEFT-OUTER JOIN: Keeps every tuple in the first or left relation. It is denoted by ⟕
Ex. Write an algebraic expression to perform the left-outer join and give the resultant relation
R1
ID Name
1 A
2 B
3 C
24 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
R2
ID Dept.
1 D1
2 D2
Soln. R1 ⟕ R2
ID Name Dept.
1 A D1
2 B D2
3 C NULL
(ii) RIGHT-OUTER JOIN: Keeps every tuple in the second or right relation. It is denoted
by ⟖
Ex. Write an algebraic expression to perform the right-outer join and give the resultant
relation.
R1
ID Name
1 A
2 B
1 C
R2
ID Dept.
1 D1
3 D3
Soln. R1 ⟖ R2
Name ID Dept.
A 1 D1
NULL 3 D3
C 1 D1
(iii) FULL-OUTER JOIN: Keeps tuples from both left and right relation
Ex. Write an algebraic expression to perform the full outer join on the following relations and
give the output.
R1
ID Name
1 A
2 B
1 C
R2
ID Dept.
1 D1
3 D3
25 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Soln. R1 ⟗ R2
Name ID Dept.
A 1 D1
B 2 NULL
C 1 D1
NULL 3 D3
A B
20 30
40 50
Tab1
B C
50 60
70 8
Tab2
(iii)Tab1⟖Tabl2;
Sol:
A B C
40 50 60
NULL 70 8
26 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Ex: Given the following relations;
27 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
6. RENAME Operator (ρ)
The RENAME operator is used to give a name to results or output of queries, returns of
selection statements, and views of queries that we should like to view at some other point in
time.
EMPLOYEE
EmployeeID EmployeeName
A 100
B 101
C 102
Soln.
(i) ρ EMLOYEE_TABLE (EMPLOYEE)
(ii) ρ ID/EmployeeID, Name/EmployeeName (EMPLOYEE)
Result:
EMPLOYEE_TABLE
ID Name
A 100
B 101
C 102
AGGREGATE FUNCTIONS
We can also apply Aggregate functions to attributes and tuples:
_ SUM
_ MINIMUM
_ MAXIMUM
_ AVERAGE, MEAN, MEDIAN
_ COUNT
28 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Table: Employee
Required:
(i) Find the minimum salary:
Results:
MIN (salary)
50000
Results:
AVG (salary)
51000
Sol:
Ԍ COUNT (name) σ Dept. = 'CS' (Employee)
Results:
COUNT (name)
2
Sol:
Ԍ SUM (salary) σ Dept. = 'Econ' (Employee)
Results:
SUM (salary)
85000
Sol:
Dept. Ԍ AVG (salary) (Employee)
29 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Results:
RELATIONAL CALCULUS
Relational Calculus is a query language which is non-procedural and instead of algebra, it
uses mathematical predicate calculus. The relational calculus is not the same like that of
differentiation and integral calculus in mathematics, but takes its name from a branch of
symbolic logic termed as predicate calculus. When applied to database, it found in two
forms:
1. Tuple relational calculus which was originally proposed by Codd in year 1972 and
is used for selecting those tuples that satisfy the given condition.
2. Domain relational calculus which was proposed by Lacroix and Pirotte in the year
1977and is used to selecting records based on the domains
Using Tuple Relational Calculus to fetch names of students, from the table student, with the
age greater than 17 then, T being our tuple variable,
30 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Ex:
Student (RollNo, Name, DepartmentNo, Sex)
Query: Find RollNo and Name of Student in DepartmentNo 2
Sol:
{t. RollNo, t. Name | Student(t) ^ t. DepartmentNo=2}
Ex:
Student (RollNo, Name, DepartmentNo, Sex)
Query: Find RollNo and Name of Male Student in DepartmentNo 2
Sol:
{t. RollNo, t. Name | Student(t) ˄ t. DepartmentNo=2 ˄ t. Sex= ‘Male’}
(i) Display all the details of students who scored above 40 Marks
(ii) Display the records for the fields SID, SNAME AND LNAME of all students
Sol:
{t. SID, t. SNAME, t. LNAME | student(t)}
(iii) Display the records in the field SNAME, LNAME and MARKS for all students with
MARKS values more than 60.
Sol:
{t. SNAME, t. LNAME, t. MARKS | student(t) ˄ t. MARKS >60}
(iv) Display the MARKS values for a student with SNAME is “Catherine” and LNAME
“Joseph”.
Sol:
{t. MARKS | student(t) ˄ t. SNAME= ‘Catherine’ ˄ t. LNAME ‘Joseph’}
Types of Quantifiers
31 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
2) Universal quantifiers (∀)
Expression Meaning
1. Ǝt for some occurrence of t
2. ∀t for every occurrence of t
Example
R
A B C
10 1 20
20 2 30
30 3 40
40 4 50
50 5 60
In this, filtering is done based on the domain of the attributes and not based on the tuple
values.
For example,
The above query will return the names and ages of the students in the table who are aged
above 17 years
Ex
Given the student relation below;
Last_name Age
First_name
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
32 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Student
(i) Query to find the first name and age of students where student age is greater than 27
(ii) Give the output result
Sol:
33 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
TOPIC FIVE: ENTITY RELATIONSHIP
The ER model defines the conceptual view of a database. It works around real-time entities
and the associations among them. At view level, the ER model is considered a good option
for designing databases.
Entity
An entity can be a real-world object, either animate or inanimate, that can be easily
identifiable. For example, in a school database, students, teachers, classes, and courses
offered can be considered as entities. All these entities have some attributes or properties that
give them their identity.
An entity set is a collection of similar types of entities. An entity set may contain entities with
attribute sharing similar values. For example, a Students set may contain all the students of a
school; likewise, a Teachers set may contain all the teachers of a school from all faculties.
Entity sets need not be disjoint.
Attributes
Entities are represented by means of their properties, called attributes. All attributes have
values. For example, a student entity may have name, class, and age as attributes.
There exists a domain or range of values that can be assigned to attributes. For example, a
student's name cannot be a numeric value. It has to be alphabetic. A student's age cannot be
negative, etc.
Types of Attributes
• Simple attribute − Simple attributes are atomic values, which cannot be divided
further. For example, a student's phone number is an atomic value of 10 digits.
• Composite attribute − Composite attributes are made of more than one simple
attribute. For example, a student's complete name may have first_name and
last_name.
• Derived attribute − Derived attributes are the attributes that do not exist in the
physical database, but their values are derived from other attributes present in the
database. For example, average_salary in a department should not be saved directly in
the database, instead it can be derived. For another example, age can be derived from
data_of_birth.
• Single-value attribute − Single-value attributes contain single value. For example −
Social_Security_Number.
• Multi-value attribute − Multi-value attributes may contain more than one values.
For example, a person can have more than one phone number, email_address, etc.
Key is an attribute or collection of attributes that uniquely identifies an entity among entity
set.
34 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
For example, the roll_number of a student makes him/her identifiable among students.
• Super Key − A set of attributes (one or more) that collectively identifies an entity in
an entity set.
• Candidate Key − A minimal super key is called a candidate key. An entity set may
have more than one candidate key.
• Primary Key − A primary key is one of the candidate keys chosen by the database
designer to uniquely identify the entity set.
Relationship
The association among entities is called a relationship. For example, an employee works_at
a department, a student enrolls in a course. Here, Works_at and Enrolls are called
relationships.
Relationship Set
A set of relationships of similar type is called a relationship set. Like entities, a relationship
too can have attributes. These attributes are called descriptive attributes.
Degree of Relationship
The number of participating entities in a relationship defines the degree of the relationship.
• Binary = degree 2
• Ternary = degree 3
• n-ary = degree
Mapping Cardinalities
Cardinality defines the number of entities in one entity set, which can be associated with the
number of entities of other set via relationship set.
• One-to-one − One entity from entity set A can be associated with at most one entity
of entity set B and vice versa.
35 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
• One-to-many − One entity from entity set A can be associated with more than one
entity of entity set B however an entity from entity set B, can be associated with at
most one entity.
• Many-to-one − More than one entity from entity set A can be associated with at most
one entity of entity set B, however an entity from entity set B can be associated with
more than one entity from entity set A.
• Many-to-many − One entity from A can be associated with more than one entity
from B and vice versa.
36 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
ER Diagrams
ER Diagram is a visual representation of data that describes how data is related to each other.
Entity, Attributes, Relationships etc. form the components of ER Diagram and there are
defined symbols and shapes to represent each one of them.
Entity
37 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Attributes for any Entity
Weak Entity
To represent a Key attribute, the attribute name inside the Ellipse is underlined.
Derived attributes are those which are derived based on other attributes, for example, age can
be derived from date of birth.
To represent a derived attribute, another dotted ellipse is created inside the main ellipse.
38 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Double Ellipse, one inside another, represents the attribute which can have multiple values.
Example 1
Draw entity relationship diagrams for the following:
(i) A student entity having RollNo as the key attribute, Name and Birthdate
(ii) A student entity having RollNo as the key attribute, Name and Birthdate but
Name is further divided as FirstName and LastName
(iii) A student entity having RollNo as the key attribute, Name and Birthdate but
Name is further divided as FirstName and LastName and a student can have more
than one PhoneNo.
(iv) A student entity having RollNo as the key attribute, Name, Birthdate, PhoneNo
and Age. A StudentName has FirstName and LastName and a student can have
more than one PhoneNo and Age does not exist in physical database.
Soln
(i) RollNo
Birthdate
Student
Name
(ii)
RollNo
Birthdate
Student
39 PREPARED BY,
Name THOMAS O. OCHIENG
FirstName
ICT DEPTMENT, SIT.
LastName
(iii)
RollNo Birthdate
Student PhoneNo
Name
FirstName
LastName
RollNo Birthdate
(iv)
Student PhoneNo
Example 2
Construct an E-R diagram (including attributes, identifiers and mandatory/optional
Name
cardinality) for the following Age
system descriptions, stating any assumptions you make.
FirstName
1) A company keeps records of the products it stores in its warehouses and factories that
make the products. LastName
2) Each product is identified by a unique product number and has name and description.
3) A product is made by a single factory but a factory can make more than one product.
4) A factory is recognized by a factory code and has an address and telephone number.
A product can be stored in one or more warehouses and a warehouse can stock a
number of products. The quantity of a product stored at a warehouse needs to be
recorded.
5) A warehouse is identified by a warehouse code and address is also held.
Soln
Step 1: Identify Entities
(i) Product
(ii) Warehouse
(iii)Factory
product_ number
location
Product
made stored
by
Quantity_stored
telephone_number
Example 3 warehouse
factory
Design an Entity Relationship (ER) model for a college database having the following. address
Warehouse code
1) A college contains manyaddress
factory_code departments
2) Each department can offer any number of courses
3) Many instructions can work in a department
4) An instructor can work only in one in a department
5) For each department there is a HOD
6) An instructor can be HOD of only one department
7) Each instructor can take any number of courses
8) A course can be taken by only one instructor
9) A student can enroll for any number of courses
10) Each course can have any number of students
Soln:
41 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
(ii) Course
(iii) Instructor
(iv) Student
(i) dept_name can identify a dept uniquely, hence dept_name is the key attribute for the
dept. entity
(ii) courseID is the key attribute for the course entity
(iii) studentID is the key attribute for the student entity
(iv) instructorID is the key attribute for the instructor entity
dept_name description
-name
Department
1 1
has
offers
N
enrolled is taught 1
instructor last_name
by by
TOPIC SIX: NORMALIZATION
Definition
is the formal process of decomposing relations with anomalies to produce smaller, well-
structured and stable relations.
Importance of normalization
The aim of normalization is to eliminate the following three file maintenance anomalies:
– Insertion Anomaly – adding new rows forces user to create duplicate data
– Deletion Anomaly – deleting a row may cause loss of other data representing completely
different facts
– Modification Anomaly – changing data in a row forces changes to other rows because of
duplication
Steps in normalization
In order to obtain a set of normalized relations, the following steps should be applied in
sequence: -
Ex1.
43 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
In a college, a lecturer may teach many subjects but may not belong to more than one
department. The college maintains information of its lecturers’ subject area as follows;
Lecturer Number, Lecturer Name, Lecturer Grade, Department Code, Department Name,
Subject Code, Subject Name and Subject Level.
1NF
Lecturer (Lecturer Number, Lecturer Name, Lecturer Grade)
Lecturer -Department (Lecturer Number, Department Code, Department Name, Subject
Code, Subject Name and Subject Level)
2NF
Lecturer (Lecturer Number, Lecturer Name, Lecturer Grade)
Department (Department Code, Department Name)
Lecturer -Department (Lecturer Number, Department Code, Subject Code, Subject Name and
Subject Level)
3NF
Lecturer (Lecturer Number, Lecturer Name, Lecturer Grade)
Department (Department Code, Department Name)
Subject (Subject Code, Subject Name and Subject Level)
Lecturer -Department (Lecturer Number, Department Code, Subject Code)
Ex2
Table 1 shows Safari company trainee’s details. Use it to answer the question that follows
2NF
Trainee (Trainee ID, Trainee Surname)
Course (Course Code, Course name)
44 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Trainee-Course (Trainee ID, Course Code, Tutor Fname, Tutor Office)
3NF
Trainee (Trainee ID, Trainee Surname)
Course (Course Code, Course name)
Tutor (Tutor Office, Tutor Fname)
Trainee-Course (Trainee ID, Course Code, Tutor office)
Trainee-Course
Ex2
Table 2 is an Employee table represented in 1NF. Use it to answer the questions that follow.
2NF
Employee (Employee ID, Employee Name)
Contract (Contract No, Hours)
45 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Employee-Contract (Employee ID, Contract No, Company ID, Company Location)
3NF
Employee (Employee ID, Employee Name)
Contract (Contract No, Hours)
Company (Company ID, Company Location)
Employee-Contract (Employee ID, Contract No, Company ID)
Employee Contract
Company Employee-Contract
Company Company
ID Location Employee Contract Company
SC111 Nrb ID No ID
SC112 Nrb 616681B SC1025 SC111
674315A SC1025 SC111
323113B SC1026 SC112
616681B SC1026 SC112
Ex3
Table 3 below shows a student’s result slip. Use it to answer the questions that follow.
Student No:1022567
Student Name: Alex James
Course Code: F105
Course Title: ICT
Subject Subject Title Number of Grade Result Results
Code Hours Code
BUS119 Business operations 20 10 PA01 Pass
COM110 Introduction to computers 20 8 PA02 Pass
COM112 Application Development 20 2 RE01 Refer Exam
COM114 HCI 10 7 RE01 Refer Exam
Table3.
Required: Normalize to 3NF
Sol:
1NF
Student (Student No, Student Name)
Student-Course (Student No, Course Code, Course Title, Subject Code, Subject Title, Number of Hours,
Grade, Result Code, Results)
2NF
46 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Student (Student No, Student Name)
Course (Course Code, Course Title)
Student-Course (Student No, Course Code, Subject Code, Subject Title, Number of Hours, Grade,
Result Code, Results)
3NF
Student (Student No, Student Name)
Course (Course Code, Course Title)
Subject (Subject Code, Subject Title, Number of Hours)
Result (Result Code, Grade, Results)
Student-Course (Student No, Course Code, Subject Code, Result Code)
Student Course
Student No Student Name Course Code Course Title
1022567 Alex James F105 ICT
Subject Result
Subject Subject Title Number Grade
Code of Hours Result Code Results
BUS119 Business operations 20 10
COM110 Introduction to 20 8 PA01 Pass
computers PA02 Pass
COM112 Application 20 2 RE01 Refer Exam
Development RE01 Refer Exam
COM114 HCI 10 7
Student-Course
Student No Course Code Subject Code Result Code
1022567 F105 BUS119 PA01
1022567 F105 COM110 PA02
1022567 F105 COM112 RE01
1022567 F105 COM114 RE01
47 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
TOPIC SEVEN: QUERYING A DATABASE
Definition of SQL
SQL stands for structured Query Language. It allows you to access a database. It can execute, retrieve,
insert, delete, and update queries against a database.
Data Types
SQL uses several data types. Data types define the properties of values for a column i.e. they define the
allowable values for a column (domain). Every data value within a column must be of the same type.
DATE: YYYY-MM-DD
TIME: HH:MM: SS
DATETIME: YYYY-MM-DD HH:MM: SS
TIMESTAMP: YYYYMMDDHHMMSS
YEAR: YYYY
48 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Any column in a table can be defined as NOT NULL. This means that the user must type a value in that
column. The default specification is that null values are allowed in a column.
Any column in a table can be defined as UNIQUE. This clause prohibits the user from entering
duplicate values into a column. The combination of NOT NULL and UNQUE is used to specify the
PRIMARY KEY
DDL STATEMENTS
Syntax:
CREATE DATABASE database_name;
DROP DATABASE: It is used to delete a database. All objects in the database (tables, views, indexes)
are deleted.
Syntax:
DROP DATABASE database_name;
CREATE TABLE: It is used when you want to create a table in a database. To create a table using SQL
the user needs to specify four components namely:
• Name of the table
• Name of each column in the table
• Data type of each column
• Maximum length of each column.
Syntax:
CREATE TABLE table_name
(
Column_name1 data_type,
Column_name2 data_type,
………………
);
Ex1. Create a table named person with four columns, FirstName, LastName, Address and Age
Ex2. Adjust the above table created to a specified maximum length for LastName up to 30 characters
and age having up to three digits.
49 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
(
FirstName VARCHAR,
LastName VARCHAR (30),
Address VARCHAR,
Age INT (3)) ;
DROP TABLE: It deletes a table (the table structure attributes, and indexes will also be deleted).
Syntax:
DROP TABLE table_name;
TRUCATE TABLE: It used to get rid of data inside the table and not the table itself.
Syntax:
TRUNCATE TABLE table_name;
ALTER TABLE: The ALTER TABLE statement is used to add or drop columns in an existing table.
Syntax:
ALTER TABLE table_name
ADD COLUMN column_name data type
Ex1. Write an SQL statement to add column named City in the Person table.
Ex2. Write an SQL statement to drop Address column in the Person table.
Ex3. Use the following description of userlogin table given below to answer the following questions.
50 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Sol.
(a
CREATE TABLE userlogin (
id INT (11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR (25) NOT NULL,
email VARCHAR (25) NOT NULL
);
(b
ALTER TABLE userlogin
ADD COLUMN passwd VARCHAR (25) NOT NULL
AFTER username;
(c
ALTER TABLE userlogin
MODIFY COLUMN email VARCHAR (30);
(d
ALTER TABLE userlogin
CHANGE id userid
INT (11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT;
Sol.
RENAME TABLE Person TO Persons;
CREATE INDEX: This statement is used to create an index in a table. Indices are created in an existing
table to locate rows more quickly and efficiently. It is possible to create an index on one or more
columns of a table, and each index is given a name. The users cannot see the indexes; they are just used
to speed up queries.
Syntax:
CREATE INDEX index_name
ON table_name (column_name)
Ex. Write an SQL statement to create an index, named PersonIndex on the LastName field of the Person
table:
Sol:
CREATE INDEX PersonIndex
ON Person (LastName);
To index the values in a column in descending order, you can add the reserved word DESC after the
column name. To index more than one column, and then list the column names within the parenthesis,
separated by commas.
51 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Ex
CREATE INDEX PersonIndex
ON Person (FirstName, LastName);
CREATE UNIQUE INDEX: It is used to create a unique index on a table. A unique index means that
two rows cannot have the same index value.
Syntax:
DROP INDEX: Used to delete an existing index in a table with the DROP statement.
Syntax:
ALTER TABLE table_name
DROP INDEX index_name;
DML Statements
SELECT Statement
Syntax
SELECT column_name(s)
FROM table_name
The table below named Student in database. Use it to answer the questions that follow:
(ii) SELECT *
FROM Student;
52 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
The SELECT DISTINCT Statement
The DISTINC keyword is used to return only distinct (different) values)
Inside a table, a column often contains many duplicate values; and sometimes you only want to list the
different (distinct) values.
Syntax
SELECT DISTINCT column_name(s)
FROM table_name
Ex
Using the Employee table given below:
Name Office Dept. Salary
Smith 400 CS 45000
Jones 220 Econ 35000
Green 160 Econ 50000
Brown 420 CS 65000
Smith 500 Fin 60000
Required: Write an SQL statement that display only distinct values from the column Dept.
Sol:
SELECT DISTINC Dept.
FROM Employee
Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between and inclusive range
53 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
LIKE Search for a pattern
IN If you know the exact value you want to
return for a least one of the columns
Syntax
Ex
Using Employee table given above, write an SQL statement that display records whose salary range
from 40,000 to 60,000.
Sol:
SELECT * FROM Employee
WHERE Salary
BETWEEN 400000 AND 60000;
Syntax
SELECT column_name FROM table_name
WHERE column_name LIKE pattern
The percentage sign (%), can be used to define wildcards (missing letters in the pattern) both before and
after the pattern while the underscore (-), can be used to find missing single character in the pattern.
Example
WHERE CustomerName LIKE 'a%' Finds any values that starts with "a"
WHERE CustomerName LIKE '%a' Finds any values that ends with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%_%' Finds any values that starts with "a" and are at least 3
characters in length
WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" and ends with "o"
54 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
The following SQL statement will return persons with first names that start with an ‘O’
The following SQL statement will return persons with first names that end with an ‘a’
The following SQL statement will return persons with first names that contain the pattern ‘la’
The following SQL Statement will return missing characters in first name field from persons table.
Ex
Using the given relation named Customers in a database given below:
Sol:
Assign: Write an SQL statement that would display letter ‘T’ and at least 3 characters in length in the
field firstname from table named persons.
Sol
Syntax
SELECT column_name FROM table_name
WHERE column_name IN (Value1, Value2…)
Ex
Using Employee table given above, write an SQL statement that display the employees with Name equal
to Smith or Brown.
Sol:
SELECT * FROM Employee
WHERE name IN (‘Smith’, ‘Brown’);
Ex.
Given the table below called Persons
Write an SQL statement to the first three records from the table
Sol:
SELECT * FROM Persons
LIMIT 3;
Ex.
Using the table given above called Persons write an SQL statement to:
(i) Display person(s) that were born after 1965
(ii) Display person(s) who were born in February
56 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
(iii) Display person(s) that have birth date greater than 12
(iv) Display the DOB value for Johnson
Sol;
(i) SELECT * FROM Students
WHERE YEAR (D.O.B)>1965;
(ii) SELECT * FROM Students
WHERE MONTH (D.O.B) =2;
Is used to sort the data in ascending or descending order, based on one or more columns
Syntax
SELECT column-list FROM table-name
WHERE condition
ORDER BY Column1…
ASC or DESC
Ex
Using Student table given below, write an SQL statement that display all records from highest to lowest
based on marks.
Table: Student
Sol:
57 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Syntax
INSER INTO table_name
VALUES (value1, value2….)
You can also specify the columns for which you want to insert data:
INSER INTO table_name (column1, column2….
VALUES (value1, value2….)
Ex
Using Student table given above, write an SQL statement to add a new record; Jonathan, Osteen,
353981, 15-02-1998, 80 in the relation.
Sol:
Syntax
It is also possible to delete all rows in a table without deleting the table. This means that
the table structure, attributes, and indexes will be intact:
58 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
having IDNo 123980.
Sol:
DELETE FROM Customer
WHERE IDNo = 123980;
The UPDATE statement is used to modify the existing records in a table. Use WHERE
clause with UPDATE statement to update selected rows otherwise all the rows would be
affected.
UPDATE table_name
SET column-name= new-value
WHERE column-name = some-value
Ex
Using Student table given above, write an SQL statement to update Surname to Osteen where FirstName
is Alex.
Sol:
UPDATE Student
SET Sirname = ‘Osteen’
WHERE FirstName = ’Alex’;
SQL Aliases
SQL aliases are used to give a table or a column in a table, a temporary name. Aliases are often used to
make column names more readable. An alias only exists for the duration of the query.
Syntax:
SELECT column-name FROM table-name AS table-alias;
Syntax
SELECT column-name AS column-alias FROM table-name;
;;
Ex
Sol
(i) SELECT * FROM Student As StudentInf;
SQL | Join
A SQL Join statement is used to combine data or rows from two or more tables based on a common field
between them. Different types of Joins are:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long as the condition
satisfies. This keyword will create the result-set by combining all rows from both the tables where the
condition satisfies i.e. value of the common field will be same.
Syntax:
Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER JOIN.
Example
Ex1:
Use the following tables Subject and Teacher to answer the questions that follow:
60 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Sid Sname
001 English
002 Kiswahili
003 Science
Table: Subject
Sol:
(i) SELECT Teacher. Pno, Teacher. Name, Subject. Sid, Subject. Sname, Teacher. Hours
FROM Teacher
INNER JOIN Subject
ON Teacher. Sid=Subject. Sid
(ii) SELECT Teacher. Pno, Teacher. Name, Subject. Sname, Teacher. Hours
FROM Teacher
INNER JOIN Subject
ON Teacher. Sid=Subject. Sid
WHERE Teachers. Hour > 10;
Ex2
Use the following tables Grade and Lecturer to answer the questions that follow:
Table: Grade
61 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
L897032 Gladys 015 50000 1998
Table: Lecturer
Sol
LEFT JOIN: This join returns all the rows of the table on the left side of the join and matching rows for
the table on the right side of join. The rows for which there is no matching row on right side, the result-
set will contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax:
SELECT table1.column1, table1.column2, table2.column1…
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN, both are same.
Ex
Given the following relations;
62 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
S2 n2 22
S3 n3 23
S4 n4 24
Student
Sol
RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the
right side of the join and matching rows for the table on the left side of join. The rows for which there is
no matching row on left side, the result-set will contain null. RIGHT JOIN is also known as RIGHT
OUTER JOIN.
Syntax:
Note: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both are same.
63 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Ex
Using the above given relations
Required:
(iii)Write an SQL statement that would perform right join
(iv) Give the output result
Sol
(ii)
FULL JOIN creates the result-set by combining result of both LEFT JOIN and RIGHT JOIN. The
result-set will contain all the rows from both the tables. The rows for which there is no matching, the
result-set will contain NULL values.
Syntax:
Ex
Using the above given relations
Required:
(v) Write an SQL statement that would perform full join
(vi) Give the output result
64 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Sol
AGGREGATE FUNCTIONS
Aggregate functions operate against a collection of values, but return a single value.
Syntax
SELECT function (Column)
FROM table;
OR
Ex.
Assume the relation Employee has the following tuples:
Office Name Dept Salary
CS001 Smith CS 45000
Econ001 Jones Econ 35000
Econ002 Green Econ 50000
CS002 Brown CS 65000
Fin001 Bright Fin 60000
Table: Employee
Sol:
65 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
(i)
SELECT SUM (Salary)
FROM Employee;
Results:
SUM (salary)
255000
(ii)
SELECT AVG (Salary)
FROM Employee;
Results:
AVG (salary)
51000
(iii)
SELECT COUNT (Name)
FROM Employee
WHERE Dept=’CS’;
Results:
COUNT (Name)
2
(iv)
SELECT SUM (Salary)
FROM Employee
WHERE Dept=’Econ’;
Results:
SUM (salary)
85000
66 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
GROUP BY… was added to SQL because aggregate functions return the aggregate of all column values
every time they are called, and without the GROUP BY function it would be impossible to find the
aggregate for each individual group of column values.
Syntax
Ex 1
Using the above relation employee, write an SQL statement find the average salary in each department.
Sol:
SELECT dept, AVG(Salary)
FROM Employee
GROUP BY dept;
Results:
Dept. AVG (salary)
CS 55000
Econ 42500
Fin 60000
Ex2
Company Amount
W3Schools 5500
Studytonight 4500
W3Schools 5100
GeeksforGeeks 6400
Table: Sales
Write an SQL statement to calculate the total salary for every company and give the output result.
Sol
67 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Output
Company SUM(Amount)
W3Schools 10600
Studytonight 4500
GeeksforGeeks 6400
HAVING… STATEMENT
HAVING… was added to SQL because the WHERE keyword could not be used against aggregate
functions and without HAVING… it would be impossible to test for result conditions.
Syntax
Using the sales table given above, write an SQL statement to calculate the total salary for every
company and display the company having salary more than 10000 giving the output result.
Sol
Output
Company SUM(Amount)
W3Schools 10600
68 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
TOPIC EIGHT: FUNCTION OF DATABASE MANAGEMENT SYSTEMS
A transaction is a logical unit of processing in a DBMS which entails one or more database access
operation. In a nutshell, database transactions represent real-world events of any enterprise.
All types of database access operation which are held between the beginning and end transaction
statements are considered as a single logical transaction. During the transaction the database is
inconsistent. Only once the database is committed the state is changed from one consistent state to
another.
In this tutorial, you will learn:
• A transaction is a program unit whose execution may or may not change the contents of a
database.
• The transaction is executed as a single unit
• If the database operations do not update the database but only retrieve data, this type of
transaction is called a read-only transaction.
• A successful transaction can change the database from one CONSISTENT STATE to another
• DBMS transactions must be atomic, consistent, isolated and durable
• If the database were in an inconsistent state before a transaction, it would remain in the
inconsistent state after the transaction.
A database is a shared resource accessed. It is used by many users and processes concurrently. For
example, the banking system, railway, and air reservations systems, stock market monitoring,
supermarket inventory, and checkouts, etc.
69 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
Not managing concurrent access may create issues like:
States of Transactions
Active State A transaction enters into an active state when the execution process
begins. During this state read or write operations can be performed.
Partially Committed A transaction goes into the partially committed state after the end of a
transaction.
Committed State When the transaction is committed to state, it has already completed its
execution successfully. Moreover, all of its changes are recorded to the
database permanently.
Failed State A transaction considers failed when any one of the checks fails or if the
transaction is aborted while it is in the active state.
Terminated State State of transaction reaches terminated state when certain transactions
which are leaving the system can't be restarted.
Let's study a state transition diagram that highlights how a transaction moves between these various
states.
1. Once a transaction states execution, it becomes active. It can issue READ or WRITE operation.
2. Once the READ and WRITE operations complete, the transactions becomes partially committed
70 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
3. state.
4. Next, some recovery protocols need to ensure that a system failure will not result in an inability
to record changes in the transaction permanently. If this check is a success, the transaction
commits and enters into the committed state.
5. If the check is a fail, the transaction goes to the Failed state.
6. If the transaction is aborted while it's in the active state, it goes to the failed state. The transaction
should be rolled back to undo the effect of its write operations on the database.
7. The terminated state refers to the transaction leaving the system.
71 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
What are ACID Properties?
For maintaining the integrity of data, the DBMS system you have to ensure ACID properties.
ACID stands for Atomicity, Consistency, Isolation, and Durability.
Example of ACID
If both transactions are submitted together, there is no guarantee that the Transaction 1 will
execute before Transaction 2 or vice versa. Irrespective of the order, the result must be as if the
transactions take place serially one after the other.
Types of Transactions
Based on Actions
• Two-step
• Restricted
72 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
• Action model
Based on Structure
What is a Schedule?
A Schedule is a process creating a single group of the multiple parallel transactions and
executing them one by one. It should preserve the order in which the instructions appear in each
transaction. If two transactions are executed at the same time, the result of one transaction may
affect the output of other.
Example
Parallel execution in a database is inevitable. But, Parallel execution is permitted when there is
an equivalence relation amongst the simultaneously executing transactions. This equivalence is
of 3 Types.
RESULT EQUIVALENCE:
If two schedules display the same result after execution, it is called result equivalent schedule.
They may offer the same result for some value and different results for another set of values. For
example, one transaction updates the product quantity, while other updates customer details.
View Equivalence
View Equivalence occurs when the transaction in both the schedule performs a similar action.
Example, one transaction inserts product details in the product table, while another transaction
inserts product details in the archive table. The transaction is the same, but the tables are
different.
CONFLICT Equivalence
73 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.
In this case, two transactions update/view the same set of data. There is a conflict amongst
transaction as the order of execution will affect the output.
What is Serializability?
Serializability is the process of search for a concurrent schedule who output is equal to a serial
schedule where transaction are execute one after the other. Depending on the type of schedules,
there are two types of serializability:
• Conflict
• View
Summary:
74 PREPARED BY,
THOMAS O. OCHIENG
ICT DEPTMENT, SIT.