Basics of Database Development Batch5 May-2023 V3
Basics of Database Development Batch5 May-2023 V3
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
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.
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.
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.
10/9/2023 RCA 14
How the database is created?
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
10/9/2023 RCA 18
Paper Based and computerized Database
• A database can be paper based or computerized.
Database
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
Design to fulfill the need of small and It is only limited to smaller DBMS
large businesses systems.
After the advent of the RDBMS, numerous types of database management systems started
to appear. Here’s a brief overview of other notable DBMS:
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.
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.
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
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
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
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
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
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.
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
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
A1 A2 A3 A2 B2 B1 B2 B3
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
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
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
✓ Nullability of columns,
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
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:
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
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
94
SQL -commands list
DDL CREATE
RENAME
DROP
TRUNCATE
ALTER
DML INSERT
SELECT
UPDATE
DELETE
DCL GRANT
REVOKE
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…)
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;
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
107
DDL command in SQL(Cont…)
●TRUNCATE: TRUNCATE operation is used to delete all table records.
Logically it’s the same as DELETE command.
108
SHOWING DATABASES/TABLES
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”
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:
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,”.
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:
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
126
✓ To change an account password from the command line, use the
mysqladmin command:
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
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
133
Drop user/check authentication of a new user
mysql>quit
mysql>mysql -u [username] -p
134
Rename MySQL Database with Command Line
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
Example:
CREATE TABLE parent (
id INT NOT NULL PRIMARY KEY, name VARCHAR(10));
LO 3: MANIPULATE
DATABASES
C
SELECT
STATEMENT
C
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
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
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
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:
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
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;
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.
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.
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;
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’’
194
10/9/2023
SQL UNION
C
• 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.
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);
• 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);
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
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
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)
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.
Note: https://www.w3schools.com/sql/sql_having.asp
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.
4. It restricts the query output by using It groups the output on the basis of
some conditions some rows or columns.
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)
244