0% found this document useful (0 votes)
77 views243 pages

Basics of Database Development Batch5 May-2023 V3

The document provides information about the basics of database development including terms, what a database is, components of a database system, the importance of databases, common uses of databases, different levels of data measurement, types of database users, and the process of creating a database.

Uploaded by

christiantc0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views243 pages

Basics of Database Development Batch5 May-2023 V3

The document provides information about the basics of database development including terms, what a database is, components of a database system, the importance of databases, common uses of databases, different levels of data measurement, types of database users, and the process of creating a database.

Uploaded by

christiantc0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 243

10/9/2023

BASICS OF DATABASE
C

DEVELOPMENT
RCA
Instructor: Larisse M. Mahoro Basics of DB Slide: 1
10/9/2023

LO 1: PERFORM
DATABASE
C

PRELIMINARY WORK
RCA Basics of DB Slide: 2
Introduction to Database
• Terms: Data, Information, database, Database Management System
• Data: It is a fact about something. it can be a name, age, gender of
a person, price, author, publishers of a book, a color, model of a car
etc
• It is the smallest unit which tell us something
• Examples
• John 31 Male
• Group B
• White Corora Toyota
• Nyabihu
• Information: When data is processed and presented in a meaningful
ways, becomes information
• Examples:
10/9/2023 • The number of students joining RCA RCA in the 2022 intake is 120. 3

• The success rate of Year 2 students in 2020 is 75%.


What is a Database
A database is an organized collection of structured information, or
data, typically stored electronically in a computer system.
A database is usually controlled by a database management system
(DBMS).
Together, the data and the DBMS, along with the applications that are
associated with them, are referred to as a database system, often
shortened to just a database.

10/9/2023 RCA 4
Components of database system

10/9/2023 RCA 5
Importance of Database
• A database allows for data to be stored quickly and
accessed easily and is used in many aspects of your daily
life. To perform all the above mentioned operations we
require DBMSs behind the scenes. Databases is responsible
for many of the services you utilize daily.

• A database gives structure to business information. It allows


for rapid creation, updating and retrieval of business
records.

10/9/2023 RCA 6
Importance of Database contd’…
✓ Independence between programs and data. Separating metadata
from applications that use data is a key attribute. The main
database characteristic is the ability to change and transfer an
organization's data to a different computer system without
changing the programs that process the information.
✓ Minimal data redundancy. Databases address the challenge of
data repetition. Information integrates into one logical structure,
and every piece of data repeats minimally.
✓ Improved data sharing
✓ Greater data security. A DBMS has administrative functions which
help control the security over sensitive information through privileges
and user roles.

10/9/2023 RCA 7
Database usage
• Databases are used just about everywhere including
government, banks, retail, schools, hospitals, websites,
and warehouses:
• Governments use databases to keep information related to
population
• Banks use databases to keep track of customer accounts,
balances and deposits.
• Retail stores can use databases to store prices, customer
information, sales information, and quantity on hand.
• Schools and universities use databases to store student
details, courses, marks
10/9/2023 RCA 8
Database usage cont’…
• Hospitals use databases to keep patients’ details and
medical records
• Bus travel agencies use databases to keep passengers’
details
• Airport use databases to store passengers’ details and flight
information
• Credit card transactions contain tabs on payments and
generate monthly reports.
• Telecommunication companies store information about calls,
generate monthly bills, track communication line, etc.
• Manufacturing businesses manage supply chains, production
lines, storages, generate invoices, etc
10/9/2023 RCA 9
Levels of data
Data can be categorized into four different levels, based on the level of
measurement. These levels are:
1. Nominal level: This is the lowest level of measurement, where data is
categorized into distinct categories or classes. Examples include
gender, race, or type of car.

2. Ordinal level: In this level of measurement, data is categorized into


distinct categories or classes, but the categories also have a natural
order. Examples include rating scales, such as 1-5 stars.

3. Interval level: Data at this level has a natural order, and the difference
between the values is meaningful. However, there is no true zero point.
Examples include temperature scales in Celsius or Fahrenheit.

4. Ratio level: This is the highest level of measurement, where data has a
natural order, the difference between the values is meaningful, and
there is a true zero point. Examples include weight, height, and time.
10/9/2023 RCA 10
Types of Database users
Database users are categorized based on their interaction
with the database.
1. Database Administrator (DBA): Database Administrator
(DBA) is a person/team who defines the schema and also
controls the 3 levels of the database.
DBA is also responsible for providing security to the database
and he allows only authorized users to access/modify the
database.
DBA also monitors the recovery and backup and provides
technical support.
The DBA has a DBA account in the DBMS which is called a
system or superuser account.
DBA repairs damage caused due to hardware and/or
software failures.
10/9/2023 RCA 11
Types of Database users cont’…
2. Naive / Parametric End Users :
Parametric End Users are the unsophisticated who don’t have
any DBMS knowledge but they frequently use database
applications in their daily life to get the desired results.

Example: Clerks in any bank is a naive users because they


don’t have any DBMS knowledge but they still use the
database and perform their given task.
3. System Analyst:
A system Analyst is a user who analyzes the requirements of
parametric end users. They check whether all the
requirements of end users are satisfied.
10/9/2023 RCA 12
Types of Database users cont’…
4. Sophisticated Users:
Sophisticated users can be engineers, scientists, and business
analysts, who are familiar with the database. They can develop
their own database applications according to their
requirement. They don’t write the program code but they
interact with the database by writing SQL queries directly
through the query processor.
5. Database Designers:
Database Designers are the users who design the structure of
the database which includes tables, indexes, views, constraints,
triggers, and stored procedures. He/she controls what data
must be stored and how the data items are related.
10/9/2023 RCA 13
Types of Database users cont’…
6. Application Program:
Application Program are the back end programmers who
writes the code for the application programs. They are the
computer professionals.

7. Casual Users / Temporary Users:


Casual Users are the users who occasionally use/access the
data base but each time when they access the data base
they require the new information, for example, Middle or
higher level manager.

10/9/2023 RCA 14
How the database is created?

There are three steps for Database


creation
• Fact-finding information(Investigation)
• Abstraction
• Organization

10/9/2023 RCA 15
1. Fact finding information/Investigation step

• Examples
• Mary is living in Nyarusange Sector, her age is 14, she is a female, with
O+ blood group, she weighs 48kg with 1.6m of height. Her father is
John and her mother is Kevine. She is a Year1 student of Software
Development in RCA. She plays basketball and likes rice.
• Mike is a boy, his age is 15, his father is Jack and he study MPC in ES
Byimana in S4. He is going to visit his friend Samantha who is living in
Nyabihu during holiday.
In fact finding information; we move and approach various people, office
and place to search for the fact about them and record them in our
document. This process is called INTERVIEWING
Another method is a QUESTIONNAIRE where we distribute the printed
forms with questions and respondents return those Questionnaire back to
use after filling them.
10/9/2023 RCA 16
2. ABSTRACTION step
• Abstraction is the process of filtering by which we keep the necessary and
important fact with us and discard unnecessary details. It is actually
summarizing the knowledge factor about something.
While applying abstraction, we need to be careful, we should keep the
context in our mind for what reason we are creating the database. A fact
about something can be useless in one context but of big importance in
another context.
Examples: If we have the following facts about someone
• Mary Nyarusange 14 O+ 48kg 1.6m Female Year1 SOD RCA John Kevine Basketball
Rice
If we are creating a database for academics institution we will keep the following
facts and discard others
Name Age Sex Combination Year of study
Mary 14 Female SOD Year1
If we are creating a database for a hospital
10/9/2023 RCA 17
Name Age Sex Co Year of study weight height Blood Group
3. Organization step
• is the process to represent abstracted data in an
appropriate format so that it appears beautiful,
reliable and understandable.
• Example: Below data are represented in tabular
form

Name Age Sex Combination Grade school


Mary 14 Female SOD Year1 RCA
Mike 15 Male MPC S4 ES B
Jacob 16 Male Welding Level5 TVET B

10/9/2023 RCA 18
Paper Based and computerized Database
• A database can be paper based or computerized.

Database

Computerised Paper based

Traditional File Database


Processing Management
System System

10/9/2023 RCA 19
Traditional File Processing system(TFPS)
• Data is stored in a computer permanent memory in form of a file but
the user must have knowledge of programming which is not easy for
common computer users even experienced programmers would find
it difficult to write a program each time a new database was to be
created.
• It is time consuming, difficult, expensive, and erroneous approach.
• Example of File concept in C++
• The user writes a program in which he creates a data file on a
permanent storage device(Hard disk)
• The user opens this file in write mode if it is the first time
• The user creates some temporary variables which receive
values from the keyboard and then after the values of variables
are written in a file
• When all important information is entered the file is closed
10/9/2023 • The program to write the fileRCAis terminated 20
Database Management and Database Management System
• Database Management: A set of activities in
which a new database is created with some
tables, new records are inserted in tables,
unnecessary records are deleted, records are
updated if information is changed in real life
and records can be searched according to a
given criteria whenever needed.
• Database Management System(DBMS):
• History of DBMSs,
Traditionally, the database is being stored in
the computer permanent memory in form of
a file but now it is not necessary for a user to
learn and use complex programming
languages.

10/9/2023 RCA 21
History of DBMS Contd’…
• History of DBMSs,
The DBMS was first introduced to the world back in 1960. In addition to
storing and extracting data sets, the DBMS allowed authorized users to
manipulate the data by way of insertion, deletion, creating, controlling,
defining, and revising.
• 1960 – Charles Bachman designed the first DBMS system
• 1970 – Codd introduced IBM’S Information Management System (IMS)
• 1976- Peter Chen coined and defined the Entity-relationship model, also known as the ER
model
• 1980 – Relational Model becomes a widely accepted database component
• 1985- Object-oriented DBMS develops.
• 1990s- Incorporation of object-orientation in relational DBMS.
• 1991- Microsoft ships MS access, a personal DBMS, and that displaces all other personal DBMS
products.
• 1995: First Internet database applications
• 1997: XML applied to database processing. Many vendors begin to integrate XML into DBMS
products

10/9/2023 RCA 22
• A DBMS is a software that provide an interface for Data entry, storage,
update, delete and retrieve in computer which is easy to understand.

10/9/2023 RCA 23
DBMS vs. Flat File system
DBMS Flat File Management System

Multi-user access It does not support multi-user access

Design to fulfill the need of small and It is only limited to smaller DBMS
large businesses systems.

Remove redundancy and Integrity. Redundancy and Integrity issues

Expensive. But in the long term Total


It’s cheaper
Cost of Ownership is cheap

Easy to implement complicated No support for complicated


transactions transactions
10/9/2023 RCA 24
Types of DBMSs
1. RDBMS
Over time, data became more complex and specialized. As such, there was a need for
a database management system that could deal with these evolving requirements.
During the 1970s, the world got its first-ever relational database management
system(RDBMS).
• A relational database management system (RDBMS) is an application on a
computer that can manage large amounts of data quickly and efficiently.
• The main property that sets relational databases apart from other databases is
that data in separate tables can be linked, meaning you can specify a
relationship between different bits of data. For example, one table could specify a
list of people, while another could list each person’s address. The tables could
then be linked closely together by having each row from the Address table
reference a particular row in the People table.
• Most popular RDBMS include:
• Oracle,
• MySQL,
• Microsoft SQL Server,
10/9/2023 • PostgreSQL. RCA 25
Other Types of DBMSs

After the advent of the RDBMS, numerous types of database management systems started
to appear. Here’s a brief overview of other notable DBMS:

• 2. Document databases – A kind of non-relational database designed for storing and


querying data as JSON-like documents. Document databases ease a developer’s
effort for storing and querying data in a database by using the same document-
model format used in the application code.
• 3. ER model databases – A high-level conceptual data model diagram, ER or entity-
relationship modeling helps in systematically analyzing data requirements for
producing a well-designed database.
• 4. Graph databases – A type of NoSQL database, a Graph database replace tables
with graphs having vertices and edges. While the vertices represent entities, such as
a person or place, the edges represent the relationship amongst two nodes.

10/9/2023 RCA 26
Other Types of DBMSs

• 5. Hierarchical databases – Follows a data model in which the data is organized into a
tree-like structure. The data is stored as records, and different records are connected
via links. Each record contains a collection of fields, whereas each field has only one
value.
• 6. NoSQL databases – An alternative to the traditional relational database, a NoSQL
database is immensely useful when working with giant sets of distributed data. Unlike
SQL databases that are table-based, NoSQL databases are document-based, key-
value pairs, graph databases, or wide-column stores.
• 7. Network databases – A form of database model in which it is possible to link several
member records or files to multiple owner files and vice-versa is a network database.
The model is like an upside-down tree where each member information is in branch
linked to the owner, represented by the bottom of the tree.
• 8. Object-oriented databases – An object-oriented database model is one in which
objects represent the information. This one is much less popular than other types of
database management systems.

10/9/2023 RCA 27
Advantages of DBMS
✓Database management systems are highly essential for organizations in
managing different databases and retrieving relevant information.
✓This management system is used in various organizations as an interface that
helps users in forming a connection with the databases.
✓ DBMS is an improved way that helps in organizing data in a way that it can be
stored and managed easily. The data obtained and stored by the
organizations play a crucial role in targeting their goals and helping in forming
their business strategy. A database management system can store, organize,
and monitor big or small information, just by using one software application. The
use and implementation of a data system can help businesses in performing well
and allows them to save overall costs.
✓The database management system helps in sharing the data within the
organization. The people who are involved in such an environment are
programmers, system administrators, and end-users. Multiple users can have
quick access to the data and share it with other users without going through
extra effort.
10/9/2023 RCA 28
Multi-user DBMS Architectures
• Multi-user database management system (DBMS) architectures are designed to support multiple
users who need to access and manipulate data simultaneously.
• Types of multi-user DBMS architectures:
1.Client-Server Architecture: In the client-server architecture, the database is stored on a server,
and the clients connect to the server to access the data. The server manages the database and
handles queries and transactions, while the clients provide the user interface.
2.Three-Tier Architecture: is an extension of the client-server architecture, in which the application
logic is separated into a middle tier. The middle tier is responsible for processing requests from the
client and accessing the database on the server.
3.Peer-to-Peer Architecture: In the peer-to-peer architecture, each node in the network has its own
database and can share data with other nodes. This architecture is typically used in
decentralized systems and can be more scalable and fault-tolerant than client-server
architectures.
4.Distributed Architecture: In the distributed architecture, the database is distributed across multiple
servers, and the processing is distributed across multiple nodes. This architecture can be used to
support large-scale, high-performance systems, but it can be complex to manage.
10/9/2023 RCA 29
Advantages of DBMS contd’…
✓DBMS allows the categorization and structuring of available data,
✓Reduced Data Redundancy compared to TFPS where multiple copies
of files that were stored in many different locations
✓Data Integrity: means the data is accurate and consistent in the
database
✓Data Security: Only authorized users will access the data using
usernames and passwords. Unauthorized users are not allowed.
✓Privacy: The privacy rule in a database means only authorized users
can access a database according to its privacy constraints. There are
levels of database access and a user can only view the data he is
allowed to
✓Backup and Recovery: Database Management System automatically
takes care of backup and recovery.
10/9/2023 RCA 30
Disadvantages of DBMS
Database Management System is quite useful compared to the file based management
system. However, it does have some disadvantages. Some of those are as follows:
1. More Costly: Creating and managing a database is quite costly. High cost software and
hardware is required for the database. Also highly trained staff is required to handle the
database and it also needs continuous maintenance. All of these ends up making a
database quite a costly venture.
2. High Complexity: A Database Management System is quite complex as it involves
creating, modifying and editing a database. Consequently, the people who handle a
database or work with it need to be quite skilled or valuable data can be lost.
3. Database handling staff required: As discussed in the previous point, databases and
DBMS are quite complex. Hence, skilled personnel is required to handle the database so
that it works in optimum condition. This is a costly venture as these professionals need to
be very well paid.
4. Database Failure: All the relevant data for any company is stored in a database. So it is
imperative that the database works in optimal condition and there are no failures. A
database failure can be catastrophic and can lead to the loss or corruption of very
important data.
10/9/2023 RCA 31
Disadvantages of DBMS contd’…
5. High Hardware Cost: A database contains a vast amount of data. So large disk storage
is required to store all this data. Sometimes extra storage may even be needed. All this
increases hardware costs by a lot and makes a database quite expensive.
6. Huge Size: A database contains a large amount of data, especially for bigger
organizations. This data may even increase as more data is updated in the database.
All of these lead to a large size of the database. The bigger the database is, the more
difficult to handle and maintain it. It is also more complex to ensure data consistency
and user authentication across big databases.
7. Upgradation Costs: Often new functionalities are added to the database. This leads to
database upgrades. All of these upgrades cost a lot of money. Moreover, it is also quite
expensive to train database managers and users to handle these new upgrades.
8. Cost of Data Conversion: If the database is changed or modified in some manner, all
the data needs to be converted to the new form. This cost may even exceed the
database creation and management costs sometimes. This is the reason most
organizations prefer to work on their old databases rather than upgrade to new ones.

10/9/2023 RCA 32
Database Architecture
• Database architecture refers to the design, structure, and organization of a
database system. It encompasses the physical, logical, and conceptual
aspects of the database and includes considerations such as data modeling,
data storage, data retrieval, and data security.
• Three Level Schema Architectures
• The three-schema architecture divides the database into three-level used to
create a separation between the physical database and the user
application.
• This architecture contains three layers of database management system:
1. External level (User view)
2. Conceptual level
3. Internal level (Physical schema)

10/9/2023 RCA 33
Three Level Schema Architectures

10/9/2023 RCA 34
Data Independence
Data independence refers to the ability to modify the schema or physical
organization of a database without affecting the application programs
that access the data. There are two types of data independence:
physical data independence and logical data independence.

• Physical data independence allows • Logical data independence allows


changes to be made to the physical changes to be made to the logical
storage structures of a database without schema of a database without affecting
affecting the schema or the application the application programs that use the
programs that use the data. This means data. This means that changes such as
that changes such as adding new adding new tables, modifying
indexes, reorganizing data files, or relationships between tables, or changing
changing the storage medium can be data types can be made without
made without modifying the application affecting the application programs.
programs.

10/9/2023 RCA 35
Data model
• A data model is a representation of data and its relationships within a database. It is a
conceptual or logical representation of the data that helps to organize, understand, and
manage the information within a database management system (DBMS).
• Types of data models:
1.Entity-Relationship (ER) Model: The ER model represents data as entities (objects or concepts),
relationships (associations between entities), and attributes (characteristics of entities). It is
used to design relational databases and is the most widely used data model.
2.Hierarchical Model: The hierarchical model organizes data in a tree-like structure, where
each record is a parent or child of another record. It is used in mainframe databases and is
less common today.
3.Network Model: The network model represents data as a set of records connected by
multiple relationships. It is used to model complex data relationships and is also less common
today.
4.Object-Oriented Model: The object-oriented model represents data as objects, with attributes
and methods that describe their behavior. It is used to model complex data relationships in
object-oriented databases.
10/9/2023 RCA 36
Relational Model database
The relation Model (RM )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.

A relational database is a set of formally described tables from which data can be accessed or
reassembled in many different ways without having to reorganize the database tables

Each table, in a relational database contains one or more data categories in columns, or attributes.

In short a relational database model:

• Use table to represent data


• Is easy to understand
• Is comparatively slower in a computer
• Is very popular
• Is very common in the market
37
Relational data languages
Data Definition Languages operations(DDL): is used to create and modify the structure of
objects in a database using predefined commands and a specific syntax.
These database objects include tables, sequences, locations, aliases, schemas and
indexes.
Examples of these commands are: CREATE, DROP, RENAME, ALTER
Data Manipulations Languages operations(DML): refers to a computer programming
language that allows you to add (insert), delete (delete), and alter (update) data in a
database.
Examples of these commands are:
• INSERT
• DELETE
• UPDATE
• SELECT

38
Relational data languages contd’…
DCL: Data Control Language consists of statements that control security
and concurrent access to table data. It is mostly concerned with rights,
permissions, and other controls of the database system.
• GRANT – allow users access privileges to the database.
• REVOKE – withdraw users’ access privileges given by using the
GRANT command.
TCL: Transaction Control Language is used for maintaining the consistency
of the database and for the management of transactions made by the
DML commands.
Example:
• COMMIT,
• ROLLBACK
39
Example of Relational Data Model concepts
STUDENT TABLE

studentCode Name Combination YearOfStudy

11234 James MPC 1

11235 Mary SOD 3

11236 Peter PCB 2

The degree of student table or number of columns=4


The cardinality of student table or the number of rows of
records=3
Degree x cardinality=Total number of cells 40
• Each row, also called a record or tuple, contains a unique
instance of data, or key, for the categories defined by the
columns.
• Each table has a unique primary key, which identifies the
information in a table. The relationship between tables can
then be set via the use of foreign keys -- a field in a table that
links to the primary key of another table
• “A table is the primary unit of physical storage for data in a
database.” Usually, a database contains more than one
table

41
Relational Data Model concepts(summary)
1. Attribute: Each column in a Table. Attributes are the properties which define a relation. e.g.,
Student_Rollno, NAME,etc. An attribute is also called a field key
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. Table is also called a relation.
3. Tuple – It is a single row of a table, which contains a single record.
4. Relation 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: represents the set of values for a specific attribute.
8. A 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 the
attribute domain
42
Entity Relationship Diagram

ERD is a graphical representation or a blueprint of database structure


43
Following are the main components and its symbols in ER Diagrams:
✓ Rectangles: This Entity Relationship Diagram symbol represents entity types
✓ Ellipses: Symbols represent attributes
✓ Diamonds: This symbol represents relationship types
✓ Lines: It links attributes to entity types and entity types with other relationship types
✓ Primary key: attributes are underlined
✓ Double Ellipses: Represent multi-valued attributes

44
45
Entity Identification(Ex. hospitaldb)
expiryDat
qualification e
medCode
address IdNumber
Medecine
Doctor type
age Name
Name

disease
type
RNumber address PatNumber

room Patient

inCharge age

capacity Name
46
Entity-Attribute Diagram
Why use ER Diagrams?
• Helps you to define terms related to entity relationship modeling
• Provide a preview of how all your tables should connect, what fields are
going to be on each table
• Helps to describe entities, attributes, relationships
• ER diagrams are translatable into relational tables which allows you to
build databases quickly
• ER diagrams can be used by database designers as a blueprint for
implementing data in specific software applications
• The database designer gains a better understanding of the information
to be contained in the database with the help of ERP diagram
• ERD Diagram allows you to communicate with the logical structure of
the database to users
47
WHAT IS ENTITY?
• A real-world thing either living or non-living that is easily recognizable and non-
recognizable. It is anything in the enterprise that is to be represented in our database. It
may be a physical thing or simply a fact about the enterprise or an event that happens
in the real world.
• An entity can be place, person, object, event or a concept, which stores data in the
database. The characteristics of entities are must have an attribute, and a unique key.
Every entity is made up of some ‘attributes’ which represent that entity.
• Examples of entities:
Person: Employee, Student, Patient
Place: Store, Building
Object: Machine, product, and Car
Event: Sale, Registration, Renewal
Concept: Account, Course

48
Weak Entities
A weak entity is a type of entity that doesn’t have its key attribute. It can be identified
uniquely by considering the primary key of another entity. For that, weak entity sets need
to have participation.
For example, a ROOM can only exist in a BUILDING.

49
Strong Entity Set Weak Entity Set
It does not have enough attributes to build a
Strong entity set always has a primary key.
primary key.
It is represented by a double rectangle
It is represented by a rectangle symbol.
symbol.
It contains a Primary key represented by the It contains a Partial Key which is represented
underline symbol. by a dashed underline symbol.

The member of a strong entity set is called as The member of a weak entity set called as a
dominant entity set. subordinate entity set.
In a weak entity set, it is a combination of
Primary Key is one of its attributes which
primary key and partial key of the strong
helps to identify its member.
entity set.
In the ER diagram the relationship between The relationship between one strong and a
two strong entity set shown by using a weak entity set shown by using the double
diamond symbol. diamond symbol.
The connecting line of the strong entity set The line connecting the weak entity set for
with the relationship is single. identifying relationship is double.
50
Relations identifications
Medecine

Doctor
Patient

Room

51
Relational database model(Librarydb)

52
Download Smartdraw

https://www.smartdraw.com/entity-relationship-
diagram/er-diagram-tool.htm

53
Books table
ISBN Title PubID Price

0-133-3416-9 FOP 1 2000

0-153-3426-9 Javascript 2 1000

0-713-3436-9 Romeo and Juliet 3 2000

0-113-3446-9 Cyber security 3 3000

0-213-3456-9 Mathematics v5 2 5000

0-613-3466-9 English for Juniors 1 3400

0-413-3476-9 C++ 2 2567

0-813-3486-9 Python 3 1290

0-114-3496-9 Motion 1 4000

0-163-3476-9 Design and Analysis 3 5000 54


Authors table
AuID AuName AuTel

1 Austin 111111111

2 Marville 222222222

3 John 333333333

4 Roman 444444444

5 Shakespeare 555555555

6 Amanda 666666666

7 Mary 777777777

8 Mike 888888888

9 Harry 999999999

10 Smith 1212121212
55
Publishers Table

PubID PubName PubTel

1 Pub House 88888888

2 Mega Press 33333333

3 Alpha House 22222222

56
Book-author Table
ISBN AuID

0-133-3416-9 1

0-133-3416-9 8

0-713-3436-9 2

0-113-3446-9 4

0-213-3456-9 3

0-613-3466-9 3

0-413-3476-9 4

0-813-3486-9 7

0-114-3496-9 7
57
hospitaldb expiryDat
qualificatio e
Name medCod
n e
Home IdNumbe
address r Medecin
gives e
Doctor type
dob

Name
Takes
treats

address PatNumb
RNumber er
type Is
room admitt Patient
inCharg ed dob
e
capacity Name
58
Entity- Relationship Diagram
Reduction of ER To tables

59
Doctor
doctor_patient Room patient_medecine
doctorId
idNumber RoomNumber idNumber
name
doctorId type doctorId
sex
patNumber
capacity patNumber
dob
date
date
qualification
consultation
Medecine medcode
address

Patient Medcode quantity


Room_Patient
patNumber name dosage
admissionId
name manufacturedDate
roomNumber
sex expiredDate
patNUmber
dob price
startDate 60
address type
Mathematical Relations in database
• Mathematical relations in databases refer to the mathematical rules and concepts that
are used to describe and define the relationships between tables in a relational
database. These relationships are expressed in terms of sets, tuples, and functions, which
are the fundamental concepts of set theory and relational algebra.
• Relational algebra is a set of operations that are used to manipulate and combine
tables in a relational database. The basic operations of relational algebra include
selection, projection, union, difference, cartesian product, and join.
• The basic operations of relational algebra include:
1.Selection (σ) The selection operation selects a subset of rows from a relation that satisfy
a certain condition (i.e., a predicate). The result of the selection operation is a new
relation that contains only the selected rows.
2.Projection (π): The projection operation selects a subset of columns from a relation. The
result of the projection operation is a new relation that contains only the selected
columns.
Reference: Introduction of Relational Algebra in DBMS – GeeksforGeeks
10/9/2023 RCA 61
Mathematical Relations in database
3. Union(Ս): The union operation combines two relations into a new relation that contains
all the tuples from both relations. The result of the union operation must have the same
schema (i.e., set of attributes) as both input relations.
4.Intersection (Ո): The intersection operation combines two relations into a new relation
that contains only the tuples that are common to both relations. The result of the
intersection operation must have the same schema as both input relations.
5.Difference(-): The difference operation combines two relations into a new relation that
contains only the tuples that are in one relation but not in the other. The result of the
difference operation must have the same schema as both input relations.
6.Cartesian product (X): The cartesian product operation combines two relations into a
new relation that contains all possible combinations of tuples from both relations. The
result of the cartesian product operation has a schema that includes all the attributes
from both input relations.
7.Join (⋈): The join operation combines two relations based on a common attribute (or set
of attributes) and creates a new relation that includes all the attributes from both input
relations. The result of the join operation must have the same schema as both input
relations.
10/9/2023 RCA 62
Database Keys
• A key in DBMS is an attribute or a set of attributes that help to uniquely
identify a tuple (or row) in a relation (or table).
• Keys are also used to establish relationships between the different
tables and columns of a relational database. Individual values in a key
are called key values.
• A Key can be a single attribute or a group of attributes, where the
combination may act as a key.

63
Why do we need a Key?

• Keys are defined to easily identify any row of data in a table. Let's try to
understand about all the keys using a simple example.

Student_id Name Phone Age

1 Amy 55555555 22

2 Amanda 66666666 12

3 Mary 77777777 14

4 Mike 88888888 15

5 Mary 99966778 18
6 Smith 44444444 21
64
1. Super key
Super Key is defined as a set of attributes within a table that can uniquely
identify each record within a table. Super Key is a superset of a Candidate
key.
In the table defined above super key would include {student_id}, {student_id,
name}, {phone}, {student_id, name, phone}, {phone}, {student_id, name,
phone, age}.
2. Candidate Key
A candidate key is a minimal super key with no redundant attributes. There
can be more than one candidate key.
In our example, student_id and phone both are candidate keys for table
Student.

65
3. Primary Key
A primary key is a candidate key that is most appropriate to become the
main key for any table. It is a key that can uniquely identify each record in a
table like any other super or candidate key. The primary key does not allow
NULL and DUPLICATE

66
4.Composite Key
A composite key is a key that consists of two or more attributes that
uniquely identify any record in a table is called Composite key. But the
attributes which together form the Composite key are not a key
independently or individually.
Composite key

student_id exam_id marks

67
5. Secondary or Alternative key
The candidate key which are not selected as primary key are known as
secondary keys or alternative keys.
6. Non-key Attributes
Non-key attributes are the attributes or fields of a table, other than super or
candidate key attributes/fields in a table. Ex: {name}, {age}
Prime Attributes
Attributes that form a candidate key of relation, i.e. attributes of the
candidate key, are called prime attributes.
7. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to
the PRIMARY KEY in another table. The table with the foreign key is called
the child table, and the table with the primary key is called the referenced
or parent table.
68
Types Relationship between tables

One-to-one
One-to-many
Many-to-One
Many-to-many

69
One to One relationship

Example

1 1
Husband Marries Wife

1 1
Peter Marries Mary

1 1
Samanth
Claude Marries
a

70
One TO Many

Table A Table B

A1 A2 A3 A1 B1 B2 B3
1 ∞
A B

1 ∞
Father Is a Son
father
of

1
Sam Is a Joy
father
of
One instance of entity A is in Relationship
with many instances of Entity B
Peter 71
Many TO One

Table A Table B

A1 A2 A3 B1 B2 B3 ∞ 1
A B

∞ 1
Son Is a Father
father
of

1
Joy Is a Sam
father
of
Many instances of entity A are in
Relationship with one Instance of Entity B Peter
72
Many To Many
A many-to-many relationship occurs when multiple records in a table are associated with
multiple records in another table.

Ex: Many students can study many courses and More courses can be taught to many students

Table A Table AB Table B

A1 A2 A3 A2 B2 B1 B2 B3

Many instances of entity A are in Relationship with Many Instances of Entity B


73
Data types
When you create a database you need to set data
types for each field/column. For example, in a film
database you might need alphabetical characters
for 'Titles', but numbers for 'Duration'. Fields are usually
restricted to a certain data type.
Data typing is a way of classifying data values that
have common properties. Different kinds of data
values also need different amounts of memory to
store them, and have different operations that can
be performed upon them.
74
1. Character
Character data consist of any combination of letters, symbols, and numeric characters.
a. CHAR: It is a fixed-length character data type. It has a maximum length of 8000
characters. It contains Fixed-length non-unicode characters(ASCII characters. ).
Example: To specify the column, Street, and to have the CHAR data type with length 30,
use:
Street CHAR(30)
b. VARCHAR: It has a maximum length of 65,535 characters. It contains variable-length
non-unicode characters.
Example: To specify the column, Description, and to have the VARCHAR data type
with the maximum length of 300, use:
Description VARCHAR(300)
C. Text : It has a maximum length of 2,147,483,647 characters. It contains variable-length
non-Unicode characters.

75
Difference between CHAR VS VARCHAR

If you are storing strings with a wildly variable length then use a VARCHAR, if the length is
always the same, then use a CHAR as it is slightly faster.
They also differ in maximum length where the length of a CHAR value can be any value
from 0 to 255 and the maximum length of a VARCHAR value is 65,535.
An example to demonstrate the difference between CHAR and VARCHAR

Value CHAR(4) Storage Required VARCHAR(4) Storage


Required
’’ ‘ ’ 4 bytes ’’ 1 byte
‘ab’ ‘ab ’ 4 bytes ‘ab’ 3 bytes
‘abcd’ ‘abcd’ 4 bytes ‘abcd’ 5 bytes
‘abcdefgh’ ‘abcd’ 4 bytes ‘abcd’ 5 bytes

76
2. Numbers Numerical datatypes
Datatype From To
Number data types include integers,
decimals, floats and bit bit 0 1
a. INT: The INT data type consists of tinyint 0 255
positive, zero, and negative whole
numbers. The range of INT is from – smallint -32,768 32,767
2,147,483,648 to 2,147,483,647.
int -2,147,483,648 2,147,483,647
Example: Integers can be used as the
data type for a key column. bigint -9,223,372,036, 9,223,372,036,
b. DECIMAL (or NUMERIC): The 854,775,808 854,775,807
DECIMAL data type includes positive
decimal -10^38 +1 10^38 -1
and negative decimal numbers. The
values of DECIMAL data are from − numeric -10^38 +1 10^38 -1
10^38 + 1 through 10^38− 1.
C. Floats: floating point numbers (with float (with decimal -1.79E + 308 1.79E + 308
points)
decimal points), for example: 4.2,
27.4, 56.8 real -3.40E + 38 3.40E + 38 77
3. Date and Time
It represents date and time values.
Datatype Description

DATE Stores date in the format YYYY-MM-DD

TIME Stores time in the format HH:MI:SS

DATETIME Stores date and time information in the format YYYY-MM-DD HH:MI:SS

TIMESTAMP Stores number of seconds passed since the Unix epoch (‘1970-01-01
00:00:00’ UTC).

YEAR Stores year in 2 digits or 4 digit format. Range 1901 to 2155 in 4-digit
format. Range 70 to 69, representing 1970 to 2069.
78
Read more

https://www.w3schools.com/sql/sql_datatypes.asp

79
Data dictionary
A Data Dictionary is a collection of names, definitions, and
attributes about data elements that are being used or captured in
a database. A data dictionary contains metadata i.e data about
the database

Each column in a table is described and datatype is added 80


DBMS data dictionary
Relational database engines enable much more
description of data models and provide this
information through their data dictionaries. This
information is:

✓ Data type of column,

✓ Default values for columns,

✓ Nullability of columns,

✓ Table relationships (foreign keys),

✓ Table constraints such as uniqueness of column


values (primary and unique keys),

✓ Data elements descriptions

81
Advantages of Data dictionary
Data Dictionaries are useful for a number of reasons. In short,
they:
• Assist in avoiding data inconsistencies across a project
• Help define conventions that are to be used across a
project
• Provide consistency in the collection and use of data
across multiple members of a research team
• Make data easier to analyze
• Enforce the use of Data Standards
82
Database constraints
A Database constraint is a rule enforced by the database
manager to limit the values that can be inserted, deleted, or
updated in a table.
Database constraints can be defined also as the restrictions on
the contents of the database or on database operations
Database constraints provide a way to guarantee that:
✓ Rows in a table have valid primary or unique key values

✓ Rows in a dependent table have valid foreign key values that

reference rows in a parent table


✓ Individual column values are valid

83
Database constraints
There are several types of constraints available in DBMS and they
are:

➢ Domain constraints
➢ Entity Integrity constraints
➢ Referential Integrity constraints
➢ Key constraints

84
Types of constraints:

1. Column Constraints
1. Not Null
2. Primary key
3. Unique
4. Foreign Key
5. Check
6. Default
7. Index
2. Table constraints
1. Primary Key
2. Unique
3. Foreign key
4. Check

85
MySQL DBMS supports the following constraints:

1. Primary key constraint (to enforce existence integrity)


2. Foreign key constraint (to enforce foreign key or referential integrity)
3. Unique constraints (to enforce candidate key integrity)
4. Check constraint (to restrict a column’s values; partial enforcement of
domain integrity)
5. Not null
6. Default
7. Index
• You specify one or more of these constraints when create a base table
by using CREATE table statement.
• You can also add or drop constraints with the ALTER Table statement.
86
MySQL DBMS constraints
NOT NULL In MySQL, NOT NULL constraints allows to specify that a column can not contain a NULL value.
MySQL NOT NULL is used to CREATE and ALTER table

UNIQUE The UNIQUE constraint in MySQL does not allow to insert a duplicate value in column value in a
column. The UNIQUE constraints maintains the uniqueness of a column in a table. More than one
unique column can be used in a table

PRIMARY KEY A PRIMARY KEY constraint for a table enforces the table to accept unique data for a specific column
and this constraints create UNIQUE index for accessing a table faster

FOREIGN KEY A FOREIGN KEY in MySQL creates a link between two tables by one specific column of both tables.
The specific column in one table must be a PRIMARY KEY and referred by another column of
another table as a FOREIGN KEY.

CHECK A check constraints controls value in the associated column. The CHECK constraint determines if the
value is valid or not from logical expression

DEFAULT In MySQL table, each column must contain a value(including a NULL), while inserting data into a
table, if no value is supplied to a column, the the column gets the value set as the default

INDEX index is a data structure that is used to improve the performance of queries by providing quick access
to specific rows in a table. 87
1. Primary Key Constraints
A primary key serves as the unique identifier for rows in the table.
✓ Each primary key column's definition must include Not Null.
✓ A table definition can have no more than one primary key constraint.
✓ Each primary key column should not contain a duplicate value.
For a table with a primary key constraint, DBMS blocks any attempt to insert
or update a row that would cause two rows in the same table to have
identical value(s) for their primary key column(s).

2. Unique Constraints
✓ A table column with unique constraint should not contain a duplicate
value.
✓ A table can have more that one columns with unique key constraint
✓ It doesn't have to be defined with Not Null.
88
3. Foreign Key Constraints
• A foreign key constraint specifies how records in different tables are
related and how a DBMS should handle row insert, delete, and update
operations that might violate the relationship.
• For example, sales rows are generally related to the customers who place
the orders. Although it might be valid for a customer row to exist without
any corresponding sale rows, it would normally be invalid for a sale row not
to have a reference to a valid customer.
• With a relational DBMS, the relationship between rows in two tables is
expressed by a foreign key in the dependent table.
A foreign key is one or more columns that contain a value identical to a
primary key (or unique key) value in some row in the parent table (i.e., the
referenced table).

89
4. Check Constraints
Check constraints are used to enforce the validity of column
values.
For example, a check constraint can compare a column to a
constant, to another column in the same table or to an
expression.
DBMSs check to make sure a new or changed row doesn't violate
any of its table’s check constraints before an insert or update
operation is allowed.

90
5. Not Null constraint
A column defined with a NOT NULL constraint requires that for every row
entered into the table, there must be a value for that column.
For example, if the column in an employees table was defined as NOT
NULL, every employee entered into the table MUST have a value in the
column.
6. Default constraint
The DEFAULT constraint is used to insert a default value into a column.
The default value will be added to all new records, if no other value is
specified.
Defaults can’t be used on columns that are automatically increasing or
changing like timestamp and identity columns. However, they can be
used on duplicating rows.

91
Homework(Deadline 17 Jan/2023)
Using the Hospital database defined previously, develop a data dictionary
and indicate the following elements for each table
Tablename

Column name Data type Nullability Default Description

Note: Your work will be sent to your class monitor and he will put in single
folder, rename it as your class. He has to compress it and forward it to my
email: [email protected]

92
10/9/2023

LO 2:
DEFINE DATABASE
C

OBJECTS
RCA Basics of DB Slide: 93
DDL, DML, DCL & TCL commands
in SQL with syntax & examples

●DDL is Data Definition Language


●DML is Data Manipulation Language
●DCL is Data Control Language
●TCL is Transaction Control Language
Those are sub-type languages of SQL

94
SQL -commands list
DDL CREATE
RENAME
DROP
TRUNCATE
ALTER

DML INSERT
SELECT
UPDATE
DELETE

DCL GRANT
REVOKE

TCL START TRANSACTION


COMMIT
ROLLBACK
SAVEPOINT
95
DDL in SQL syntax
1. CREATE DATABASE
CREATE DATABASE DatabaseName;
2. CREATING A TABLE
CREATE TABLE table_name IF NOT EXISTS(
column1 datatype,
column2 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns ));
3. ADDING NEW Column to a table
ALTER TABLE TableName ADD ColumnNameData_Type;

96
DDL in SQL syntax
4. DELETING A COLUMN FROM A TABLE
ALTER TABLE TableName DROPColumnName;
5. MODIFYING EXISTING DATATYPE
ALTER TABLE TableName MODIFY COLUMN ColumnName Data_Type;
or
ALTER TABLE TableName CHANGE oldColumnName NewColumnName Data_Type;
6. DELETING A TABLE OR DATABASE
DROP TABLE TableName;
DROP DATABASE DatabaseName;
7. Deleting Every thing in a table
TRUNCATE TABLE TableName;
8. Comments
Single line comments: -- Any comment here
Multiline comment: /* Comments here */
97
DDL in SQL
DDL allows you to create SQL statements to make operations with database data
structures (schemas, tables etc.)
CREATE: This statement is used to create a new database, table,
index or stored procedure, …
DDL command in SQL
Examples:
1. CREATE DATABASE test_database;
2. CREATE TABLE user IF NOT EXISTS(
id INT(16) PRIMARY KEY AUTO_INCREMENT,
firstname VARCHAR(100) NOT NULL
);

98
DDL in SQL create cont’’
1. Creating a table with a default value
CREATE TABLE customers (
cust_id int(3),
first_name varchar(20),
last_name varchar(20),
gender varchar(5) default "male",
PRIMARY KEY (cust_id));
2. Create a table with a foreign key
CREATE TABLE book (
BookID int NOT NULL PRIMARY KEY,
BookName varchar(4) NOT NULL,
authoId int, Note: Create table with all options;
FOREIGN KEY REFERENCES author(authoID) https://dev.mysql.com/doc/refman/8.0/
); en/create-table.html
99
DDL in SQL create cont’’
1. Creating a table with UNIQUE
CREATE TABLE products
(prod_id int NOT NULL PRIMARY KEY,
product_name varchar(20) NOT NULL UNIQUE,
Product_type varchar(10) NOT NULL,
Quantity int(20));
2. Create a table person with a UNIQUE KEY
CREATE TABLE person (
personID int NOT NULL,
firstName varchar(10) NOT NULL,
LastName varchar(10) NOT NULL,
Phone varchar(10) UNIQUE,
Email varchar(15) NOT NULL UNIQUE,
PRIMARY KEY (personID));
100
Database command in SQL(Cont…)

DROP: DROP statement allows you to remove database, table,


index, stored procedure or any other database object.
Examples:
1. DROP DATABASE test_database;
2. DROP TABLE user;

101
Database command in SQL(Cont…)

ALTER: ALTER is used to modify existing database data structures (database, table)
Example:
➢ ADDING NEW Column to the table
ALTER TABLE user ADD lastname VARCHAR(100) NOT NULL;
➢ To specify the column location
ALTER TABLE user ADD lastname VARCHAR(100) NOT NULL AFTER firstname;

➢ TO CHANGE THE COLUMN NAME OR DATATYPE OR NUMBER OF CHARACTERS


ALTER TABLE student CHANGE lastname lastname varchar(50) NOT NULL;
➢ TO DELETE A COLUMN IN A TABLE
ALTER TABLE student DROP gender;
102
Database command in SQL(Cont…)
➢ RENAME: RENAME command is used to rename SQL table
Example:
RENAME TABLE user TO student;
➢ ADDING FOREIGN KEY to the EXISTING TABLE
ALTER TABLE Orders ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

➢ Defining a FOREIGN KEY constraint on multiple columns,


use the following SQL syntax:
ALTER TABLE Orders ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
➢ DROP a FOREIGN KEY Constraint
ALTER TABLE Orders DROP FOREIGN KEY FK_PersonOrder;

103
Database command in SQL(Cont…)
➢ Note: You can check the created FOREIGN KEY CONSTRAINT by issuing this query
SELECT
TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME,
REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME = 'ReferenceTableName’; -- “Put reference table”
➢ SYNTAX for dropping FOREIGN KEY is:
ALTER TABLE tablename DROP FOREIGN KEY foreign_key_name;
➢ DROP a FOREIGN KEY Constraint
ALTER TABLE Orders DROP FOREIGN KEY FK_PersonOrder;

104
Add constraints
1. Check constraint
Check if the added student have 18 years old
ALTER TABLE student change dob dob date CHECK
(YEAR(NOW())-YEAR(dob))>=18);
--dob: date of birth
2. CREATE TABLE publisher(
pub_id int PRIMARY KEY AUTO_INCREMENT,
publisher_name varchar(20),
published_date date,
age int CHECK(age>18),
phone varchar(10) UNIQUE);
105
2. UNIQUE constraint
1. Using creation table statement
CREATE TABLE IF NOT EXISTS doctor(
doctorId int NOT NULL PRIMARY KEY AUTO_INCREMENT,
… Telephone INT NOT NULL UNIQUE );
2. Add a unique column for an existing table
Add a telephone to Doctor table and make it UNIQUE
ALTER TABLE doctors ADD COLUMN telephone INT NOT NULL UNIQUE;
3. Change the available column in a UNIQUE.
Let us consider the autel or pubtel in library database
ALTER TABLE authors CHANGE pubtel pubtel INT NOT NULL UNIQUE ;

106
3. FOREIGN KEY CONSTRAINT

1. Using create statement


CREATE TABLE book (

ISBN VARCHAR(10) NOT NULL PRIMARY KEY,

title varchar(50) NOT NULL, pubId int,

CONSTRAINT pub_identity FOREIGN KEY (pubId ) REFERENCES publishers(pubId ) );

2. Using ALTER To change the available column in a foreign key


ALTER TABLE book ADD CONSTRAINT FK_publisher FOREIGN KEY (pubId) REFERENCES
publishers(pubId);
Or
ALTER TABLE book ADD FOREIGN KEY (pubId) REFERENCES Publishers(pubId);

107
DDL command in SQL(Cont…)
●TRUNCATE: TRUNCATE operation is used to delete all table records.
Logically it’s the same as DELETE command.

Differences between DELETE and TRUNCATE commands are:


1. TRUNCATE is really faster
2. TRUNCATE cannot be rolled back
3. TRUNCATE command does not invoke ON DELETE triggers
Example: TRUNCATE student;

108
SHOWING DATABASES/TABLES

1. To list available databases


SHOW DATABASES;
2. To select a database
USE databasename;
3. To list tables
SHOW TABLES;
4. To describe a table
DESCRIBE tablename;
5. To show the SQL used in table creation
SHOW CREATE TABLE tablename;
109
Concurrency Control
• Concurrency Control in DBMS is a procedure of managing simultaneous
transactions ensuring their atomicity, isolation, consistency, and serializability.
• In a multi-user database system, multiple transactions may attempt to access or
modify the same data simultaneously, and without proper control, this can lead
to inconsistencies, errors, or even data corruption.
• Types of concurrency control:
1. Two-phase locking (2PL): In 2PL, transactions acquire locks on data items before
accessing them, and hold these locks until they are ready to commit.
2. Timestamp ordering: In timestamp ordering, each transaction is assigned a
unique timestamp when it begins. When transactions request access to data
items, the system checks the timestamps to determine whether the requested
data has been modified by a more recent transaction. If a conflict is detected,
the older transaction is rolled back and retried

10/9/2023 RCA 110


Concurrency Control cont’’
3. Multi-version concurrency control (MVCC): In MVCC, each transaction works
with a snapshot of the database at the time it began, rather than the most
current version of the data. This allows multiple transactions to work with the
same data simultaneously without causing conflicts.
4. Optimistic concurrency control (OCC): In OCC, transactions do not acquire
locks on data items before accessing them. Instead, they check for conflicts
during the commit phase, and if any conflicts are detected, the transaction is
rolled back and retried.
5. Snapshot isolation: In snapshot isolation, each transaction works with a snapshot
of the database at the time it began, and any changes made by other
transactions are not visible to it until it commits. This can reduce the need for
locking and improve performance, but can also lead to issues with consistency.

10/9/2023 RCA 111


DML command in MYSQL
DML stands for Data Manipulation Language which basically deals
with the modification of data in the database.
1. Select: select keyword in MySQL is used to retrieve data from a
table.
2. Insert: insert or add data into a table.
3. Update: update the existing data in a table.
4. Delete: Delete all records from a database table.
5. Merge: It is a kind of insert or update operation.
6. Call: call statement is used to call a PL/SQL or any subprogram.
7. Explain plan: interpretation of the data access path.
8. Lock table: concurrency Control.
112
DML command in MYSQL Syntax
1. Adding data into a table
INSERT INTO <table_name>
(column1,column2,column3,….,columnn)
values
(<value1>,<value2>,<value3>…….,<valuen>);
2. Displaying the content from a table
Select column1,column2,…..column n from table_table;
Or
Select column1,column2,…..column n from table_table WHERE
<condition>;
3. CHANGE THE EXISTING VALUES OF THE COLUMN
Update <table_name> set <column_name>=value where <condition>;
113
DML command in MYSQL Cont’
CHANGE THE EXISTING VALUES OF THE COLUMN
1. Changing a single column’s data
UPDATE student SET lastname=claudien WHERE st_id=10;
2. Changing multiple columns’ data
UPDATE student SET lastname=Claude, age=10 WHERE st_id=10;

114
DML command in MYSQL Syntax
4. Delete a record from a table
DELETE FROM <table_name> WHERE <condition>;
5. Delete all records from a table
DELETE FROM <table_name>;
6.MERGE
MySQL Merge is a MySQL statement which allows us to update records in a specific table
on the basis of values that matches from another database table.
Merge TargetTableName USING SourceTableName
ON Merging_Condition
WHEN MATCHED
THEN Update_Query
WHEN NOT MATCHED
THEN Insert_Query
WHEN NOT MATCHED BY SOURCE
THEN DELETE;
115
DML in SQL
DML is a Data Manipulation Language, it’s used to build SQL queries to
manipulate (select, insert, update, delete etc.) data in the database.
• INSERT: INSERT command is used to add new
rows into the database table.
Example:
INSERT INTO students (fname, lastname)
VALUES ('James', 'Muvara');
• SELECT: SELECT query is used to retrieve a
data from SQL tables.
Example:
1. SELECT * FROM students;
2. SELECT lastname FROM students;
116
DML in SQL(cont…)
DELETE: DELETE query removes entries from the table.
Example:
1. DELETE FROM students; #delete all records
2. DELETE FROM students WHERE fname=”James”;
#delete records where attribute fname has value “James”

UPDATE: UPDATE statement modifies records into the table.


Example:
UPDATE students SET fname = 'Claude' WHERE lastname = 'Muvara';

117
10/9/2023

DBMS
CONFIGURATION ANDC

CUSTOMIZATION
MySQL users and permissions
RCA Basics of DB Slide: 118
User Configurations
MySQL is an open-source database management software that helps
users store, organize, and later retrieve data.
It has a variety of options to grant specific users with permissions within the
tables and databases.

In the previous section, we did all of the editings in MySQL as the root user,
with full access to all of the databases. However, in cases where more
restrictions may be required, there are ways to create users with custom
permissions.

119
Create a new User

To make a new user within the MySQL shell; authenticate as a root user
and type the following:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

Note: When adding users within the MySQL shell , we specify the user’s host
as localhost and not the server’s IP address. localhost is a hostname which
means “this computer,”.

At this point newuser has no permissions to do anything with the databases.


In fact, even if newuser tries to login (with the password, password), they will
not be able to reach the MySQL shell.

120
Password policy
The MySQL database ships with a validate_password plugin which when enabled, enforces a
password validation policy. There are 3 levels of password validation policy that are enforced by the
plugin.
LOW: Allows users to set a password of 8 or fewer characters.
Example: rcaa2019
MEDIUM: Allows users to set a password of 8 or fewer characters with mixed cases and special
characters.
Example Rca@2019
STRONG: Allows users to set a password that has all the attributes of a medium-level password with the
inclusion of a dictionary file.
For MySQL databases, to meet the minimum conditions required for a strong password, the password
must contain at least:
✓ Ten characters
✓ Two uppercase letters
✓ Two lowercase letters
✓ Two numbers
✓ Two of the following allowed special characters:
‘ ~ ! @ # $ % ^ & * ( ) _ - + = { } [ ] / < > , . ; ? ' : | (space)
To change the password validation policy use the following command:
mysql> SET GLOBAL validate_password_policy=LOW;(OR MEDIUM; OR |STRONG;)
121
Check if validate_password is enabled
After authenticating as root user, type the following:

SELECT PLUGIN_NAME, PLUGIN_STATUS FROM


INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE 'validate%';

To install the plugin if not available use the following:


INSTALL PLUGIN validate_password SONAME 'validate_password.so';// for
Linux
INSTALL PLUGIN validate_password SONAME 'validate_password.dll';// for
Windows

Note: If the version of mysql installed is not supporting plugin installation


check and install mysql on mysql.com
https://dev.mysql.com/downloads/windows/installer/
122
Check User Password requirements

123
Change password policy variables
mysql>SET GLOBAL validate_password.policy=STRONG,
validate_password.special_char_count=2,validate_password.number_coun
t =2,validate_password.length=9,validate_password.mixed_case_count=2;

124
Revert to MySQL default
mysql> SET GLOBAL validate_password.policy=MEDIUM,
validate_password.special_char_count=1,validate_password.number_count =1,
validate_password.length=8,validate_password.mixed_case_count=1;

125
Set up a new user with a valid Password

• CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'passworD!123';

To change the user password. Let us consider the root user


>ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyN3wP4ssw0rd’;
>flush privileges;
>exit;

126
✓ To change an account password from the command line, use the
mysqladmin command:

mysqladmin -u user_name -h host_name password “Newpassword“

✓ To check available users with their password:


select user, password from mysql.user;

127
GRANT PRIVILEGES
The newuser created has no permissions to do anything
with the databases. If newuser tries to login (with the
password, password), he will not be able to reach the
MySQL shell. Therefore, the first thing to do is to provide
the user with access to the information they will need.
mysql>GRANT ALL PRIVILEGES ON * . * TO
'newuser'@'localhost';
The asterisks in this command refer to the database and
table (respectively) that they can access—this specific
command allows to the user to read, edit, execute and
perform all tasks across all the databases and tables.
128
GRANT PRIVILEGES cont’
Please note that in this example; we are granting
newuser full root access to everything in our database.
While this is helpful for explaining some MySQL concepts, it
may be impractical for most use cases and could put
your database’s security at high risk.
Once you have finalized the permissions that you want to
set up for your new users, always be sure to reload all the
privileges.
mysql>FLUSH PRIVILEGES;
Your changes will now be in effect.
129
Grant Different User Permissions

Here is a short list of other common possible permissions that users can enjoy.
✓ ALL PRIVILEGES- as we saw previously, this would allow a MySQL user full
access to a designated database (or if no database is selected, global
access across the system)
✓ CREATE- allows them to create new tables or databases or other objects
✓ DROP- allows them to them to delete tables or databases
✓ DELETE- allows them to delete rows from tables
✓ INSERT- allows them to insert rows into tables
✓ SELECT- allows them to use the SELECT command to read through databases
✓ UPDATE- allow them to update table rows
✓ GRANT OPTION- allows them to grant or remove other users’ privileges

130
GRANT SPECIFIC PERMISSION
To provide a specific user with a permission, you can use this framework:
mysql>GRANT type_of_permission ON database_name.table_name TO
'username'@'localhost';
Example1: Granting SELECT permission to a specific table in Hospitaldb
GRANT SELECT ON hospitaldb.patient TO 'username'@'localhost';
Example2: Granting SELECT permission to all tables in Hospitaldb
GRANT SELECT ON hospitaldb.* TO 'username'@'localhost';
If you want to give them access to any database or to any table, make sure to
put an asterisk (*) in the place of the database name or table name.
Each time you update or change a permission be sure to use the Flush
Privileges command.
>flush privileges;
131
WITH GRANT OPTION

If you need to revoke a permission, the structure is almost identical to granting


it:
mysql>REVOKE type_of_permission ON database_name.table_name
FROM 'username'@'localhost';
Note that when revoking permissions, the syntax requires that you use

FROM, instead of TO as we used when granting permissions.


Example1: REVOKING SELECT permission to all tables in Hospitaldb
REVOKE SELECT ON hospitaldb.* FROM 'username'@'localhost';
Example2: REVOKING ALL permission to all Databases and tables
REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'localhost';

132
REVOKING PERMISSION
If you need to revoke a permission, the structure is almost identical to granting
it:
mysql>REVOKE type_of_permission ON database_name.table_name
FROM 'username'@'localhost';
Note that when revoking permissions, the syntax requires that you use

FROM, instead of TO as we used when granting permissions.


Example1: REVOKING SELECT permission to all tables in Hospitaldb
REVOKE SELECT ON hospitaldb.* FROM 'username'@'localhost';
Example2: REVOKING ALL permission to all Databases and tables
REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'localhost';

133
Drop user/check authentication of a new user

You can use DROP to delete a user altogether:

mysql>DROP USER 'username'@'localhost';

To test out your new user, log out by typing:

mysql>quit

and log back in with this command in the terminal:

mysql>mysql -u [username] -p

134
Rename MySQL Database with Command Line

1. First, take a backup of the current database:


c:/xampp/mysql/bin>mysqldump -u root -p old_db >
old_db.sql
2. Then create a new database with the desired name in the
MySQL server.
c:/xampp/mysql/bin >mysqladmin -u root -p create new_db
3. Finally restore the backup taken above to the newly created
database.
c:/xampp/mysql/bin >mysql -u root -p new_db < old_db.sql

135
Troubleshoot related errors
Q1: #1194 - Table 'tablename' is marked as crashed and should be
repaired
solution:
c:/xampp/mysql/bin>mysqlcheck -u root -p --auto-repair --check --all-
databases

Q2: error 1030 (hy000): got error 122 from storage engine
solution: Restart mysql server
https://www.zen-cart.com/showthread.php?105864-1030-Got-error-122-
from-storage-engine-!-!help!
136
Troubleshoot related errors
Q3: # mysqli::real_connect(): (HY000/1045): Access denied for user
'root'@'localhost' (using password: NO)
Answer: Open my.ini from the bin folder, add skip-grant-tables under
[mysqld]

137
Exporting a database in MySQL
• To create a dump file from the command line, you can use the mysqldump
command:
• mysqldump –u ‹username› –p ‹password› database_name
table_name > dumpfile_name.sql
Where:
• username and password are your credentials to connect to a
MySQL server
• database_name is the name of the database you want to dump
• table_name is an optional parameter. You can specify it when you
need to back up a certain table
• ">" states that you back up the database. This sign "<" is used for
showing that you restore the database
• dumpfile_name.sql is the name of the dump file
• Example:
C:\xampp\mysql\bin>mysqldump -u root -p year1a>year1db.sql

10/9/2023 RCA 138


Importing a database in MySQL
• You can also import MySQL data with the command-line interface using the
mysql program:
• mysql –u ‹username› –p ‹password› database_name
<import_file_name.sql
Where:
• username and password are your credentials to connect to a
MySQL server
• database_name is the name of the database you want to import
• "<" states that you import the database
• import_file_name.sql is the name of the import file
• Example:
C:\xampp\mysql\bin>mysql -u root -p year1a<year1db.sql

10/9/2023 RCA 139


Cloning or Copying a Table
When you just want to create an exact copy or clone of an existing
table to test or perform something without affecting the original
table.
Step 1: Creating an Empty Table
CREATE TABLE new_table LIKE original_table;
Step 2: Inserting Data into Table
INSERT INTO new_table SELECT * FROM original_table;
Simple Cloning
CREATE TABLE new_table SELECT * FROM original_table;

10/9/2023 RCA 140


Cloning or Copying a Table Cont’’
MySQL copy table to another database:
Step1: Creating a Table
CREATE TABLE destination_db.new_table LIKE source_db.existing_table;
Step2: copying data into new created table
INSERT destination_db.new_table
SELECT * FROM source_db.existing_table;

10/9/2023 RCA 141


Temporary Tables
A TEMPORARY table is visible only within the current session, and is
dropped automatically when the session is closed.
CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl;

10/9/2023 RCA 142


Referential actions
Referential actions are policies that define how a referenced
record is handled by the database when you run an update or
delete query.
Referential actions are features of foreign key constraints that
exist to preserve referential integrity in your database.
Referential actions are:
✓ No Action
✓ Restrict
✓ Cascade
✓ Set Null
10/9/2023 RCA 143
Referential actions Cont’’
1. CASCADE: Delete or update the row from the parent table and
automatically delete or update the matching rows in the child table. Both
ON DELETE CASCADE and ON UPDATE CASCADE are supported.
Example:
CREATE TABLE parent (
id INT NOT NULL PRIMARY KEY, name VARCHAR(10));

CREATE TABLE child (


id INT, parent_id INT, name VARCHAR(10),
INDEX par_ind (parent_id),
PRIMARY KEY(id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE ON UPDATE CASCADE);

10/9/2023 RCA 144


Referential actions Cont’’
2. SET NULL: Delete or update the row from the parent table and set the
foreign key column or columns in the child table to NULL. Both ON DELETE
SET NULL and ON UPDATE SET NULL clauses are supported.
Example:
CREATE TABLE parent (
id INT NOT NULL PRIMARY KEY, name VARCHAR(10));

CREATE TABLE child (


id INT, parent_id INT, name VARCHAR(10),
INDEX par_ind (parent_id),
PRIMARY KEY(id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE SET NULL ON UPDATE SET NULL);

10/9/2023 RCA 145


Referential actions Cont’’
3. RESTRICT: Rejects the delete or update operation for the parent table.
Specifying RESTRICT (or NO ACTION) is the same as omitting the ON
DELETE or ON UPDATE clause.

Example:
CREATE TABLE parent (
id INT NOT NULL PRIMARY KEY, name VARCHAR(10));

CREATE TABLE child (


id INT, parent_id INT, name VARCHAR(10),
INDEX par_ind (parent_id),
PRIMARY KEY(id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE RESTRICT ON UPDATE RESTRICT);
10/9/2023 RCA 146
Referential actions Cont’’
4. NO ACTION: A keyword from standard SQL. In MySQL, equivalent to
RESTRICT. The MySQL Server rejects the delete or update operation for
the parent table if there is a related foreign key value in the referenced
table.
Example:
CREATE TABLE parent (
id INT NOT NULL PRIMARY KEY, name VARCHAR(10));

CREATE TABLE child (


id INT, parent_id INT, name VARCHAR(10),
INDEX par_ind (parent_id),
PRIMARY KEY(id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE NO ACTION ON UPDATE NO ACTION);
10/9/2023 RCA 147
Referential actions Cont’’
You can obtain information about foreign keys from the Information Schema
KEY_COLUMN_USAGE table.
mysql> SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME,
CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA IS NOT NULL;
Results of the above query:

10/9/2023 RCA 149


10/9/2023

LO 3: MANIPULATE
DATABASES
C

RCA Basics of DB Slide: 150


10/9/2023

SELECT
STATEMENT
C

RCA Basics of DB Slide: 151


SQL -SELECT
The SQL SELECT statement is used to fetch the data from a database table which
returns this data in the form of a result table. These result tables are called result
sets.

Syntax
The basic syntax of the SELECT statement is as follows −
SELECT column1, column2, columnN FROM table_name;
select AttrExpr [[ as ] Alias ] {, AttrExpr [[ as ] Alias ] }
from Table [[ as ] Alias ] {, [[ as ] Alias ] }
[ where Condition ]

If you want to fetch all the fields available in the field, then you can use the
following simplified syntax.
SELECT * FROM table_name;
10/9/2023 RCA 152
Alias
SELECT with alias is 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 that query.
An alias is created with the AS keyword.
Alias Column Syntax
SELECT column_name AS alias_name
FROM table_name;
Example
SELECT provinceId as ID, provinceName as province from provinces;

153
Table aliases
Alias Table Syntax
SELECT column_name(s)
FROM table_name AS alias_name;
Example
mysql> SELECT pro.provinceId as ID, pro.provinceName as province from
provinces pro;

154
SELECT WITH CONDITION (WHERE Clause)
The SQL WHERE clause is used to specify a condition while fetching the data from a single table or by joining
multiple tables. If the given condition is satisfied, then only it returns a specific value from the table. You should
use the WHERE clause to filter the records and fetch only the necessary records.
The WHERE clause is not only used in the SELECT statement but is also used in the UPDATE, DELETE statement, etc.,
which we would examine in the subsequent chapters.
WHERE Syntax
SELECT column1, column2, ... Consider the following employee table
FROM table_name
WHERE condition; ID Name Age Address Salary
Create an employee table and add the records in
the right table 1 Charles 32 Kigali 1000
CREATE TABLE `employee` (
`id` int NOT NULL AUTO_INCREMENT, 2 Peter 25 Nyabihu 3000
`name` char(30) NOT NULL,
`age` int NOT NULL, 3 Mary 24 Kigali 2000
`address` char(40) NOT NULL,
`salary` int NOT NULL, 4 Ange 18 Nyabihu 5000
PRIMARY KEY (`id`));
155
The SQL WHERE Clause
The following code is an example which would fetch the ID, Name and Salary fields from
the employee table, where the salary is greater than 2000 −
MySQL> SELECT ID,Name,Address,Salary
FROM employee
WHERE Salary > 2000;

Output
ID Name Age Address Salary

2 Peter 25 Nyabihu 3000

4 Ange 18 Nyabihu 5000

156
The SQL WHERE Clause

The following query is an example, which would fetch the ID, Name and Salary
fields from the employee table for a customer with the name Ange.
Here, it is important to note that all the strings should be given inside single
quotes (''). Whereas, numeric values should be given without any quote as in the
above example.
SQL> SELECT ID,Name,Address,Salary
FROM employee
WHERE Name= ‘Ange’;

Output

ID Name Age Address Salary

4 Ange 18 Nyabihu 5000


157
Operators in WHERE clause
OPERATOR DESCRIPTION EXAMPLE

= Equal SELECT * FROM employee


WHERE age = 25;
> Greater than SELECT * FROM employee
WHERE age > 25;
< Less than SELECT * FROM employee
WHERE age < 25;
>= Greater than or equal SELECT * FROM employee
WHERE age >= 25;
<= Less than or equal SELECT * FROM employee
WHERE age <=25 158
Operators in WHERE clause

OPERATOR DESCRIPTION Example

<> Not equal. SELECT * FROM employee


!= WHERE age <>25

BETWEEN Between a certain range SELECT * FROM employee


WHERE age BETWEEN 20 AND
30
IN To specify multiple possible SELECT * FROM employee
values for a column WHERE Address IN(‘Kigali’,
‘Musanze’)
LIKE Search for a pattern
159
LIKE IN WHERE Clause

The SQL LIKE clause is used to compare a value to similar values using wildcard
operators. There are two wildcards used in conjunction with the LIKE operator.
• The percent sign (%)
• The underscore (_)
The percent sign represents zero, one or multiple characters. The underscore
represents a single number or character. These symbols can be used in
combinations.
Now play around with different options and observe the results.
For example:
1. WHERE sectorName = 'term'
2. WHERE sectorName like 'term%'
3. WHERE sectorName like '%term%'
4. WHERE sectorName LIKE 'term%'
5. WHERE sectorName LIKE 'term'

160
LIKE Clause
SELECT FROM table_name
WHERE column LIKE 'XXXX%'
or
SELECT FROM table_name
WHERE column LIKE '%XXXX%'
or
SELECT FROM table_name
WHERE column LIKE 'XXXX_'
or
SELECT FROM table_name
WHERE column LIKE '_XXXX'
or
SELECT FROM table_name
WHERE column LIKE '_XXXX_'

161
LIKE Clause
Sr.No. Statement & Description

WHERE SALARY LIKE '200%'


1
Finds any values that start with 200.

WHERE SALARY LIKE '%200%'


2
Finds any values that have 200 in any position.

WHERE SALARY LIKE '_00%'


3
Finds any values that have 00 in the second and third positions.

WHERE SALARY LIKE '2_%_%'


4
Finds any values that start with 2 and are at least 3 characters in length.

WHERE SALARY LIKE '%2'


5
Finds any values that end with 2.

WHERE SALARY LIKE '_2%3'


6
Finds any values that have a 2 in the second position and end with a 3.

WHERE SALARY LIKE '2___3'


7
Finds any values in a five-digit number that start with 2 and end with 3.
162
AND Logical operator
In SQL, the AND operator is a logical operator used to combine two or more conditions in a query. It
is used in the WHERE clause of a SELECT statement to retrieve rows that satisfy all the conditions
specified.
The syntax for using the OR operator in SQL is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Example of using the OR operator in a SQL query:

SELECT * FROM customers WHERE country = 'USA’ AND city = ‘New York’;
Explanation:
This query retrieves all the rows from the "customers" table where the "country" column is
"USA" and the "city" column is "New York".

163
CONSIDER THE FOLLOWING DATABASE (Download from the link below)
https://docs.google.com/document/d/1PuOXt-q0gnQQnhy70iX_ar1l9BbLuC8o/edit?usp=share_link&ouid=113439904283695917085&rtpof=true&sd=true

Employee_role
Table

Employee Table

164
Write a query that will select the title AS POSITION
and salary as REMUNERATION
SELECT title AS POSITION, salary AS REMUNERATION FROM employee_role;

165
WRITE A QUERY THAT WILL DISPLAY THE fist_name
last_name and Salary of Gates

SELECT first_name,last_name,salary
FROM employee,
employee_role
WHERE
employee.id=41
AND
employee_role.id=employee.role_id;

166
/*Query to select from 3 tables
WRITE A QUERY THAT WILL DISPLAY THE fist_name
last_name, department_name and Salary of Gates*/ (Considering
employee_db)
SELECT e.first_name,
e.last_name, er.salary,d.name
FROM
employee as e,
employee_role as er,
department as d
WHERE
e.id=41
AND
er.id=e.role_id
AND
d.id=er.department_id;
167
WRITE A QUERY THAT WILL DISPLAY THE fist_name
last_name, department_name,salary of employees
WHOSE salary is great than 50000.

SELECT e.first_name,e.last_name,
d.name as Department,er.salary
FROM
employee as e, employee_role as er,
department as d
WHERE
er.id=e.role_id
AND
er.department_id=d.id
AND
er. salary>50000;
168
ORDER BY clause
The ORDER BY clause is used to sort the result set in ascending or
descending order.
The ORDER BY clause sorts the records in ascending order by default. To
sort the records in descending order, use the DESC keyword.
SYNTAX:
SELECT column_name(s) FROM table_name ORDER BY column_name(s)
ASC|DESC

Example:
SELECT * FROM districts ORDER BY districtName ASC;
169
MySQL Limit Clause

MySQL provides a LIMIT clause that is used to specify the number of records
to return.
The LIMIT clause makes it easy to code multi page results or pagination with
SQL, and is very useful on large tables. Returning a large number of records
can impact on performance.
Assume we wish to select all records from 1 - 30 (inclusive) from a table
called "sectors". The SQL query would then look like this:
SELECT * FROM sectors LIMIT 30;
When the SQL query above is run, it will return the first 30 records.

170
MySQL Limit Clause
What if we want to select records 16 - 25 (inclusive)?
Mysql also provides a way to handle this: by using OFFSET.

The SQL query below says "return only 10 records, start on record 16 (OFFSET 15)":
SELECT * FROM sectors LIMIT 10 OFFSET 15

You could also use a shorter syntax to achieve the same result:
SELECT * FROM sectors LIMIT 15, 10
171
SELECT DISTINCT
The SELECT DISTINCT statement 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.
SELECT DISTINCT Syntax:

SELECT DISTINCT column1, column2, ...

FROM table_name;
Example:
Display sector names without duplication
SELECT DISTINCT sectorName FROM sectors
172
Exercise
1. Searching sectors
Create a Database rwanda and import the administration entities through the
link provided
https://drive.google.com/file/d/11crIfuW1mmIrco5CyHIRIjPxtqdgwTqo/vi
ew?usp=sharing
Note: To import the database, create the database rwanda and place admin_entities.sql file in bin directory of your
mysql DBMS
For windows
XamppFor MySQL
C:/Xampp/mysql/bin
tools
C:/Program files/MySQL/MySQL Server 8/bin
To import, Write the following:
….bin>mysql -u username -p rwanda<admin_entities.sql
FOR LINUX
place the file in the Home directory
username:~$ mysql -u username -p rwanda<admin_entities.sql 173
Exercises-next
a. Display districts in Northern and Western with names
starting with ‘RU’ and sort by district Name
b. WRITE a query to display 30 SECTORS ONLY
c. Display sectors that contain the characters ‘KW’
d.Display sectors that have the third and fourth
characters as ‘GA’
e. Display district in DESCENDING order;

174
Assignment (to be submitted 2-May-2023)
a. Create database called “Employeedb”
b. Import the database found on: https://docs.google.com/document/d/1PuOXt-
q0gnQQnhy70iX_ar1l9BbLuC8o/edit?usp=share_link&ouid=113439904283695917085&rtpof=true&s
d=true
c. Write an SQL query to display 10 employees only
d. Write a query to display employee’s information whose last name contains “an”
e. Display all employees that have the third and fourth characters in the first name as ‘an’ and sort
them in DESCENDING order.
f. Display in Ascending order all employees whose first name contains only three (3) characters;
g. Display 4 employees considering 2 as offset
h. Display employees’ names and their positions/roles whose salary ranges between 40000 and
80000
i. Display employee names, Department names, positions, and their salaries whose first name ends
with “s” and salary above 50000.
j. Write an SQL query to display salaries without duplication.

175
Assignment-Next
k. Write a query to employee’s information with their positions and salary,
sort your results based on first name in ascending order.
l. Write a query to select employee’s information with their department
who have maximum salary.
m.Write a query to select employee’s information who have minimum
salary
n. Write a query to select employee’s information whose salary is below
the average of all employees salary

176
10/9/2023

SELECT WITH
JOINS IN MYSQL
C

RCA Basics of DB Slide: 177


Definition
A JOIN clause is used to combine rows from two or more
tables, based on a related column between them and the
defined conditions.
A join tells the database to combine columns from different
tables. We normally join tables by matching the foreign keys in
one table to the primary keys in another
There are several types of JOIN clauses, including INNER JOIN,
LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.

178
TYPES OF JOIN
TYPE OF JOIN SHORT DESCRIPTION

Joins or inner joins Uses a comparison operator to match rows from two tables
that are based on the values in common columns from each
table.

Left join/left outer Returns all the rows from the left table that are specified in the
join left outer join clause, not just the rows in which the columns
match.

Right join/right outer Returns all the rows from the right table that are specified in
join the right outer join clause, not just the rows in which the
columns match.

Full outer join Returns all the rows in both the left and right tables.

Cross joins Returns all possible combinations of rows from two tables.
179
Sample Data- Companydb
--Creating the database
CREATE DATABASE companydb;
USE companydb;
-- Create a department table
CREATE TABLE department(
DepartmentID INT PRIMARY KEY NOT NULL,
DepartmentName VARCHAR(20));
-- Create an employee table
CREATE TABLE employee (
LastName VARCHAR(20),
DepartmentID INT REFERENCES department(DepartmentID));
--Inserting data into the department
INSERT INTO department
VALUES (31, 'Sales'), (33, 'Engineering'), (34, 'Management'),(35, 'Marketing');
-- Insert data into the employee table
INSERT INTO employee VALUES ('Ange', 31), ('Joy', 33),('Peter', 33), ('Robinson', 34),('Smith',
34),('Mary', NULL);
180
Sample Data- Companydb Cont’’
• Department table Employee tabl
> SELECT * FROM department > SELECT * FROM employee;

181
1. JOIN or INNER JOIN
An inner join requires each row in the two joined tables to have matching column
values and is a commonly used join operation in applications but should not be
assumed to be the best choice in all situations.
The INNER JOIN clause returns only the rows from both tables that have matching
values in the related columns.

182
Inner Join(next…)
SQL specifies two different syntactical ways to express joins: the "explicit join notation" and the "implicit join
notation". The "implicit join notation" is no longer considered a best practice, although database systems
still support it.
The "explicit join notation" uses the JOIN keyword, optionally preceded by the INNER keyword, to specify
the table to join, and the ON keyword to specify the predicates for the join, as in the following example:

(1)
SELECT employee.LastName, employee.DepartmentID, department.DepartmentName
FROM employee INNER JOIN department ON
employee.DepartmentID = department.DepartmentID;

183
Inner Join(next…)
(2)
SELECT employee.LastName, employee.DepartmentID, department.DepartmentName
FROM employee JOIN department ON
employee.DepartmentID = department.DepartmentID;

(3) USING keyword

We can also express JOIN through the USING keyword. The query returns only rows with matching
values in the column indicated in the USING clause and that column must exist in both tables.

SELECT employee.LastName, employee.DepartmentID, department.DepartmentName


FROM employee JOIN department USING (DepartmentID);

184
Inner Join(next…)
The "implicit join notation" simply lists the tables for joining, in the FROM
clause of the SELECT statement, using commas to separate them.
Thus it specifies a cross join and the WHERE clause may apply
additional filter-predicates (which function comparably to the join-
predicates in the explicit notation).
The following example is equivalent to the previous one, but this time
using implicit join notation:
SELECT employee.LastName, employee.DepartmentID,
department.DepartmentName
FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID;

185
Inner Join(next…)
Both queries (implicit and explicit) given will join the Employee and Department tables
using the DepartmentID column of both tables. Where the DepartmentID of these tables
match (i.e. the join-predicate is satisfied), the query will combine the LastName,
DepartmentID and DepartmentName columns from the two tables into a result row.
Where the DepartmentID does not match, no result row is generated.

Note : The employee "Mary" and the


department "Marketing" do not
appear in the query execution
results. Neither of these has any
matching rows in the other
respective table: "Mary" has no
associated department, and no
employee has the department ID 35
("Marketing").

186
Inner join and NULL values

You should take special care when joining tables on columns that can
contain NULL values, since NULL will never match any other value (not
even NULL itself), unless the join condition explicitly uses a combination
predicate that first checks that the joins columns are NOT NULL before
applying the remaining predicate condition(s). The Inner Join can only
be safely used in a database that enforces referential integrity or where
the join columns are guaranteed not to be NULL.
Many transaction-processing relational databases rely on Atomicity,
Consistency, Isolation, and Durability (ACID) data update standards to
ensure data integrity, making inner joins an appropriate choice.

187
2. OUTER JOIN
The joined table retains each row—even if no other matching
row exists. Outer joins subdivide further into:
1. left outer join,
2. right outer joins,
3. full outer joins,
Depending on which table's rows are retained: left, right, or
both (in this case left and right refer to the two sides of the
JOIN keyword).

188
2. Left outer join

The LEFT JOIN clause returns all the rows from the left table (i.e., the first table specified in the SQL
statement) and matching rows from the right table. If there are no matches, the result will contain
NULL values for the right table. The OUTER keyword is optional.
Example:
SELECT * FROM employee LEFT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;

189
3. Right outer join
The OUTER keyword is optional:
Example:
SELECT * FROM employee RIGHT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;

190
4. FULL OUTER JOIN

The FULL OUTER JOIN clause returns all the rows from both tables,
including any unmatched rows. If there is no match on either side, the
result will contain NULL values for the missing columns.
For those rows that do match, a single row will be produced in the result
set (containing columns populated from both tables).
For example:
SELECT * FROM employee FULL OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;

NOTE: FULL OUTER JOIN is not supported by MySQL DBMS

191
4. CROSS JOIN
CROSS JOIN returns the Cartesian product ( all possible combinations ) of rows from
tables in the join. In other words, it will produce rows which combine each row from the
first table with each row from the second table. All of the columns from one table are
followed by all of the columns from the other table. The result set usually does not make
sense as in this case. You must add conditions to further define what you want to obtain
from the cross joined information.

With a cross join, the number of rows in the resultant table is equal to the number of rows
in the first table times the number of rows in the second table. CROSS JOIN does not itself
apply any predicate to filter rows from the joined table.

192
1. CROSS JOIN CONT’’

The results of a CROSS JOIN can be


filtered using a WHERE clause, which
may then produce the equivalent of
an inner join.
Explicit Cross Join
SELECT * FROM employee CROSS
JOIN department;
Implicit Cross Join
SELECT * FROM employee,
department;
193
Practice Exercises
Question: Using rwanda administrative entities Database
1. Create a query returning all districts with their respective provinces(30 Records)
2. Create a query which returns all sectors with their respective districts and provinces(416
Records)
3. Create a query which returns all cells, their respective sectors in Nyabihu district(73 Records)
4. Create a query which returns all villages and respective cells in Mukamira Sector(35 records)
5. Create a query which returns all villages, cells, sectors, districts in Western province(3624
Records)

194
10/9/2023

SQL UNION
C

RCA Basics of DB Slide: 195


UNION
• The SQL UNION operator is used to combine the results of two or more SELECT
statements into a single result set.
• The syntax for the UNION operator is as follows:
SELECT column1, column2, ... FROM table1
UNION
SELECT column1, column2, ... FROM table2;
Here, the two SELECT statements should have the same number of columns, with corresponding
columns having compatible data types. The UNION operator will eliminate duplicate rows from the
result set by default.
“Let’s use the previously created companydb”
Insert new record in the employee table
> INSERT INTO employee (DepartmentID,LastName) values (34,'Management');

10/9/2023 RCA 196


UNION
• Example:
SELECT DepartmentID, DepartmentName FROM department
UNION
SELECT DepartmentID, LastName FROM employee;

10/9/2023 RCA 197


UNION ALL
• UNION ALL operator is similar to the UNION operator, but it does not remove duplicate rows
from the result set.
• The result set of a UNION ALL operation will include all the rows from both tables, including
any duplicate rows. The columns in the two SELECT statements must be of the same data
type or compatible data types.
• The syntax for the UNION ALL operator is as follows:
SELECT column1, column2, ... FROM table1
UNION ALL
SELECT column1, column2, ... FROM table2;
Example:
SELECT DepartmentID, DepartmentName FROM department
UNION ALL
SELECT DepartmentID, LastName FROM employee;

10/9/2023 RCA 198


UNION with ORDER BY
UNION operator can be used with the ORDER BY clause to sort the combined result set. The
ORDER BY clause is used to sort the rows in the result set by one or more columns.
The syntax for using UNION with ORDER BY is as follows:
SELECT column1, column2, ... FROM table1
UNION
SELECT column1, column2, ... FROM table2
ORDER BY column1, column2,…;
Example:
SELECT DepartmentID, DepartmentName FROM department
UNION ALL
SELECT DepartmentID, LastName FROM employee
ORDER BY DepartmentID ASC;

10/9/2023 RCA 199


SQL INTERSECT operator
INTERSECT operator is used to combine the results of two SELECT statements and returns only
the rows that are common to both result sets.
The INTERSECT operator requires that the columns in both SELECT statements have the same
data types and are in the same order. Also, the operator removes duplicates from the final
result set by default.
The syntax for the INTERSECT operator is as follows:
SELECT column1, column2, ... FROM table1
INTERSECT
SELECT column1, column2, ... FROM table2
Example:
SELECT DepartmentID FROM department
INTERSECT
SELECT DepartmentID FROM employee;

10/9/2023 RCA 200


10/9/2023

MANIPULATEC DATA USING


NESTED QUERIES

RCA Basics of DB Slide: 201


Nested queries
• Nested queries, also known as subqueries, are queries that are placed inside another
query. Nested queries are usually enclosed within parentheses and are written as a
SELECT statement inside another SELECT, INSERT, UPDATE, or DELETE statement. The result
of the nested query is then used as a parameter for the outer query.
• The result of the nested query is then used as a parameter for the outer query.
• The syntax of a nested query:
SELECT column_name1, column_name2, ... FROM table_name1 WHERE column_name1 OPERATOR
(SELECT column_name FROM table_name2 WHERE condition);
Here, table_name1 is the outer table, and table_name2 is the inner table. The
column_name1, column_name2, etc., are the columns that you want to select from the
outer table
The condition in the inner query is a condition that must be satisfied for the rows to be
selected from the inner table. The OPERATOR is a comparison operator, such as =, >, <,
>=, <=, <>, IN, LIKE, BETWEEN, or EXISTS.

10/9/2023 RCA 202


Types of Nested Queries in SQL
• Nested queries in SQL can be classified into two different types:
1. Independent Nested Queries
2. Co-related Nested Queries

2. Independent Nested Queries


In independent nested queries, the execution order is from the innermost
query to the outer query. An outer query won't be executed until its inner
query completes its execution. The outer query uses the result of the inner
query. Operators such as IN, NOT IN, ALL, and ANY are used to write
independent nested queries.

• The IN operator checks if a column value in the outer query's result is


present in the inner query's result. The final result will have rows that satisfy
the IN condition.

10/9/2023 RCA 203


Types of Nested Queries in SQL CONT’’
• The NOT IN operator checks if a column value in the outer query's
result is not present in the inner query's result. The final result will have
rows that satisfy the NOT IN condition.
• The ALL operator compares a value of the outer query's result with all
the values of the inner query's result and returns the row if it matches
all the values.
• The ANY operator compares a value of the outer query's result with
all the inner query's result values and returns the row if there is a
match with any value.
2. Co-related Nested Queries
In co-related nested queries, the inner query uses the values from the
outer query to execute the inner query for every row processed by
the outer query. The co-related nested queries run slowly because the
inner query is executed for every row of the outer query's result.
10/9/2023 RCA 204
Nested queries
• Note: The user can use N Number of Inner Queries to fetch the
required output. But using nesting of Queries is not a good
practice for performance tuning perspective.
• The general syntax of nested queries will be:
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE]
)
10/9/2023 RCA 205
Database to be used
--Creating the database
CREATE DATABASE employeedbok;
USE employeedbok;
-- Create an employee table
CREATE TABLE employee ( employee_id INT(2) PRIMARY KEY AUTO_INCREMENT, Employee_Name
VARCHAR(20), Department_name VARCHAR(15));
-- Create a salary table
CREATE TABLE salary(s_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, employee_id int(2), salary
int(5) , Paid_date DATE, FOREIGN KEY (employee_id) REFERENCES employee (employee_id));
-- Insert data into the employee table
INSERT INTO employee (Employee_name,Department_name) VALUES ('Ange', 'Sales'), ('Joy',
'Engineering'),('Peter', 'Engineering'), ('Robinson', 'Engineering'),('Smith', 'Marketing'),('Mary',
'Marketing');
--Inserting data into the department
INSERT INTO salary (employee_id, salary, Paid_date)VALUES (1, 30000, "2017-06-15"), (3, 70000, "2016-06-
15"), (4, 80000, "2017-06-15"),(2, 40000, "2019-06-15");
10/9/2023 RCA 206
SQL Nested Queries with SELECT Statement
• 1. You want to fetch all records of Employees whose salary is greater than 25000 by
using the subquery:
SELECT * FROM employee
WHERE employee_id
IN (SELECT employee_id FROM salary WHERE salary > 25000);

10/9/2023 RCA 207


SQL Nested Queries with INSERT Statement
Syntax :
INSERT INTO Tablename
(SELECT Column_1,Column2….FROM Tablename_1 CONDITION);
• User need to create the replica of Employee table and needs the data
where salary is greater than 25000. The Employee table replica name is
Employee_Bkp
• Step 1: CREATE TABLE Employee_Bkp LIKE employee;
• Step2:
INSERT INTO Employee_Bkp
(SELECT * FROM employee WHERE employee_id
IN (SELECT employee_id FROM salary WHERE salary > 25000));

10/9/2023 RCA 208


SQL Nested Queries with UPDATE Statement
• Syntax :
UPDATE TABLE_NAME SET Column_name =
(SELECT column1,Column2….FROM table CONDITION);

• User wants to change the name of Employee to GIRAMATA where


the Salary of that Employee is 30000.
UPDATE employee SET Employee_name='GIRAMATA’
WHERE employee_id= (SELECT employee_id FROM salary
WHERE salary=30000);

10/9/2023 RCA 209


SQL Nested Queries with DELETE Statement

• Syntax :
DELETE FROM TABLE_NAME Where Column_Name Operator
(SELECT column1,Column2….FROM table CONDITION);
• We need to delete data from Employee table where salary is greater
than 25000.

DELETE FROM employee


WHERE
employee_id= (SELECT employee_id FROM salary WHERE
salary=25000);
• Reference: https://www.scaler.com/topics/nested-sql-query/

10/9/2023 RCA 210


SQL Nested Queries with ANY Operator
• The ANY operator:
• returns a Boolean value as a result
• returns TRUE if ANY of the subquery values meet the condition
• ANY means that the condition will be true if the operation is true for any of the
values in the range.
• ANY Syntax:
• SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);
• The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=)

10/9/2023 RCA 211


SQL Nested Queries with ANY Operator

• The ANY operator:


Write a query to fetch any records of Employees whose salary is greater
than 25000 by using the subquery.
SELECT * FROM employee
WHERE employee_id = ANY
(SELECT employee_id FROM salary WHERE salary > 25000);

10/9/2023 RCA 212


SQL Nested Queries with ALL Operator
• The ALL operator:
• returns a boolean value as a result
• returns TRUE if ALL of the subquery values meet the condition
• is used with SELECT, WHERE and HAVING statements
ALL means that the condition will be true only if the operation is true for all values
in the range
ALL Syntax With WHERE or HAVING
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);
The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).

10/9/2023 RCA 213


SQL Nested Queries with ALL Operator

Write a query to fetch ALL records of Employees whose salary is greater than
25000 by using the subquery.
SELECT * FROM employee
WHERE employee_id = ALL
(SELECT employee_id FROM salary WHERE salary > 25000);

SELECT * FROM employee WHERE employee_id > ALL (SELECT employee_id FROM salary WHERE salary > 25000);

10/9/2023 RCA 214


SQL Nested Queries with EXISTS or NOT EXISTS operator

• The SQL EXISTS and NOT EXISTS operators are used to test for the existence
of records in a sub-query.
Example: Write a query to fetch Employee_Name from Employees whose
salary is greater than 30000 by using the subquery.
SELECT Employee_Name FROM employee WHERE EXISTS
(SELECT employee_id FROM salary WHERE employee.employee_id
=salary.employee_id AND salary>30000);

10/9/2023 RCA 215


SQL Nested Queries with EXISTS or NOT EXISTS operator

Example: Write a query to fetch Employee_Name from Employees whose


salary is greater than 30000 by using the subquery and NOT EXISTS operator.
SELECT Employee_Name FROM employee WHERE NOT EXISTS
(SELECT salary FROM salary WHERE employee.employee_id
=salary.employee_id AND salary>30000);

10/9/2023 RCA 216


Noncorrelated and Correlated Subqueries

Subqueries can be categorized into two types:


• A noncorrelated (simple) subquery obtains its results
independently of its containing (outer) statement.
• A correlated subquery requires values from its outer
query in order to execute.

Source: https://www.geeksforgeeks.org/sql-correlated-subqueries/
10/9/2023 RCA 217
SELECT name, street, city, state FROM addresses WHERE state IN (SELECT state FROM states);

Non-correlated Subqueries

A noncorrelated subquery executes independently of the outer


query. The subquery executes first, and then passes its results to the
outer query, For example:

SELECT name, street, city, state FROM addresses WHERE state IN


(SELECT state FROM states);

10/9/2023 RCA 218


Co-related Nested Queries

A correlated subquery typically obtains values from its outer


query before it executes. When the subquery returns, it passes its
results to the outer query. For example:

Select all employees whose salary is above the average salary of


employees in their role.
SELECT * FROM employees emp1
WHERE salary > (
SELECT AVG(salary)
FROM employees emp2
WHERE emp1.role = emp2.role );

10/9/2023 Source: https://www.scaler.com/topics/nested-sql-query/


RCA 219
10/9/2023

DATA MANIPULATION
USING AGGREGATE
C

FUNCTIONS
RCA Basics of DB Slide: 220
Introduction
An aggregate function performs a calculation on a set of
values and returns a single value.
SQL provides many aggregate functions that include avg,
count, sum, min, max, etc.
Aggregate functions are often used with the GROUP BY clause
of the SELECT statement
Except for COUNT(*), aggregate functions ignore null values.

221
Some Basic Aggregate functions

Function Output

COUNT A number of rows containing a non-null values

MIN The minimum attribute value encountered in a given


column
MAX The maximum value encountered in a given column

SUM The sum of all values in a given column

AVG The arithmetic mean(average) of a specified column


222
COUNT
Note: Use employeedbok database.
The COUNT function is used to count the number of non-null values of an attribute. COUNT
can be used in conjunction with the DISTINCT clause.
Suppose you want to find out how many employees exist in the following employee table.
The following is the SQL code for the answer:
SELECT COUNT(Employee_Name) as Number_of_Employee FROM employee;
This will not consider a NULL value.

SELECT COUNT(*) AS Number_of_employee FROM employee; This will consider the null value
223
COUNT(next)
COUNT the number of employees with salary greater than 30000
SELECT COUNT(Employee_Name) AS number_of_employee FROM
employee WHERE employee_id IN (SELECT employee_id FROM
salary WHERE salary>30000);

224
COUNT(next)

In contrast, COUNT(*) will return the total


number of rows from the Query including
the rows that contain nulls.

SELECT COUNT(departmentID) FROM


employee;

SELECT COUNT(*) FROM employee;

225
MAX and MIN
The MAX and MIN help you to find answers such as the highest and lowest salary in the employee
table.
SQL
SELECT MAX(Salary) FROM salary;

You can also use numeric functions in conjunction with more complex queries.
For example which employee has the highest salary
SQL
SELECT * FROM employee WHERE employee_id IN (SELECT employee_id FROM salary WHERE salary
=(SELECT MAX(salary) FROM salary));

226
SUM
The SUM compute the total sum for any computed attribute, using any
condition(s) you have imposed. For example how much all employee are
paid:
SQL
SELECT SUM(salary) as TotalPayment FROM salary;

227
AVG
AVG function is similar to those of MIN or MAX and is used to return the arithmetic
mean(average) of a specified column.
For example: what is the average salary in the salary table?
SQL: SELECT AVG(salary) FROM salary;

Using ROUND
SQL: SELECT ROUND(AVG(salary),1) FROM salary;

228
AVG
Example: Which employees are paid a salary less than the average salary?
SQL: SELECT * FROM employee WHERE employee_id IN (SELECT employee_id FROM salary
WHERE salary <(SELECT AVG(salary) FROM salary));

229
GROUPING DATA
In previous examples, the aggregate functions summarize data across all
rows in the given tables. Sometimes, however, you do not want to treat
the entire table as a single collection of data for summarizing. Rows can
be grouped into smaller collections quickly and easily using the GROUP
BY clause within the SELECT statement. The aggregate function will
summarize the data within each smaller collection. See the syntax:
SELECT column list
FROM table list
[WHERE condition list]
[GROUP BY column list]
[ORDER BY column list|ASC|DESC]

230
GROUPING DATA cont’’
Example: Using sectors and districts tables, return the number of
sectors by district
SQL
SELECT d.districtName, COUNT(s.sectorId) as Numberofsectors FROM
sectors s INNER JOIN districts d ON s.districtId=d.districtId GROUP BY
d.districtId ORDER BY d.districtName ASC;

231
Derived tables
The results of a query can be treated as a new virtual or derived
table and can also be queried.

Example: From the previous Query, we can also treat SELECT


query results as a new derived table using an alias.

SELECT * FROM (SELECT d.districtName, COUNT(s.sectorId)


AS Numberofsectors FROM sectors s INNER JOIN districts d
ON s.districtId=d.districtId GROUP BY d.districtId ORDER BY
d.districtName ASC) AS disectors;
232
Exercises (Use Rwanda_entity database)
Q1.Write a query to return the number of villages by sector in Nyabihu district
Q2. Write a query to return the number of cells by district in Rwanda
Q3. Write a query that return the name of the district and the number of its sectors . The
district should have the maximum number of sectors.
• using max function.
• Using ORDER BY and LIMIT clause
Q4. Display top 5 districts with the biggest number of sectors.
Q5. Create the SUM of sectors in Western Province
Q6. Write a query that return the name of the district and the number of its sectors. The
district should have the minimum number of sectors.
Q7. Display districts and their respective number of sectors if the district has between 10 to 15
sectors
Q8. Display districts and their respective number of sectors if the district has 17 sectors
233
Sample answer .Q3. District with maximum number of sectors
SELECT districtName, Numberofsectors FROM (SELECT d.districtName,
COUNT(s.sectorId) as Numberofsectors FROM sectors s INNER JOIN districts d
(a) ON s.districtId=d.districtId GROUP BY d.districtId ORDER BY d.districtName
ASC) as disectors WHERE Numberofsectors=(SELECT MAX(Numberofsectors)
FROM (SELECT d.districtName, COUNT(s.sectorId) as Numberofsectors FROM
sectors s INNER JOIN districts d ON s.districtId=d.districtId GROUP BY
d.districtId ORDER BY d.districtName ASC) as dise);

SELECT districtName, Numberofsectors FROM (SELECT d.districtName,


COUNT(s.sectorId) as Numberofsectors FROM sectors s INNER JOIN
(b) districts d ON s.districtId=d.districtId GROUP BY d.districtId ORDER BY
Numberofsectors desc, d.districtName ASC) as disectors LIMIT 1;
or
SELECT d.districtName, COUNT(s.sectorId) as Numberofsectors FROM sectors
s INNER JOIN districts d ON s.districtId=d.districtId GROUP BY d.districtId
234
ORDER BY Numberofsectors desc, d.districtName ASC LIMIT 1;
SQL HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword
cannot be used with aggregate functions.
HAVING Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

10/9/2023 RCA 235


SQL HAVING Clause
Custom CustomerName ContactName Address City PostalCode Country
erID
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedados y Constitución
helados 2222
3 Antonio Moreno Antonio Mataderos México D.F. 05023 Mexico
Taquería Moreno 2312
4 Around the Horn Thomas Hardy 120 Hanover London WA1 1DP UK
Sq.
5 Berglunds snabbköp Christina Berguvsvägen Luleå S-958 22 Sweden
Berglund 8

Consider the above Customer table:


Write an SQL statement that lists the number of customers in each country. Only include
countries with more than 5 customers:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
10/9/2023 HAVING COUNT(CustomerID)
RCA > 5; 236
SQL HAVING Clause
Write an SQL statement listing the number of customers in each
country, sorted from high to low (Only include countries with more
than 5 customers:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

Note: https://www.w3schools.com/sql/sql_having.asp

10/9/2023 RCA 237


Difference between WHERE and HAVING
Comparison WHERE Clause HAVING Clause
Basis
Definition It is used to perform filtration on individual rows. It is used to perform filtration on
groups.
Basic It is implemented in row operations. It is implemented in column
operations.
Data fetching The WHERE clause fetches the specific data from The HAVING clause first fetches
particular rows based on the specified condition the complete data. It then separates
them according to the given
condition.
Aggregate The WHERE clause does not allow to work with aggregate The HAVING clause can work with
Functions functions. aggregate functions.

Act as The WHERE clause acts as a pre-filter. The HAVING clause acts as a post-
filter.
Used with We can use the WHERE clause with the SELECT, UPDATE, The HAVING clause can only use
and DELETE statements. with the SELECT statement.

GROUP BY The GROUP BY clause comes after the WHERE clause. The GROUP BY clause comes before
the HAVING clause.
10/9/2023 RCA 238
Difference between GROUP BY and HAVING
S.No Having Clause Group By Clause
.
1. It is used for applying some extra The group by clause is used to group
conditions to the query. the data according to a particular
column or row.
2. Having cannot be used without group by Group by can be used without having
clause, in an aggregate function, in that clause with the select statement.
case, it behaves like a where clause.

3. The having clause can contain aggregate It cannot contain aggregate


functions. functions.

4. It restricts the query output by using It groups the output on the basis of
some conditions some rows or columns.

10/9/2023 RCA 239


DATABASE VIEW
In a database, a view is the result set of a stored query on the data, which
the database users can query just as they would in a persistent database
collection object.
In SQL, a view is a virtual table based on the result set of an SQL statement.
This pre-established query command is kept in the database dictionary.
Unlike ordinary base tables in a relational database, a view does not form
part of the physical schema: as a result set, it is a virtual table computed or
collated dynamically from data in the database when access to that view
is requested. Changes applied to the data in a relevant underlying table
are reflected in the data shown in subsequent invocations of the view.

240
DATABASE VIEW
Some of the benefits of using SQL views include:
• Simplify complex queries: SQL views can simplify complex queries by
consolidating data from multiple tables into a single view. This can make it
easier to write and understand queries, and can also reduce the amount of
code needed to retrieve data.
• Security: SQL views can be used to restrict access to sensitive data by providing
a controlled view of the data. This means that users can be granted access to
the view without granting access to the underlying tables.
• Data abstraction: SQL views can be used to abstract the underlying data
structures, making it easier to work with the data. This can simplify the
application code and reduce the risk of errors.
• Performance: SQL views can be used to improve query performance by pre-
computing complex queries and storing the results in the view. This can reduce
the amount of processing required when the query is executed.
241
DATABASE VIEW
Views can provide advantages over tables:
• Views can represent a subset of the data contained in a table. Consequently,
a view can limit the degree of exposure of the underlying tables to the outer
world: a given user may have permission to query the view, while denied
access to the rest of the base table.
• Views can join and simplify multiple tables into a single virtual table.
• Views can act as aggregated tables, where the database engine
aggregates data (sum, average, etc.) and presents the calculated results as
part of the data.
• Views can hide the complexity of data.
• Views take very little space to store; the database contains only the definition
of a view, not a copy of all the data that it presents.
• Depending on the SQL engine used, views can provide extra security.
242
Syntax
A view is created with the CREATE VIEW statement.

TO CREATE A VIEW
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Examples (1)

CREATE VIEW expensiveProducts AS


SELECT ProductName, Price
FROM Products Note: You can add SQL statements and
WHERE Price > (SELECT AVG(Price) functions to a view and present the
FROM Products); data as if the data were coming from
one single table.
243
View examples
CREATE VIEW pdsdetails AS (2)
SELECT p.provinceId, p.provinceName, d.districtId, d.districtName, s.sectorId,
s.sectorName
FROM sectors s INNER JOIN districts d ON s.districtId=d.districtId INNER JOIN
provinces p ON d.provinceId=p.provinceId;

244

You might also like