0% found this document useful (0 votes)
65 views112 pages

Essential Database Management Skills

Uploaded by

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

Essential Database Management Skills

Uploaded by

w19582005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CHAPTER 6: DATABASE MANAGEMENT SKILLS

6.1 Introduction
This unit covers the competencies required to demonstrate database management skills. It
involves understanding database fundamentals, designing a database, using Structured
Query Language, understanding the design of object oriented databases, understanding
indexing and hashing and understanding database applications.

6.2 Performance Standard


1. Installed MS SQL server
2. Created an entity relationship model
3. Normalized database tables
4. Validated an ER model
5. Created and queried a database from a validated ER model.
6. Retrieved data from several tables using joins
7. Prescribed a database type based on user requirements.
8. Demonstrated Object Oriented Concepts
9. Demonstrated designing of views and triggers in object oriented databases.
10. Implemented Indexing and hashing

6.3 Learning Outcomes


6.3.1 List of Learning Outcomes
1. Understand database fundamentals
2. Design a data base
3. Use Structured Query Language
4. Design of object oriented database
5. Understand indexing and hashing
6. Understand database applications
6.3.2 Learning Outcome 1: Understand database fundamentals
[Link] Learning Activities
Learning Outcome 1: Understand database fundamentals
Learning Activities Special Instructions

1.1 A database is defined Activities may be carried out in


1.2 Terminologies used with databases are explained groups or as individual
1.3 Reasons of using databases are explained
1.4 Relational Model is defined
1.5 Key concepts in relational modelling are explained
1.6 Properties of a table/relation are explained
1.7 Relational Database Management Systems (RDBMSs)
products are compared
1.8 Installation of MS SQL server is demonstrated
1.9 MS SQL server interface is explained
1.10 Properties of MS SQL server database are explained

[Link] Information Sheet No 6:LO1: Understand database fundaments


Introduction

This learning outcome covers the definition of database, relational model and other
terminologies used in databases. It also explains the reasons for using databases, key concepts
in relational modelling, properties of tables and the MS SQL server interface. It also involves
comparing relational database management systems, demonstrating MS SQL server
installation and explaining MS SQL server database properties.

Database
A Database is a collection of related data organised in a way that data can be easily
accessed, managed and updated. Database can be software based or hardware based, with
one sole purpose, storing data

Figure 1: MSAccess Database Sample


Database terminologies

 Table
A database table is composed of records and fields that hold data. Tables are also called
datasheets. Each table in a database holds data about a different, but related, subject.

Figure 2:: MSAccess Table Sample

 Database engine

A database engine (or storage engine) is the underlying software component that a
database management system (DBMS) uses to create, read, update and delete (CRUD)
data from a database. ... The term "database engine" is frequently used interchangeably
with "database server" or "database management system".

 Records
Data is stored in records. A record is composed of fields and contains all the data about one
particular person, company, or item in a database. In this database, a record contains the data
for one customer support incident report. Records appear as rows in the database table. A
record for Log ID 1201242 is highlighted in below.

Figure 3: : MSAccess Table Records Sample

 Field
A field is part of a record and contains a single piece of data for the subject of the record. In
the database table illustrated in Figure below, each record contains four fields:
Log ID A number assigned to this customer support incident for
identification purposes
Operator The code for the customer support operator who handled
this incident
Resolved A check box to indicate whether the incident was resolved
Duration The time in seconds the operator spent on this incident
Fields appear as columns in a database table. Data from the Log ID field for five records is
highlighted in the Figure below.

Figure 4:: MSAccess Table Fields Sample.

Reasons of using databases


The various reasons a database is important are:
Manages large amounts of data
A database stores and manages a large amount of data on a daily basis. This would not
be possible using any other tool such as a spreadsheet as they would simply not work.

Accurate
A database is pretty accurate as it has all sorts of build in constraints, checks etc. This
means that the information available in a database is guaranteed to be correct in most
cases.

Easy to update data


In a database, it is easy to update data using various Data Manipulation languages
(DML) available. One of these languages is SQL.

Security of data
Databases have various methods to ensure security of data. There are user logins
required before accessing a database and various access specifiers. These allow only
authorised users to access the database.

Data integrity
This is ensured in databases by using various constraints for data. Data integrity in
databases makes sure that the data is accurate and consistent in a database.

Easy to research data


It is very easy to access and research data in a database. This is done using Data
Query Languages (DQL) which allow searching of any data in the database and
performing computations on it.

Control of data redundancy


In the database approach, ideally, each data item is stored in only one place in the
database. In some cases, data redundancy still exists to improve system performance,
but such redundancy is controlled by application programming and kept to minimum
by introducing as little redundancy as possible when designing the database.

Data sharing
The integration of all the data, for an organization, within a database system has many
advantages. First, it allows for data sharing among employees and others who have access
to the system. Second, it gives users the ability to generate more information from a given
amount of data than would be possible without the integration.

Data independence
Another advantage of a database management system is how it allows for data
independence. In other words, the system data descriptions or data describing data
(metadata) are separated from the application programs. This is possible because changes
to the data structure are handled by the database management system and are not embedded
in the program itself.

Backup and recovery facilities


Backup and recovery are methods that allow you to protect your data from loss. The
database system provides a separate process, from that of a network backup, for
backing up and recovering data. If a hard drive fails and the database stored on the
hard drive is not accessible, the only way to recover the database is from a backup.

Support for multiple views of data


A database supports multiple views of data. A view is a subset of the database, which
is defined and dedicated for particular users of the system. Multiple users in the
system might have different views of the system. Each view might contain only the
data of interest to a user or group of users.

Insulation between program and data


In the file-based system, the structure of the data files is defined in the application
programs so if a user wants to change the structure of a file, all the programs that
access that file might need to be changed as well. On the other hand, in the database
approach, the data structure is stored in the system catalogue and not in the programs.
Therefore, one change is all that is needed to change the structure of a file. This
insulation between the programs and data is also called program-data independence.

Relational model
The relational model represents the database as a collection of relations. A relation is
nothing but a table of values. In this model, data is organised in two-dimensional
tables and the relationship is maintained by storing a common field.
.

Relational Modeling Concepts


 Relations/tables
A relational database system contains one or more objects called tables or relations. The
data or information for the database are stored in these tables. Tables are uniquely
identified by their names and are comprised of columns and rows. Columns contain the
column name, data type, and any other attributes for the column. Rows contain the records
or data for the columns. Here is a sample table called "weather". City, state, high, and low
are the columns. The rows contain the data for this table:

Weather
city state high low
Phoenix Arizona 105 90
Tucson Arizona 101 92
Flagstaff Arizona 88 69
San Diego California 77 60
New
Albuquerque 80 72
Mexico
Table Weather

 Attributes/Columns
Each named column in a Table is an attribute. Attributes are the properties which define a
relation. e.g., Student_Rollno, NAME, etc.

 Domain
A domain is the original sets of atomic values used to model data. By atomic value, we
mean that each value in the domain is indivisible as far as the relational model is
concerned. For example:
 The domain of Marital Status has a set of possibilities: Married, Single, Divorced.
 The domain of Shift has the set of all possible days: {Mon, Tue, Wed…}.
 The domain of Salary is the set of all floating-point numbers greater than 0 and less
than 200,000.
 The domain of First Name is the set of character strings that represents names of
people.
In summary, a domain is a set of acceptable values that a column is allowed to contain.
This is based on various properties and the data type for the column. We will discuss data
types in another chapter.

 Tuples/Rows
A row—also called a tuple—represents a single, implicitly structured data item in a table.
Each row in a table represents a set of related data, and every row in the table has the same
structure.
 Primary Key
A primary key is a field in a table which uniquely identifies each row/record in a
database table. Primary keys must contain unique values. A primary key column cannot
have NULL values. A table can have only one primary key, which may consist of single
or multiple fields.
.
Example:
Employee ID FirstName LastName
11 Andrew Johnson
22 Tom Wood
33 Alex Hale
Demonstration of a Primary Key

In the above-given example, employee ID is a primary key because it uniquely identifies an


employee record. In this table, no other employee can have the same employee ID.
 Foreign Key
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table. The table containing the foreign key is called the child
table, and the table containing the candidate key is called the referenced or parent table.
Look at the following two tables:

"Persons" table:
Persons table
PersonID LastNam FirstName Age
e
1 Hansen Ola 30
2 Svendson Tove 23
3 Pettersen Kari 20

"Orders" table:
Orders table
OrderI OrderNumbe PersonI
D r D
1 77895 3
2 44678 3
3 22456 2
4 24562 1
 Notice that the "PersonID" column in the "Orders" table points to the "PersonID"
column in the "Persons" table.
 The "PersonID" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
 The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders"
table.
 The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
 The FOREIGN KEY constraint also prevents invalid data from being inserted into the
foreign key column, because it has to be one of the values contained in the table it
points to.

Properties of a relation/table

Relational tables have six properties:


Values Are Atomic
This property implies that columns in a relational table are not repeating group or arrays.
Such tables are referred to as being in the "first normal form" (1NF). The atomic value
property of relational tables is important because it is one of the cornerstones of the
relational model.
The key benefit of the one value property is that it simplifies data manipulation logic.

Column Values Are of the Same Kind


In relational terms this means that all values in a column come from the same domain. A
domain is a set of values which a column may have. For example, a Monthly_Salary
column contains only specific monthly salaries. It never contains other information such as
comments, status flags, or even weekly salary.
This property simplifies data access because developers and users can be certain of the type
of data contained in a given column. It also simplifies data validation. Because all values
are from the same domain, the domain can be defined and enforced with the Data
Definition Language (DDL) of the database software.

Each Row is Unique


This property ensures that no two rows in a relational table are identical; there is at least
one column, or set of columns, the values of which uniquely identify each row in the table.
Such columns are called primary keys.
This property guarantees that every row in a relational table is meaningful and that a
specific row can be identified by specifying the primary key value.

e Sequence of Columns is Insignificant


This property states that the ordering of the columns in the relational table has no meaning.
Columns can be retrieved in any order and in various sequences. The benefit of this
property is that it enables many users to share the same table without concern of how the
table is organized. It also permits the physical structure of the database to change without
affecting the relational tables.

The Sequence of Rows is Insignificant


This property is analogous the one above but applies to rows instead of columns. The main
benefit is that the rows of a relational table can be retrieved in different order and
sequences. Adding information to a relational table is simplified and does not affect
existing queries.

Each Column Has a Unique Name


Because the sequence of columns is insignificant, columns must be referenced by name and
not by position. In general, a column name need not be unique within an entire database
but only within the table to which it belongs.

Comparison of RDBMS products


 Oracle
Oracle Corporation owns Oracle Database, and the code is not open sourced.

Oracle DB is for large applications, particularly in the banking industry. Most of the world’s
top banks run Oracle applications because Oracle offers a powerful combination of
technology and comprehensive, pre-integrated business applications, including essential
functionality built specifically for banks.

The main disadvantage of using Oracle is that it is not free to use like its open source
competitors and can be quite expensive.

 MS SQL server
Microsoft owns SQL Server. Like Oracle DB, the code is close sourced. Large enterprise
applications mostly use SQL Server.

Microsoft offers a free entry-level version called Express but can become very expensive as
you scale your application.

 My SQL
MySQL is the most popular open source SQL database. It is typically used for web
application development, and often accessed using PHP.

The main advantages of MySQL are that it is easy to use, inexpensive, reliable (has been
around since 1995), and has a large community of developers who can help answer questions.

Some of the disadvantages are that it has been known to suffer from poor performance when
scaling, open source development has lagged since Oracle has taken control of MySQL, and
it does not include some advanced features that developers may be used to
.
 Ms Access
Microsoft Access is a Database Management System offered by Microsoft. It uses the
Microsoft Jet Database Engine and comes as a part of the Microsoft Office suite of
application.
Microsoft Access offers the functionality of a database and the programming capabilities to
create easy to navigate screens (forms)
Installation of MS SQL server
Beginning with SQL Server 2016 (13.x), SQL Server is only available as a 64-bit application.
Here are important details about how to get SQL Server and how to install it.

Editions and features: Review the supported features for the different editions and versions
of SQL Server to determine which best suits your business needs.
 SQL Server 2019 (15.x).
 SQL Server 2017 (14.x).
 SQL Server 2016 (13.x).
 SQL Server 2014 (12.x)
Requirements: Review installation requirements, system configuration checks, and security
considerations in Planning a SQL Server Installation

Install SQL Server 2017 Developer Edition


To install SQL Server, you need to download it from the [Link] website via the
following link: [Link]
Once the download completes, you double-click the file [Link] to
launch the installer.

1. The installer asks you to select the installation type, choose the Custom installation type
allows you to step through the SQL Server installation wizard and select the features that you
want to install.

2. Specify the folder for storing the installation files that the installer will download, then
click the Install button.
3. The installer starts downloading the install package for a while.

4. Once the download completes, open the folder that stores the install package and double-
click the [Link] file.

5. The following window displays; select the installation option on the left.
6. Click the first link to launch a wizard to install SQL Server 2017.

7. Specify the edition that you want to install, select Developer edition and click the Next
button.
8. Select the “I accept the license terms.” and click the Next button.

9. Check the “Use Microsoft Update to check for updates (recommended)” to get the security
and other important updates for the SQL Server and click the Next button.
10. The installation checks for the prerequisites before installation. If no error found, click the
Next button.

11. Select the features that you want to install. For now, you just need the Database Engine
Services, just check the checkbox and click the Next button to continue
12. Specify the name and install ID for the instance of the SQL Server and click the Next
button.

13. Specify the service account and collation configuration. Just use the default configuration
and click the Next button.
14. Specify the database engine security mode. First, choose Mixed Mode. Next, enter the
password for the SQL Server system administrator (sa) account. Then, re-enter the same
password to confirm it. After that, click the Add Current User button. Finally, click the Next
button.

15. Verify the SQL Server 2017 features to be installed:


16. The installer starts the installation process
17. Once it completes, the following window displays. Click the OK button.

18. Click the Close button to complete the installation

Congratulation! you have successfully installed SQL Server Developer Edition.


Install Microsoft SQL Server Management Studio
To interact with SQL Servers, you need to install SQL Server Management Studio (SSMS).
The SQL Server Management Studio is a software for querying, designing, and managing
SQL Server on your local computer or in the cloud. It provides you with tools to configure,
monitor, and administer SQL Server instances.

First, download the SSMS from the Microsoft website via the following link:
Download SQL Server Management Studio

Second, double-click the installation file [Link] to starting installing. The


installation process of SMSS is straightforward which you just need to follow the screen
sequence.

1. Click the Install button

2. Wait for few minutes while the installer sets up the software.

3. Once setup is completed, click the Close button


Now, you should have a SQL Server 2017 and SQL Server Management Studio installed on
your computer.

MS SQL server interface


Normally, when you install Microsoft SQL Server, you also install the SQL Server
Management Studio (SSMS), which is the primary tool for managing the server and its
databases using a graphical interface.

Figure 5:: MSSQL Server Interface

 Object Explorer (F8): Object Explorer is a tree view of all the database objects in a
server. This view includes the databases of the SQL Server Database Engine, SQL
Server Analysis Services, SQL Server Reporting Services, and SQL Server Integration
Services. Object Explorer includes information for all servers that are connected to it.
 Query Window (Ctrl+N): After you select New Query, enter your Transact-SQL (T-
SQL) queries in this window. The results of your queries also appear here.

 Properties (F4): You can see the Properties view when the Query Window is open.
The view displays basic properties of the query. For example, it shows the time that a
query started, the number of rows returned, and connection details.
 Template Browser (Ctrl+Alt+T): Template Browser has various pre-built T-SQL
templates. You can use these templates to perform various functions, such as creating
or backing up a database.

 Object Explorer Details (F7): This view is more granular than the view in Object
Explorer. You can use Object Explorer Details to manipulate multiple objects at the
same time. For example, in this window, you can select multiple databases, and then
either delete them or script them out simultaneously.
Change the environment layout
This section describes how to change the environment layout, such as how to move various
windows.
 To move a window, press and hold the title, and then drag the window.
 To pin or unpin a window, select the pushpin icon in the title bar:

 Each window component has a drop-down menu that you can use to manipulate the
window in various ways:
 When two or more query windows are open, the windows can be tabbed vertically or
horizontally so that both query windows are visible. To view tabbed windows, right-
click the title of the query, and then select the tabbed option that you want:

o Here's a Horizontal Tab Group:

o Here's a Vertical Tab Group:


o To merge the tabs, right-click the query title, and then select Move to Previous Tab
Group or Move to Next Tab Group:

 To restore the default environment layout, in the Window menu, select Reset Window
Layout:
Maximize Query Editor
You can maximize Query Editor to full-screen mode:
1. Click anywhere in the Query Editor window.
2. Press Shift+Alt+Enter to toggle between full-screen mode and regular mode.
This keyboard shortcut works with any document window.

Change basic settings


This section describes how to modify some basic settings in SSMS from the Tools menu.

 To modify the highlighted toolbar, select Tools > Customize:


Change the font
 To change the font, select Tools > Options > Fonts and Colors:

Change startup options


 Startup options determine what your workspace looks like when you first open SSMS.
To change startup options, select Tools > Options > Startup:
Reset settings to the default
 You can export and import all these settings from the menu. To import or export
settings, or to restore default settings, select Tools > Import and Export Settings

Properties of MS SQL server Database


The following a properties apply to a SQL server database. From the object explorer, right
click on an existing database to see the properties as follows:
 Deleting a database
 Deleting data or log files
 Increasing database size
 Shrinking database
 Renaming database
 Importing a database
 Exporting a database

Conclusion
At the end of this learning outcome, the learner should be able to define of database,
relational model and other terminologies used in databases. The learner should also be able to
explain the reasons for using databases, key concepts in relational modelling, properties of
tables and the MS SQL server interface. In addition, a learner should be able to compare
relational database management systems, demonstration of MS SQL server installation and
explanation of MS SQL server database properties should be one.

Further Reading
1. Elmasri, R., & Navathe, S. B. (2011). Database systems (Vol. 9). Boston, MA:
Pearson Education.
2. Hoffer, J. A., Ramesh, V., & Topi, H. (2011). Modern database management. Upper
Saddle River, NJ: Prentice Hall,.
3. Rankins, R., Jensen, P., & Bertucci, P. (2002). Microsoft SQL Server 2000
Unleashed. Sams Publishing.
4. Davidson, L. (2001). Professional SQL server 2000 database design. Birmingham,
UK: Wrox Press.
[Link] Self-Assessment

Written Assessment

1 A relational database consists of a collection of


a) Tables
b) Fields
c) Records
d) Keys
2 A ________ in a table represents a relationship among a set of values.
a) Column
b) Key
c) Row
d) Entry
3 The term _______ is used to refer to a row.
a) Attribute
b) Tuple
c) Field
d) Instance
4 The term attribute refers to a ___________ of a table.
a) Record
b) Column
c) Tuple
d) Key
5 For each attribute of a relation, there is a set of permitted values, called the ________ of
that attribute.
a) Domain
b) Relation
c) Set
d) Schema
6 Course(course_id,sec_id,semester)
Here the course_id,sec_id and semester are __________ and course is a _________
a) Relations, Attribute
b) Attributes, Relation
c) Tuple, Relation
d) Tuple, Attributes
7 A _____ is a property of the entire relation, rather than of the individual tuples in which
each tuple is unique.
a) Rows
b) Key
c) Attribute
d) Fields
8 Which one of the following attribute can be taken as a primary key?
a) Name
b) Street
c) Id
d) Department
9 Which one of the following cannot be taken as a primary key?
a) Id
b) Register number
c) Dept_id
d) Street
10 Which language can a user request information from a database with?
a) Query
b) Relational
c) Structural
d) Compiler
11 Student(ID, name, dept name, tot_cred)
In this statement which attributes form the primary key?
a) Name
b) Dept
c) Tot_cred
d) ID
12 Database __________ which is the logical design of the database, and the database
_______ which is a snapshot of the data in the database at a given instant in time.
a) Instance, Schema
b) Relation, Schema
c) Relation, Domain
d) Schema, Instance

Oral Assessment

1. Explain 5 applications of databases in your real life


2. Explain 5 properties of a relational database

Practical Assesssment

1. In groups of two install MS SQL server on your personal Laptop


2. Document the steps of installing MS SQL Server as you install.

[Link] Tools, Equipment, Supplies and Materials


 Microsoft Office with MS Visio Modelling tool
 MS SQL server software
 Computers
 Instruction material
 Stationery
 Internet

[Link] References
1. [Link]
2. [Link]
db_elements/db_elements2.htm
3. [Link] on 2nd March 2020)
6.3.3 Learning Outcome 2: Design a database
[Link] Learning Activities
Learning Outcome No.2: Design a Database

Learning Activities Special Instructions

Activities may be carried out in groups


1.1 Phases of database design are explained or as individual
1.2 Entity modelling is illustrated
1.3 Entity model is designed using UML notation
2.4 Validation of the ERM model is done according to ·
the requirements

·
[Link] Information Sheet No 6/LO2: Design a Database
Introduction
This learning outcome covers explanation of the phases of database design,
illustration of entity modelling, entity model design and validation of the ER
model.

Phases of database Design


There are three phases of database design, namely conceptual, logical, and physical database
design.

Figure 6: Database Data Modelling

a) Conceptual database design (ERM Modeling)


Conceptual database design is the process of constructing a model of the data used in an
enterprise, independent of all physical considerations.

The conceptual database design phase starts with the formation of a conceptual data model
of the enterprise that is entirely independent of implementation details such as the target
DBMS, use of application programs, programming languages used, hardware platform,
performance issues, or any other physical deliberations.

The main aim of this model is to establish the entities, their attributes, and their
relationships. In this Data modeling level, there is hardly any detail available of the actual
Database structure.
The 3 basic tenants of Data Model are
Entity: A real-world thing
Attribute: Characteristics or properties of an entity
Relationship: Dependency or association between two entities
For example:
 Customer and Product are two entities. Customer number and name are attributes of
the Customer entity
 Product name and price are attributes of product entity
 Sale is the relationship between the customer and product

Figure 7: Demonstrating Conceptual Data Model

Characteristics of a conceptual data model


 Offers Organisation-wide coverage of the business concepts.
 This type of Data Models are designed and developed for a business audience.
 The conceptual model is developed independently of hardware specifications like data
storage capacity, location or software specifications like DBMS vendor and
technology. The focus is to represent data as a user will see it in the "real world."
Conceptual data models known as Domain models create a common vocabulary for all
stakeholders by establishing basic concepts and scope.

Steps of Conceptual database Design


Conceptual database design steps are:
 Build a conceptual data model
 Recognize entity types
 Recognize the relationship types
 Identify and connect attributes with entity or relationship types
 Determine attribute domains
 Determine candidate, primary, and alternate key attributes
 Consider the use of improved modeling concepts (optional step)
 Check model for redundancy
 Validate the conceptual model against user transactions
 Review the conceptual data model with user

Building a Conceptual Database Model


The first step in conceptual database design is to build one (or more) conceptual data
replica of the data requirements of the enterprise. A conceptual data model comprises these
following elements:
 entity types
 types of relationship
 attributes and the various attribute domains
 primary keys and alternate keys
 integrity constraints
The conceptual data model is maintained by documentation, including ER diagrams and a
data dictionary, which is produced throughout the development of the model.

b) Logical database design


Logical database design is the process of constructing a model of the data used in an
enterprise based on a specific data model, but independent of a particular DBMS and other
physical considerations.

The objective of logical database design methodology is to interpret the conceptual data
model into a logical data model and then authorize this model to check whether it is
structurally correct and able to support the required transactions or not.
Logical data models add further information to the conceptual model elements. It defines
the structure of the data elements and set the relationships between them.

Figure 8:Logical data Modelling

The advantage of the Logical data model is to provide a foundation to form the base for the
Physical model. However, the modeling structure remains generic.

Characteristics of a Logical data model


 Describes data needs for a single project but could integrate with other logical data
models based on the scope of the project.
 Designed and developed independently from the DBMS.
 Data attributes will have data types with exact precisions and length.
 Normalization processes to the model is applied typically till 3NF

In this step of the database development life cycle, the main purpose is to translate the
conceptual data model created in conceptual methodology into a logical data model of the
data requirements of the enterprise. This objective can be achieved by following the
activities given below:
 Obtain the relations for the logical data model
 Authorize those relations using normalization
 Validate those relations against user transactions
 Check integrity control and its limitation
 Evaluate the logical data model with user
 Combine logical data models into the global model (This step is an optional one)
 Check for future growth and development

The structure of the relational schema is authorized using normalization. It then makes sure
to ensure that the relations are capable of supporting the transactions given in the users'
requirements specification. You can then check those all-important integrity constraints
that are characterized by the logical data model. At this stage, the logical data model is
authorized by the users to ensure that they consider the model to be a true demonstration of
the data requirements for the enterprise.

c) Physical database design

Physical database design is the process of producing a description of the implementation of


the database on secondary storage; it describes the base relations, file organizations, and
indexes used to achieve efficient access to the data, and any associated integrity constraints
and security measures. It offers an abstraction of the database and helps generate schema.
This is because of the richness of meta-data offered by a Physical Data Model.

Figure 9:Physical Data Modelling

This type of Data model also helps to visualize database structure. It helps to model database
columns keys, constraints, indexes, triggers, and other RDBMS features.

Steps of the physical database design


The steps of the physical database design methodology are as follows:
 Transform the logical data model for target DBMS
o Design base relations
o Design representation of derived data
o Design general constraints
 Design file organizations and indexes
o Analyze transactions
o Choose file organizations
o Choose indexes
o Estimate disk space requirements
o Design user views
o Design security mechanisms
o Consider the introduction of controlled redundancy
o Monitor and tune the operational system
Entity Modelling Concepts

ER modeling helps you to analyze data requirements systematically to produce a well-


designed database. So, it is considered a best practice to complete ER modeling before
implementing your database.
a) Entities
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
Entity set:
An entity set is a group of similar kind of entities. It may contain entities with attribute
sharing similar values. Entities are represented by their properties, which also called
attributes. All attributes have their separate values. For example, a student entity may have a
name, age, class, as attributes.
Figure 10:Demonstrating an Entity

Example of Entities:
A university may have some departments. All these departments employ various lecturers
and offer several programs.

Some courses make up each program. Students register in a particular program and enroll in
various courses. A lecturer from the specific department takes each course, and each lecturer
teaches a various group of students.

b) Attributes

It is a single-valued property of either an entity-type or a relationship-type.


For example, a lecture might have attributes: time, date, duration, place, etc.

An attribute is represented by an Ellipse

Attributes

Types of Attributes Description


Simple attribute Simple attributes can't be divided any
further. For example, a student's contact
number. It is also called an atomic value.
Composite attribute It is possible to break down composite
attribute. For example, a student's full
name may be further divided into first
name, second name, and last name.
Derived attribute This type of attribute does not include in
the physical database. However, their
values are derived from other attributes
present in the database. For example, age
should not be stored directly. Instead, it
should be derived from the DOB of that
employee.
Multivalued attribute Multivalued attributes can have more than
one values. For example, a student can
have more than one mobile number, email
address, etc.

c) Relationships
Relationship is nothing but an association among two or more entities. E.g., Tom works in
the Chemistry department.

Figure 11:Entity Relationships

Entities take part in relationships. We can often identify relationships with verbs or verb
phrases.
For example:
 You are attending this lecture
 I am giving the lecture
 Just like entities, we can classify relationships according to relationship-types:
 A student attends a lecture
 A lecturer is giving a lecture.
 Designing an Entity Model using UML (Unified Modelling Language)

An Entity Relationship Diagram (ERD) is a visual representation of different entities


within a system and how they relate to each other. For example, the elements writer,
novel, and a consumer may be described using ER diagrams the following way:
Figure 12:ER diagram with basic objects

ER Diagram Symbols and Notations


A number of symbols and notations are used for ER Modelling as shown below

Figure 13: Elements in ER diagrams

There are three basic elements in an ER Diagram: entity, attribute, relationship. There are
more elements which are based on the main elements. They are weak entity, multi valued
attribute, derived attribute, weak relationship, and recursive relationship. Cardinality and
ordinality are two other notations used in ER diagrams to further define relationships
How to Draw ER Diagrams
Below points show how to go about creating an ER diagram.
1. Identify all the entities in the system. An entity should appear only once in a
particular diagram. Create rectangles for all entities and name them properly.
2. Identify relationships between entities. Connect them using a line and add a
diamond in the middle describing the relationship.
3. Add attributes for entities. Give meaningful attribute names so they can be
understood easily.
Sounds simple right? In a complex system, it can be a nightmare to identify relationships.
This is something you’ll perfect only with practice.
ER Diagram Best Practices
1. Provide a precise and appropriate name for each entity, attribute, and relationship in
the diagram. Terms that are simple and familiar always beats vague, technical-
sounding words. In naming entities, remember to use singular nouns. However,
adjectives may be used to distinguish entities belonging to the same class (part-time
employee and full-time employee, for example). Meanwhile attribute names must be
meaningful, unique, system-independent, and easily understandable.
2. Remove vague, redundant or unnecessary relationships between entities.
3. Never connect a relationship to another relationship.
4. Make effective use of colors. You can use colors to classify similar entities or to
highlight key areas in your diagrams.
Worked Examples in ER Modelling
ER diagram of Branch Has Staff relationship

Has Entity relationship


Relationship Types
Degree of a Relationship
– Number of participating entities in relationship.
Relationship of degree:
– two is binary
– three is ternary
– four is quaternary.
Binary relationship called POwns

Figure 14:Binary relationship

Ternary relationship called Registers


Figure 15:Ternary relationship

Quaternary relationship called Arranges

Figure 16:Quaternary relationship

Recursive relationship called Supervises with role names

Figure 17:Recursive relationship


Entities associated through two distinct relationships with role names

Figure 18:Entities associated through two distinct relationships with role names

ER diagram of Staff and Branch entities and their attributes

Figure 19:ER diagram of Staff and Branch entities and their attributes
 Normalization
Database normalization is a database schema design technique, by which an existing
schema is modified to minimize redundancy and dependency of data.

Reasons for Database Normalization


There are three main reasons to normalize a database. The first is to minimize duplicate data,
the second is to minimize or avoid data modification issues, and the third is to simplify
queries.
Normalization fixes anomalies and Data redundancy especially those occasioned by data
updates. There are three modification anomalies that can occur:
a) Insert Anomaly
There are facts we cannot record until we know information for the entire row. In our
example we cannot record a new sales office until we also know the sales person.
Why? Because in order to create the record, we need provide a primary key. In our case this
is the EmployeeID.

b) Update Anomaly
In this case we have the same information in several rows. For instance if the office number
changes, then there are multiple updates that need to be made. If we don’t update all rows,
then inconsistencies appear.
c) Deletion Anomaly

Deletion of a row causes removal of more than one set of facts. For instance, if John Hunt
retires, then deleting that row cause us to lose information about the New York office.

Database Normalization Example


We will study normalization with the help of a case study. Assume, a video library maintains
a database of movies rented out. Without any normalization, all information is stored in one
table as shown below.

Here you see Movies Rented column has multiple values. Now let's move into 1st
Normal Forms:
1NF (First Normal Form) Rules
 Each table cell should contain a single value.
 Each record needs to be unique.
The above table in 1NF-
1NF Example

Table A: In 1NF Form

Before we proceed let's understand a few things --


What is a KEY?
A KEY is a value used to identify a record in a table uniquely. A KEY could be a single
column or combination of multiple columns
Note: Columns in a table that are NOT used to identify a record uniquely are called non-key
columns.
What is a Primary Key?

A primary is a single column value used to identify a database


record uniquely.
It has following attributes
 A primary key cannot be NULL
 A primary key value must be unique
 The primary key values should rarely be changed
 The primary key must be given a value when a new
record is inserted.

What is Composite Key?


A composite key is a primary key composed of multiple columns used to identify a record
uniquely
In our database, we have two people with the same name Robert Phil, but they live in
different places.
Hence, we require both Full Name and Address to identify a record uniquely. That is a
composite key.
Let's move into second normal form 2NF
2NF (Second Normal Form) Rules
 Rule 1- Be in 1NF
 Rule 2- Single Column Primary Key
It is clear that we can't move forward to make our simple database in 2nd Normalization form
unless we partition the table above.

Decomposition of out 1NF table to table 1

Normalized table members -Table 2

We have divided our 1NF table into two tables viz. Table 1 and Table2. Table 1 contains
member information. Table 2 contains information on movies rented.
We have introduced a new column called Membership_id which is the primary key for table
1. Records can be uniquely identified in Table 1 using membership id
Database - Foreign Key
In Table 2, Membership ID is the Foreign Key

Introducing foreign Key in members table


Foreign Key references the primary key of another
Table. It helps connect your Tables
 A foreign key can have a different name from its
primary key
 It ensures rows in one table have corresponding
rows in another
 Unlike the Primary key, they do not have to be
unique. Most often they aren't
 Foreign keys can be null even though primary
keys can not

Why do you need a foreign key?


Suppose, a novice inserts a record in Table B such as
You will only be able to insert values into your foreign key that exist in the unique key in the
parent table. This helps in referential integrity.
The above problem can be overcome by declaring membership id fromTable2 as foreign key
of membership id from Table1
Now, if somebody tries to insert a value in the membership id field that does not exist in the
parent table, an error will be shown!
What are transitive functional dependencies?
A transitive functional dependency is when changing a non-key column, might cause any of
the other non-key columns to change
Consider the table 1. Changing the non-key column Full Name may change Salutation.

Let's move into 3NF


3NF (Third Normal Form) Rules
 Rule 1- Be in 2NF
 Rule 2- Has no transitive functional dependencies
To move our 2NF table into 3NF, we again need to again divide our table.
3NF Example
Table staff in 3 NF-Table 1

Table Members Normalized-Table 2


Table Salutation Introduced-Table 3

We have again divided our tables and created a new table which stores Salutations.
There are no transitive functional dependencies, and hence our table is in 3NF
In Table 3 Salutation ID is primary key, and in Table 1 Salutation ID is foreign to primary
key in Table 3
Now our little example is at a level that cannot further be decomposed to attain higher forms
of normalization. In fact, it is already in higher normalization forms. Separate efforts for
moving into next levels of normalizing data are normally needed in complex databases.
However, we will be discussing next levels of normalizations in brief in the following.

CRUD is an acronym that refers to the following actions on an object (typically a data
entity):
 Create - to create and store new data
 Read - to retrieve and read data
 Update - to change or modify then store the data.
 Delete - to delete or remove the data

A CRUD matrix is a very useful way to capture and display activities and permissions
within a system. It is very valuable to combine a CRUD Matrix with the analysis of user
processes within the system, especially in the context of the actors and roles involved to
complete the picture. The analysis helps to identify the usage of entities and associated
GUIs. The information that is used heavily, and may therefore be a performance
bottleneck, is also easily identified.
The simple example below illustrates how to capture this information. In this case what is
shown is what access or usage that a role has with a particular protected object (i.e., data
entity, or activity, etc.).

Crud Matrix A
In the following example, another way to capture this information, the protected objects are
list across the top, with permissions/usage define in each cell.

Crud Matrix alternative way

Object
Order Invoice Customer Employee Product
Role
Sales VP crud crud crud crud crud
Sales Manager crud crud ru r r
Sales Rep crud r ru r R
Stock Manager - - - r Ru
Note that "crude" appears, which is create; read; update; delete; and execute. Execute is
added here, indicating that a "Sale Manager" role can execute (fulfill) an Order.

Conclusion
At the end of this learning outcome, the leaner should be able to explain of the phases of
database design, illustrate entity modelling, entity model design and validate other ER
model.

Further Reading
1. Batini, C., Ceri, S., & Navathe, S. B. (1992). Conceptual database design: an Entity-
relationship approach (Vol. 116). Redwood City, CA: Benjamin/Cummings.
2. Elmasri, R., & Navathe, S. B. (2011). Database systems (Vol. 9). Boston, MA:
Pearson Education.
3. Hoffer, J. A., Ramesh, V., & Topi, H. (2011). Modern database management. Upper
Saddle River, NJ: Prentice Hall,.
4. Rankins, R., Jensen, P., & Bertucci, P. (2002). Microsoft SQL Server 2000 Unleashed.
Sams Publishing.

[Link] Self-Assessment
Written Assessment

1. _____________ can help us detect poor E-R design.


a) Database Design Process
b) E-R Design Process
c) Relational scheme
d) Functional dependencies

2. In the __________ normal form, a composite attribute is converted to individual


attributes.
a) First
b) Second
c) Third
d) Fourth

3. Tables in second normal form (2NF):


a) Eliminate all hidden dependencies
b) Eliminate the possibility of a insertion anomalies
c) Have a composite key
d) Have all non-key fields depend on the whole primary key
4. Which is a bottom-up approach to database design that design by examining the
relationship between attributes:
a) Functional dependency
b) Database modeling
c) Normalization
d) Decomposition
5. An ________ is a set of entities of the same type that share the same properties, or
attributes.
a) Entity set
b) Attribute set
c) Relation set
d) Entity model

6. Entity is a _________
a) Object of relation
b) Present working model
c) Thing in real world
d) Model of relation

7. Which of the following can be a multivalued attribute?


a) Phone_number
b) Name
c) Date_of_birth
d) All of the mentioned

8. The Rectangles divided into two parts represents


a) Entity set
b) Relationship set
c) Attributes of a relationship set
d) Primary key

9. Consider a directed line (->) from the relationship set advisor to both entity sets
instructor and student. This indicates _________ cardinality
a) One to many
b) One to one
c) Many to many
d) Many to one

10. We indicate roles in E-R diagrams by labeling the lines that connect ___________ to
__________
a) Diamond , diamond
b) Rectangle, diamond
c) Rectangle, rectangle
d) Diamond, rectangle

Oral Assessment
1. What is the difference between Primary and Unique Key
2. Explain any 3 symbols used in ER Modelling.
Practical Assessment
1. Model the relationship between Bank, Employees and Clients
2. Use ER diagram to describe a Weak Entity set
[Link] Tools, Equipment, Supplies and Materials
 Microsoft Office with MS Visio Modelling tool
 MS SQL server software
 Computers
 Instruction material
 Stationery
 Internet

[Link] References

1) Davidson, L. (2001). Professional SQL server 2000 database design. Birmingham,


UK: Wrox Press.
2) [Link]
3) [Link]
4) [Link]
explained-in-simple-english/(Retrieved on 2nd March 2020
5) [Link]
6.3.4 Learning Outcome 3: Use Structured Query Language (SQL)
[Link] Learning Activities
Learning Outcome No.3: Use Structured Query Language

Learning Activities Special Instructions

Activities may be carried out in groups


3.1 Structured Query Language (SQL) is explained or as individual
3.2 Data definition queries are explained
3.3 Creation of tables using the SQL CREATE TABLE
statement is demonstrated ·
3.4 CREATE TABLE statement constraints are
demonstrated
3.5 The table schema is edited using the SQL ALTER
statement
3.6 A table is dropped using the SQL DROP TABLE
statement
3.7 Data manipulation query statements are
demonstrated.
3.8 SQL joins are explained
3.9 Database is created and queried from validated ER
model
3.10 Types of joins are demonstrated

[Link] Information Sheet No 6/LO3: Use Structured Query Language (SQL)

Introduction

This learning outcome covers explaining SQL and data definition, creating tables using
CREATE TABLE constraints, using the ALTER TABLE AND DROP TABLE statements,
demonstrating data manipulation query statements, explaining joins and demonstrating
types of joins and creating and querying a database from validated ER model.

Structured Query Language (SQL)


Structured query language is a standard language in programming specifically for relational
databases. It is designed mainly for management of data i.e. insertion, update and retrieval
into and from relational databases. It can also be seen as a standard interface through which
access and manipulation of data in relational databases is done.
SQL in itself is non graphical but different industry players have come up with GUI’s to
ease the process of SQL use in relational database management.
SQL code is generally divided into the following categories:
 Data Querying – done using the common and familiar statement SELECT. The
SELECT statement has additional helpful clauses to aid further its operation –
FROM, WHERE, ORDER BY, GROUP BY, LIMIT.
 Data Manipulation – this consist of at least three statements INSERT, UPDATE
AND DELETE used to add, edit/update and delete data respectively.
 Data Definition – used for table management and table structures including
indexing. This includes such statements as CREATE, ALTER, DROP.
 Data Control – majorly for database control and management, i.e. assign and
revoke database rights and permissions to respective users. Its statements include
GRANT, REVOKE.

Data definition queries


Data Definition Queries as stated above are mainly for management of relations/tables –
creation, update and deletion. The statements here as explained below:
CREATE – this query is for creating tables and table structure.
ALTER – used to update/alter/change a table and/or table structure.
DROP – this is for total removal of a table and its structure inclusive of existing data.

Creation of tables using the SQL CREATE TABLE statement


Using CREATE statement is a basic way of naming a table and defining its structure. The
create statement has a table name and its columns with respective data types. The below is
a sample statement.

CREATE TABLE table_name(


column1 datatype,
column2 datatype,
column3 datatype,
column4 datatype,

PRIMARY KEY( column1,.. )
);

The columns can range from one to a standard 1024 number of columns though other
management tools could allow for more.

The data types do not differ much with regular data types used in programming languages,
e.g. integer, varchar, date, datetime, timestamp, float, lately json among others.

CREATE TABLE statement constraints


SQL CREATE table constraints are set integrity conditions and rules to manage data in a
table. This is to ensure consistency, accuracy and reliability of data within a table. The
constraints are implemented on per column basis as shown in the syntax below:
CREATE TABLE table_name(
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
column4 datatype constraint
);

The common constraints used as below:

NOT NULL – this ensures that a column cannot have a NULL value.
UNIQUE - Ensures that all values in a column are different.
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table.
FOREIGN KEY - Uniquely identifies a row/record in another table.
CHECK - Ensures that all values in a column satisfy a specific condition.
DEFAULT - Sets a default value for a column when no value is specified.
INDEX - Used to create and retrieve data from the database very quickly.

Examples: Tables with constraints


CREATE TABLE department
(
dept_code integer PRIMARY KEY,
dept_name varchar(10) not null,
);

CREATE TABLE employee


( id integer PRIMARY KEY,
name varchar(20) NOT NULL,
nationality varchar(30) DEFAULT "KENYA”,
age integer NOT NULL,
gender char(1) CHECK (gender in ('M','F')),
salary float,
location char(10) UNIQUE,
dept_code integer FOREIGN KEY REFERENCES department(dept_code)
);

Data Querying USING the SELECT statement

SQL SELECT Statement


The most commonly used SQL command is SELECT statement. The SQL SELECT
statement is used to query or retrieve data from a table in the database.
A query may retrieve information from specified columns or from all of the columns in the
table. To create a simple SQL SELECT Statement, you must specify the column(s) name and
the table name. The whole query is called SQL SELECT Statement.

Syntax of SQL SELECT Statement:


SELECT column_list FROM table-name
[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];
 table-name is the name of the table from which the information is retrieved.
 column_list includes one or more columns from which data is retrieved.
 The code within the brackets is optional.
database table student_details;
id first_name last_name age subject games
100 Rahul Sharma 10 Science Cricket
101 Anjali Bhagwat 12 Maths Football
102 Stephen Fleming 09 Science Cricket
103 Shekar Gowda 18 Maths Badminton
104 Priya Chandra 15 Economics Chess
NOTE: These database tables are used here for better explanation of SQL commands. In
reality, the tables can have different columns and different data.

For example, consider the table student_details. To select the first name of all the students the
query would be like:

SELECT first_name FROM student_details;


NOTE: The commands are not case sensitive. The above SELECT statement can also be
written as "select first_name from students_details;"

 You can also retrieve data from more than one column. For example, to select first
name and last name of all the students.

SELECT first_name, last_name FROM student_details;


You can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT
statement. (Discussed later)

NOTE: In a SQL SELECT statement only SELECT and FROM statements are mandatory.
Other clauses like WHERE, ORDER BY, GROUP BY, HAVING are optional.

How to use expressions in SQL SELECT Statement


 Expressions combine many arithmetic operators, they can be used in SELECT,
WHERE and ORDER BY Clauses of the SQL SELECT Statement.
 The operators are evaluated in a specific order of precedence, when more than one
arithmetic operator is used in an expression. The order of evaluation is: parentheses,
division, multiplication, addition, and subtraction. The evaluation is performed from
the left to the right of the expression.

For example: If we want to display the first and last name of an employee combined
together, the SQL Select Statement would be like

SELECT first_name || ' ' || last_name FROM employee;


Output:
Rahul Sharma
Anjali Bhagwat
Stephen Fleming
Shekar Gowda
Priya Chandra

You can also provide aliases as below.


SELECT first_name || ' ' || last_name AS emp_name FROM employee;

Output:
emp_name
Rahul Sharma
Anjali
Bhagwat
Stephen
Fleming
Shekar
Gowda
Priya Chandra

SQL ALIAS
SQL Aliases are defined for columns and tables. Basically aliases is created to make the
column selected more readable.

Aliases for columns:

For Example: To select the first name of all the students, the query would be like:

SELECT first_name AS Name FROM student_details;

or

SELECT first_name Name FROM student_details;


In the above query, the column first_name is given a alias as 'name'. So when the result is
displayed the column name appears as 'Name' instead of 'first_name'.
Output:
Name
Rahul Sharma
Anjali
Bhagwat
Stephen
Fleming
Shekar Gowda
Priya Chandra

Aliases for tables:


SELECT s.first_name FROM student_details s;

In the above query, alias 's' is defined for the table student_details and the column first_name
is selected from the table.

Aliases is more useful when


 There are more than one tables involved in a query,
 Functions are used in the query,
 The column names are big or not readable,
 More than one columns are combined together

SQL WHERE Clause


The WHERE Clause is used when you want to retrieve specific information from a table
excluding other irrelevant data.

The condition you provide in the WHERE clause filters the rows retrieved from the table and
gives you only those rows which you expected to see. WHERE clause can be used along with
SELECT, DELETE, UPDATE statements.

Syntax of SQL WHERE Clause:


WHERE {column or expression} comparison-operator value

Syntax for a WHERE clause with Select statement is:

SELECT column_list FROM table-name


WHERE condition;
 column or expression - Is the column of a table or a expression
 comparison-operator - operators like = < > etc.
 value - Any user value or a column name for comparison
For Example: To find the name of a student with id 100, the query would be like:

SELECT first_name, last_name FROM student_details


WHERE id = 100;

 Comparison Operators and Logical Operators are used in WHERE Clause (Discussed
later)

How to use expressions in the WHERE Clause


 Expressions can also be used in the WHERE clause of the SELECT statement.
For example: Lets consider the employee table.
database table "employee";

ag salar
id name dept location
e y
10 Rames 2500 Bangalor
Electrical 24
0 h 0 e
10 Hrithi Electroni 3500 Bangalor
28
1 k cs 0 e
10 Harsh Aeronauti 3500
28 Mysore
2 a cs 0
10 Soum Electroni 2000 Bangalor
22
3 ya cs 0 e
10 3000 Mangalo
Priya InfoTech 25
4 0 re

If you want to display employee name, current salary, and a 20% increase in the
salary for only those employees where the percentage increase in salary is greater than
30000, the SELECT statement can be written as shown below

SELECT name, salary, salary*1.2 AS new_salary FROM employee


WHERE salary*1.2 > 30000;

Output:
Name salary new_salary
Hrithik 35000 42000
Harsha 35000 42000
Priya 30000 36000

SQL OPERATORS
There are two types of Operators, namely Comparison Operators and Logical Operators.
These operators are used mainly in the WHERE clause, HAVING clause to filter the data to
be selected.

Comparison Operators:
Comparison operators are used to compare the column data with specific values in a
condition.

Comparison Operators are also used along with the SELECT statement to filter data based on
specific conditions.

The below table describes each comparison operator.


Comparison
Description
Operators
= equal to
<>, != is not equal to
< less than
> greater than
greater than or equal
>=
to
<= less than or equal to

Logical Operators:
There are three Logical Operators namely, AND, OR, and NOT. These operators compare
two conditions at a time to determine whether a row can be selected for the output. When
retrieving data using a SELECT statement, you can use logical operators in the WHERE
clause, which allows you to combine more than one condition.

Logical
Description
Operators
For the row to be selected
OR at least one of the
conditions must be true.
For a row to be selected
AND all the specified
conditions must be true.
For a row to be selected
NOT the specified condition
must be false.

"OR" Logical Operator:


If you want to select rows that satisfy at least one of the given conditions, you can use the
logical operator, OR.

For example: if you want to find the names of students who are studying either Maths or
Science, the query would be like,

SELECT first_name, last_name, subject


FROM student_details
WHERE subject = 'Maths' OR subject = 'Science'

The output would be something like,


first_name last_name subject
Anajali Bhagwat Maths
Shekar Gowda Maths
Rahul Sharma Science
Stephen Fleming Science
The following table describes how logical "OR" operator selects a row.
Column1 Column2 Row
Satisfied? Satisfied? Selected
YES YES YES
YES NO YES
NO YES YES
NO NO NO

"AND" Logical Operator:


If you want to select rows that must satisfy all the given conditions, you can use the logical
operator, AND.

For Example: To find the names of the students between the age 10 to 15 years, the query
would be like:
SELECT first_name, last_name, age
FROM student_details
WHERE age >= 10 AND age <= 15;

The output would be something like,


first_name last_name Age
Rahul Sharma 10
Anajali Bhagwat 12
Shekar Gowda 15

The following table describes how logical "AND" operator selects a row.
Column1 Column2 Row
Satisfied? Satisfied? Selected
YES YES YES
YES NO NO
NO YES NO
NO NO NO

"NOT" Logical Operator:

If you want to find rows that do not satisfy a condition, you can use the logical operator,
NOT. NOT results in the reverse of a condition. That is, if a condition is satisfied, then the
row is not returned.

For example: If you want to find out the names of the students who do not play football, the
query would be like:

SELECT first_name, last_name, games


FROM student_details
WHERE NOT games = 'Football'

The output would be something like,


first_name last_name Games
Rahul Sharma Cricket
Stephen Fleming Cricket
Shekar Gowda Badminton
Priya Chandra Chess

The following table describes how logical "NOT" operator selects a row.
NOT
Column1 Row
Column1
Satisfied? Selected
Satisfied?
YES NO NO
NO YES YES

Nested Logical Operators:


You can use multiple logical operators in an SQL statement. When you combine the logical
operators in a SELECT statement, the order in which the statement is processed is
1) NOT
2) AND
3) OR

For example: If you want to select the names of the students whose’ age is between 10 and
15 years, or those who do not play football, the SELECT statement would be
SELECT first_name, last_name, age, games
FROM student_details
WHERE age >= 10 AND age <= 15
OR NOT games = 'Football'

The output would be something like,


first_name last_name age games
Rahul Sharma 10 Cricket
Priya Chandra 15 Chess

In this case, the filter works as follows:


Condition 1: All the students you do not play football are selected.
Condition 2: All the students whose are aged between 10 and 15 are selected.
Condition 3: Finally the result is, the rows which satisfy at least one of the above conditions
is returned.

NOTE:The order in which you phrase the condition is important, if the order changes you
are likely to get a different result.

SQL Comparison Keywords


There are other comparison keywords available in sql which are used to enhance the search
capabilities of a sql query. They are "IN", "BETWEEN...AND", "IS NULL", "LIKE".

Table 1: SQL comparison keywords

Comparision Operators Description


LIKE column value is similar to specified character(s).
column value is equal to any one of a specified set of
IN
values.
column value is between two values, including the end
BETWEEN...AND
values specified in the range.
IS NULL column value does not exist.

SQL LIKE Operator


The LIKE operator is used to list all rows in a table whose column values match a specified
pattern. It is useful when you want to search rows to match a specific pattern, or when you do
not know the entire value. For this purpose we use a wildcard character '%'.

For example: To select all the students whose name begins with 'S'

SELECT first_name, last_name


FROM student_details
WHERE first_name LIKE 'S%';
The output would be similar to:
first_name last_name
Stephen Fleming
Shekar Gowda

The above select statement searches for all the rows where the first letter of the column
first_name is 'S' and rest of the letters in the name can be any character.

There is another wildcard character you can use with LIKE operator. It is the underscore
character, ' _ ' . In a search string, the underscore signifies a single character.

For example: to display all the names with 'a' second character,

SELECT first_name, last_name


FROM student_details
WHERE first_name LIKE '_a%';

The output would be similar to:


first_name last_name
Rahul Sharma

NOTE: Each underscore act as a placeholder for only one character. So you can use more
than one underscore. Eg: ' __i% '-this has two underscores towards the left, 'S__j%' - this has
two underscores between character 'S' and 'i'.

SQL BETWEEN ... AND Operator


The operator BETWEEN and AND, are used to compare data for a range of values.
For Example: to find the names of the students between age 10 to 15 years, the query would
be like,
SELECT first_name, last_name, age
FROM student_details
WHERE age BETWEEN 10 AND 15;

The output would be similar to:


first_name last_name age
Rahul Sharma 10
Anajali Bhagwat 12
Shekar Gowda 15

SQL IN Operator:
The IN operator is used when you want to compare a column with more than one value. It is
similar to an OR condition.
For example: If you want to find the names of students who are studying either Maths or
Science, the query would be like,

SELECT first_name, last_name, subject


FROM student_details
WHERE subject IN ('Maths', 'Science');

The output would be similar to:


first_name last_name subject
Anajali Bhagwat Maths
Shekar Gowda Maths
Rahul Sharma Science
Stephen Fleming Science

You can include more subjects in the list like ('maths','science','history')


NOTE:The data used to compare is case sensitive.

SQL IS NULL Operator


A column value is NULL if it does not exist. The IS NULL operator is used to display all the
rows for columns that do not have a value.

For Example: If you want to find the names of students who do not participate in any games,
the query would be as given below

SELECT first_name, last_name


FROM student_details
WHERE games IS NULL

There would be no output as we have every student participate in a game in the table
student_details, else the names of the students who do not participate in any games would be
displayed.

SQL ORDER BY
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or
descending order. Oracle sorts query results in ascending order by default.

Syntax for using SQL ORDER BY clause to sort data is:


SELECT column-list
FROM table_name [WHERE condition]
[ORDER BY column1 [, column2, .. columnN] [DESC]];
database table "employee";

ag salar
id name dept location
e y
10 Rames Electrical 24 2500 Bangalor
0 h 0 e
10 Hrithi Electroni 3500 Bangalor
28
1 k cs 0 e
10 Harsh Aeronauti 3500
28 Mysore
2 a cs 0
10 Soum Electroni 2000 Bangalor
22
3 ya cs 0 e
10 3000 Mangalo
Priya InfoTech 25
4 0 re

For Example: If you want to sort the employee table by salary of the employee, the sql query
would be.

SELECT name, salary FROM employee ORDER BY salary;

The output would be like


name Salary
Soumya 20000
Ramesh 25000
Priya 30000
Hrithik 35000
Harsha 35000

The query first sorts the result according to name and then displays it.
You can also use more than one column in the ORDER BY clause.

If you want to sort the employee table by the name and salary, the query would be like,
SELECT name, salary FROM employee ORDER BY name, salary;

The output would be like:


name salary
Soumya 20000
Ramesh 25000
Priya 30000
Harsha 35000
Hrithik 35000

NOTE:The columns specified in ORDER BY clause should be one of the columns selected
in the SELECT column list.

You can represent the columns in the ORDER BY clause by specifying the position of a
column in the SELECT list, instead of writing the column name.
The above query can also be written as given below,
SELECT name, salary FROM employee ORDER BY 1, 2;

By default, the ORDER BY Clause sorts data in ascending order. If you want to sort the data
in descending order, you must explicitly specify it as shown below.

SELECT name, salary


FROM employee
ORDER BY name, salary DESC;

The above query sorts only the column 'salary' in descending order and the column 'name' by
ascending order.

If you want to select both name and salary in descending order, the query would be as given
below.

SELECT name, salary


FROM employee
ORDER BY name DESC, salary DESC;

How to use expressions in the ORDER BY Clause


For example: If you want to display employee name, current salary, and a 20% increase in
the salary for only those employees for whom the percentage increase in salary is greater than
30000 and in descending order of the increased price, the SELECT statement can be written
as shown below

SELECT name, salary, salary*1.2 AS new_salary


FROM employee
WHERE salary*1.2 > 30000
ORDER BY new_salary DESC;

The output for the above query is as follows.


name salary new_salary
Hrithik 35000 42000
Harsha 35000 42000
Priya 30000 36000

NOTE: Aliases defined in the SELECT Statement can be used in ORDER BY Clause.

SQL GROUP Functions


Group functions are built-in SQL functions that operate on groups of rows and return one
value for the entire group. These functions are: COUNT, MAX, MIN, AVG, SUM,
DISTINCT
SQL COUNT (): This function returns the number of rows in the table that satisfies the
condition specified in the WHERE condition. If the WHERE condition is not specified,
then the query returns the total number of rows in the table.

For Example: If you want the number of employees in a particular department, the query
would be:
SELECT COUNT (*) FROM employee
WHERE dept = 'Electronics';
The output would be '2' rows.
If you want the total number of employees in all the department, the query would take the
form:
SELECT COUNT (*) FROM employee;

The output would be '5' rows.

SQL DISTINCT(): This function is used to select the distinct rows.

For Example: If you want to select all distinct department names from employee table, the
query would be:

SELECT DISTINCT dept FROM employee;

To get the count of employees with unique name, the query would be:
SELECT COUNT (DISTINCT name) FROM employee;

SQL MAX(): This function is used to get the maximum value from a column.
To get the maximum salary drawn by an employee, the query would be:

SELECT MAX (salary) FROM employee;

SQL MIN(): This function is used to get the minimum value from a column.
To get the minimum salary drawn by an employee, The query would be:

SELECT MIN (salary) FROM employee;

SQL AVG(): This function is used to get the average value of a numeric column.
To get the average salary, the query would be

SELECT AVG (salary) FROM employee;

SQL SUM(): This function is used to get the sum of a numeric column
To get the total salary given out to the employees,

SELECT SUM (salary) FROM employee;


SQL GROUP BY Clause
The SQL GROUP BY Clause is used along with the group functions to retrieve data
grouped according to one or more columns.

For Example: If you want to know the total amount of salary spent on each department,
the query would be:

SELECT dept, SUM (salary)


FROM employee
GROUP BY dept;

The output would be like:

dept salary
Electrical 25000
Electronics 55000
Aeronautics 35000
InfoTech 30000

SQL HAVING Clause


Having clause is used to filter data based on the group functions. This is similar to WHERE
condition but is used with group functions. Group functions cannot be used in a WHERE
Clause but can be used in HAVING clause.

For Example: If you want to select the department that has total salary paid for its
employees more than 25000, the sql query would be like;

SELECT dept, SUM (salary)


FROM employee
GROUP BY dept
HAVING SUM (salary) > 25000

The output would be like:


dept Salary
Electronics 55000
Aeronautics 35000
InfoTech 30000
Note: When WHERE, GROUP BY and HAVING clauses are used together in a SELECT
statement, the WHERE clause is processed first, then the rows that are returned after the
WHERE clause is executed are grouped based on the GROUP BY clause.

Finally, any conditions on the group functions in the HAVING clause are applied to the
grouped rows before the final output is displayed.

Edit table schema using SQL ALTER statement


The ALTER statement in SQL is used to create, update or delete table columns and their
respective data types and constraints.

Syntax to add a column


ALTER TABLE table_name ADD column_name datatype;

For Example: To add a column "experience" to the employee table, the query
would be like

ALTER TABLE employee ADD experience integer;

Syntax to drop a column


ALTER TABLE table_name DROP column_name;

For Example: To drop the column "location" from the employee table, the
query would be like

ALTER TABLE employee DROP location;

Syntax to modify a column


ALTER TABLE table_name MODIFY column_name datatype;

For Example: To modify the column salary in the employee table, the query would
be like

ALTER TABLE employee MODIFY salary FLOAT(15,2);

SQL DROP command


The SQL DROP command is used to remove an object from the database. If you drop a table,
all the rows in the table is deleted and the table structure is removed from the database. Once
a table is dropped we cannot get it back, so be careful while using DROP command. When a
table is dropped all the references to the table will not be valid.

Syntax to drop a sql table structure:

DROP TABLE table_name;

For Example: To drop the table employee, the query would be like
DROP TABLE employee;

Data Manipulation using SQL


The INSERT statement

The INSERT Statement is used to add new rows of data to a table.

We can insert data to a table in two ways,

1) Inserting the data directly to a table.


Syntax for SQL INSERT is:

INSERT INTO TABLE_NAME


[ (col1, col2, col3,...colN)]
VALUES (value1, value2, value3,...valueN);
 col1, col2,...colN -- the names of the columns in the table into which you want to
insert data.

While inserting a row, if you are adding value for all the columns of the table you need not
specify the column(s) name in the sql query. But you need to make sure the order of the
values is in the same order as the columns in the table.

The sql insert query will be as follows .

INSERT INTO TABLE_NAME


VALUES (value1, value2, value3,...valueN);

For Example: If you want to insert a row to the employee table, the query would be like,

INSERT INTO employee (id, name, dept, age, salary)


VALUES (105, 'Srinath', 'Aeronautics', 27, 33000);

NOTE: When adding a row, only the characters o te values r da should be enclosed with
single quotes.

If you are inserting data to all the columns, the column names can be omitted. The above
insert statement can also be written as,

INSERT INTO employee


VALUES (105, 'Srinath', 'Aeronautics', 27, 33000);

Inserting data to a table through a select statement.


Syntax for SQL INSERT is:

INSERT INTO table_name


[(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN
FROM table_name [WHERE condition];

For Example: To insert a row into the employee table from a temporary table, the sql insert
query would be like,

INSERT INTO employee (id, name, dept, age, salary location)


SELECT emp_id, emp_name, dept, age, salary, location
FROM temp_employee;

If you are inserting data to all the columns, the above insert statement can also be written as,

INSERT INTO employee


SELECT * FROM temp_employee;

NOTE: We have assumed the temp_employee table has columns emp_id, emp_name, dept,
age, salary, location in the above given order and the same datatype.

IMPORTANT NOTE:
1) When adding a new row, you should ensure the datatype of the value and the column
matches
2) You follow the integrity constraints, if any, defined for the table.

The UPDATE statement


The UPDATE Statement is used to modify the existing rows in a table.
The Syntax for SQL UPDATE Command is:

UPDATE table_name
SET column_name1 = value1,
column_name2 = value2, ...
[WHERE condition]
 table_name - the table name which has to be updated.
 column_name1, column_name2.. - the columns that gets changed.
 value1, value2... - are the new values.

NOTE: In the Update statement, WHERE clause identifies the rows that get affected. If you
do not include the WHERE clause, column values for all the rows get affected.

For Example: To update the location of an employee, the sql update query would be like,

UPDATE employee
SET location ='Mysore'
WHERE id = 101;
To change the salaries of all the employees, the query would be,

UPDATE employee
SET salary = salary + (salary * 0.2);

The SQL DELETE statement


The DELETE Statement is used to delete rows from a table.

The Syntax of a SQL DELETE statement is:

DELETE FROM table_name [WHERE condition];


 table_name -- the table name which has to be updated.

NOTE:The WHERE clause in the sql delete command is optional and it identifies the rows
in the column that gets deleted. If you do not include the WHERE clause all the rows in the
table is deleted, so be careful while writing a DELETE query without WHERE clause.

For Example: To delete an employee with id 100 from the employee table, the sql delete
query would be like,

DELETE FROM employee WHERE id = 100;

To delete all the rows from the employee table, the query would be like,

DELETE FROM employee;

The SQL TRUNCATE statement


The SQL TRUNCATE command is used to delete all the rows from the table and free the
space containing the table.
Syntax to TRUNCATE a table:
TRUNCATE TABLE table_name;

For Example: To delete all the rows from employee table, the query would be like,
TRUNCATE TABLE employee;

Difference between DELETE and TRUNCATE Statements:

DELETE Statement: This command deletes only the rows from the table based on the
condition given in the where clause or deletes all the rows from the table if no condition is
specified. But it does not free the space containing the table.

TRUNCATE statement: This command is used to delete all the rows from the table and free
the space containing the table.
Sql joins
SQL Joins are used to relate information in different tables. A Join condition is a part of the
SQL query that retrieves rows from two or more tables. A SQL Join condition is used in
the SQL WHERE Clause of select, update, delete statements.

The Syntax for joining two tables is:

SELECT col1, col2, col3...


FROM table_name1, table_name2
WHERE table_name1.col2 = table_name2.col1;

If an SQL join condition is omitted or if it is invalid the join operation will result in a
Cartesian product.

The Cartesian product returns a number of rows equal to the product of all rows in all the
tables being joined. For example, if the first table has 20 rows and the second table has 10
rows, the result will be 20 * 10, or 200 rows. This query takes a long time to execute.

Lets use the below two tables to explain the sql join conditions.
database table "product";
produ product_ supplier_ unit_p
ct_id name name rice
100 Camera Nikon 300
Televisio
101 Onida 100
n
Refrigera
102 Vediocon 150
tor
103 Ipod Apple 75
104 Mobile Nokia 50

database table "order_items";


order_ product total_un custom
id _id its er
5100 104 30 Infosys
5101 102 5 Satyam
5102 103 25 Wipro
5103 101 10 TCS

SQL Joins can be classified into Equi join and Non Equi join.

1) SQL Equi joins


It is a simple SQL join condition which uses the equal sign as the comparison operator.
Two types of equi joins are SQL Outer join and SQL Inner join.

For example: You can get the information about a customer who purchased a product and
the quantity of product.

2) SQL Non equi joins


It is a SQL join condition which makes use of some comparison operator other than the
equal sign . For example >, <, >=, <=

SQL Equi Joins:


An equi-join is further classified into two categories:
a) SQL Inner Join
b) SQL Outer Join
a) SQL Inner Join:
All the rows returned by the SQL query satisfy the SQL join condition specified.

For example: If you want to display the product information for each order the query will be
as given below.

Since you are retrieving the data from two tables, you need to identify the common column
between these two tables, which is the product_id.

The query for this type of sql joins would be like,

SELECT order_id, product_name, unit_price, supplier_name, total_units


FROM product, order_items
WHERE order_items.product_id = product.product_id;

The columns must be referenced by the table name in the join condition, because product_id
is a column in both the tables and needs a way to be identified. This avoids ambiguity in
using the columns in the SQL SELECT statement.

The number of join conditions is (n-1), if there are more than two tables joined in a query
where 'n' is the number of tables involved. The rule must be true to avoid Cartesian product.

We can also use aliases to reference the column name, then the above query would be like,

SELECT o.order_id, p.product_name, p.unit_price, p.supplier_name, o.total_units


FROM product p, order_items o
WHERE o.product_id = p.product_id;

b) SQL Outer Join:


This SQL join condition returns all rows from both tables which satisfy the join condition
along with rows which do not satisfy the join condition from one of the tables. The SQL outer
join operator in Oracle is ( + ) and is used on one side of the join condition only.

The syntax differs for different RDBMS implementation. Few of them represent the join
conditions as "SQL left outer join", "SQL right outer join".

If you want to display all the product data along with order items data, with null values
displayed for order items if a product has no order item, the SQL query for outer join would
be as shown below:

SELECT p.product_id, p.product_name, o.order_id, o.total_units


FROM order_items o, product p
WHERE o.product_id (+) = p.product_id;

The output would be like,


product_id product_name order_id total_units
100 Camera
101 Television 5103 10
102 Refrigerator 5101 5
103 Ipod 5102 25
104 Mobile 5100 30

NOTE:If the (+) operator is used in the left side of the join condition it is equivalent to left
outer join. If used on the right side of the join condition it is equivalent to right outer join.

Phases of Database Development


 Establish and specify the organisation requirements
 Design the conceptual database – ER model
 Design the logical database – Specify table schemas, normalize data tables
 Implement the database - Create the physical design; Create tables, insert data and
create required queries, create indexes, views, security systems, etc.
 Test the database
 Review and maintain the database – Make changes as necessary

Conclusion
At the end of this learning outcome, the learner should be able to eexplain SQL and data
definition, create tables using CREATE TABLE constraints, use the ALTER TABLE AND
DROP TABLE statements, demonstrate data manipulation query statements, explain and
demonstrating types of joins, create and query a database from validated ER model.

Further Reading
Davidson, L. (2001). Professional SQL server 2000 database design. Birmingham, UK:
Wrox Press.

[Link] Self Assessment


Written Assessment
1. Which are data manipulation commands in SQL?
2. How does the ORDER by clause worn in SQL?
3. What is the difference between the DELETE and TRUNCATE commands?
4. Which are the uses of the asterisk (*) in various SQL commands?
5. ------------ command is used erase a table.
a. DELETE
b. ALTER
c. ALIAS
d. DROP

6. Which command is used to extract data from a database table?


a. Get
b. Select
cc Pop
[Link]

7. Which operator allows you to specify multiple values in WHERE clause?


a. In
b. Range
c. Like
d. Between

9. Which key word is used with update command to change the value?
a. Alter
b. Set
c Change
d. Add

10. Which key word is used with alter-command to delete a column?


a. Remove
b. Change
c. Drop
d. delete.

Oral Assessment
1. What are the risks involved in using the DROP table command
2. What is the role of a join?

Practical assessment
Consider the following table to answer the questions that follow:
Customer
Customer_I Name Address Town Age
D
503998 Charles 1423 Nakuru 43
504321 Mary 564 Nairobi 23
502342 Judy 4234 Mombasa 12
502321 Maina 7765 Kisumu 23

Required
Write the SQL statement which could be used to
(a) Create the table above
(b) Display a list of customer who are below 30yrs and come from Nairobi
(c) Add a new column called Physical Location to the Table
(d) Delete all the rows whose customers are above 40 yrs of age
e) Update the address of whose Customer_ID is 504321 to 68232

[Link] Tools, Equipment, Supplies and Materials


 MS SQL server software
 Computers
 Instruction material
 Stationery
 Internet

[Link] References
1) Davidson, L. (2001). Professional SQL server 2000 database design. Birmingham,
UK: Wrox Press.
2) [Link]
3) [Link]
explained-in-simple-english/(Retrieved on 2nd March 2020
6.3.5 Learning Outcome 4: Understand design of object oriented databases
[Link] Learning Activities
Learning Outcome No. 4: Understand design of object oriented databases

Learning Activities Special Instructions

Activities may be carried out in groups


4.1 An object oriented database is explained or as individual
4.2 Object oriented database concepts are explained.
4.3 Object Oriented database concepts are implemented
from a set of requirements. ·
4.4 Creating of views and triggers in object oriented
databases is demonstrated.

·
[Link] Information Sheet No 6/LO4: Understand design of object oriented databases

Introduction

This learning outcome covers explaning object oriented databases and their concepts,
implementing object oriented concepts and creating views and triggers.

Object oriented database- A database management system in which data is stored in the
form of objects which are instances of classes.

Comparison with other RDBMS


BASIS RDBMS OODBMS

Connection Between Two A Primary key distinctively An object identifier (OID) is


Relations identifies an object in a a name for any type of object
table. or entity.

Way of storing data Stores data in Entities, Stores data as Objects.


defined as tables hold
specific information.

Data Handling Handles comparatively Handles larger and complex


simpler data. data than RDBMS.

Object oriented database concepts

Object-A real-world entity which has a unique identity

Class-A collection of objects with the same or similar attributes and behavior.

Attributes-The characteristics used to describe objects, also called instance variables.

Inheritance-Creation of a new object from an existing object in such a way that a new
object inherits all characteristics of an existing object.

Encapsulation-A data hiding concept in which attributes, methods and functions to


manipulate the data are hidden from the outside world

Implementation of Object Oriented Database (OOD) Concepts


Below is a demonstration of OOD concepts using PL/SQL – Oracle Programming
Language for SQL.

Objects are created using the CREATE [OR REPLACE] TYPE statement. Following is an
example to create a simple address object consisting of few attributes –

CREATE OR REPLACE TYPE address AS OBJECT


(house_no varchar2(10),
street varchar2(30),
city varchar2(20),
state varchar2(10),
pincode varchar2(10)
);
/
When the above code is executed at the SQL prompt, it produces the following result –

Type created.

Let's create one more object customer where we will wrap attributes and methods together
to have object-oriented feeling –

CREATE OR REPLACE TYPE customer AS OBJECT


(code number(5),
name varchar2(30),
contact_no varchar2(12),
addr address,
member procedure display
);
/
When the above code is executed at the SQL prompt, it produces the following result –
Type created.

Instantiating an Object
Defining an object type provides a blueprint for the object. To use this object, you need to
create instances of this object. You can access the attributes and methods of the object using
the instance name and the access operator (.) as follows −
DECLARE
residence address;
BEGIN
residence := address('103A', '[Link]', 'Jaipur', 'Rajasthan','201301');
dbms_output.put_line('House No: '|| residence.house_no);
dbms_output.put_line('Street: '|| [Link]);
dbms_output.put_line('City: '|| [Link]);
dbms_output.put_line('State: '|| [Link]);
dbms_output.put_line('Pincode: '|| [Link]);
END;
/
When the above code is executed at the SQL prompt, it produces the following result −

House No: 103A


Street: [Link]
City: Jaipur
State: Rajasthan
Pincode: 201301

PL/SQL procedure successfully completed.

Member Methods
Member methods are used for manipulating the attributes of the object. You provide the
declaration of a member method while declaring the object type. The object body defines the
code for the member methods. The object body is created using the CREATE TYPE BODY
statement.
Constructors are functions that return a new object as its value. Every object has a system
defined constructor method. The name of the constructor is same as the object type. For
example –

residence := address('103A', '[Link]', 'Jaipur', 'Rajasthan','201301');

The comparison methods are used for comparing objects. There are two ways to compare
objects
Map method
The Map method is a function implemented in such a way that its value depends upon the
value of the attributes. For example, for a customer object, if the customer code is same for
two customers, both customers could be the same. So the relationship between these two
objects would depend upon the value of code.

Order method
The Order method implements some internal logic for comparing two objects. For
example, for a rectangle object, a rectangle is bigger than another rectangle if both its sides
are bigger.

Map method Demonstrated


Let us try to understand the above concepts using the following rectangle object −
CREATE OR REPLACE TYPE rectangle AS OBJECT
(length number,
width number,
member function enlarge( inc number) return rectangle,
member procedure display,
map member function measure return number
);
/
When the above code is executed at the SQL prompt, it produces the following result :
Type created.

Creating the type body

CREATE OR REPLACE TYPE BODY rectangle AS


MEMBER FUNCTION enlarge(inc number) return rectangle IS
BEGIN
return rectangle([Link] + inc, [Link] + inc);
END enlarge;
MEMBER PROCEDURE display IS
BEGIN
dbms_output.put_line('Length: '|| length);
dbms_output.put_line('Width: '|| width);
END display;
MAP MEMBER FUNCTION measure return number IS
BEGIN
return (sqrt(length*length + width*width));
END measure;
END;
/
When the above code is executed at the SQL prompt, it produces the following result:
Type body created.
Now using the rectangle object and its member functions −
DECLARE
r1 rectangle;
r2 rectangle;
r3 rectangle;
inc_factor number := 5;
BEGIN
r1 := rectangle(3, 4);
r2 := rectangle(5, 7);
r3 := [Link](inc_factor);
[Link];
IF (r1 > r2) THEN -- calling measure function
[Link];
ELSE
[Link];
END IF;
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
Length: 8
Width: 9
Length: 5
Width: 7

PL/SQL procedure successfully completed.

Inheritance for PL/SQL Objects


PL/SQL allows creating object from the existing base objects. To implement inheritance, the
base objects should be declared as NOT FINAL. The default is FINAL.
The following programs illustrate the inheritance in PL/SQL Objects. Let us create another
object named TableTop, this is inherited from the Rectangle object. For this, we need to
create the base rectangle object –

CREATE OR REPLACE TYPE rectangle AS OBJECT


(length number,
width number,
member function enlarge( inc number) return rectangle,
NOT FINAL member procedure display) NOT FINAL
/

When the above code is executed at the SQL prompt, it produces the following result −
Type created.

Creating the base type body


CREATE OR REPLACE TYPE BODY rectangle AS
MEMBER FUNCTION enlarge(inc number) return rectangle IS
BEGIN
return rectangle([Link] + inc, [Link] + inc);
END enlarge;
MEMBER PROCEDURE display IS
BEGIN
dbms_output.put_line('Length: '|| length);
dbms_output.put_line('Width: '|| width);
END display;
END;
/
When the above code is executed at the SQL prompt, it produces the following result –
Type body created.

Creating the child object tabletop −


CREATE OR REPLACE TYPE tabletop UNDER rectangle
(
material varchar2(20),
OVERRIDING member procedure display
)
/
When the above code is executed at the SQL prompt, it produces the following result −
Type created.

Creating the type body for the child object tabletop


CREATE OR REPLACE TYPE BODY tabletop AS
OVERRIDING MEMBER PROCEDURE display IS
BEGIN
dbms_output.put_line('Length: '|| length);
dbms_output.put_line('Width: '|| width);
dbms_output.put_line('Material: '|| material);
END display;
/
When the above code is executed at the SQL prompt, it produces the following result –
Type body created.

Using the tabletop object and its member functions −


DECLARE
t1 tabletop;
t2 tabletop;
BEGIN
t1:= tabletop(20, 10, 'Wood');
t2 := tabletop(50, 30, 'Steel');
[Link];
[Link];
END;
/
When the above code is executed at the SQL prompt, it produces the following result –

Length: 20
Width: 10
Material: Wood
Length: 50
Width: 30
Material: Steel

PL/SQL procedure successfully completed.

Creation of views and triggers.


Views-A virtual table used for hiding unwanted information.
Syntax

create or replace
view view_name
as
select column_name1, column_name2,...
from table_name
where condition;

For example

CREATE VIEW Details View AS


SELECT NAME, ADDRESS
FROM Student Details
WHERE STU_ID < 4;

A table of names and addresses is created of students whose student ID value is less than 4.

Trigger-These are stored programs which are automatically executed whenever a special
event occurs in the data

Triggers are used to:


● Automatically generate derived column values
● Prevent invalid transactions
● Enforce complex security authorizations
● Enforce complex business rules
● Provide transparent event logging
Syntax

➢ create trigger [trigger_name]: Creates or replaces an existing trigger with the


trigger_name.
➢ [before | after]: This specifies when the trigger will be executed.
➢ {insert | update | delete}: This specifies the DML operation.
➢ on [table_name]: This specifies the name of the table associated with the trigger.
➢ [for each row]: This specifies a row-level trigger, i.e., the trigger will be
executed for each row being affected.
➢ [trigger_body]: This provides the operation to be performed as trigger is fired

SQL Server CREATE TRIGGER example

“Virtual” tables for triggers: INSERTED and DELETED


SQL Server provides two virtual tables that are available specifically for triggers
called INSERTED and DELETED tables. SQL Server uses these tables to capture the data of the
modified row before and after the event occurs.

The following table shows the content of the INSERTED and DELETED tables before and after
each event:
DML event INSERTED table holds DELETED table holds
INSERT rows to be inserted empty
UPDATE new rows modified by the update existing rows modified by the update
DELETE Empty rows to be deleted

We will use the following [Link] table from the sample database for the
demonstration.
Create a table for logging the changes
The following statement creates a table named production.product_audits to record
information when an INSERT or DELETE event occurs against
the [Link] table:

CREATE TABLE production.product_audits(


change_id INT IDENTITY PRIMARY KEY,
product_id INT NOT NULL,
product_name VARCHAR(255) NOT NULL,
brand_id INT NOT NULL,
category_id INT NOT NULL,
model_year SMALLINT NOT NULL,
list_price DEC(10,2) NOT NULL,
updated_at DATETIME NOT NULL,
operation CHAR(3) NOT NULL,
CHECK(operation = 'INS' or operation='DEL')
);
The production.product_audits table has all the columns from
the [Link] table. In addition, it has a few more columns to record the
changes e.g., updated_at, operation, and the change_id.

Creating an after DML trigger


First, to create a new trigger, you specify the name of the trigger and schema to which the
trigger belongs in the CREATE TRIGGER clause:

CREATE TRIGGER production.trg_product_audit


Next, you specify the name of the table, which the trigger will fire when an event occurs, in
the ON clause:
ON [Link]

Then, you list the one or more events which will call the trigger in the AFTER clause:
AFTER INSERT, DELETE

The body of the trigger begins with the AS keyword:


AS
BEGIN

After that, inside the body of the trigger, you set the SET NOCOUNT to ON to suppress the
number of rows affected messages from being returned whenever the trigger is fired.
SET NOCOUNT ON;

The trigger will insert a row into the production.product_audits table whenever a row is
inserted into or deleted from the [Link] table. The data for insert is fed from
the INSERTED and DELETED tables via the UNION ALL operator:INSERT INTO

production.product_audits
(
product_id,
product_name,
brand_id,
category_id,
model_year,
list_price,
updated_at,
operation
)
SELECT
i.product_id,
product_name,
brand_id,
category_id,
model_year,
i.list_price,
GETDATE(),
'INS'
FROM
inserted AS i
UNION ALL
SELECT
d.product_id,
product_name,
brand_id,
category_id,
model_year,
d.list_price,
getdate(),
'DEL'
FROM
deleted AS d;

The following put all parts together:


CREATE TRIGGER production.trg_product_audit
ON [Link]
AFTER INSERT, DELETE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO production.product_audits(
product_id,
product_name,
brand_id,
category_id,
model_year,
list_price,
updated_at,
operation
)
SELECT
i.product_id,
product_name,
brand_id,
category_id,
model_year,
i.list_price,
GETDATE(),
'INS'
FROM
inserted i
UNION ALL
SELECT
d.product_id,
product_name,
brand_id,
category_id,
model_year,
d.list_price,
GETDATE(),
'DEL'
FROM
deleted d;
END

Finally, you execute the whole statement to create the trigger. Once the trigger is created, you
can find it under the triggers folder of the table as shown in the following picture:
Testing the trigger
The following statement inserts a new row into the [Link] table:

INSERT INTO [Link](


product_name,
brand_id,
category_id,
model_year,
list_price
)
VALUES (
'Test product',
1,
1,
2018,
599
);
Because of the INSERT event, the production.trg_product_audit trigger
of [Link] table was fired.

Let’s examine the contents of the production.product_audits table:

SELECT
*
FROM
production.product_audits;
Here is the output:
The following statement deletes a row from the [Link] table:
DELETE FROM
[Link]
WHERE
product_id = 322;
As expected, the trigger was fired and inserted the deleted row into
the production.product_audits table:

SELECT
*
FROM
production.product_audits;
The following picture shows the output:

Conclusion
At the end of this learning outcome, the learner should be able to explain object oriented
databases and their concepts, implement object oriented concepts and create views and
triggers.

Further Reading
Davidson, L. (2001). Professional SQL server 2000 database design. Birmingham, UK:

[Link] Self-Assessment
Written Assessment

1.A __________ is a special kind of a stored procedure that executes in response to certain
actions on the table like insertion, deletion or updating of data.
a. Procedures
B. B. Triggers
c. Functions
d. None of the mentioned

2. The CREATE TRIGGER statement is used to create the trigger. THE _____ clause
specifies the table name on which the trigger is to be attached. The ______ specifies that
this is an AFTER-INSERT trigger.
a. for insert, on
b. On, for insert
c. For, insert
d. Both a and c
3. What is the purpose of a view?
4. What command do you use to create a view?
5. How is a view different from a normal table?

Oral Assessment
Explain object oriented concepts that can be used in object oriented databases.

Practical Assessment
Create a database with an Employee table that has the following schema, then demonstrate
how insertion and deletion of records using a trigger.

Employee (EmployeeNumber, FirstName, LastName, Age)

[Link] Tools. Equipment, Supplies and Materials


 MS SQL server software
 Computers
 Instruction material
 Stationery
[Link] Reference.
[Link]

6.3.6 Learning Outcome 5: Understand indexing and hashing


[Link] Learning Activities
Learning Outcome No.5: Understand indexing and hashing

Learning Activities Special Instructions

Activities may be carried out in groups or


1.1 Indexing and hashing are explained. as individual
1.2 Indexing in databases is demonstrated.
1.3 Hashing in databases is demonstrated.
1.4 Indexing and hashing is implemented in an ·
existing database

·
[Link] Information Sheet No 6/LO5: Understand indexing and hashing

Introduction

This learning outcome covers explaining, demonstrating and implementing indexing and
hashing in a database.

Indexing
Indexes are special lookup tables that the database search engine can use to speed up data
retrieval. Simply put, an index is a pointer to data in a table. An index in a database is very
similar to an index in the back of a book.

For example, if you want to reference all pages in a book that discusses a certain topic, you
first refer to the index, which lists all the topics alphabetically and are then referred to one or
more specific page numbers.

An index helps to speed up SELECT queries and WHERE clauses, but it slows down data
input, with the UPDATE and the INSERT statements. Indexes can be created or dropped
with no effect on the data.

Creating an index involves the CREATE INDEX statement, which allows you to name the
index, to specify the table and which column or columns to index, and to indicate whether
the index is in an ascending or descending order.
Indexes can also be unique, like the UNIQUE constraint, in that the index prevents
duplicate entries in the column or combination of columns on which there is an index.

Types of indexing
Primary Indexing- Data is sorted according to the search key the primary key of the database
table is used to create the index .There is a one-to-one relationship between the entries in the
index table and the records in the main table making quick and efficient.

There are two types of primary indexing;


● Dense indexing-For every search key value in the data file, there is an
index record which contains the search key and a reference to the first
data record with that search key value.
● Sparse index-The index record appears only for a few items in the data
file. Each item points to a block. To locate a record, we find the index
record with the largest search key value less than or equal to the search
key value we are looking for.
We start at that record pointed to by the index record, and proceed along
with pointers in the file (sequentially) until we find the desired record.
Secondary Indexing-In this method, the huge range for the columns is selected initially so that
the mapping size of the first level becomes small. Then each range is further divided into
smaller ranges. The mapping of the first level is stored in the primary memory, so that address
fetch is faster. The mapping of the second level and actual data are stored in the secondary
memory (hard disk).

When should indexes be avoided?


Although indexes are intended to enhance a database's performance, there are times when
they should be avoided.
The following guidelines indicate when the use of an index should be reconsidered.
 Indexes should not be used on small tables.
 Tables that have frequent, large batch updates or insert operations.
 Indexes should not be used on columns that contain a high number of NULL values.
 Columns that are frequently manipulated should not be indexed

Hashing
This is a technique used to directly search the location of desired data on the disk without
using index structure. Data is stored in the form of data blocks whose address is generated
by applying a hash function in the memory location where these records are stored known
as a data block or data bucket.

Data bucket-The memory locations where the records are stored.

Hash Function-It that maps all the set of search keys to actual record address. It can be a single
mathematical function

Hash Index-An address of the data block.

Types of Hashing
1. Static Hashing-when a search-key value is provided, the hash function always computes
the same address.
2. Dynamic Hashing-Static hashing does not expand or shrink as the size of the database
grows or shrinks. Dynamic hashing provides a mechanism in which data buckets are
added and removed dynamically and on-demand.
Figure 20:Hashing in a database

SQL CREATE INDEX Statement


The CREATE INDEX statement is used to create indexes in tables. Indexes are used to
retrieve data from the database more quickly than otherwise. The users cannot see the
indexes, they are just used to speed up searches/queries.

Note: Updating a table with indexes takes more time than updating a table without (because
the indexes also need an update). So, only create indexes on columns that will be frequently
searched against.

CREATE INDEX Syntax


Creates an index on a table. Duplicate values are allowed:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
CREATE UNIQUE INDEX Syntax

Creates a unique index on a table. Duplicate values are not allowed:


CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ...);
Note: The syntax for creating indexes varies among different databases. Therefore: Check the
syntax for creating indexes in your database.

CREATE INDEX Example


The SQL statement below creates an index named "idx_lastname" on the "LastName"
column in the "Persons" table:
CREATE INDEX idx_lastname
ON Persons (LastName);
If you want to create an index on a combination of columns, you can list the column names
within the parentheses, separated by commas:

CREATE INDEX idx_pname


ON Persons (LastName, FirstName);

DROP INDEX Statement


The DROP INDEX statement is used to delete an index in a table.
DROP INDEX table_name.index_name;
Conclusion
At the end of this learning outcome, the learner should be able to explain, demonstrate and
implement indexing and hashing in a database.

Further Reading
1. [Link]
2. [Link]
3. [Link]

[Link] Self-Assessment
Written Assessment
1. What is the purpose of index in sql server
A. To enhance the query performance
B. To provide an index to a record
C. To perform fast searches
D. All of the mentioned
2. An indexing operation
A. sorts a file using a single key
B. sorts file using two keys
C. establishes an index for a file
D. both (b) and (c)
3. The index consists of
A. a list of keys
B. pointers to the master list
C. both (a) and (b)
D. All of the above
4. The hashing technique which allocates fixed number of buckets is classified as
A. dynamic hashing
B. Static hashing
C. external hashing
D. internal hashing
5. The index which has an entry for some of the key value is classified as
A. linear index
B. dense index
C. Sparse index
D. cluster index
6. What is the name given to indexing based on assorted order?
A. Sequenced indices
B. hashing indices
C ordered indices
D. Structured indices
9. A harsh function must meet______criteria
A. 2
B. 3
C. 4
D. none of the above

Oral Assessment
When should use of indexes be avoided?

Practical Assessment
Create a database with an Employee table that has the following schema, then demonstrate
how a single column index can be created for the table.

Employee (EmployeeNumber, FirstName, LastName, Age)

[Link] Tools. Equipment, Supplies and Materials


 MS SQL server software
 Computers
 Instruction material
 Stationery

[Link] References
1. [Link]
2. [Link]
6.3.7 Learning Outcome 6: Understand Database applications
[Link] Learning Activities
Learning Outcome No.6: Understand Database applications

Learning Activities Special Instructions

6.1 Decision support systems are explained. Activities may be carried out in
6.2 Data mining is explained groups or as individual
6.3 Distributed databases are demonstrated
6.4 Data warehousing is illustrated
6.5 Spatial and geographical databases are explained ·
6.6 Multi-media databases are illustrated
6.7 Mobility and personal databases are explained.
6.8 Data warehouses are designed and implemented from
a given set of requirements.

·
[Link] Information Sheet No 6/LO6: Understand Database applications

Introduction

This learning outcome covers explaining decision support systems, data mining, mobility
and personal databases, spatial and geographical databases, demonstrating distributed
databases, illustrating multi-media databases, illustrating, designing and implementing a
data warehouse.

Decision support system


DSS is an interactive computer-based system to help decision makers use communications
technologies, data, documents, knowledge and/or models to identify and solve problems,
complete decision process tasks, and make decisions e.g. Geographic Information
Systems(GI.S)

Database Mining-In organizations data is stored in databases. Data mining is the process
of automatically searching large databases to discover anomalies, patterns and correlations
within large data sets to predict outcomes. Data mining is used in sectors like
Marketing -To explore databases and to improve market segmentation by analysing the
relationships between parameters such as customer age, gender, tastes, etc. The trends can
predict which users are likely to unsubscribe from a service, what interests them based on
their searches, to achieve a higher response rate.
Retail- Supermarkets, for example, use joint purchasing patterns to identify product
associations and decide how to place them in the aisles and on the shelves.
Medicine-Data mining enables more accurate diagnostics. Having all of the patient's
information, such as medical records, physical examinations, and treatment patterns, allows
more effective treatments to be prescribed. It also enables more effective, efficient and
cost-effective management of health resources by identifying risks, predicting illnesses in
certain segments of the population or forecasting the length of hospital admission

Distributed database systems


This is a collection of logically related databases that are physically distributed across
sites in a computer network.A distributed database management system(DDMS) is a
software that manages the DDB and provides an access mechanism that makes this
distribution transparent to the users.

Illustration of a Distributed Database

Consider a banking system consisting of four branches in four different cities. Each branch
has its own computer, with a database of all the accounts maintained at that branch. Each
installation is a site. There also exists one single site that maintains information about all
the branches of the bank.

Each branch maintains (among others) a relation account where

Account schema is follows: (account_number, branch-name, balance)

The site containing information about all the branches of the bank maintains the relation
branch
Branch schema is as follows: ( branch_name, branch-city, assets).

Of course there are other relations maintained at the various sites.

Types of Distributed Databases


1. Homogeneous systems-is a network of two or more databases with the same type of
operating system, DBMS software and the data structures at all sites.
2. Heterogeneous Systems-is a network of two or more databases with different types of
DBMS software and schema at different sites

Features of distributed database systems


1. It’s a collection of logically related shared data
2. Data is split into a number of fragments or partitions.
3. Fragments may be duplicated
4. Fragments/Duplicates are allocated to different sites-Data available at site A might be
available at site B
5. The sites are linked by a communications network.
6. The data at each site is controlled by the local DBMS.

The DBMS Distributed Data Storage


Consider a database table named t that is to be stored in the database. There are two approaches to
storing this table in the distributed database:

- Replication: The system maintains several identical replicas (copies) of the table, and stores
each replica at a different site. The alternation to replication is to store only one copy of
relation t.

- Fragmentation. The system partitions the table into several fragments, and stores each
fragment at a different site.

The two can be combined. A table can be partitioned into several fragments and there may be
several replicas of each fragment.

Data Replication
If table t replicated, a copy of the table is stored in two or more sites. In the most extreme case, we
have full replication, in which a copy is stored in every site in the system.

Advantages of replication
- Availability: If one of the sites containing table t fails, then the table can be found in another
site. Thus the system can continue to process queries involving the table t, despite the failure
of one site.

- Increased parallelism: Several sites can process queries involving the table t in parallel. The
more replicas of r there are, the greater the chance that the needed data will be found in the
site where the transaction is executing. Hence data replication minimizes movement of data
between sites.

Disadvantages of replication
- Increased overhead on update: The system must ensure that all replicas of a table t, are
consistent, otherwise erroneous computations may result. Thus whenever t is updated, the
update must be communicated to all sites containing replicas. The result is increased
overhead. For example, in a banking system, where account information is replicated in
various sites; it is necessary to ensure that the balance in a particular account agrees in all
sites.

Data Fragmentation
If table t is fragmented, t is divided into a number of fragments t1, t2, t3,…., tn. These fragments
contain sufficient information to allow reconstruction of the original t.

Goals of fragmentation
Fragmentation aims to improve:
- Reliability
- Performance
- Balanced storage capacity and costs
- Communication costs
- Security

Types of fragmentation
There are two different schemes for fragmenting a table: Horizontal fragmentation and Vertical
fragmentation.

Horizontal fragmentation
- It splits the table by assigning each row to one or more fragments.

Figure 21: Horizontal database fragmentation

Vertical Fragmentation
- Splits the table by assigning each column to one or more fragments.

Figure 22:Vertical database fragmentation

A hybrid (Mixed fragmentation) of horizontal and vertical fragmentation is also possible

Figure 23:Hybrid database fragmentation

Example
Horizontal fragmentation of PROJ table
 PROJ1: projects with budgets less than 200, 000
 PROJ2: projects with budgets greater than or equal to 200, 000

Vertical fragmentation of PROJ table


 PROJ1: information about project budgets
 PROJ2: information about project names and locations
HOMOGENEOUS DDBMS versus HETEROGENEOUS DDBMS
DDBMS might be implemented as homogeneous or heterogeneous DDBMS

Homogeneous DDBMS
- All sites use same DBMS product
- All sites are aware of one another and agree to co-operate in processing
- Sites surrender a portion of their autonomy in terms of their right to change schemas or
DBMS software.
- The DBMS software must also co-operate with other sites in exchanging information about
transactions to make transaction processing across multiple sites.
- It is much easier to design and manage
- The approach provides incremental growth and allows increased performance

Heterogeneous DDBMS
- Sites may run different DBMS products, with possibly different underlying data models
- Sites may not be aware of one another and may provide only limited facilities for co-
operation in transaction processing. This occurs when sites have implemented their own
databases first, and integration is considered later.
- Translations are required to allow for different hardware and/or different DBMS products
- Differences in schema often a major problem for query processing, while divergence in
software becomes a hindrance for processing transactions that access multiple sites.

Reasons for building distributed databases


- Sharing Data
 Users at one site may be able to access the data residing at other sites.
 For example in a distributed banking system, where each branch stores data
relevant t that branch, it is possible for a user in one branch to access the data
in another branch.

- Autonomy
 Each site is able to maintain a degree of control over data that are stored
locally.
- Availability
 If one site fails in a distributed system, the remaining sites may be able to
continue operating. This is because data items are replicated in several sites.

Applications of Distributed Databases


- Manufacturing, especially multi-plant manufacturing
- Military command and control
- Airlines
- Hotel chains
- Banking
- Any organization which has a decentralized organization structure

Advantages of distributed databases

(i) Higher reliability


o Replication of components
o No single points of failure
e.g., a broken communication link or processing element does not bring down the
entire system
o Distributed transaction processing guarantees the consistency of the database and
concurrency

(ii) Improved performance


o Proximity of data to its points of use
– Reduces remote access delays
– Requires some support for fragmentation and replication

o Parallelism in execution

(iii ) Easier system expansion


o Database upgrade is easier

(iv) Transparency of distributed and replicated data

Transparency: Hiding database implementation details from the users such as where the data is
physically located or how the data is accessed at the specific local site.

Challenges of Distributed Databases


 Complexity – A distributed DBMS is more complex to build than a centralized DBMS.
 Cost – Procurement and maintenance costs for a Distributed DBMS are higher than those for
a centralized DBMS. Furthermore, a distributed DBMS requires additional hardware to
establish a network between sites.
 Security – In a centralized system, access to the data can be easily controlled. However, in a
distributed DBMS not only does access to replicated data have to be controlled in multiple
locations, but the network itself has to be made secure.
 Integrity control more difficult because of the large amount of data required to define
constraints.
 Distribution of control: Distribution creates problems of synchronization and coordination.
 Distributed database design: Various alternatives of achieving distribution (replication or
fragmentation must be weighed).
7. At each site has its own right, i.e. it can handle local applications independently.
8. Each DBMS in a distributed system participates in at least one global application

Data Warehousing
This is a system of pulling together data from many different sources within an
organization for reporting and analysis to make viable decisions
Features of data warehouse
1. Subject oriented-It provides information around a subject rather than the
organization's ongoing operations e.g. product, customers, suppliers, sales, revenue
2. Integrated –Data from different heterogeneous sources is stored in a common and
universally accepted manner
3. Time-Variant –Data collected in a data warehouse is identified with a particular time
period e. g. Weekly, Monthly, Annually
4. Non-volatile-Data residing in a data warehouse is permanent, meaning that data is not
erased or deleted when new data is inserted.

Spatial Databases/Geographical database-A database used to store geographical


information e.g. Raster data such as satellite imagery represented in matrix form or vector
data represented as discrete points, lines and polygons and support efficient storage,
indexing and querying of spatial data.

Application of spatial databases


● Geographic Information Systems (GIS) to store geospatial information.
● Computer-Aided Design/Manufacturing to store spatial objects such as surface of
airplane fuselage
● Multimedia Databases where the images, video, text, etc. stored and retrieved by
content

Multimedia databases - A database that stores data in the form of text, images, videos,
audio animations.

Features of Multimedia Databases


1. Media data – Refers to the actual data representing an object.
2. Media format data – Information such as sampling rate, resolution, encoding
scheme etc. about the format of the media data after it goes through the
acquisition, processing and encoding phase.
3. Media keyword data – Keywords description relating to the generation of data.
It is also known as content descriptive data. Example: date, time and place of
recording.
4. Media feature data – Content dependent data such as the distribution of colors,
kinds of texture and different shapes present in data
Mobile and personal databases
A mobile database is a database that is transportable, portable, and can be physically
separated or detached from the main database server but has the capability to communicate
with those servers from remote sites allowing the sharing of various kinds of data.

Design and implementation of data warehouses.


Steps towards implementing a data warehouse.
1. Requirements analysis and capacity planning: It involves defining enterprise
needs, defining architectures, carrying out capacity planning, and selecting the
hardware and software tools.
2. Hardware integration: Once the hardware and software has been selected, they
require to be put by integrating the servers, the storage methods, and the user software
tools.
3. Modelling -It involves designing the warehouse schema and views.
4. Physical Modelling-. This contains designing the physical data warehouse
organization, data placement, data partitioning, deciding on access techniques, and
indexing.
5. Sources: This step contains identifying and connecting the sources for the
information for a data warehouse using the gateway, ODBC drives, or another
wrapper.
6. Extract, Transform, Load(ETL):It's the process by which data is extracted from
data sources and moved to a central host
7. Populate the data warehouses:
8. User applications: For the data warehouses to be helpful, there must be end-user
applications. This step contains designing and implementing applications required by
the end-users to access the database
9. Roll-out the warehouses and applications: Once the data warehouse has been
populated and the end-client applications tested, the warehouse system and the
operations may be rolled out for the user's community to use.
Two types of database processing in a data warehouse
OLTP - On-line transaction processing.
- It is a class of program that facilitates and manages transaction-oriented applications.
- It is used for supporting daily busyness.

OLAP - On-line analytical processing


- It is a way of viewing data in a multidimensional format.
- It is used for supporting decision making.
Data Warehouse Design – Schemas
Schema is a logical description of the entire database. It includes the name and description
of records of all record types including all associated data-items and aggregates. Much like a
database, a data warehouse also requires to maintain a schema. A database uses relational
model, while a data warehouse uses Star, Snowflake, and Fact Constellation schema. In this
chapter, we will discuss the schemas used in a data warehouse.

Star Schema
 Each dimension in a star schema is represented with only one-dimension table.
 This dimension table contains the set of attributes.
 The following diagram shows the sales data of a company with respect to the four
dimensions, namely time, item, branch, and location.
Figure 24:Star Schema

 There is a fact table at the center. It contains the keys to each of four dimensions.
 The fact table also contains the attributes, namely dollars sold and units sold.

Note − each dimension has only one dimension table and each table holds a set of attributes.
For example, the location dimension table contains the attribute set {location_key, street,
city, province_or_state,country}. This constraint may cause data redundancy. For example,
"Vancouver" and "Victoria" both the cities are in the Canadian province of British
Columbia. The entries for such cities may cause data redundancy along the attributes
province_or_state and country.

Snowflake Schema
 Some dimension tables in the Snowflake schema are normalized.
 The normalization splits up the data into additional tables.
 Unlike Star schema, the dimensions table in a snowflake schema are normalized. For
example, the item dimension table in star schema is normalized and split into two
dimension tables, namely item and supplier table.
Figure 25:Snowflake Schema

 Now the item dimension table contains the attributes item_key, item_name, type,
brand, and supplier-key.
 The supplier key is linked to the supplier dimension table. The supplier dimension
table contains the attributes supplier_key and supplier_type.
Note − Due to normalization in the Snowflake schema, the redundancy is reduced and
therefore, it becomes easy to maintain and the save storage space.

Fact Constellation Schema


 A fact constellation has multiple fact tables. It is also known as galaxy schema.
 The following diagram shows two fact tables, namely sales and shipping.

Figure 26:Fact Constellation Schema

 The sales fact table is same as that in the star schema.


 The shipping fact table has the five dimensions, namely item_key, time_key,
shipper_key, from_location, to_location.
 The shipping fact table also contains two measures, namely dollars sold and units
sold.
 It is also possible to share dimension tables between fact tables. For example, time,
item, and location dimension tables are shared between the sales and shipping fact
table.

Schema Definition
Multidimensional schema is defined using Data Mining Query Language (DMQL). The two
primitives, cube definition and dimension definition, can be used for defining the data
warehouses and data marts.

Syntax for Cube Definition


define cube < cube_name > [ < dimension-list > }: < measure_list >

Syntax for Dimension Definition


define dimension < dimension_name > as ( < attribute_or_dimension_list > )

Star Schema Definition


The star schema that we have discussed can be defined using Data Mining Query Language
(DMQL) as follows −

define cube sales star [time, item, branch, location]:

dollars sold = sum(sales in dollars), units sold = count(*)

define dimension time as (time key, day, day of week, month,


quarter, year)
define dimension item as (item key, item name, brand, type, supplier
type)
define dimension branch as (branch key, branch name, branch type)

define dimension location as (location key, street, city, province


or state, country)

Snowflake Schema Definition


Snowflake schema can be defined using DMQL as follows −
define cube sales snowflake [time, item, branch, location]:
dollars sold = sum(sales in dollars), units sold = count(*)

define dimension time as (time key, day, day of week, month,


quarter, year)
define dimension item as (item key, item name, brand, type, supplier
(supplier key, supplier type))
define dimension branch as (branch key, branch name, branch type)
define dimension location as (location key, street, city (city key,
city, province or state, country))

Fact Constellation Schema Definition


Fact constellation schema can be defined using DMQL as follows −
define cube sales [time, item, branch, location]:

dollars sold = sum(sales in dollars), units sold = count(*)

define dimension time as (time key, day, day of week, month,


quarter, year)
define dimension item as (item key, item name, brand, type, supplier
type)
define dimension branch as (branch key, branch name, branch type)
define dimension location as (location key, street, city, province
or state,country)
define cube shipping [time, item, shipper, from location, to
location]:

dollars cost = sum(cost in dollars), units shipped = count(*)

define dimension time as time in cube sales


define dimension item as item in cube sales
define dimension shipper as (shipper key, shipper name, location as
location in cube sales, shipper type)
define dimension from location as location in cube sales
define dimension to location as location in cube sales

Conclusion
At the end of this learning outcome, the learner should be able to explain decision support
systems, data mining, mobility and personal databases, spatial and geographical databases,
demonstrate distributed databases, illustrate multi-media databases, illustrate, design and
implement a data warehouse.

Further Reading
[Link]

[Link] Self assessment


Written Assessment
1 Which of the following is an autonomous homogenous environment ?
A .The same DBMS is at each node and each DBMS works independently.
B. The same DBMS is at each node and a central DBMS coordinates database
access.
C. A different DBMS is at each node and each DBMS works independently.
D. A different DBMS is at each node and a central DBMS coordinates database
access.

2. Which of the following is storing a separate copy of the database at multiple locations?
A. Data Replication
B. Horizontal Partitioning
C. Vertical Partitioning
D. Horizontal and Vertical Partitioning

3. What is a distributed database?


A. A single logical database that is spread to multiple locations and is interconnected
by a network
B. A loose collection of file that is spread to multiple locations and is interconnected
by a network
C. A single logical database that is limited to one location.
D. A loose collection of file that is limited to one location.

4. What are Decision support systems (DSS)?


A. A family of relational database management systems marketed by IBM
B. Interactive systems that enable decision makers to use databases and models on a
computer in order to solve ill-structured problems
C. It consists of nodes and branches starting from a single root node. Each node
represents a test, or decision
D. None of these
5. What is Data Mining?
A. Data Mining is set to be a process of analyzing the data in different dimensions
or perspectives and summarizing into a useful information.
B. Is the subset of organization-wide data. This subset of data is valuable to
specific groups of an organization.
C. A data that contains data specific to a particular group.
D. None of the above
5. Which are the key features of a data warehouse?
6. Where are multimedia databases used in real life?
7. Outline the key reasons that organisations are increasingly implementing data
warehouse

Oral Assessment
1. What is the difference between OLAP and OLTP?
2. Using the example of any business you are familiar with, outline how a data warehous
e might be used to identify trends that can help a business be competitive.

Practical Assessment
Consider a bank system with at least the following tables: Customer, branch, Withdrawals,
Deposits. Demonstrate how you can build a star schema for the bank system.

[Link] Tools, Equipment, Supplies and Materials


 MS SQL server software
 Computers
 Instruction material
 Stationery
 Internet

[Link] References

1. [Link]
2. [Link]

You might also like