BSCIT 201 Database Management System
BSCIT 201 Database Management System
Course Writer
Content Editor
ISBN:
Printed and published by: Dr. Babasaheb Ambedkar Open University, Ahmedabad While all efforts
have been made by editors to check accuracy of the content, the representation of facts, principles,
descriptions and methods are that of the respective module writers. Views expressed in the publication
are that of the authors, and do not necessarily reflect the views of Dr. Babasaheb Ambedkar Open
University. All products and services mentioned are owned by their respective copyrights holders, and
mere presentation in the publication does not mean endorsement by Dr. Babasaheb Ambedkar Open
University. Every effort has been made to acknowledge and attribute all sources of information used in
preparation of this learning material. Readers are requested to kindly notify missing attribution, if any.
Dr. Babasaheb BSCIT-201
Ambedkar Open
University
Page | 1
UNIT-4
RELATIONAL DESIGN AND REDUNDANCY 75
BLOCK-3: FUNCTIONAL DEPENDENCIES AND
NORMALIZATION
UNIT-1
FUNCTIONAL DEPENDENCIES 84
UNIT-2
INTRODUCTION TO DATA NORMALIZATION 93
BLOCK-4: SQL-STATEMENTS
UNIT-1
INTRODUTION TO SQL 106
UNIT-2
SQL-DATA MANIPULATION LANAGUAGE 123
UNIT-3
SQL- JOIN STATEMENT 149
UNIT-4
DATABASE DEVELOPMENT PROCESS 157
Page | 2
BLOCK – 1
INTRODUCTION TO DATABASE AND
DATAMODEL
Page | 3
Unit 1: Introduction to
Database systems
1
Unit Structure
Page | 4
1.1 LEARNING OBJECTIVES
Introduction to database
Database Management System
Characteristics And Benefits Of A Database
What Is a Database?
DATABASE PROPERTIES
Page | 5
• A database is logical, coherent and internally consistent.
• A database is designed, built and populated with data for a specific purpose.
A database can contain many tables. For example, a membership system may contain
an address table and an individual member table as shown in Figure 2.2. Members of
Science World are individuals, group homes, businesses and corporations who have an
active membership to Science World. Memberships can be purchased for a one- or two-
year period, and then renewed for another one- or two-year period.
In Figure, Minnie Mouse renewed the family membership with Science World. Everyone
with membership
ID#100755 lives at 8932 Rodent Lane. The individual members are Mickey Mouse,
Minnie Mouse, Mighty Mouse, Door Mouse, Tom Mouse, King Rat, Man Mouse and
Moose Mous
Page | 6
1.2 DATABASE MANAGEMENT SYSTEM
With the database approach, we can have the traditional banking system as shown
in Figure. In this bank example, a DBMS is used by the Personnel Department, the
Account Department and the Loan Department to access the shared corporate
database.
Managing information means taking care of it so that it works for us and is useful for
the tasks we perform. By using a DBMS, the information we collect and add to its
database is no longer subject to accidental disorganization. It becomes more
accessible and integrated with the rest of our work. Managing information using a
database allows us to become strategic users of the data we have.
We often need to access and re-sort data for various uses. These may include:
Page | 7
Writing management reports
Sort
Match
Link
Aggregate
Skip fields
Calculate
Arrange
Because of the versatility of databases, we find them powering all sorts of projects. A
database can be linked to:
There are a number of characteristics that distinguish the database approach from
the file-based system or approach. This chapter describes the benefits (and
features) of the database system.
Page | 8
A database system is referred to as self-describing because it not only contains the
database itself, but also metadata which defines and describes the data and
relationships between tables in the database. This information is used by the DBMS
software or database users if needed. This separation of data and information about
the data makes a database system totally different from the traditional file-based
system in which the data definition is part of the application programs.
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.
Current database systems are designed for multiple users. That is, they allow many
users to access the same database at the same time. This access is achieved
through features called concurrency control strategies. These strategies ensure that
the data accessed are always correct and that data integrity is maintained.
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
Page | 9
performance, but such redundancy is controlled by application programming and
kept to minimum by introducing as little redudancy 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.
Database management systems must provide the ability to define and enforce
certain constraints to ensure that users enter valid information and maintain data
integrity. A database constraint is a restriction or rule that dictates what can be
entered or edited in a table such as a postal code using a certain format or adding a
valid city in the City field.
There are many types of database constraints. Data type, for example, determines
the sort of data permitted in a field, for example numbers only. Data uniqueness
such as the primary key ensures that no duplicates are entered. Constraints can be
simple (field based) or complex (programming).
Not all users of a database system will have the same accessing privileges. For
example, one user might have read- only access (i.e., the ability to read a file but not
make changes), while another might have read and write privileges, which is the
ability to both read and modify a file. For this reason, a database management
system should provide a security subsystem to create and control different types of
user accounts and restrict unauthorized access.
Data independence
Page | 10
(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.
Transaction processing
By its very nature, a DBMS permits many users to have access to its database either
individually or simultaneously. It is not important for users to be aware of how and
where the data they access is stored
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.
If a computer system fails in the middle of a complex update process, the recovery
subsystem is responsible for making sure that the database is restored to its original
state. These are two more benefits of a database management system.
In this chapter, we have studied the what is database and database properties. We
also studied about how to work database management system. Finally, we ended
the discussion with the Characteristics and Benefits of a Database.
1.5 GLOSSARY
Page | 11
database management system (DBMS): a collection of programs that enables
users to create and maintain databases and control all access to them
concurrencycontrolstrategies:featuresofadatabasethatallowseveralusersaccesstot
hesamedataitematthesametime
datatype:determinesthesortofdatapermittedinafield,forexamplenumbersonly
datauniqueness:ensuresthatnoduplicatesareentered
databaseconstraint:arestrictionthatdetermineswhatisallowedtobeenteredoreditedina
table
metadata:definesanddescribesthedataandrelationshipsbetweentablesinthedatabase
readandwriteprivileges: theabilitytobothreadandmodifyafile
read-onlyaccess:theabilitytoreadafilebutnotmakechanges
view:asubsetofthedatabase
Page | 12
CHECK YOUR PROGRESS: POSSIBLE ANSWER
1. B-Data
2. C-Database management system
3. C- All of these
4. B- All of these
5. C- Charles Bachman
Website:
https://www.sanfoundry.com/database-mcqs
1.7ASSIGNMENT
Page | 13
Unit 2: Database History 2
Unit Structure
2.6. Glossary
2.9. Assignment
Page | 14
2.1 LEARNING OBJECTIVES
File-based system
Classification based database system
Disadvantages of the file-based approach
Roles of databases in business
The way in which computers manage data has come a long way over the last few
decades. Today’s users take for granted the many benefits found in a database
system. However, it wasn’t that long ago that computers relied on a much less
elegant and costly approach to data management called the file-based system.
File-based System
Consider a traditional banking system that uses the file-based system to manage the
organization’s data shown in Figure 2.1. As we can see, there are different
departments in the bank. Each has its own applications that manage and manipulate
different data files. For banking systems, the programs may be used to debit or credit
an account, find the balance of an account, add a new mortgage loan and generate
monthly statements.
Page | 15
Figure 2.1. Example of a file-based system used by banks to manage data.
There are four main distribution systems for database systems and these, in turn,
can be used to classify the DBMS.
Centralized systems
With a centralized database system, the DBMS and database are stored at a single
site that is used by several othersystems too. This is illustrated in Figure 2.2.
In the early 1980s, many Canadian libraries used the GEAC 8000 to convert their
manual card catalogues tomachine-readable centralized catalogue systems. Each
book catalogue had a barcode field similar to those onsupermarket products.
In a distributed database system, the actual database and the DBMS software are
distributed from various sites thatare connected by a computer network, as shown in
Figure 2.3.
Page | 16
Figure 2.3. Example of a distributed database system.
Homogeneous distributed database systems use the same DBMS software from
multiple sites. Data exchangebetween these various sites can be handled easily. For
example, library information systems by the same vendor,such as Geac Computer
Corporation, use the same DBMS software which allows easy data exchange
between thevarious Geac library sites.
Data redundancy
Page | 17
data redundancy, a situation that occurs in a database when a field needs to be
updated in more than one table. This practice can lead to several problems such as:
• Data inconsistency, a situation where various copies of the same data are
conflicting, wastes storage space and duplicates effort
Data isolation
Data isolation is a property that determines when and how changes made by one
operation become visible to other concurrent users and systems. This issue occurs
in a concurrency situation. This is a problem because:
Integrity problems
• Data values must satisfy certain consistency constraints that are specified in
the application programs.
Security problems
Concurrency access
Page | 18
Concurrency is the ability of the database to allow multiple users access to the same
record without adversely affecting transaction processing. A file-based system must
manage, or prevent, concurrency by the application programs. Typically, in a file-
based system, when an application opens a file, that file is locked. This means that
no one else has access to the file at the same time.
Database Approach
The difficulties that arise from using the file-based system have prompted the
development of a new approach in managing large amounts of organizational
information called the database approach.
Databases and database technology play an important role in most areas where
computers are used, including business, education and medicine. To understand the
fundamentals of database systems, we will start by introducing some basic concepts
in this area.
Everybody uses a database in some way, even if it is just to store information about
their friends and family. That data might be written down or stored in a computer by
using a word-processing program or it could be saved in a spreadsheet. However,
the best way to store data is by using database management software. This is a
powerful software tool that allows you to store, manipulate and retrieve data in a
variety of different ways.
Data are factual information such as measurements or statistics about objects and
concepts. We use data for discussions or as part of a calculation. Data can be a
Page | 19
person, a place, an event, an action or any one of a number of things. A single fact is
an element of data, or a data element.
If data are information and information is what we are in the business of working
with, you can start to see where you might be storing it. Data can be stored in:
• Filing cabinets
• Spreadsheets
• Folders
• Ledgers
• Lists
All of these items store information, and so too does a database. Because of the
mechanical nature of databases, they have terrific power to manage and process the
information they hold. This can make the information they house much more useful
for your work.
With this understanding of data, we can start to see how a tool with the capacity to
store a collection of data and organize it, conduct a rapid search, retrieve and
process, might make a difference to how we can use data. This book and the
chapters that follow are all about managing information.
In this chapter, we have studied the database historyin file based system how it
works and classification based database system. We also studied disadvantages of
file based system approach. Finally,we ended the discussion with role of databases
in business.
2.6 GLOSSARY
Page | 20
database management software: a powerful software tool that allows you to store,
manipulate and retrieve data in a variety of ways
centralized database system: the DBMS and database are stored at a single site
that is used by several othersystems too
distributed database system: the actual database and the DBMS software are
distributed from various sitesthat are connected by a computer network
Page | 21
system?
A Image B Text
C Table D Graph
5 Which of the following is not a type of database?
A Hierarchical B Network
C Distributed D Decentralized
1. D-Data record
2. D—all of these
3. D-- All options are correct
4. C—Table
5. D- Decentralized
Website:
https://www.sanfoundry.com/database-mcqs
2.9 ASSIGNMENT
Page | 22
Unit 3: Data Modelling
3
Unit Structure
Page | 23
3.1 LEARNING OBJECTIVES
Degrees of abstraction
Data abstraction Layer
Schemas
3.2 Introduction
Data modellingis the first step in the process of database design. This step is
sometimes considered to be a high- level and abstract design phase, also referred to
as conceptual design. The aim of this phase is to describe:
• The constraints on data (e.g., student number has exactly eight digits; a
subject has four or six units of credit only)
In the second step, the data items, the relationships and the constraints are all
expressed using the concepts provided by the high-level data model. Because these
concepts do not include the implementation details, the result of the data modelling
process is a (semi) formal representation of the database structure. This result is
quite easy to understand so it is used as reference to make sure that all the user’s
requirements are met.
The third step is database design. During this step, we might have two sub-steps:
one called database logical design, which defines a database in a data model of a
specific DBMS, and another called database physical design, which defines the
internal database storage structure, file organization or indexing techniques. These
Page | 24
two sub-steps are database implementation and operations/user interfaces building
steps.
In the database design phases, data are represented using a certain data model.
The data model is a collection of concepts or notations for describing data, data
relationships, data semantics and data constraints. Most data models also include a
set of basic operations for manipulating data in the database.
In this section we will look at the database design process in terms of specificity. Just
as any design starts at ahigh level and proceeds to an ever-increasing level of detail,
so does database design. For example, when buildinga home, you start with how
many bedrooms and bathrooms the home will have, whether it will be on one level
ormultiple levels, etc. The next step is to get an architect to design the home from a
more structured perspective. Thislevel gets more detailed with respect to actual
room sizes, how the home will be wired, where the plumbing fixtureswill be placed,
etc. The last step is to hire a contractor to build the home. That’s looking at the
The database design is very much like that. It starts with users identifying the
business rules; then the databasedesigners and analysts create the database
design; and then the database administrator implements the design usinga DBMS.
External models
Conceptual models
Page | 25
• Contain data stored in the database
• Show relationships among data including:
Constraints
Semantic information (e.g., business rules)
Security and integrity information
• Consider a database as a collection of entities (objects) of various kinds
• Are the basis for identification and high-level description of main data objects;
they avoid details
• Are database independent regardless of the database you will be using
Internal models
The three best-known models of this kind are the relational data model, the network
data model and the hierarchicaldata model. These internal models:
Physical models
• Provide concepts that describe the details of how data are stored in the
computer’s memory
Page | 26
3.4 Data Abstraction Layer
In a pictorial view, you can see how the different models work together. Let’s look at
this from the highest level, the external model.
The external model is the end user’s view of the data. Typically a database is an
enterprise system that serves the needs of multiple departments. However, one
department is not interested in seeing other departments’ data (e.g., the human
resources (HR) department does not care to view the sales department’s data).
Therefore, one user view will differ from another.
The external model requires that the designer subdivide a set of requirements and
constraints into functional modules that can be examined within the framework of
their external models (e.g., human resources versus sales).
As a data designer, you need to understand all the data so that you can build an
enterprise-wide database. Based on the needs of various departments, the
conceptual model is the first model created.
At this stage, the conceptual model is independent of both software and hardware. It
does not depend on the DBMS software used to implement the model. It does not
depend on the hardware used in the implementation of the model. Changes in either
hardware or DBMS software have no effect on the database design at the
conceptual level.
Once a DBMS is selected, you can then implement it. This is the internal model.
Here you create all the tables, constraints, keys, rules, etc. This is often referred to
as the logical design.
Page | 27
Figure 3.1: Data abstraction layers.
3.5 Schemas
A schemais an overall description of a database, and it is usually represented by the
entity relationship diagram(ERD). There are many subschemas that represent
external models and thus display external views of the data.
Below is a list of items to consider during the design process of a database.
• Conceptual schema: there is only one. This schema includes data items,
Page | 28
In other words, changes to the logical schema (e.g., alterations to the structure of the
database like adding a columnor other tables) should not affect the function of the
application (external views).
3.7 GLOSSARY
Page | 29
external model: represents the user’s view of the database
external schema: user view
internal model: a representation of the database as seen by the DBMS
logical data independence: the ability to change the logical schema without
changing the external schema
logical design: where you create all the tables, constraints, keys, rules, etc.
logical schema: a conceptual design of the database done on paper or a
whiteboard, much like architectural drawings for a house
operating system (OS): manages the physical level of the physical model
physical data independence: the immunity of the internal model to changes in the
physical model
physical model: the physical representation of the database
schema: an overall description of a database
1. D- critical level
2. D-critical schema
Page | 30
3. B- database schema
4. B- physical level
5. True
Website:
https://www.sanfoundry.com/database-mcqs
3.10 ASSIGNMENT
Page | 31
Unit 4: Data Models
4
Unit Structure
Page | 32
4.1 LEARNING OBJECTIVES
• The relational model represents data asrelations, or tables. For example, in the
membership system atScience World, each membership has many members (see
Figure 1.2 in Chapter 1). The membershipidentifier, expiry date and address
information are fields in the membership. The members are individualssuch as
Mickey, Minnie, Mighty, Door, Tom, King, Man and Moose. Each record is said to be
aninstance of the membership table.
Page | 33
• The network model represents data as record types. This model also represents a
limited type of one tomany relationships called a set type, as shown in Figure 4.1.
branch of the hierarchyrepresents a number of related records. Figure 4.2 shows this
schema in hierarchical model notation.
In this chapter, we have studied the high level conceptual data and record –based
logical data. Finally we ended the discussion data models summarize the models of
record-based logical data type are relational data models, network data models and
hierarchical data models.
4.5 GLOSSARY
Page | 34
network model: represents data as record types
relation: another term for table
relational model: represents data as relations or tables
set type: a limited type of one to many relationship
1. A -Network model
2. B- conceptual based data hiding
3. A- relationship
4. D- All of these
Website:
https://www.sanfoundry.com/database-mcqs
4.7 ASSIGNMENT
Page | 35
2) What is a high-level conceptual data model?
3) What is an entity? An attribute? A relationship?
4) List and briefly describe the common record-based logical data models.
BLOCK – 2
ENTITY-RELATIONSHIP MODEL
Page | 36
Unit 1: Relational Data Model
1
Unit Structure
Page | 37
1.1 LEARNING OBJECTIVES
The relational data model was introduced by C. F. Codd in 1970. Currently, it is the
most widely used data model.
The relational model has provided the basis for:
(SQL)
Relation
A relation, also known as a table or file, is a subset of the Cartesian product of a list
of domains characterized by aname. And within a table, each row represents a group
of related data values. A row, or record, is also known as atuple. The column in a
Page | 38
table is a field and is also referred to as an attribute. You can also think of it this way:
anattribute is used to define the record and a record contains a set of attributes.
The steps below outline the logic between a relation and its domains.
1. Given n domains are denoted by D1, D2, ….. Dn
2. And r is a relation defined on these domains
3. Then r? D1×D2×…×Dn
Table
A database is composed of multiple tables and each table holds the data. Figure
shows a database that containsthree tables.
Column
A database stores pieces of information or facts in an organized way. Understanding
how to use and get the mostout of databases requires us to understand that method
of organization.
The principal storage units are called columns or fields or attributes. These house
the basic components of data intowhich your content can be broken down. When
deciding which fields to create, you need to think generically aboutyour information,
for example, drawing out the common components of the information that you will
store in the database and avoiding the specifics that distinguish one item from
another.
Look at the example of an ID card in Figure to see the relationship between fields
and their data.
Page | 39
Figure.1.2 Example of an ID card by A. Watt.
Domain
A domain is the original sets of atomic values used to model data. By atomic value,
we mean that each value in thedomain is indivisible as far as the relational model is
concerned. For example:
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
• 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 variousproperties and the data type for the column. We will
discuss data types in another chapter.
Records
Just as the content of any one document or item needs to be broken down into its
constituent bits of data for storagein the fields, the link between them also needs to
be available so that they can be reconstituted into their whole form.Records allow us
to do this. Records contain fields that are related, such as a customer or an
employee. As notedearlier, a tuple is another term used for record.
Records and fields form the basis of all databases. A simple table gives us the
clearest picture of how records andfields work together in a database storage
project.
Page | 40
Figure1.3. Example of a simple table by A. Watt.
The simple table example in Figure 7.3 shows us how fields can hold a range of
different sorts of data. This one has:
• An Author field: this is displayed as Initial. Surname; its data type is text.
You can command the database to sift through its data and organize it in a particular
way. For example, you canrequest that a selection of records be limited by date: 1.
all before a given date, 2. all after a given date or 3. Allbetween two given dates.
Similarly, you can choose to have records sorted by date. Because the field, or
record,containing the data is set up as a Date field, the database reads the
information in the Date field not just as numbersseparated by slashes, but rather, as
dates that must be ordered according to a calendar system.
Degree
The degree is the number of attributes in a table. In our example in Figure 7.3, the
degree is 4.
Properties of a Table
• A table has a name that is distinct from all other tables in the database.
• There are no duplicate rows; each row is distinct.
• Entries in columns are atomic. The table does not contain repeating groups or
multivalued attributes.
• Entries from columns are from the same domain based on their data type
including:
Page | 41
o character (string)
o date
o logical (true or false)
• Operations combining different data types are disallowed.
• Each attribute has a distinct name.
• The sequence of columns is insignificant.
• The sequence of rows is insignificant.
1.4LET US SUM UP
In this chapter, we have studied concepts of relational data model. In relational data
model discuss the topic Relation,Table,Column,Domain,Records ,Degree,etc.
1 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
2 The term attribute refers to a ___________ of a table.
A Record B Column
C Tuple D Key
3 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
4 A relational database consists of a collection of
A Tables B Records
C Fields D Keys
5 A domain is atomic if elements of the domain are considered to be ____________
units.
A Different B Indivisible
C Constant D divisible
1. A -entity set
2. B- column
Page | 42
3. A- domain
4. A- tables
5. B-indivisible
1.5 GLOSSARY
atomic value: each value in the domain is indivisible as far as the relational model is
concerned
attribute: principle storage unit in a database
column: see attribute
degree: number of attributes in a table
domain: the original sets of atomic values used to model data; a set of acceptable
values that a column isallowed to contain
field: see attribute
file: see relation
record: contains fields that are related; see tuple
relation: a subset of the Cartesian product of a list of domains characterized by a
name; the technical termfor table or file
row: see tuple
structured query language (SQL): the standard database access language
table: see relation
tuple: a technical term for row or record
Website:
https://www.sanfoundry.com/database-mcqs
1.6 ASSIGNMENT
Page | 43
1) Using correct terminology, identify and describe all the components in Table
1.
Table 1.
Unit Structure
Page | 44
2.1 LEARNING OBJECTIVES
ER model
Entity, Entity set and Entity Type
Attributes
Types of keys
Relationships
The Entity relationship (ER) data model has existed for over 35 years. It is well
suited to data modelling for usewith databases because it is fairly abstract and is
easy to discuss and explain. ER models are readily translated torelations. ER
models, also called an ER schema, are represented by ER diagrams.
Page | 45
For the rest of this chapter, we will use a sample database called the COMPANY
database to illustrate the conceptsof the ER model. This database contains
information about employees, departments and projects. Important pointsto note
include:
An entity is an object in the real world with an independent existence that can be
differentiated from other objects.An entity might be
Page | 46
• A table without a foreign key or a table that contains a foreign key that can
contain nulls is a strong entity
Another term to know is entity type which defines a collection of similar entities.
Existence dependency
existence-dependent if it has amandatory foreign key (i.e., a foreign key attribute that
cannot be null). For example, in the COMPANY database,a Spouse entity is
existence -dependent on the Employee entity.
Kinds of Entities
You should also be familiar with different kinds of entities including independent
entities, dependent entities andcharacteristic entities. These are described below.
Independent entities
Independent entities, also referred to as kernels, are the backbone of the database.
They are what other tables arebased on. Kernels have the following characteristics:
Page | 47
Dependent entities
Dependent entities, also referred to as derived entities, depend on other tables for
their meaning. These entities havethe following characteristics:
Characteristic entities provide more information about another table. These entities
have the followingcharacteristics:
2.4 ATTRIBUTES
Page | 48
Each entity is described by a set of attributes (e.g., Employee = (Name, Address,
Birthdate (Age), Salary).
Each attribute has a name, and is associated with an entity and a domain of legal
values. However, the informationabout attribute domain is not presented on the
ERD.In the entity relationship diagram, shown in Figure 2.2, each attribute is
represented by an oval with a name inside.
Types of Attributes
There are a few types of attributes you need to be familiar with. Some of these are to
be left as is, but some need tobe adjusted to facilitate representation in the relational
model. This first section will discuss the types of attributes.Later on we will discuss
fixing the attributes to fit correctly into the relational model.
Simple attributes
Simple attributes are those drawn from the atomic value domains; they are also
called single-valued attributes. Inthe COMPANY database, an example of this would
be: Name = {John} ; Age = {23}
Composite attributes
Composite attributes are those that consist of a hierarchy of attributes. Using our
database example, and shown inFigure 8.3, Address may consist of Number, Street
‘Kingsford’}
Page | 49
Figure 2.3. An example of composite attributes.
Multivalued attributes
Multivalued attributes are attributes that have a set of values for each entity. An
example of a multivalued attributefrom the COMPANY database, as seen in Figure
2.4, are the degrees of an employee: BSc, MIT, PhD.
Derived attributes
Derived attributes are attributes that contain values calculated from other attributes.
An example of this can beseen in Figure 2.5. Age can be derived from the attribute
Birthdate. In this situation, Birthdate is called a storedattribute, which is physically
saved to the database.
Page | 50
Figure 2.5. Example of a derived attribute.4
Keys
Types of Keys
Candidate key
A candidate key is a simple or composite key that is unique and minimal. It is unique
because no two rows in a tablemay have the same value at any time. It is minimal
because every column is necessary in order to attain uniqueness.
From our COMPANY database example, if the entity is Employee(EID, First Name,
Last Name, SIN, Address,Phone, BirthDate, Salary, DepartmentID), possible
candidate keys are:
EID, SIN
First Name and Last Name – assuming there is no one else in the company
Last Name and DepartmentID – assuming two people with the same last
Composite key
Using the example from the candidate key section, possible composite keys are:
First Name and Last Name – assuming there is no one else in the company
Page | 51
Last Name and Department ID – assuming two people with the same last
Primary key
The primary key is a candidate key that is selected by the database designer to be
used as an identifying mechanismfor the whole entity set. It must uniquely identify
tuples in a table and not be null. The primary key is indicated inthe ER model by
underlining the attribute.
for the whole entityset. This is referred to as the primary key. This key is indicated by
underlining the attribute in the ERmodel.
Employee(EID, First Name, Last Name, SIN, Address, Phone, BirthDate, Salary,
DepartmentID)
Secondary key
Alternate key
Alternate keys are all candidate keys not chosen as the primary key.
Foreign key
A foreign key (FK) is an attribute in a table that references the primary key in another
table OR it can be null. Bothforeign and primary keys must be of the same data type.
Page | 52
Employee(EID, First Name, Last Name, SIN, Address, Phone, BirthDate, Salary,
DepartmentID)
Nulls
A null is a special symbol, independent of data type, which means either unknown or
inapplicable. It does not meanzero or blank. Features of null include:
No data entry
Not permitted in the primary key
Should be avoided in other attributes
Can represent
o An unknown attribute value
o A known, but missing, attribute value
o A “not applicable” condition
Can create problems when functions such as COUNT, AVERAGE and SUM
are used
Can create logical problems when relational tables are linked
NOTE: The result of a comparison operation is null when either argument is null. The
result of an arithmetic operation is null when either argument is null (except functions
that ignore nulls).
Page | 53
• WHERE jobName = Sales AND
• (commission + salary) > 30,000 –> E10 and E12
This result does not include E13 because of the null value in the commission
column. To ensure that the row withthe null value is included, we need to look at the
individual fields. By adding commission and salary for employeeE13, the result will
be a null value. The solution is shown below.
• SELECT emp# FROM Salary_tbl
• WHERE jobName = Sales AND
• (commission > 30000 OR
• salary > 30000 OR
• (commission + salary) > 30,000 –>E10 and E12 and E13
2.6 RELATIONSHIPS
Relationships are the glue that holds the tables together. They are used to connect
related information betweentables.
Relationship strength is based on how the primary key of a related entity is defined.
A weak, or non-identifying,relationship exists if the primary key of the related entity
does not contain a primary key component of the parententity. Company database
examples include:
• Customer(CustID, CustName)
A strong, or identifying, relationship exists when the primary key of the related entity
contains the primary keycomponent of the parent entity. Examples include:
Types of Relationships
Page | 54
One to many (1:M) relationship
A one to many (1:M) relationship should be the norm in any relational database
design and is found in all relationaldatabase environments. For example, one
department has many employees. Figure 2.7 shows the relationship of oneof these
employees to the department.
A one to one (1:1) relationship is the relationship of one entity to only one other
entity, and vice versa. It should berare in any relational database design. In fact, it
could indicate that two entities actually belong in the same table.
An example from the COMPANY database is one employee is associated with one
spouse, and one spouse isassociated with one employee.
Page | 55
• It can avoid problems inherent in an M:N relationship by creating a composite
entity or bridge entity. For
• example, an employee can work on many projects OR a project can have
many employees working on it,depending on the business rules. Or, a student
can have many classes and a class can hold many students.
Figure 2.8 shows another another aspect of the M:N relationship where an
employee has different start dates for different projects. Therefore, we need a
JOIN table that contains the EID, Code and StartDate.
Figure 2.8. Example where employee has different start dates for different projects.
Page | 56
Figure 2.9. Example of a unary relationship.
For some entities in a unary relationship, a separate column can be created that
refers to the primary key of the sameentity set.
Ternary Relationships
Refer to Figure 2.10 for an example of mapping a ternary relationship type. Note n-
ary means multiple tables in arelationship. (Remember, N = many.)
• For each n-ary (> 2) relationship, create a new relation to represent the
relationship.
• The primary key of the new relation is a combination of the primary keys of
the participating entities thathold the N (many) side.
• In most cases of an n-ary relationship, all the participating entities hold a
many side.
Page | 57
2.7 LET US SUM UP
In this chapter, we have studied what is E-R model. We also studied about how to
work E-R model in Entity, Entityset and Entity type. We also studied the types of
attributes, Types of keys. Finally, we ended the discussion with the types of
relationships: one to one relationship, many to many relationships, unary relationship
and ternary relationship.
2.8 GLOSSARY
alternate key: all candidate keys not chosen as the primary key
candidate key: a simple or composite key that is unique (no two rows in a table may
have the same value)and minimal (every column is necessary)
characteristic entities: entities that provide more information about another table
composite attributes: attributes that consist of a hierarchy of attributes
composite key: composed of two or more attributes, but it must be minimal
dependent entities: these entities depend on other tables for their meaning
derived attributes: attributes that contain values calculated from other attributes
derived entities: see dependent entities
EID: employee identification (ID)
entity: a thing or object in the real world with an independent existence that can be
differentiated from otherobjects
entity relationship (ER) data model: also called an ER schema, are represented by
ER diagrams. Theseare well suited to data modelling for use with databases.
entity relationship schema: see entity relationship data model
entity set: a collection of entities of an entity type at a point of time
entity type: a collection of similar entities
foreign key (FK): an attribute in a table that references the primary key in another
table OR it can be null
independent entity: as the building blocks of a database, these entities are what
other tables are based on
kernel: see independent entity
key: an attribute or group of attributes whose values can be used to uniquely identify
an individual entity inan entity set
Page | 58
multivalued attributes: attributes that have a set of values for each entity
n-ary: multiple tables in a relationship
null: a special symbol, independent of data type, which means either unknown or
inapplicable; it does notmean zero or blank
recursive relationship: see unary relationship
relationships: the associations or interactions between entities; used to connect
related information betweentables
relationship strength: based on how the primary key of a related entity is defined
secondary key an attribute useSd strictly for retrieval purposes
simple attributes: drawn from the atomic value domains
SIN: social insurance number
single-valued attributes: see simple attributes
stored attribute: saved physically to the database
1. B-Undivided rectangles
2. A- entity set
Page | 59
3. B-one to one
4. C- weak entity set
5. B-composite attribute
Website:
https://www.sanfoundry.com/database-mcqs
2.10 ASSIGNMENT
Page | 60
Figure 2.1. Director and Play tables for question 2, by A. Watt.
e. Does the PLAY table exhibit referential integrity? Why or why not?
3. Define the following terms (you may need to use the Internet for some of these):
a. schema
b. host language
c. data sublanguage
d. data definition language
e. unary related
Unit Structure
Page | 61
3.1 LEARNING OBJECTIVES
Constraints
Types of Integrity
Foreign key rules
Relationship types
Constraints are a very important feature in a relational model. In fact, the relational
model supports the well-definedtheory of constraints on attributes or tables.
Constraints are useful because they allow a designer to specifythe semantics of data
in the database. Constraints are the rules that force DBMSs to check that data
satisfies thesemantics.
Domain Integrity
Page | 62
Domain restricts the values of attributes in the relation and is a constraint of the
relational model. However, there arereal-world semantics for data that cannot be
specified if used only with domain constraints. We need more specificways to state
what data values are or are not allowed and which format is suitable for an attribute.
For example,the Employee ID (EID) must be unique or the employee Birthdate is in
the range [Jan 1, 1950, Jan 1, 2000]. Suchinformation is provided in logical
statements called integrity constraints.
Entity integrity
To ensure entity integrity, it is required that every table have a primary key. Neither
the PK nor any part of itcan contain null values. This is because null values for the
primary key mean we cannot identify some rows. Forexample, in the EMPLOYEE
table, Phone cannot be a primary key since some people may not have a telephone.
Referential integrity
Referential integrity requires that a foreign key must have a matching primary key or
it must be null. This constraintis specified between two tables (parent and child); it
maintains the correspondence between rows in these tables. Itmeans the reference
from a row in one table to another table must be valid.
• Customer(CustID, CustName)
To ensure that there are no orphan records, we need to enforce referential integrity.
An orphan record is one whoseforeign key FK value is not found in the
corresponding entity – the entity where the PK is located. Recall that atypical join is
between a PK and FK.
The referential integrity constraint states that the customer ID (CustID) in the Order
table must match a valid CustIDin the Customer table. Most relational databases
Page | 63
have declarative referential integrity. In other words, when thetables are created the
referential integrity constraints are set up.
The referential integrity constraint states that CrsCode in the Class table must match
a valid CrsCode in the Coursetable. In this situation, it’s not enough that the
CrsCode and Section in the Class table make up the PK, we must alsoenforce
referential integrity.
When setting up referential integrity it is important that the PK and FK have the same
data types and come from thesame domain, otherwise the relational database
management system (RDBMS) will not allow the join. RDBMS is apopular database
system that is based on the relational model introduced by E. F. Codd of IBM’s San
Jose ResearchLaboratory. Relational database systems are easier to use and
understand than other database systems.
Page | 64
Referential integrity using Transact-SQL (MS SQL Server)
When using Transact-SQL, the referential integrity is set when creating the Order
table with the FK. Listed beloware the statements showing the FK in the Order table
referencing the PK in the Customer table.
CREATE TABLE Customer
OrderDate DATETIME )
Foreign key rules
Additional foreign key rules may be added when setting referential integrity, such as
what to do with the child rows(in the Orders table) when the record with the PK, part
of the parent (Customer), is deleted or changed (updated). For example, the Edit
Relationships window in MS Access (see Figure 9.1) shows two additional options
for FK rules: Cascade Update and Cascade Delete. If these are not selected, the
system will prevent the deletion or update of PK values in the parent table (Customer
table) if a child record exists. The child record is any record with a matching PK.
In some databases, an additional option exists when selecting the Delete option
called Set to Null. In this is chosen, the PK row is deleted, but the FK in the child
table is set to NULL. Though this creates an orphan row, it is acceptable.
Enterprise Constraints
Enterprise constraints – sometimes referred to as semantic constraints – are
additional rules specified by users or database administrators and can be based on
multiple tables. Here are some examples.
• A class can have a maximum of 30 students.
• A teacher can teach a maximum of four classes per semester.
• An employee cannot take part in more than five projects.
• The salary of an employee cannot exceed the salary of the employee’s
manager.
Page | 65
Business Rules
Business rules are obtained from users when gathering requirements. The
requirements-gathering process is veryimportant, and its results should be verified
by the user before the database design is built. If the business rules areincorrect, the
design will be incorrect, and ultimately the application built will not function as
expected by the users.Some examples of business rules are:
The outermost symbol of the relationship symbol, on the other hand, represents the
connectivity between the twotables. Connectivity is the relationship between two
Page | 66
tables, e.g., one to one or one to many. The only time it is zerois when the FK can be
null. When it comes to participation, there are three options to the relationship
between theseentities: either 0 (zero), 1 (one) or many. In Figure 9.2, for example,
the connectivity is 1 (one) on the outer, lefthandside of this line and many on the
outer, right-hand side.
Figure 3.3. shows the symbol that represents a one to many relationship.
Figure:3.3
Figure:3.4
Relationship Types
The line that connects two tables, in an ERD, indicates the relationship type between
the tables: either identifying ornon-identifying. An identifying relationship will have a
solid line (where the PK contains the FK). A non-identifyingrelationship is indicated
by a broken line and does not contain the FK in the PK. See the section in Chapter 8
thatdiscusses weak and strong relationships for more explanation.
Page | 67
Figure 3.5. Identifying and non-identifying relationship, by A. Watt.
Optional relationships
In an optional relationship, the FK can be null or the parent table does not need to
have a corresponding childtable occurrence. The symbol, shown in Figure 3.6,
illustrates one type with a zero and three prongs (indicating many) which is
interpreted as zero OR many.
Figure 3.6.
For example, if you look at the Order table on the right-hand side of Figure 3.7,
you’ll notice that a customer doesn’tneed to place an order to be a customer. In
other words, the many side is optional.
Page | 68
Figure 3.7. Example usage of a zero to many optional relationship symbol,
• Left side: The order entity must contain a minimum of one related entity in the
Customer table and amaximum of one related entity.
• Right side: A customer can place a minimum of zero orders or a maximum of many
orders.
Figure 3.8 shows another type of optional relationship symbol with a zero and one,
meaning zero OR one. Theone side is optional.
Figure 3.8.
Figure 3.9 gives an example of how a zero to one symbol might be used
Page | 69
Mandatory relationships
Figure:3.10
See Figure 3.11 for an example of how the one and only one mandatory symbol is
used.
Figure 3.11 Example usage of one and only one mandatory symbol
Figure 3.12 illustrates what a one to many relationship symbol looks like where the
many side is mandatory.
Figure3.12.
Refer to Figure 3.13 for an example of how the one to many symbol may be used.
Page | 70
Figure 3.13. Example of a one to many mandatory relationship symbol, by
So far we have seen that the innermost side of a relationship symbol (on the left-side
of the symbol in Figure 3.14)can have a 0 (zero) cardinality and a connectivity of
many (shown on the right-side of the symbol in Figure 3.14),or one (not shown).
Figure:3.14
Figure:3.15
The connectivity symbols show maximums. So if you think about it logically, if the
connectivity symbol on the leftside shows 0 (zero), then there would be no
connection between the tables.
The way to read a relationship symbol, such as the one in Figure 3.16, is as follows.
Page | 71
Figure 3.16. The relationship between a Customer table and an Order
• The CustID in the Order table must also be found in the Customer table a
minimum of 0 and a maximum of 1 times.
• The 0 means that the CustID in the Order table may be null.
• The left-most 1 (right before the 0 representing connectivity) says that if there
is a CustID in the Order table, it can only be in the Customer table once.
• When you see the 0 symbol for cardinality, you can assume two things: T
o the FK in the Order table allows nulls, and
o the FK is not part of the PK since PKs must not contain null values.
In this chapter, we have studied what is integrity and types of integrity. We also
studied cardinality and connectivity. Finally, we ended the discussion with the
relation types.
3.6 GLOSSARY
business rules: obtained from users when gathering requirements and are used to
determine cardinality
cardinality: expresses the minimum and maximum number of entity occurrences
associated with oneoccurrence of a related entity
connectivity: the relationship between two tables, e.g., one to one or one to many
constraints: the rules that force DBMSs to check that data satisfies the semantics
entity integrity: requires that every table have a primary key; neither the primary
key, nor any part of it, cancontain null values
Page | 72
identifying relationship: where the primary key contains the foreign key; indicated
in an ERD by a solidline
integrity constraints: logical statements that state what data values are or are not
allowed and which formatis suitable for an attribute
mandatory relationship: one entity occurrence requires a corresponding entity
occurrence.
non-identifying relationship: does not contain the foreign key in the primary key;
indicated in an ERD bya dotted line
optional relationship: the FK can be null or the parent table does not need to have
a corresponding childtable occurrence
orphan record: a record whose foreign key value is not found in the corresponding
referential integrity: requires that a foreign key must have a matching primary key
or it must be null
relational database management system (RDBMS): a popular database system
Laboratory
relationship type: the type of relationship between two tables in an ERD (either
identifying or no identifying);this relationship is indicated by a line drawn between the
two tables.
Page | 73
number) before the record is
accepted
C Information on the customer D When entering an order
must be known before anything quantity, the user must input a
can be sold to that customer number and not some text (i.e.,
12 rather than ‘a dozen’)
4 To include integrity constraint in an existing relation use :
A Create table B Alter table
C Modify table D Drop table
5 CREATETABLE Manager(ID NUMERIC,Name VARCHAR(20),budget
NUMERIC,Details VARCHAR(30));
Inorder to ensure that the value of budget is non-negative which of the following
should be used?
A Check(budget>0) B Alter(budget>0)
C Check(budget<0) D Alter(budget<0)
1. B-Positive
2. C-Improve the quality of data entered for a specific property (i.e., table
column)
3. C- Information on the customer must be known before anything can be sold to
that customer
4. B- Alter table
5. A - Check(budget>0)
Website:
https://www.sanfoundry.com/database-mcqs
Page | 74
Unit 4: Relational Design and
redundancy 4
Unit Structure
Page | 75
After studying this unit you should be able to understand following:
One important theory developed for the entity relational (ER) model involves the
notion of functional dependency(FD). The aim of studying this is to improve your
understanding of relationships among data and to gain enoughformalism to assist
with practical database design.
Like constraints, FDs are drawn from the semantics of the application domain.
Essentially, functional dependenciesdescribe how individual attributes are related.
FDs are a kind of constraint among attributes within a relation andcontribute to a
good relational schema design. In this chapter, we will look at:
Generally, a good relational database design must capture all of the necessary
attributes and associations. The designshould do this with a minimal amount of
stored information and no redundant data.
Page | 76
Consider Figure 4.1: customer 1313131 is displayed twice, once for account no. A-
101 and again for accountA-102. In this case, the customer number is not redundant,
although there are deletion anomalies with the table.
Having a separate customer table would solve this problem. However, if a branch
address were to change, it wouldhave to be updated in multiple places. If the
customer number was left in the table as is, then you wouldn’t need abranch table
and no join would be required, and performance is improved .
Insertion Anomaly
An insertion anomaly occurs when you are inserting inconsistent information into a
table. When we insert a newrecord, such as account no. A-306 in Figure 10.2, we
need to check that the branch data is consistent with existingrows.
Page | 77
Figure 4.2. Example of an insertion anomaly.
Update Anomaly
If a branch changes address, such as the Round Hill branch in Figure 10.3, we need
to update all rows referring tothat branch. Changing existing information incorrectly is
called an update anomaly.
Deletion Anomaly
A deletion anomaly occurs when you delete a record that may contain attributes that
shouldn’t be deleted. Forinstance, if we remove information about the last account at
a branch, such as account A-101 at the Downtownbranch in Figure 10.4, all of the
branch information disappears.
Page | 78
Figure 4.4. Example of a deletion anomaly.
The problem with deleting the A-101 row is we don’t know where the Downtown
branch is located and we lose all information regarding customer 1313131. To avoid
these kinds of update or deletion problems, we need to decompose the original table
into several smaller tables where each table has minimal overlap with other tables.
Each bank account table must contain information about one entity only, such as the
Branch or Customer, asdisplayed in Figure 10.5.
Figure 4.5. Examples of bank account tables that contain one entity each,
Following this practice will ensure that when branch information is added or updated
it will only affect one record.So, when customer information is added or deleted, the
branch information will not be accidentally modified orincorrectly recorded.
Figure 10.6 shows an example of an employee project table. From this table, we can
assume that:
Page | 79
Next, let’s look at some possible anomalies that might occur with this table during
the following steps.
6. Problem: Step #5 creates two tuples with different values for project P1’s budget
7. Solution: Create a separate table, each, for Projects and Employees, as shown in
Figure 4.7.
The best approach to creating tables without anomalies is to ensure that the tables
are normalized, and that’s accomplished by understanding functional dependencies.
FD ensures that all attributes in a table belong to that table.
Figure 4.8. Separate Project and Employee tables with data, by A. Watt.
Page | 80
By keeping data separate using individual Project and Employee tables:
2. No dummy values are needed for projects that have no employees assigned.
In this chapter, we have studied the relational design and redundancy. Finally, we
ended the discussion with the some types of anomalies : insertion anomaly, update
anomaly, deletion anomaly.
4.4 GLOSSARY
deletion anomaly: occurs when you delete a record that may contain attributes that
shouldn’t’t be deleted
Page | 81
C Deletion D All of the above
4 Due to _______, the database design precludes some data from being stored.
A Insertion anomalies B Update anomalies
C Deletion anomalies D Selection anomalies
5 A ________ occurs when you delete a record that may contain attributes that
shouldn’t be deleted.
A Insertion anomaly B Update anomaly
C Deletion anomaly D None of these.
Website:
https://www.sanfoundry.com/database-mcqs
Page | 82
BLOCK-3
FUNCTIONAL DEPENDENCIES AND
NORMALIZATION
Page | 83
Unit 1: Functional
Dependencies
1
Unit Structure
Page | 84
1.1 LEARNING OBJECTIVES
X ———–> Y
The left side of the above FD diagram is called the determinant, and the right side is
the dependent. Here are a few examples.
In the first example, below, SIN determines Name, Address and Birthdate. Given
SIN———->Name,Address,Birthdate
SIN, we can determine any of the other attributes within the table.
For the second example, SIN and Course determine the date completed
SIN,Course———> DateCompleted
Page | 85
ISBN———–>Title
Consider the following table of data r(R) of the relation schema R(ABCDE) shown in
Table 1.1.
As you look at this table, ask yourself: What kind of dependencies can we observe
among the attributes in Table R?
Since the values of A are unique (a1, a2, a3, etc.), it follows from the FD definition
that: A → B, A → C, A → D, A→E
Since the values of E are always the same (all e1), it follows that:
A → E, B → E, C → E, D → E
Page | 86
Other observations:
a. Therefore, C → D
Looking at actual data can help clarify which attributes are dependent and which are
determinants.
Inference Rules
Armstrong’s axioms are a set of inference rules used to infer all the functional
dependencies on a relational database. They were developed by William W.
Armstrong. The following describes what will be used, in terms of notation, to explain
these axioms.
Let R(U) be a relation scheme over the set of attributes U. We will use the letters X,
Y, Z to represent any subset of and, for short, the union of two sets of attributes,
instead of the usual X U Y.
Axiom of reflexivity
For example, PartNo —> NT123 where X (PartNo) is composed of more than one
piece of information; i.e., Y (NT) and partID (123).
Page | 87
Axiom of augmentation
The axiom of augmentation says that every non-key attribute must be fully
dependent on the PK. In the example shown below, StudentName, Address, City,
Prov, and PC (postal code) are only dependent on the StudentNo, not on the
StudentNo and Grade.
This situation is not desirable because every non-key attribute has to be fully
dependent on the PK. In this situation, student information is only partially dependent
on the PK (StudentNo).
To fix this problem, we need to break the original table down into two as follows:
Axiom of transitivity
The table below has information not directly related to the student; for instance,
ProgramID and ProgramName should have a table of its own. ProgramName is not
dependent on StudentNo; it’s dependent on ProgramID.
Page | 88
StudentNo —> StudentName, Address, City, Prov, PC, ProgramID, ProgramName
To fix this problem, we need to break this table into two: one to hold information
about the student and the other to hold information about the program.
However we still need to leave an FK in the student table so that we can identify
which program the student is enrolled in.
Union
This rule suggests that if two tables are separate, and the PK is the same, you may
want to consider putting them together. It states that if X determines Y and X
determines Z then X must also determine Y and Z (see Figure 1.4).
You may want to join these two tables into one as follows:
Some database administrators (DBA) might choose to keep these tables separated
for a couple of reasons. One, each table describes a different entity so the entities
should be kept apart. Two, if SpouseName is to be left NULL most of the time, there
is no need to include it in the same table as EmpName.
Decomposition
Page | 89
Decomposition is the reverse of the Union rule. If you have a table that appears to
contain two entities that are determined by the same PK, consider breaking them up
into two tables. This rule states that if X determines Y and Z, then X determines Y
and X determines Z separately (see Figure 1.5).
Dependency Diagram
• Partial Dependencies:
ProjectNo —> ProjName EmpNo —> EmpName, DeptNo, ProjectNo, EmpNo —>
HrsWork
• Transitive Dependency:
DeptNo —>DeptName
Page | 90
1.4 LET US SUM UP
In this chapter, we have studied what are Functional dependencies. We also studied
about rules of Functional dependencies. Finally, we ended the discussion with the
rules are inference rules, axiom of reflexivity ,axiom of augmentation ,axiom of
transitivity ,union, dependency diagram .
1.5 GLOSSARY
Armstrong’s axioms: a set of inference rules used to infer all the functional
dependencies on a relationaldatabase
DBA: database administrator
decomposition: a rule that suggests if you have a table that appears to contain two
entities that aredetermined by the same PK, consider breaking them up into two
tables
dependent: the right side of the functional dependency diagram
determinant: the left side of the functional dependency diagram
functional dependency (FD): a relationship between two attributes, typically
between the PK and othernon-key attributes within a table
non-normalized table: a table that has data redundancy in it
Union: a rule that suggests that if two tables are separate, and the PK is the same,
consider putting themtogether
1 Functional Dependencies are the types of constraints that are based on______
A Key B Key revisited
C Superset key D None of the above
2 We can use the following three rules to find logically implied functional dependencies.
This collection of rules is called
A Axioms B Armstrong’s axioms
C Armstrong D Closure
3 Consider a relation R(A,B,C,D,E) with the following functional dependencies:
Page | 91
D-> AB The number of superkeys of R is:
A 2 B 7
C 10 D 12
4 Suppose relation R(A,B,C,D,E) has the following functional dependencies:
A -> B
B -> C
BC -> A
A -> D
E -> A
D -> E
Which of the following is not a key?
A A B E
C B,C D D
5 There are two functional dependencies with the same set of attributes on the left side of
the arrow:
A->BC
A->B
This can be combined as
A A->BC B A->B
C B->C D None of the mentioned
1. A-key
2. B-Armstrong’s axioms
3. C- 10
4. C- B,C
5. A- A->BC
Website:
https://www.sanfoundry.com/database-mcqs
Page | 92
Unit 2: Introduction to Data
Normalization
2
Unit Structure
Page | 93
2.1 LEARNING OBJECTIVES
Normalization
Database Normal forms
o 1NF
o 2NF
o 3NF
o BCNF
Use an entity relation diagram (ERD) to provide the big picture, or macro view, of an
organization’s data requirements and operations. This is created through an iterative
process that involves identifying relevant entities, their attributes and their
relationships.
What Is Normalization?
Normalization is the branch of relational theory that provides design insights. It is the
process of determining how much redundancy exists in a table. The goals of
normalization are to:
Page | 94
Normalization theory draws heavily on the theory of functional dependencies.
Normalization theory defines six normal forms (NF). Each normal form involves a set
of dependency properties that a schema must satisfy and each normal form gives
guarantees about the presence and/or absence of update anomalies. This means
that higher normal forms have less redundancy, and as a result, fewer update
problems.
Normal Forms
All the tables in any database can be in one of the normal forms we will discuss next.
Ideally we only want minimal redundancy for PK to FK. Everything else should be
derived from other tables. There are six normal forms, but we will only look at the
first four, which are:
In the first normal form, only single values are permitted at the intersection of each
row and column; hence, there are no repeating groups.
To normalize a relation that contains a repeating group, remove the repeating group
and form two new relations.
The PK of the new relation is a combination of the PK of the original relation plus an
attribute from the newly created relation for unique identification.
Student_Grade_Report(StudentNo,StudentName,Major,CourseNo,CourseName,InstructorNo,Instru
ctorName,InstructorLocation, Grade)
Page | 95
• In the Student Grade Report table, the repeating group is the course
information. A student can take many courses.
• Remove the repeating group. In this case, it’s the course information
for each student.
• After removing all the attributes related to the course and student,
you are left with the student course table (StudentCourse).
• The Student table (Student) is now in first normal form with the
Student(StudentNo,StudentName,Major)
StudentCourse(StudentNo,CourseNo,CourseName,InstructorNo,InstructorName,InstructorLocation,
Grade)
Page | 96
2.3 SECOND NORMAL FORM(2NF)
For the second normal form, the relation must first be in 1NF. The relation is
automatically in 2NF if, and only if, the PK comprises a single attribute.
If the relation has a composite PK, then each non-key attribute must be fully
dependent on the entire PK and not on a subset of the PK (i.e., there must be no
partial dependency or augmentation).
• When examining the Student Course table, we see that not all the attributes
are fully dependent on the PK; specifically, all course information. The only
attribute that is fully dependent is grade.
Student(StudentNo,StudentName,Major)
CourseGrade(StudentNo,CourseNo,Grade)
CourseInstructor(CourseNo,CourseName,InstructorNo,Instructor
Name,InstructorLocation)
Page | 97
• Deleting a course may also delete instructor information
To be in third normal form, the relation must be in second normal form. Also all
transitive dependencies must be removed; a non-key attribute may not be
functionally dependent on another non-key attribute.
Student(StudentNo,StudentName,Major)
CourseGrade(StudentNo,CourseNo,Grade)
Course(CourseNo,CourseName,InstructorNo)
Instructor(InstructorNo,InstructorName,InstructorLocation)
At this stage, there should be no anomalies in third normal form. Let’s look at the
dependency diagram (Figure 12.1) for this example. The first step is to remove
repeating groups, as discussed above.
Page | 98
StudentCourse (StudentNo, CourseNo, CourseName, InstructorNo,
InstructorName, InstructorLocation, Grade) To recap the normalization process for
the School database, review the dependencies shown in Figure 2.1.
When a table has more than one candidate key, anomalies may result even though
the relation is in 3NF. Boyce- Codd normal form is a special case of 3NF. A relation
is in BCNF if, and only if, every determinant is a candidate key.
BCNF Example 1
Page | 99
671 Physics White
The semantic rules (business rules applied to the database) for this table are:
The functional dependencies for this table are listed below. The first one is a
candidate key; the second is not.
3. Update – inconsistencies
To reduce the St_Maj_Adv relation to BCNF, you create two new tables:
St_Adv table
Page | 100
Student_id Advisor
111 Smith
111 Chan
320 Dobbs
671 White
803 Smith
Adv_Maj table
Advisor Major
Smith Physics
Chan Music
Dobbs Math
White Physics
BCNF Example 2
Date Time No
Page | 101
FD1 – ClientNo, InterviewDate –> InterviewTime, StaffNo, RoomNo (PK)
A relation is in BCNF if, and only if, every determinant is a candidate key. We need
to create a table that incorporates the first three FDs (Client_Interview2 table) and
another table (StaffRoom table) for the fourth FD.
Client_Interview2 table
StaffRoom table
Page | 102
Normalization and Database Design
During the normalization process of database design, make sure that proposed
entities meet required normal form before table structures are created. Many real-
world databases have been improperly designed or burdened with anomalies if
improperly modified during the course of time. You may be asked to redesign and
modify existing databases. This can be a large undertaking if the tables are not
properly normalized.
2.7 GLOSSARY
Page | 103
A Eliminate all hidden B Eliminate the possibility of a
dependencies insertion anomalies
C Have a composite key D Have all non key fields depend
on the whole primary key
3 Which-one ofthe following statements about normal forms is FALSE?
A BCNF is stricter than 3 NF B Lossless, dependency -preserving
decomposition into 3 NF is always
possible
C Loss less, dependency – D Any relation with two attributes is
preserving decomposition into BCNF
BCNF is always possible
4 Empdt1(empcode, name, street, city, state, pincode).
For any pincode, there is only one city and state. Also, for given street, city and
state, there is just one pincode. In normalization terms, empdt1 is a relation in
1. A- first
2. A-Eliminate all hidden dependencies
3. C- Loss less, dependency – preserving decomposition into BCNF is always
possible
4. B- 2 NF and hence also in 1 NF
5. D- 4NF
Website:
https://www.sanfoundry.com/database-mcqs
Page | 104
BLOCK-4
SQL-STATEMENTS
Page | 105
Unit 1: Introduction to SQL
1
Unit Structure
Page | 106
1.1 LEARNING OBJECTIVES
SQL introduction
How to create database
Page | 107
In a DBMS, the SQL database language is used to:
In this chapter, we will focus on using SQL to create the database and table
structures, mainly using SQL as a data definition language (DDL). In Chapter 16, we
will use SQL as a data manipulation language (DML) to insert, delete, select and
update data within the database tables.
Create Database
CREATETABLE<tablename>(
ColumnName,Datatype,Optional
ColumnConstraint,ColumnName,
Datatype,OptionalColumnConstr
aint,OptionaltableConstraints
);
Page | 108
Tablename is the name of the database table such as Employee. Each field in the
CREATE TABLE has three parts (see above):
1. ColumnName
2. Data type
ColumnName
Data Type
The data type, as described below, must be a system data type or a user-defined
data type. Many of the data types have a size such as CHAR(35) or Numeric(8,2).
Int –Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 – 1
(2,147,483,647)
Decimal –Fixed precision and scale numeric data from -10^38 -1 through 10^38
Page | 109
Float –Floating precision number data from -1.79E + 308 through 1.79E + 308
Datetime –Date and time data from January 1, 1753, to December 31, 9999, with an
accuracy of one-three- hundredths of a second, or 3.33 milliseconds
Smalldatetime –Date and time data from January 1, 1900, through June 6, 2079,
with an accuracy of one minute
The Optional ColumnConstraints are NULL, NOT NULL, UNIQUE, PRIMARY KEY
and DEFAULT, used to initialize a value for a new record. The column constraint
NULL indicates that null values are allowed, which means that a row can be created
without a value for this column. The column constraint NOT NULL indicates that a
value must be supplied when a new row is created.
To illustrate, we will use the SQL statement CREATE TABLE EMPLOYEES to create
the employees table with 16 attributes or fields.
Page | 110
USESW
CREATETABLEEMPLOYEES(
The first field is EmployeeNo with a field type of CHAR. For this field, the field length
is 10 characters, and the user cannot leave this field empty (NOT NULL)
Similarly, the second field is DepartmentName with a field type CHAR of length 30.
After all the table columns are defined, a table constraint, identified by the word
CONSTRAINT, is used to create the primary key:
Page | 111
Likewise, we can create a Department table, a Project table and an Assignment
table using the CREATE TABLE SQL DDL command as shown in the below
example.
USESW
CREATETABLEDEPAR
TMENT(
OfficeNumberChar(15)NOT NULL,
Phone Char(15)NOTNULL,
CONSTRAINTDEPARTMENT_PKPRIMARYKEY(DepartmentName)
);
In this example, a project table is created with seven fields: ProjectID, ProjectName,
Department, MaxHours, StartDate, and EndDate.
USESW
CREATETABLEPROJECT(
ProjectID IntNOTNULLIDENTITY(1000,100),
ProjectName Char(50)NOTNULL,
Department Char(35)NOTNULL,
StartDate DateTimeNULL,
EndDate DateTimeNULL,
CONSTRAINT ASSIGNMENT_PKPRIMARYKEY(ProjectID)
);
Page | 112
In this last example, an assignment table is created with three fields: ProjectID,
EmployeeNumber, and HoursWorked. The assignment table is used to record who
(EmployeeNumber) and how much time(HoursWorked) an employee worked on the
particular project(ProjectID).
USESW
CREATETABLEASSIGNMENT(
EmployeeNumberIntNOT NULL,
HoursWorkedNumeric(6,2)NULL,
);
Table Constraints
Table constraints are identified by the CONSTRAINT keyword and can be used to
implement various constraints described below.
IDENTITY constraint
Page | 113
• Cannot bind defaults and default constraints to the column
We will use another database example to further illustrate the SQL DDL statements
by creating the table tblHotel in this HOTEL database.
UNIQUE constraint
The UNIQUE constraint prevents duplicate values from being entered into a column.
• A UNIQUE constraint can be placed on columns that accept nulls. Only one
row can be NULL.
Page | 114
This is the general syntax for the UNIQUE constraint:
[CONSTRAINTconstraint_name]
UNIQUE[CLUSTERED|NONCLUSTERED]
(col_name[,col_name2[…,col_name16]])
[ONsegment_name]
CREATETABLEEMPLO
YEES
(
EmployeeNo CHAR(10) NOTNULL UNIQUE,
Page | 115
• No index is created on the columns that participate in an FK
constraint.
[CONSTRAINTconstraint_name]
In this example, the field HotelNo in the tblRoom table is a FK to the field HotelNo in
the tblHotel table shown previously.
USE HOTEL
GO
CREATE TABLE tblRoom
(
HotelNo Int NOT NULL ,
RoomNo Int NOT NULL,
Type Char(50) NULL,
Price Money NULL,
PRIMARY KEY (HotelNo, RoomNo),
FOREIGN KEY (HotelNo) REFERENCES tblHotel
)
CHECK constraint
The CHECK constraint restricts values that can be entered into a table.
Page | 116
• It can be defined for a column that has a rule bound to it.
[CONSTRAINTconstraint_name]
CHECK[NOTFORREPLICATION](expression)
Inthisexample,theTypefieldisrestrictedtohaveonlythetypes‘Single’,‘Double’,‘Suite’or‘Executive’.
USE HOTEL
GO
CONSTRAINT Valid_Type
In this second example, the employee hire date should be before January 1, 2004, or
have a salary limit of $300,000.
Page | 117
GO
CREATE TABLE SALESREPS
(
Empl_num Int Not Null
CHECK (Empl_num BETWEEN 101 and 199),
Name Char (15),
Age Int CHECK (Age >= 21),
Quota Money CHECK (Quota >= 0.0),
HireDate DateTime,
CONSTRAINT QuotaCap CHECK ((HireDate < “01-01-2004”) OR (Quota <=300000))
)
DEFAULT constraint
The DEFAULT constraint is used to supply a value that is automatically added for a
column if the user does not supply one.
[CONSTRAINTconstraint_name]
DEFAULT{constant_expression|niladic-
function|NULL}
[FORcol_name]
This example sets the default for the city field to ‘Vancouver’.
USE HOTEL
Page | 118
User Defined Types
User defined types are always based on system-supplied data type. They can
enforce data integrity and they allow nulls.
sp_addtypessn,‘varchar(11)’,‘NOTNULL’
In this example, the field EmployeeSIN uses the user-defined data type SIN.
EmployeeSIN SIN,
CONSTRAINT CheckSIN
ALTER TABLE
You can use ALTER TABLE statements to add and drop constraints.
• When a constraint is added, all existing data are verified for violations.
Page | 119
In this example, we use the ALTER TABLE statement to the IDENTITY property to a
ColumnName field.
USE HOTEL
GO
ALTER TABLE tblHotel
ADD CONSTRAINT unqName UNIQUE (Name)
Use the ALTER TABLE statement to add a column with the IDENTITY property such
as ALTER TABLETableName.
ADD
DROP TABLE
The DROP TABLE will remove a table from the database. Make sure you have the
correct database selected.
Executing the above SQL DROP TABLE statement will remove the table tblHotel
from the database.
In this chapter, we have studied what is SQL. We also studied about how to create
the database SQL. Finally, we ended the discussion with types of constraint
property: optional column constraints, identityconstraints, table ,Unique constraints,
Foreign key constraints ,check constraints,default constraints.
1.4 GLOSSARY
Page | 120
DDL: abbreviation for data definition language
DML: abbreviation for data manipulation language
SEQUEL: acronym for Structured English Query Language; designed to manipulate
System R
Structured Query Language (SQL): a database language designed for managing
data held in a relationaldatabase management system
1 The ______ clause allows us to select only those rows in the result relation of the
____ clause that satisfy a specified predicate.
A Where, from B From, select
C Select, from D From, where
2 Constraints can be applied on ___________
A Column B Table
C Field D All of the above
3 Point out the wrong statement.
1. C- Select,from
2. A- column
3. D- Primary keys allow for NULL as one of the unique values
Page | 121
4. B- CASCADE
5. A- Primary key constraint
Website:
https://www.sanfoundry.com/database-mcqs
Page | 122
Unit 2:SQL – Data
Manipulation Language
2
Unit Structure
Page | 123
2.1 LEARNING OBJECTIVES
The SQL data manipulation language (DML) is used to query and modify database
data. In this chapter, we will describe how to use the SELECT, INSERT, UPDATE,
and DELETE SQL DML command statements, defined below.
Page | 124
• If a clause has several parts, they should appear on separate lines
and be indented under the start of the clause to show the
relationship.
The SELECT statement, or command, allows the user to extract data from tables,
based on specific criteria. It is processed according to the following sequence:
We can use the SELECT statement to generate an employee phone list from the
Employees table as follows:
SELECTFirstName,LastName,ph
oneFROMEmployees
ORDERBYLastName
This action will display employee’s last name, first name, and phone number from
the Employees table, seen in Table 2.1.
Page | 125
In this next example, we will use a Publishers table (Table 16.2). (You will notice that
Canada is mispelled in the Publisher Country field for Example Publishing and ABC
Publishing. To correct mispelling, use the UPDATE statement to standardize the
country field to Canada – see UPDATE statement later in this chapter.)
If you add the publisher’s name and city, you would use the SELECT statement
followed by the fields name separated by a comma:
Thisactionwilldisplaythepublisher’snameandcityfromthePublisherstable.
Ifyoujustwantthepublisher’snameunderthedisplaynamecity,youwouldusetheSELECTstate
mentwithnocommaseparatingpub_name andcity:
SELECTPubNamecityFROMPu
blishers
Performing this action will display only the pub_name from the Publishers
table with a “city” heading. If you do not include the comma, SQL Server
assumes you want a new column name for pub_name.
Page | 126
Sometimes you might want to focus on a portion of the Publishers table, such as
only publishers that are in Vancouver. In this situation, you would use the SELECT
statement with the WHERE criterion, i.e., WHERE city = ‘Vancouver’.
These first two examples illustrate how to limit record selection with the WHERE
criterion using BETWEEN. Each of these examples give the same results for store
items with between 20 and 50 items in stock.
SELECTStorID,qty,TitleID
FROMSales
WHEREqtyBETWEEN20and50(includesthe20and50)
Example #2, on the other hand, uses qty >=20 and qty <=50 .
SELECTStorID,qty,TitleID
FROMSales
WHEREqty>=20andqty<=50
Example #3 illustrates how to limit record selection with the WHERE criterion using
NOT BETWEEN
SELECTStorID,qty,TitleID
FROMSales
WHEREqtyNOTBETWEEN20and50
Page | 127
The next two examples show two different ways to limit record selection with the
WHERE criterion using IN, with each yielding the same results.
Example #4 shows how to select records using province= as part of the WHERE
statement.
SELECT*
FROMPublishers
SELECT*
FROMPublishers
WHEREprovinceIN (‘BC’,‘AB’,‘ON’)
The final two examples illustrate how NULL and NOT NULL can be used to select
records. For these examples, a Books table (not shown) would be used that contains
fields called Title, Quantity, and Price (of book). Each publisher has a Books table
that lists all of its books.
Page | 128
Example #6 uses
NULL.
FROMBooks
WHEREpriceISNULL
Example#7usesNOTNULL.
WHEREpriceISNOTNULL
UsingwildcardsintheLIKEclause
The LIKE keyword selects rows containing fields that match specified
portions of character strings. LIKE is usedwith char, varchar, text, datetime
and smalldatetime data. A wildcard allows the user to match fields that
containcertain letters. For example, the wildcard province = ‘N%’ would
give all provinces that start with the letter
‘N’.Table16.3showsfourwaystospecifywildcardsintheSELECTstatementinre
gularexpressformat.
[] Any single character within the specified range (e.g., [a-f]) or set (e.g.,
Page | 129
[abcdef])
[^] Any single character not within the specified range (e.g., [^a – f]) or set
(e.g., [^abcdef])
In example #1, LIKE ‘Mc%’ searches for all last names that begin with the letters
“Mc” (e.g., McBadden).
SELECTLastName
FROMEmployees
WHERELastNameLIKE‘Mc%’
For example #2: LIKE ‘%inger’ searches for all last names that end with the letters
“inger” (e.g., Ringer, Stringer).
SELECTLastName
FROMEmployees
WHERELastNameLIKE‘%inger’
In, example #3: LIKE ‘%en%’ searches for all last names that have the letters “en”
(e.g., Bennett, Green, McBadden).
SELECTLastName
FROMEmployees
WHERELastNameLIKE‘%en%’
Page | 130
SELECT statement with ORDER BY clause
You use the ORDER BY clause to sort the records in the resulting list. Use ASC to
sort the results in ascending order and DESC to sort the results in descending order.
SELECT*
FROMEmployees
ORDERBYHireDateASC
SELECT*
FROMBooks
ORDERBYtype,priceDESC
The GROUP BY clause is used to create one output row per each group and
produces summary values for the selected columns, as shown below.
GROUPBYtype
Page | 131
Here is an example using the above statement.
SELECTtypeAS‘Type’,MIN(price)AS‘MinimumPrice
’FROMBooks
WHEREroyalty>10
GROUPBYtype
If the SELECT statement includes a WHERE criterion where price is not null,
SELECTtype,price
FROMBooks
WHEREpriceisnotnull
then a statement with the GROUP BY clause would look like this:
SELECTtypeAS‘Type’,MIN(price)AS‘MinimumPrice’
FROMBooks
WHEREpriceisnotnul
lGROUPBYtype
We can use COUNT to tally how many items are in a container. However, if we want
to count different items into separate groups, such as marbles of varying colours,
then we would use the COUNT function with the GROUP BY command.
The below SELECT statement illustrates how to count groups of data using the
COUNT function with the GROUP BY clause.
Page | 132
Using AVG and SUM with GROUP BY
We can use the AVG function to give us the average of any group, and SUM to give
the total. Example #1 uses the AVG FUNCTION with the GROUP BY type.
SELECTAVG(qty)
FROM Books
GROUPBYtype
SELECTSUM(qty)
FROM Books
GROUPBYtype
SELECT‘TotalSales’=SUM(qty),‘AverageSales’=AVG(qty),s
tor_idFROMSales
GROUPBYStorIDORDERBY‘TotalSales’
Example #3 uses both the AVG and SUM functions with the GROUP BY type in the
SELECT statement.
The HAVING clause can be used to restrict rows. It is similar to the WHERE
condition except HAVING can include the aggregate function; the WHERE cannot do
this.
Page | 133
The HAVING clause behaves like the WHERE clause, but is applicable to groups. In
this example, we use the HAVING clause to exclude the groups with the province
‘BC’.
SELECTau_fnameAS‘Author”sFirstName’,provinceas‘Province’
FROMAuthors
GROUPBYau_fname,province
HAVINGprovince<>‘BC’
• INSERT specifies the table or view that data will be inserted into.
• VALUES specifies the data that you want to insert into the table.
VALUES is required.
INSERT[INTO]Table_name|viewname[column_list]
DEFAULTVALUES|values_list|selectstatement
Page | 134
When inserting rows with the INSERT statement, these rules apply:
• All trailing spaces are removed from data inserted into varchar columns,
except in strings that contain only spaces. These strings are truncated to a
single space.
When you specify values for only some of the columns in the column_list, one of
three things can happen to the columns that have no values:
2. NULL is entered if the column allows NULLs and no default value exists for
the column.
This example uses INSERT to add a record to the publisher’s Authors table.
INSERTINTOAuthors
VALUES(‘555-093-467’,‘Martin’,‘April’,‘281555-
5673’,‘816MarketSt.,’,‘Vancouver’,‘BC’,‘V7G3P4’, 0)
Page | 135
This following example illustrates how to insert a partial row into the Publishers table
with a column list. The country column had a default value of Canada so it does not
require that you include it in your values.
INSERTINTOPublishers(PubID,PubName,city,province)
VALUES(‘9900’,‘AcmePublishing’,‘Vancouver’,‘BC’)
To insert rows into a table with an IDENTITY column, follow the below example. Do
not supply the value for the IDENTITY nor the name of the column in the column list.
VALUES(‘DBA’,100,175)
IDENTITY_INSERT option
Page | 136
To allow an insert with a specific identity value, the IDENTITY_INSERT option can
be used as follows.
SETIDENTITY_INSERTjobsON
INSERTINTOjobs(job_id,job_desc,min_lvl,max_lvl)
SETIDENTITY_INSERTjobsOFF
We can sometimes create a small temporary table from a large table. For this, we
can insert rows with a SELECT statement. When using this command, there is no
validation for uniqueness. Consequently, there may be many rows with the same
pub_id in the example below.
This example creates a smaller temporary Publishers table using the CREATE
TABLE statement. Then the INSERT with a SELECT statement is used to add
records to this temporary Publishers table from the publis table.
CREATETABLEdbo.tmpPublishers(
PubNamevarchar(40) NULL,
provincechar(2)NULL,
countryvarchar(30)NULLDEFAULT(‘Canada’)
INSERTtmpPublishers
SELECT*FROMPublishers
Page | 137
In this example, we’re copying a subset of data.
In this example, the publishers’ data are copied to the tmpPublishers table and the
country column is set to Canada.
INSERTtmpPublishers(PubID,PubName,city,province,country)
SELECTPubID,PubName,city,province,‘Canada’
FROMPublishers
The UPDATE statement changes data in existing rows either by adding new data or
modifying existing data.
INSERTtmpPublishers(pub_id,pub_name)
SELECTPubID,PubName
FROMPublishers
This example uses the UPDATE statement to standardize the country field to be
Canada for all records in the Publishers table.
UPDATE Publishers
SETcountry=‘Canada’
This example increases the royalty amount by 10% for those royalty amounts
between 10 and 20.
UPDATEroysched
WHEREroyaltyBETWEEN10and20
Including subqueries in an UPDATE statement
The employees from the Employees table who were hired by the publisher in 2010
are given a promotion to the highest job level for their job type. This is what the
UPDATEEmployees
SETjob_lvl=
(SELECTmax_lvlFROMjobs
WHEREemployee.job_id=jobs.job_id)
WHEREDATEPART(year,employee.hire_date)=2010
The DELETE statement removes rows from a record set. DELETE names the table
or view that holds the rows that will be deleted and only one table or row may be
listed at a time. WHERE is a standard WHERE clause that limits the deletion to
select records.
DELETE[FROM]{table_name|view_name}[
WHEREclause]
1. If you omit a WHERE clause, all rows in the table are removed
(except for indexes, the table, constraints).
Page | 139
2. DELETE cannot be used with a view that has a FROM clause
naming more than one table. (Delete can affect only one base table
at a time.)
What follows are three different DELETE statements that can be used.
DELETE
FROMDiscounts
DELETE
FROMSales
WHEREstor_id=‘6380’
DELETEFROMSales
WHEREtitle_idIN
(SELECTtitle_idFROMBooks WHEREtype=‘mod_cook’)
2.7Built-in Functions
Page | 140
1. Aggregate: returns summary values
Below you will find detailed descriptions and examples for the first four functions.s
Aggregate functions
FUNCTION DESCRIPTION
AVG Returns the average of all the values, or only the DISTINCT values, in
the expression.
Returns the maximum value in the expression. MAX can be used with
numeric, character and datetime columns, but not with bit columns. With
MAX
character columns, MAX finds the highest value in the collating
sequence. MAX ignores any null values.
Returns the minimum value in the expression. MIN can be used with
numeric, character and datetime columns, but not with bit columns. With
Page | 141
MIN character columns, MIN finds the value that is lowest in the sort
sequence. MIN ignores any null values.
SUM Returns the sum of all the values, or only the DISTINCT values, in the
expression. SUM can be used with numeric columns only.
Below are examples of each of the aggregate functions listed in Table 16.4.
SELECTAVG(price)AS‘AverageTitlePrice’
FROMBooks
SELECTCOUNT(PubID)AS‘NumberofPublishers’
FROMPublishers
SELECTCOUNT(province)AS‘NumberofPublishers’
FROMPublishers
Page | 142
SELECTCOUNT(*)
FROM Employees
WHEREjob_lvl=35
FROMEmployees
FROMBooks
SELECTSUM(discount)AS‘TotalDiscounts’
FROMDiscounts
Conversion function
Page | 143
In the example below, a price that contains two 9s is converted into five characters.
The syntax for this statement is SELECT ‘The date is ‘ + CONVERT(varchar(12),
getdate()).
SELECTCONVERT(int,10.6496)
SELECTtitle_id,price
FROMBooks
WHERECONVERT(char(5),price)LIKE‘%99%’
In this second example, the conversion function changes data to a data type with a
different size.
SELECTtitle_id,CONVERT(char(4),ytd_sales)as‘Sales’
FROMBooks
WHEREtypeLIKE‘%cook’
Date function
The date function produces a date by adding an interval to a specified date. The
result is a datetime value equal to the date plus the number of date parts. If the date
parameter is a smalldatetime value, the result is also a smalldatetime value.
The DATEADD function is used to add and increment date values. The
syntax for this function is DATEADD(datepart, number, date).
Page | 144
SELECT DATEADD(day, 3, hire_date)
FROMEmployees
This command returns the number of datepart “boundaries” crossed between two
specified dates. The method of counting crossed boundaries makes the result given
by DATEDIFF consistent across all data types such as minutes, seconds, and
milliseconds.
SELECTDATEDIFF(day,HireDate,‘Nov301995’)
FROMEmployees
For any particular date, we can examine any part of that date from the year to the
millisecond.
The date parts (DATEPART) and abbreviations recognized by SQL Server, and the
acceptable values are listed in Table 16.5.
Year yy 1753-9999
Quarter qq 1-4
Month mm 1-12
Page | 145
Day dd 1-31
Week wk 1-53
Hour hh 0-23
Minute mi 0-59
Second ss 0-59
Millisecond ms 0-999
Mathematical functions
SELECTPrice,(price*1.1)AS‘NewPrice’,title
FROMBooks
SELECT‘Rounded‘=ROUND(4567.9876,2)
SELECTFLOOR(123.45)
Page | 146
2.8 LET US SUM UP
In this chapter, we have studied introduce the SQL- data manipulation language. We
also studied in SQL-DML some statement like select, insert ,update ,delete
statements. Finally, we ended the discussion about built in functions. Built in
function is aggregate, conversion, Date, mathematical Function etc.
2.9 GLOSSARY
Page | 147
2.9 CHECK YOUR PROGRESS
1 The query given below will not give an error. Which one of the following has to be
replaced to get the desired output?
SELECT ID, name, dept name, salary * 1.1
WHERE instructor;
A Salary*1.1 B ID
C Where D Instructor
2 SELECT * FROM employee WHERE dept_name="Comp Sci";
In the SQL given above there is an error . Identify the error.
A Dept_name B Employee
C “Comp Sci” D From
3 SELECT instructor.*
FROM instructor, teaches
WHERE instructor.ID= teaches.ID;
This query does which of the following operation?
A All attributes of instructor and B All attributes of instructor are
teaches are selected selected on the given condition
C All attributes of teaches are D Only the some attributes from
selected on given condition instructed and teaches are
selected
4
Employee_id Name Salary
Page | 148
CHECK YOUR PROGRESS: POSSIBLE ANSWER
1. C- where
2. C-“Comp Sci”
3. B - All attributes of instructor are selected on the given condition
4. D-1018
5. B- values
Website:
https://www.sanfoundry.com/database-mcqs
Page | 149
Unit 3: SQL – Join Statements
3
Unit Structure
Page | 150
3.1 LEARNING OBJECTIVES
Inner join
Outer join
o Left outer join
o Right outer join
Cross join
Joining Tables
Joining two or more tables is the process of comparing the data in specified columns
and using the comparison results to form a new table from the rows that qualify. A
join statement:
Although the comparison is usually for equality – values that match exactly – other
types of joins can also be specified. All the different joins such as inner, left (outer),
right (outer), and cross join will be described below.
An inner join connects two tables on a column with the same data type. Only the
rows where the column values match are returned; unmatched rows are discarded.
Page | 151
Example #1
SELECTjobs.job_id,job_desc
FROMjobs
INNERJOINEmployeesONemployee.job_id=jobs.job_id
WHEREjobs.job_id<7
Example#2
SELECTauthors.au_fname,authors.au_lname,books.royalty,title
FROMauthorsINNERJOINtitleauthorONauthors.au_id=titleauthor.au_id
INNERJOINbooksONtitleauthor.title_id=books.title_id
GROUPBYauthors.au_lname,authors.au_fname,title,title.royalty
ORDERBYauthors.au_lname
This first example uses the new syntax for a left outer join.
FROMPublishers,Books
WHEREpublishers.pub_id*=books.pub_id
Page | 152
3.5 Right Outer Join
A right outer join includes, in its result set, all rows from the right table that did not
meet the condition specified. Output columns that correspond to the other table are
set to NULL.
Below is an example using the new syntax for a right outer join.
FROMtitleauthor
RIGHTOUTERJOINauthorsONtitleauthor.au_id=authors.au_id
ORDERYBYau_lname
This second example show the old syntax used for a right outer join.
FROMtitleauthor,authors
WHEREtitleauthor.au_id=*authors.au_id
ORDERYBYau_lname
A full outer join specifies that if a row from either table does not match the selection
criteria, the row is included in the result set, and its output columns that correspond
to the other table are set to NULL.
Page | 153
Here is an example of a full outer join.
FROMPublishers
WHERE(publishers.province<>“BC”andpublishers.province<>“ON”)
ORDERBYbooks.title_id
A cross join is a product combining two tables. This join returns the same rows as if
no WHERE clause were specified. For example:
SELECTau_lname,pub_name,
FROMAuthorsCROSSJOINPublishers
In this chapter, we have studied the SQL- Joining tables. In the joining tables are
three types: inner join, outer join, cross join. Finally, we ended the discussion outer
join types: left outer join. Right outer join and full outer join.
3.9 GLOSSARY
Page | 154
right outer join: includes all rows from the right table that did not meet the condition
specified
4 What type of join is needed when you wish to include rows that do not have
matching values?
A Equii–join B Natural join
C Outer join D All of the above
5 Which are the join types in join condition:
A Cross join B Natural join
C Join with USING clause D All of the above.
1. A-on
2. C-inner join
3. A-SELECT *
FROM student INNERJOIN takes USING (ID);
Page | 155
4. C-outer join
5. D –all of the above
Website:
https://www.sanfoundry.com/database-mcqs
Page | 156
Unit 4:Database Development
Process
4
Unit Structure
Page | 157
4.1 LEARNING OBJECTIVES
SDLC-WATERFAL
DATABSE LIFE CYCLE
o Requirement gathering
o Analysis
o Logical design
o Implementation
Realize the design
Populating the database
Let us start with an overview of the waterfall model such as you will find in most
software engineering textbooks.
This waterfall figure, seen in Figure 4.1, illustrates a general waterfall model that
could apply to any computersystem development. It shows the process as a strict
sequence of steps where the output of one step is the input tothe next and all of one
step has to be completed before moving onto the next.
Page | 158
Figure 4.1. Waterfall model.
We can use the waterfall process as a means of identifying the tasks that are
required, together with the input andoutput for each activity. What is important is the
scope of the activities, which can be summarized as follows:
We can use the waterfall cycle as the basis for a model of database development
that incorporates three assumptions:
3. We can represent the constraints to enforce the semantics of the data once
within a database, rather than within every user process that uses the data.
Using these assumptions and Figure 4.2, we can see that this diagram represents a
model of the activities and their outputs for database development. It is applicable to
any class of DBMS, not just a relational approach.
Requirements Gathering
The first step is requirements gathering. During this step, the database designers
have to interview the customers (database users) to understand the proposed
system and obtain and document the data and functional requirements. The result of
this step is a document that includes the detailed requirements provided by the
users.
Page | 160
Establishing requirements involves consultation with, and agreement among, all the
users as to what persistent data they want to store along with an agreement as to
the meaning and interpretation of the data elements. The data administrator plays a
key role in this process as they overview the business, legal and ethical issues within
the organization that impact on the data requirements.
Figure 4.2. A waterfall model of the activities and their outputs for database
development.
The requirements should not describe how the data is to be processed, but rather
what the data items are, what attributes they have, what constraints apply and the
relationships that hold between the data items.
Analysis
Data analysis begins with the statement of data requirements and then produces a
conceptual data model. The aim of analysis is to obtain a detailed description of the
data that will suit user requirements so that both high and low level properties of data
and their use are dealt with. These include properties such as the possible range of
Page | 161
values that can be permitted for attributes (e.g., in the school database example, the
student course code, course title and credit points).
The conceptual data model provides a shared, formal representation of what is being
communicated between clients and developers during database development – it is
focused on the data in a database, irrespective of the eventual use of that data in
user processes or implementation of the data in specific computer environments.
Therefore, a conceptual data model is concerned with the meaning and structure of
data, but not with the details affecting how they are implemented.
The conceptual data model then is a formal representation of what data a database
should contain and the constraints the data must satisfy. This should be expressed
in terms that are independent of how the model may be implemented. As a result,
analysis focuses on the questions, “What is required?” not “How is it achieved?”
Logical Design
Database design starts with a conceptual data model and produces a specification of
a logical schema; this will determine the specific type of database system (network,
relational, object-oriented) that is required. The relational representation is still
independent of any specific DBMS; it is another conceptual data model.
We can use a relational representation of the conceptual data model as input to the
logical design process. The output of this stage is a detailed relational specification,
the logical schema, of all the tables and constraints needed to satisfy the description
of the data in the conceptual data model. It is during this design activity that choices
are made as to which tables are most appropriate for representing the data in a
database. These choices must take into account various design criteria including, for
example, flexibility for change, control of duplication and how best to represent the
constraints. It is the tables defined by the logical schema that determine what data
are stored and how they may be manipulated in the database.
Database designers familiar with relational databases and SQL might be tempted to
go directly to implementation after they have produced a conceptual data model.
However, such a direct transformation of the relational representation to SQL tables
does not necessarily result in a database that has all the desirable properties:
completeness, integrity, flexibility, efficiency and usability. A good conceptual data
Page | 162
model is an essential first step towards a database with these properties, but that
does not mean that the direct transformation to SQL tables automatically produces a
good database. This first step will accurately represent the tables and constraints
needed to satisfy the conceptual data model description, and so will satisfy the
completeness and integrity requirements, but it may be inflexible or offer poor
usability. The first design is then flexed to improve the quality of the database
design. Flexing is a term that is intended to capture the simultaneous ideas of
bending something for a different purpose and weakening aspects of it as it is bent.
Figure 4.3 summarizes the iterative (repeated) steps involved in database design,
based on the overview given. Its main purpose is to distinguish the general issue of
what tables should be used from the detailed definition of the constituent parts of
each table – these tables are considered one at a time, although they are not
independent of each other. Each iteration that involves a revision of the tables would
lead to a new design; collectively they are usually referred to as second-cut designs,
even if the process iterates for more than a single loop.
First, for a given conceptual data model, it is not necessary that all the user
requirements it represents be satisfied by a single database. There can be various
reasons for the development of more than one database, such as the need for
independent operation in different locations or departmental control over “their” data.
However, if the collection ofdatabases contains duplicated data and users need to
access data in more than one database, then there are possible reasons that one
Page | 163
database can satisfy multiple requirements, or issues related to data replication and
distribution need to be examined.
It is not uncommon to find that a single design cannot simultaneously satisfy all the
properties of a good database. So it is important that the designer has prioritized
these properties (usually using information from the requirements specification); for
example, to decide if integrity is more important than efficiency and whether usability
is more important than flexibility in a given development.
At the end of our design stage, the logical schema will be specified by SQL data
definition language (DDL) statements, which describe the database that needs to be
implemented to meet the user requirements.
Implementation
Page | 164
relating to the users and user processes need to be addressed, and the
management activities associated with wider aspects of corporate data management
need to be supported. In keeping with the DBMS approach, we want as many of
these concerns as possible to be addressed within the DBMS. We look at some of
these concerns briefly now.
After the logical design has been created, we need our database to be created
according to the definitions we have produced. For an implementation with a
relational DBMS, this will probably involve the use of SQL to createtables and
constraints that satisfy the logical schema description and the choice of appropriate
storage schema (if the DBMS permits that level of control).
One way to achieve this is to write the appropriate SQL DDL statements into a file
that can be executed by a DBMSso that there is an independent record, a text file, of
the SQL statements defining the database. Another method isto work interactively
using a database tool like SQL Server Management Studio or Microsoft Access.
Whatevermechanism is used to implement the logical schema, the result is that a
database, with tables and constraints, isdefined but will contain no data for the user
processes.
Page | 165
After a database has been created, there are two ways of populating the tables –
either from existing data or through the use of the user applications developed for
the database.
For some tables, there may be existing data from another database or data files. For
example, in establishing a database for a hospital, you would expect that there are
already some records of all the staff that have to be included in the database. Data
might also be brought in from an outside agency (address lists are frequently brought
in from external companies) or produced during a large data entry task (converting
hard-copy manual records into computer files can be done by a data entry agency).
In such situations, the simplest approach to populate the database is to use the
import and export facilities found in the DBMS.
Facilities to import and export data in various standard formats are usually available
(these functions are also known in some systems as loading and unloading data).
Importing enables a file of data to be copied directly into a table. When data are held
in a file format that is not appropriate for using the import function, then it is
necessary to prepare an application program that reads in the old data, transforms
them as necessary and then inserts them into the database using SQL code
specifically produced for that purpose. The transfer of large quantities of existing
data into a database is referred to as a bulk load. Bulk loading of data may involve
very large quantities of data being loaded, one table at a time so you may find that
there are DBMS facilities to postpone constraint checking until the end of the bulk
loading.
Note: These are general guidelines that will assist in developing a strong basis for
the actual database design (the logical model).
2. Document all attributes that belong to each entity. Select candidate and
primary keys. Ensure that all non-key attributes for each entity are full-
functionally dependent on the primary key.
Page | 166
3. Develop an initial ER diagram and review it with appropriate personnel.
(Remember that this is an iterative process.)
In this chapter, we have studied the SDLC-Waterfall model. We also studied about
Database Life Cycle. Finally, we ended the discussion with database life cycle how
to work database process.
4.5 GLOSSARY
Page | 167
requirements gathering: a process during which the database designer interviews
the database user tounderstand the proposed system and obtain and document the
data and functional requirements
second-cut designs: the collection of iterations that each involves a revision of the
tables that lead to a newdesign
software development life cycle (SDLC): the series of steps involved in the
database development process
testing: compares the implemented system against the design documents and
requirements specification andproduces an acceptance report
waterfall model: shows the database development process as a strict sequence of
steps where the output ofone step is the input to the next
waterfall process: a means of identifying the tasks required for database
development, together with theinput and output for each activity (see waterfall
model)
1. D -feasibility study
Page | 168
2. C-Requirement analysis
3. D- A&B both
4. C - represents relationship
5. A- to map conceptual database model to an implementation model
Website:
https://www.sanfoundry.com/database-mcqs
4.7 ASSIGNMENT
Page | 169