Database Note I

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Database

A database is a collection of related data. By data, we mean known facts that can be recorded
and that have implicit meaning. For example, consider the names, telephone numbers, and
addresses of the people you know. Defining a database involves specifying the data types,
structures, and constraints of the data to be stored in the database. The database definition
or descriptive information is also stored by the DBMS in the form of a database catalog or
dictionary; it is called meta-data.
Database management system (DBMS):
A database management system (DBMS) is a collection of programs that enables users to
create and maintain a database. The DBMS is a general-purpose software system that
facilitates the processes of defining, constructing, manipulating, and sharing databases
among various users and applications.
Application Program
An application program accesses the database by sending queries or requests for data to the
DBMS.
Query
A query typically causes some data to be retrieved.
 Disadvantages of Traditional File System
A traditional file system has the following disadvantages.
1) Data Redundancy: Since each application has its own data file, the same data may have
to be recorded and stored in many files. For example, personal file and payroll file, both
contain data on employee name, designation etc. The result is unnecessary duplicate or
redundant data items. This redundancy requires additional or higher storage space, costs
extra time and money, and requires additional efforts to keep all files upto-date.

2) Data Inconsistency: Data redundancy leads to data inconsistency especially when data
is to be updated. Data inconsistency occurs due to the same data items that appear in more
than one file do not get updated simultaneously in each and every file. For example, an
employee is promoted from Clerk to Superintendent and the same is immediately updated
in the payroll file may not necessarily be updated in provident fund file. This results in two
different designations of an employee at the same time. Over the period of time, such
discrepencis degrade the quality of information contain in the data file that affects the
accuracy of reports.
3) Lack of Data Integration: Since independent data file exists, users face difficulty in
getting information on any ad hoc query that requires accessing the data stored in many files.
In such a case complicated programs have to be developed to retrieve data from every file or
the users have to manually collect the required information.
4) Program Dependence: The reports produced by the file processing system are program
dependent, which means if any change in the format or structure of data and records in the
file is to be made, the programs have to modified correspondingly. Also, a new program will
have to be developed to produce a new report.
5) Data Dependence: The Applications/programs in file processing system are data
dependent i.e., the file organization, its physical location and retrieval from the storage
media are dictated by the requirements of the particular application.
6) Limited Data Sharing: There is limited data sharing possibilities with the traditional file
system. Each application has its own private files and users have little choice to share the
data outside their own applications. Complex programs required to be written to obtain data
from several incompatible files.
7) Problem of Security: It is very difficult to enforce security checks and access rights in a
traditional file system, since application programs are added in an adhoc manner.

 Advantages of Database Systems


The Database Systems provide the following advantages over the traditional file system.

1) Controlled redundancy: In a traditional file system, each application program has its
own data, which causes duplication of common data items in more than one file. This
duplication/redundancy requires multiple updations for a single transaction and wastes a
lot of storage space. We cannot eliminate all redundancy due to technical reasons. But in a
database, this duplication can be carefully controlled, that means the database system is
aware of the redundancy and it assumes the responsibility for propagating updates.

2) Data consistency: The problem of updating multiple files in traditional file system leads
to inaccurate data as different files may contain different information of the same data item
at a given point of time. This causes incorrect or contradictory information to its users. In
database systems, this problem of inconsistent data is automatically solved by controlling
the redundancy.
3) Program data independence: The traditional file systems are generally data dependent,
which implies that the data organization and access strategies are dictated by the needs of
the specific application and the application programs are developed accordingly. However,
the database systems provide an independence between the file system and application
program, that allows for changes at one level of the data without affecting others. This
property of database systems allow to change data without changing the application
programs that process the data.
4) Sharing of data: In database systems, the data is centrally controlled and can be shared
by all authorized users. The sharing of data means not only the existing applications
programs can also share the data in the database but new application programs can be
developed to operate on the existing data. Furthermore, the requirements of the new
application programs may be satisfied without creating any new file.

5) Enforcement of standards: In database systems, data being stored at one central place,
standards can easily be enforced by the DBA. This ensures standardised data formats to
facilitate data transfers between systems. Applicable standards might include any or all of
the following—departmental, installation, organizational, industry, corporate, national or
international.

6) Improved data integrity: Data integrity means that the data contained in the database is
both accurate and consistent. The centralized control property allow adequate checks can be
incorporated to provide data integrity. One integrity check that should be incorporated in
the database is to ensure that if there is a reference to certain object, that object must exist.

7) Improved security: Database security means protecting the data contained in the
database from unauthorised users. The DBA ensures that proper access procedures are
followed, including proper authentical schemes for access to the DBMS and additional checks
before permitting access to sensitive data. The level of security could be different for various
types of data and operations.
8) Data access is efficient: The database system utilizes different sophisticated techniques
to access the stored data very efficiently.
9) Conflicting requirements can be balanced: The DBA resolves the conflicting
requirements of various users and applications by knowing the overall requirements of the
organization. The DBA can structure the system to provide an overall service that is best for
the organization.
10) Improved backup and recovery facility: Through its backup and recovery subsystem,
the database system provides the facilities for recovering from hardware or software
failures. The recovery subsystem of the database system ensures that the database is
restored to the state it was in before the program started executing, in case of system crash.
11) Minimal program maintenance: In a traditional file system, the application programs
with the description of data and the logic for accessing the data are built individually. Thus,
changes to the data formats or access methods results in the need to modify the application
programs. Therefore, high maintenance effort are required. These are reduced to minimal in
database systems due to independence of data and application programs.

12) Data quality is high: The quality of data in database systems are very high as compared
to traditional file systems. This is possible due to the presence of tools and processes in the
database system.
13) Good data accessibility and responsiveness: The database systems provide query
languages or report writers that allow the users to ask ad hoc queries to obtain the needed
information immediately, without the requirement to write application programs (as in case
of file system), that access the information from the database. This is possible due to
integration in database systems.
14) Concurrency control: The database systems are designed to manage simultaneous
(concurrent) access of the database by many users. They also prevents any loss of
information or loss of integrity due to these concurrent accesses.
15) Economical to scale: In database systems, the operational data of an organization is
stored in a central database. The application programs that work on this data can be built
with very less cost as compared to traditional file system. This reduces overall costs of
operation and management of the database that leads to an economical scaling.

16) Increased programmer productivity: The database system provides many standard
functions that the programmer would generally have to write in file system. The availability
of these functions allow the programmers to concentrate on the specific functionality
required by the users without worrying about the implementation details. This increases the
overall productivity of the programmer and also reduces the development time and cost.

 Disadvantages of Database Systems

In contrast to many advantages of the database systems, there are some disadvantages as
well. The disadvantages of a database system are as follows:
1) Complexity increases: The data structure may become more complex because of the
centralised database supporting many applications in an organization. This may lead to
difficulties in its management and may require professionals for management.
2) Requirement of more disk space: The wide functionality and more complexity increase
the size of DBMS. Thus, it requires much more space to store and run than the traditional
file system
3) Additional cost of hardware: The cost of database system's installation is much more.
It depends on environment and functionality, size of the hardware and maintenance costs
of hardware.
4) Cost of conversion: The cost of conversion from old file-system to new database system
is very high. In some cases the cost of conversion is so high that the cost of DBMS and extra
hardware becomes insignificant. It also includes the cost of training manpower and hiring
the specialized manpower to convert and run the system.
5) Need of additional and specialized manpower: Any organization having database
systems, need to be hire and train its manpower on regular basis to design and implement
databases and to provide database administration services.
6) Need for backup and recovery: For a database system to be accurate and available all
times, a procedure is required to be developed and used for providing backup copies to all
its users when damage occurs.
7) Organizational conflict: A centralised and shared database system requires a consensus
on data definitions and ownership as well as responsibilities for accurate data maintenance.
8) More installational and management cost: The big and complete database systems are
more costly. They require trained manpower to operate the system and has additional
annual maintenance and support costs.
 Three Tier Architecture:
A database system is a collection of interrelated files and a set of programs that allow users
to access and modify these files. A major purpose of a database system is to provide users
with an abstract view of the data. That is, the system hides certain details of how the data
are stored and maintained.
I) Data Abstraction :
For the system to be usable, it must retrieve data efficiently. The need for efficiency has led
designers to use complex data structures to represent data in the database. Since many
database-systems users are not computer trained, developers hide the complexity from
users through several levels of abstraction, to simplify users’ interactions with the system:
• Physical level: The lowest level
of abstraction describes how the
data are actually stored. The
physical level describes complex
low-level data structures in detail.
• Logical level: The next-higher
level of abstraction describes
what data are stored in the
database, and what relationships
exist among those data. The logical
level thus describes the entire
database in terms of a small
number of relatively simple
structures. Although implementation of the simple structures at the logical level may
involve complex physical-level structures, the user of the logical level does not need to be
aware of this complexity. Database administrators, who must decide what information to
keep in the database, use the logical level of abstraction.
•View level: The highest level of abstraction describes only part of the entire database.
Even though the logical level uses simpler structures, complexity remains because of the
variety of information stored in a large database. Many users of the database system do not
need all this information; instead, they need to access only a part of the database. The view
level of abstraction exists to simplify their interaction with the system. The system may
provide many views for the same database. Figure 1.1 shows the relationship among the
three levels of abstraction. An analogy to the concept of data types in programming
languages may clarify the distinction among levels of abstraction. Most high-level
programming languages support the notion of a record type.
The database system hides many of the lowest-level storage details from database
programmers. Database administrators, on the other hand, may be aware of certain details
of the physical organization of the data.
At the logical level, each such record is described by a type definition, as in the previous
code segment, and the interrelationship of these record types is defined as well.
Programmers using a programming language work at this level of abstraction. Similarly,
database administrators usually work at this level of abstraction.
Finally, at the view level, computer users see a set of application programs that hide details
of the data types. Similarly, at the view level, several views of the database are defined, and
database users see these views. In addition to hiding details of the logical level of the
database, the views also provide a security mechanism to prevent users from accessing
certain parts of the database. For example, tellers in a bank see only that part of the database
that has information on customer accounts; they cannot access information about salaries of
employees.
II) Instances
Databases change over time as information is inserted and deleted. The collection of
information stored in the database at a particular moment is called an instance of the
database.
III) Schemas:
A database schema is the skeleton structure that represents the logical view of the entire
database. It defines how the data is organized and how the relations among them are
associated. It formulates all the constraints that are to be applied on the data.
A database schema defines its entities and the relationship among them. It contains a
descriptive detail of the database, which can be depicted by means of schema diagrams. It’s
the database designers who design the schema to help programmers understand the
database and make it useful.
A database schema can be divided broadly into two categories −
• Physical Database Schema − This schema pertains to the actual storage of data and
its form of storage like files, indices, etc. It defines how the data will be stored in a secondary
storage.
• Logical Database Schema − This schema defines all the logical constraints that need
to be applied on the data stored. It defines tables, views, and integrity constraints.

 Keys:
A key allows us to identify a set of attributes that suffice to distinguish entities from each
other. Keys also help uniquely identify relationships, and thus distinguish relationships from
each other.
 superkey
A superkey is a set of one or more attributes that, taken collectively, allow us to identify
uniquely an entity in the entity set. For example, the customer-id attribute of the entity set
customer is sufficient to distinguish one customer entity from another. Thus, customer-id is
a superkey. Similarly, the combination of customer-name and customer-id is a superkey for
the entity set customer. The customer-name attribute of customer is not a superkey, because
several people might have the same name.
 candidate keys
The concept of a superkey is not sufficient for our purposes, since, as we saw, a superkey
may contain extraneous attributes. If K is a superkey, then so is any superset of K. We are
often interested in superkeys for which no proper subset is a superkey. Such minimal
superkeys are called candidate keys. It is possible that several distinct sets of attributes
could serve as a candidate key. A candidate key is a subset of a super key set where the key which
contains no redundant attribute is none other than a Candidate
 Primary key Key. In order to select the candidate keys from the set of super
key, we need to look at the super key set.
A Primary key denotes a candidate key that is chosen by the database designer as the
principal means of identifying entities within an entity set.
A key (primary, candidate, and super) is a property of the entity set, rather than of the
individual entities. Any two individual entities in the set are prohibited from having the same
value on the key attributes at the same time.

 Entity Integrity
The entity integrity constraint states that no primary key value can be null. This is because
the primary key value is used to identify individual tuples in a relation. Having null values
for the primary key implies that we cannot identify some tuples.
For example, if two or more tuples had null for their primary keys, we might not be able to
distinguish them if we tried to reference them from other relations. Key constraints and
entity integrity constraints are specified on individual relations.
 Referential Integrity and Foreign Keys:
The referential integrity constraint is specified between two relations and is used to
maintain the consistency among tuples in the two relations.
Informally, the referential integrity constraint states that a tuple in one relation that
refers to another relation must refer to an existing tuple in that relation.
To define referential integrity more formally, we first define the concept of a foreign key.
 Data Independence
The three-schema architecture can be used to further explain the concept of data
independence, which can be defined as the capacity to change the schema at one level of a
database system without having to change the schema at the next higher level. We can define
two types of data independence:
1. Logical data independence is the capacity to change the conceptual schema without
having to change external schernas or application programs. We may change the
conceptual schema to expand the database (by adding a record type or data item), to
change constraints, or to reduce the database (by removing a record type or data item).
In the last case, external schemas that refer only to the remaining data should not be
affected.
For example, the external schema of Figure l.4a should not be affected by changing the
GRADE_REPORT file shown in Figure 1.2 into the one shown in Figure 1.5a. Only the view
definition and the mappings need be changed in a DBMS that supports logical data
independence. After the conceptual schema undergoes a logical reorganization, application
programs that reference the external schema constructs must work as before. Changes to
constraints can be applied to the conceptual schema without affecting the external schernas
or application programs.
2. Physical data independence is the capacity to change the internal schema without
having to change the conceptual schema. Hence, the external schemas need not be
changed as well. Changes to the internal schema may be needed because some physical
files had to be reorganized-for example, by creating additional access structures-to
improve the performance of retrieval or update. If the same data as before remains in the
database, we should not have to change the conceptual schema.
For example, providing an access path to improve retrieval speed of SECTION records by
Semester and Year should not require a query such as "list all sections offered in fall 1998"
to be changed, although the query would be executed more efficiently by the DBMS by
utilizing the new access path.
 Mapping Cardinalities
Cardinality defines the number of entities in one entity set, which can be associated with
the number of entities of other set via relationship set.
 One-to-one − One entity from entity set A can be associated with at most one entity
of entity set B and vice versa.
 One-to-many − One entity from entity set A can be associated with more than one
entities of entity set B however an entity from entity set B, can be associated with at
most one entity.
 Many-to-one − More than one entities from entity set A can be associated with at
most one entity of entity set B, however an entity from entity set B can be associated
with more than one entity from entity set A.
 Many-to-many − One entity from A can be associated with more than one entity from
B and vice versa.
 Binary Relationship and Cardinality
A relationship where two entities are participating is called a binary relationship.
Cardinality is the number of instance of an entity from a relation that can be associated with
the relation.
 One-to-one − When only one instance
of an entity is associated with the
relationship, it is marked as '1:1'. The
following image reflects that only one
instance of each entity should be
associated with the relationship. It depicts one-to-one relationship.

 One-to-many − When more than one


instance of an entity is associated
with a relationship, it is marked as
'1:N'. The following image reflects
that only one instance of entity on the
left and more than one instance of an entity on the right can be associated with the
relationship. It depicts one-to-many relationship.
 Many-to-one − When more than one
instance of entity is associated with
the relationship, it is marked as
'N:1'. The following image reflects
that more than one instance of an
entity on the left and only one
instance of an entity on the right can be associated with the relationship. It depicts
many-to-one relationship.
 Many-to-many − The following image
reflects that more than one instance
of an entity on the left and more than
one instance of an entity on the right
can be associated with the
relationship. It depicts many-to-many relationship.
 Participation Constraints
 Total Participation − Each entity is
involved in the relationship. Total
participation is represented by double
lines.
 Partial participation − Not all entities are involved in the relationship. Partial
participation is represented by single lines.
 Entity-Relationship Model
Entity-Relationship (ER) Model is based on the notion of real-world entities and
relationships among them. While formulating real-world scenario into the database model,
the ER Model creates entity set, relationship set, general attributes and constraints.
ER Model is best used for the conceptual design of a database.
ER Model is based on
 Entities and their attributes.
 Relationships among entities.
These concepts are explained below.
 Entity − An entity in an ER Model is a real-world entity having properties
called attributes. Every attribute is defined by its set of values called domain. For
example, in a school database, a student is considered as an entity. Student has
various attributes like name, age, class, etc.
 Relationship − The logical association among entities is called relationship.
Relationships are mapped with entities in various ways. Mapping cardinalities
define the number of association between two entities.
Mapping cardinalities −

o one to one
o one to many
o many to one
o many to many

 Specialization

Specialization is the process of defining a set of subclasses of an entity type; this entity type
is called the superclass of the specialization. The set of subclasses that forms a specialization
is defined on the basis of some distinguishing characteristic of the entities in the superclass.

The specialization process allows us to do the following:

■ Define a set of subclasses of an entity type

■ Establish additional specific attributes with each subclass

■ Establish additional specific relationship types between each subclass and other
entity types or other subclasses
 Generalization

A reverse process of abstraction in which we suppress the differences among several entity
types, identify their common features, and generalize them into a single superclass of which
the original entity types are special subclasses.
Relational Model

The most popular data model in


DBMS is the Relational Model. It is
more scientific a model than
others. This model is based on
first-order predicate logic and
defines a table as an n-ary
relation.

The main highlights of this model


are

 Data is stored in tables called relations.


 Relations can be normalized.
 In normalized relations, values saved are atomic values.
 Each row in a relation contains a unique value.
 Each column in a relation contains values from a same domain.
Relational data model is the primary data model, which is used widely around the world for
data storage and processing. This model is simple and it has all the properties and
capabilities required to process data with storage efficiency.
Tables − In relational data model, relations are saved in the format of Tables. This format
stores the relation among entities. A table has rows and columns, where rows represents
records and columns represent the attributes.
Tuple − A single row of a table, which contains a single record for that relation is called a
tuple.
Relation instance − A finite set of tuples in the relational database system represents
relation instance. Relation instances do not have duplicate tuples.
Relation schema − A relation schema describes the relation name (table name), attributes,
and their names.
Relation key − Each row has one or more attributes, known as relation key, which can
identify the row in the relation (table) uniquely.
Attribute domain − Every attribute has some pre-defined value scope, known as attribute
domain.
 Constraints
Every relation has some conditions that must hold for it to be a valid relation. These
conditions are called Relational Integrity Constraints. There are three main integrity
constraints −
 Key constraints
 Domain constraints
 Referential integrity constraints
 Key Constraints
There must be at least one minimal subset of attributes in the relation, which can identify a
tuple uniquely. This minimal subset of attributes is called key for that relation. If there are
more than one such minimal subsets, these are called candidate keys.
Key constraints force that −
 in a relation with a key attribute, no two tuples can have identical values for key
attributes.
 a key attribute can not have NULL values.
Key constraints are also referred to as Entity Constraints.
 Domain Constraints
Attributes have specific values in real-world scenario. For example, age can only be a
positive integer. The same constraints have been tried to employ on the attributes of a
relation. Every attribute is bound to have a specific range of values. For example, age cannot
be less than zero and telephone numbers cannot contain a digit outside 0-9.
 Referential integrity Constraints
Referential integrity constraints work on the concept of Foreign Keys. A foreign key is a key
attribute of a relation that can be referred in other relation.
Referential integrity constraint states that if a relation refers to a key attribute of a different
or same relation, then that key element must exist.
 Relational algebra
Relational database systems are expected to be equipped with a query language that can
assist its users to query the database instances. There are two kinds of query languages −
relational algebra and relational calculus. Relational algebra is a procedural query language,
which takes instances of relations as input and yields instances of relations as output. It uses
operators to perform queries. An operator can be either unary or binary. They accept
relations as their input and yield relations as their output. Relational algebra is performed
recursively on a relation and intermediate results are also considered relations.
The fundamental operations of relational algebra are as follows −

 Select
 Project
 Union
 Set different
 Cartesian product
 Rename
We will discuss all these operations in the following sections.
 Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation : σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic
formula which may use connectors like and, or, and not. These terms may use relational
operators like − =, ≠, ≥, < , >, ≤.
For example −

σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.

σsubject = "database" and price = "450"(Books)


Output − Selects tuples from books where subject is 'database' and 'price' is 450.

σsubject = "database" and price = "450" or year > "2010"(Books)


Output − Selects tuples from books where subject is 'database' and 'price' is 450 or
those books published after 2010.
 Project Operation ( ∏ )
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −

∏subject, author (Books)


Selects and projects columns named as subject and author from the relation Books.
Union Operation (∪)
It performs binary union between two given relations and is defined as −

r ∪ s = { t | t ∈ r or t ∈ s}
Notation : r ∪ s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −

 r, and s must have the same number of attributes.


 Attribute domains must be compatible.
 Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article
or both.
 Set Difference (−)
The result of set difference operation is tuples, which are present in one relation but are
not in the second relation.
Notation : r − s
Finds all the tuples that are present in r but not in s.

∏ author (Books) − ∏ author (Articles)


Output − Provides the name of authors who have written books but not articles.
 Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}

σauthor = 'DBMS'(Books Χ Articles)


Output − Yields a relation, which shows all the books and articles written by DBMS.
 Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The rename
operation allows us to rename the output relation. 'rename' operation is denoted with
small Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.

You might also like