0% found this document useful (0 votes)
44 views

Dbms

The document discusses the basics of databases and database management systems. It defines what a database is, the components of a DBMS, data models, and roles in database environments. It also covers traditional filing systems compared to database approaches, functions of DBMS, and advantages and disadvantages of using a DBMS.

Uploaded by

Gk Pradeep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Dbms

The document discusses the basics of databases and database management systems. It defines what a database is, the components of a DBMS, data models, and roles in database environments. It also covers traditional filing systems compared to database approaches, functions of DBMS, and advantages and disadvantages of using a DBMS.

Uploaded by

Gk Pradeep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 93

1) Introduction - Basics

DataBase: A database is a collection of interrelated information stored in a database server;


these data will be stored in the form of tables

Database management system: The term 'database management system', often abbreviated
to DBMS, refers to a software system used to create and manage databases. The software of
such systems is complex, consisting of a number of different components.
System catalogue/Data dictionary: The description of the data in the database management
system.

2) The traditional filing system


The traditional filing system (TFS) is a method of storing and arranging computer files and the
information in the file (data). Basically it organizes these files into a database for the storage,
organization, manipulation, and retrieval by the computer’s operating system.

File-based systems were an early attempt to computerize the manual filing system. File-based
system is a collection of application programs that perform services for the end-users, such as
updating, insertion, deletion adding new files to database etc. Each program defines and
manages its data.

Disadvantages of traditional file based system

No need of external storage

Provides less security.

No need of a highly technical person to handle the database.

Redundancy is more.

Processing speed is high as compared to DBMS

Less integrity.

High complexity in updating of database.

1
3) The database approach
The database approach is an improvement on the shared file solution as the use of a database
management system (DBMS) provides facilities for querying, data security and integrity, and
allows simultaneous access to data by a number of different users.
One of the benefits of the database approach is that the problem of physical data dependence
is resolved; this means that the underlying structure of a data file can be changed without the
application programs needing amendment. This is achieved by a hierarchy of levels of data
specification. Each such specification of data in a database system is called a schema.

Benefits of the database approach

The benefits of the database approach are as follows:


 Ease of application development: The programmer is no longer burdened with
designing, building and maintaining master files.
 Minimal data redundancy: All data files are integrated into a composite data structure. In
practice, not all redundancy is eliminated, but at least the redundancy is controlled. Thus
inconsistency is reduced.
 Enforcement of standards: The database administrator can define standards for names,
etc.
 Data can be shared. New applications can use existing data definitions.
 Logical data independence: Data can be viewed in different ways by different users.

2
4) ANSI/SPARC three-level architecture

ANSI = American National Standards Institute


SPARC = Standards Planning and Requirements Committee
The external schema

The external schemas describe the database as it is seen by the user, and the user
applications.
The external schema allows the application programs to see as much of the data as they
require, while excluding other items that are not relevant to that application. In this way, the
external schema provides a view of the data that corresponds to the nature of each task.
.
The conceptual schema

The conceptual schema describes the universe of interest to the users of the database system.
At the level of the conceptual schema we are concerned with the data itself, rather than
storage or the way data is physically accessed on disk. The definition of storage and access
details is the preserve of the internal schema.
The internal schema

which contains definitions of the way in which data is physically stored. The interface between
the internal schema and the conceptual schema identifies how an element in the conceptual
schema is stored, and how it may be accessed.

3
5) Components of a DBMS
The major components of a DBMS are as follows:
DBMS engine :

The engine is the central component of a DBMS. This component provides access to the
database and coordinates all of the functional elements of the DBMS.

User interface subsystem:

The interface subsystem provides facilities for users and applications to access the various
components of the DBMS

Backup and recovery subsystem

Periodically making backup copies of the database, and recovering the database in the event
of some type of failure
Data integrity management subsystem

The data integrity management subsystem provides facilities for managing the integrity of data
in the database and the integrity of metadata in the dictionary
Security management subsystem

The security management subsystem provides facilities to protect and control access to the
database and data dictionary.
Data dictionary subsystem

The data dictionary subsystem is used to store data about many aspects of how the DBMS
works
Performance management subsystem

The performance management subsystem provides facilities to optimise (or at least improve)
DBMS performance

6)Advantage and Disadvantages of DBMS

Advantage of DBMS

Improved data sharing:

 The DBMS helps create an environment in which end users have better access to more
and better-managed data.

4
 Such access makes it possible for end users to respond quickly to changes in their
environment.

Improved data security:

 A DBMS provides a framework for better enforcement of data privacy and security
policies.

Better data integration:

 Wider access to well-managed data promotes an integrated view of the organization’s


operations and a clearer view of the big picture.

Minimized data inconsistency:

 Data inconsistency exists when different versions of the same data appear in different
places.

Improved data access:

 The DBMS makes it possible to produce quick answers to ad hoc queries.


 The DBMS sends back an answer (called the query result set) to the application.

Improved decision making:

 Better-managed data and improved data access make it possible to generate better-
quality information, on which better decisions are based.
 Increased end-user productivity

Disadvantage of DBMS

Increased costs:

 The cost of maintaining the hardware, software, and personnel required to operate and
manage a database system can be substantial.

Management complexity:

 Database systems interface with many different technologies and have a significant
impact on a company’s resources and culture.

5
Maintaining currency:

 Because database technology advances rapidly, personnel training costs tend to be


significant. Vendor dependence.

Frequent upgrade/replacement cycles:

DBMS vendors frequently upgrade their products by adding new functionality. Such new
features often come bundled in new upgrade versions of the software

7) Functions of a DBMS
A DBMS makes it possible for users to create, edit and update data in database files. Once
created, the DBMS makes it possible to store and retrieve data from those database files.
More specifically, a DBMS provides the following functions:

 Concurrency: concurrent access (meaning 'at the same time') to the same database by
multiple users
 Security: security rules to determine access rights of users
 Backup and recovery: processes to back-up the data regularly and recover data if a
problem occurs
 Integrity: database structure and rules improve the integrity of the data
 Data descriptions: a data dictionary provides a description of the data

8) Roles in Database Environment


people that participate in DBMS environment are

data administrator : ( DA )responsible for management of data resources e,g planning ,

policies, standards., procedures, conceptual/logical db design..

database administrator : ( DBA ) ….responsible for physical realization of db e,g physical

db design, performance, implementation, security, integrity, maintenance, users/ groups

database designer:

a. logical designer : identify data (entities & attributes), relationships b/w data,

constraints on the data

b. physical designer : mapping the logical data models in to a set of tables & integrity
constraints, select separate storage structures & access methods, designing security
measures.

6
application Programmer : responsible for developing application programs. uses a 3rd or 4th
generation programming languages

End Users:

a . Naive users : unaware of DBMS.

b . Sophisticated users : familiar wth the str & facilities of DBMS.

9) Data Models

a) Object Based Data models

There are two types of object based data Models – Entity Relationship Model and Object
oriented data model.

 Entity Relationship Model

ER model is a graphical representation of real world objects with their attributes and
relationship. It makes the system easily understandable.

Entities or real world objects are represented in a rectangular box. Their attributes are
represented in ovals. All the entities are mapped using diamonds

 Object oriented model

This data model is another method of representing real world objects. It considers each object
in the world as objects and isolates it from each other. It groups its related functionalities
together and allows inheriting its functionality to other related sub-groups.

b) Physical Data Model

Physical data model represent the model where it describes how data are stored in computer
memory.

7
It is represented as UML diagram along with table and its columns. Primary key is represented
at the top. The relationship between the tables is represented by interconnected arrows from
table to table.

c) Record based data model

There are 3 types of record based data models defined so far- Hierarchical, Network and
Relational data models

Hierarchial model Network model

Below table provides the comparison among the three models

Hierarchical Data Model Network Data Models Relational Data Models


Supports One-Many Supports both one to many Supports both one to many
Relationship and Many to Many and Many to Many
relationship relationship

8
Because of single parent- It establishes the It provides SQL, which
child relationship, difficult to relationship between most makes the access to the
navigate through the child of the objects, hence easy data simpler and quicker.
to access compared to
hierarchical model
Flexibility among the Because of the mapping Primary and foreign key
different object is restricted among the sub level tables, constraint makes the
to the child. flexibility is more flexibility much simpler than
other models.
Based on the physical Based on the physical Based on the logical data
storage details storage details view

10) Relational Model concept- Terminology


Relational model can represent as a table with columns and rows. Each row is known as a tuple. Each
table of the column has a name or attribute.

Domain: It contains a set of atomic values that an attribute can take.

Attribute: It contains the name of a column in a particular table. Each attribute Ai must have a domain,
dom(Ai)

Relational instance: In the relational database system, the relational instance is represented by a
finite set of tuples. Relation instances do not have duplicate tuples.

Relational schema: A relational schema contains the name of the relation and name of all columns or
attributes.

Relational key: In the relational key, each row has one or more attributes. It can identify the row in the
relation uniquely.

Properties of Relations

Name of the relation is distinct from all other relations.

o Each relation cell contains exactly one atomic (single) value


o Each attribute contains a distinct name
o Attribute domain has no significance
o tuple has no duplicate value
o Order of tuple can have a different sequence

Example: STUDENT Relation

9
NAME ROLL_NO PHONE_NO ADDRESS AGE

Ram 14795 7305758992 Noida 24

Shyam 12839 9026288936 Delhi 35

Laxman 33289 8583287182 Gurugram 20

Mahesh 27857 7086819134 Ghaziabad 27

Ganesh 17282 9028 9i3988 Delhi 40

o In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are the attributes.
o The instance of schema STUDENT has 5 tuples.

11) Integrity Constraints


o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database

Types of Integrity Constraint

1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc. The value
of the attribute must be available in the corresponding domain.

10
2. Entity integrity constraints
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in relation and if the
primary key has a null value, then we can't identify those rows.
o A table can contain a null value other than the primary key field.

3. Referential Integrity Constraints


o A referential integrity constraint is specified between two tables.
o In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of
Table 2, then every value of the Foreign Key in Table 1 must be null or be available in Table 2.

11
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key. A primary
key can contain a unique and null value in the relational table.

12) Relational Algebra


Relational algebra is a procedural query language. It gives a step by step process to obtain the result of
the query. It uses operators to perform queries.

Types of Relational operation


1. Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).

Notation: σ p(r)

Where:

σ is used for selection prediction


r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR and NOT. These
relational can use as relational operators like =, ≠, ≥, <, >, ≤.

2. Project Operation:
o This operation shows the list of those attributes that we wish to appear in the result. Rest of the
attributes are eliminated from the table.
o It is denoted by ∏.

Notation: ∏ A1, A2, An (r)

12
Where

A1, A2, A3 is used as an attribute name of relation r.

3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the tuples that are
either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.

Notation: R ∪ S

A union operation must hold the following condition:

o R and S must have the attribute of the same number.


o Duplicate tuples are eliminated automatically.

4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples that are
in both R & S.
o It is denoted by intersection ∩.

Notation: R ∩ S

5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples that are
in R but not in S.
o It is denoted by intersection minus (-).

Notation: R - S

6. Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the other
table. It is also known as a cross product.
o It is denoted by X.

Notation: E X D

13
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).

Example: We can use the rename operator to rename STUDENT relation to STUDENT1.

ρ(STUDENT1, STUDENT)

13) Join Operations:

A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.

Types of Join operations:

There are 3 types of joins. They are

1. Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are equal on their
common attribute names.
o It is denoted by ⋈.

Example: Let's use the above EMPLOYEE table and SALARY table:

Input:

1. ∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)

Output:

EMP_NAME SALARY

Stephan 50000

Jack 30000

Harry 25000

2. Outer Join:

The outer join operation is an extension of the join operation. It is used to deal with missing
information.
14
EMPLOYEE

EMP_NAME STREET CITY

Ram Civil line Mumbai

Shyam Park street Kolkata

Ravi M.G. Street Delhi

Hari Nehru nagar Hyderabad

FACT_WORKERS

EMP_NAME BRANCH SALARY

Ram Infosys 10000

Shyam Wipro 20000

Kuber HCL 30000

Hari TCS 50000

Input:

1. (EMPLOYEE ⋈ FACT_WORKERS)

Output:

15
EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru nagar Hyderabad TCS 50000

An outer join is basically of three types:

Left outer join

a. Right outer join


b. Full outer join

a. Left outer join:


o Left outer join contains the set of tuples of all combinations in R and S that are equal on
their common attribute names.
o In the left outer join, tuples in R have no matching tuples in S.
o It is denoted by ⟕.

Example: Using the above EMPLOYEE table and FACT_WORKERS table

Input:

EMPLOYEE ⟕ FACT_WORKERS

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

16
Hari Nehru Hyderabad TCS 50000
street

Ravi M.G. Street Delhi NULL NULL

b. Right outer join:


o Right outer join contains the set of tuples of all combinations in R and S that are equal
on their common attribute names.
o In right outer join, tuples in S have no matching tuples in R.
o It is denoted by ⟖.

Example: Using the above EMPLOYEE table and FACT_WORKERS Relation

Input:EMPLOYEE ⟖ FACT_WORKERS Output:

EMP_NAME BRANCH SALARY STREET CITY

Ram Infosys 10000 Civil line Mumbai

Shyam Wipro 20000 Park street Kolkata

Hari TCS 50000 Nehru street Hyderabad

Kuber HCL 30000 NULL NULL

3. Equi join: It is also known as an inner join. It is the most common join. It is based on matched
data as per the equality condition. The equi join uses the comparison operator(=).
Example:CUSTOMER RELATION

CLASS_ID NAME

17
1 John

2 Harry

3 Jackson

PRODUCT

PRODUCT_ID CITY

1 Delhi

2 Mumbai

3 Noida

Input: CUSTOMER ⋈ PRODUCT Output:

CLASS_ID NAME PRODUCT_ID CITY

1 John 1 Delhi

2 Harry 2 Mumbai

3 Harry 3 Noida

Aggregation is a process in which a single entity alone is not able to make sense in a
relationship so the relationship of two entities acts as one entity.

18
Unit-II
19
Structured Query Language (SQL)

1 Introduction
o SQL stands for Structured Query Language. It is used for storing and managing
data in relational database management system (RDMS).
o It is a standard language for Relational Database System. It enables a user to
create, read, update and delete relational databases and tables.
o All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL as
their standard database language.
o SQL allows users to query the database in a number of ways, using English-like
statements.

Rules:
SQL follows the following rules:

o Structure query language is not case sensitive. Generally, keywords of SQL are
written in uppercase.
o Statements of SQL are dependent on text lines. We can use a single SQL statement
on one or multiple text line.
o Using the SQL statements, you can perform most of the actions in a database.
o SQL depends on tuple relational calculus and relational algebra.

SQL process:
o When an SQL command is executing for any RDBMS, then the system figure out the
best way to carry out the request and the SQL engine determines that how to
interpret the task.
o In the process, various components are included. These components can be
optimization Engine, Query engine, Query dispatcher, classic, etc.
o All the non-SQL queries are handled by the classic query engine, but SQL query
engine won't handle logical files.

20
2) Characteristics of SQL
o SQL is easy to learn.
o SQL is used to access data from relational database management systems.
o SQL can execute queries against the database.
o SQL is used to describe the data.
o SQL is used to define the data in the database and manipulate it when needed.
o SQL is used to create and drop the database and table.
o SQL is used to create a view, stored procedure, function in a database.
o SQL allows users to set permissions on tables, procedures, and views.

3) SQL Datatype
o SQL Datatype is used to define the values that a column can contain.
o Every column is required to have a name and data type in the database table.

The subtypes are given below:

21
Data type Description

char It has a maximum length of 8000 characters. It contains Fixed-


length non-unicode characters.

Varchar It has a maximum length of 8000 characters. It contains variable-


length non-unicode characters.

It is similar to varchar but it save memory space


Varchar2

date It is used to store the year, month, and days value.

time It is used to store the hour, minute, and second values.

int It is used to specify an integer value.

numeric It is used to specify a numeric value.

decimal It specifies a numeric value that can have a decimal number.

4) SQL Commands
o SQL commands are instructions. It is used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the
table, modify the table, set permission for users.

22
Types of SQL Commands

There are five types of SQL commands: DDL, DML, DCL.

i) Data Definition Language (DDL)


o DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
o All the command of DDL are auto-committed that means it permanently save all the
changes in the database.

Here are some commands that come under DDL:

o CREATE
o ALTER
o DROP

a. CREATE It is used to create a new table in the database.

Syntax: CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);

Example:

CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);

b. DROP: It is used to delete both the structure and record stored in the table.

Syntax: DROP TABLE ;

Example: DROP TABLE EMPLOYEE;

c. ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.

Syntax:

To add a new column in the table

ALTER TABLE table_name ADD column_name COLUMN-definition;

To modify existing column in the table:

23
ALTER TABLE MODIFY(COLUMN DEFINITION....);

EXAMPLE

ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));


ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));

ii) Data Manipulation Language


o DML commands are used to modify the database. It is responsible for all form of
changes in the database.

Here are some commands that come under DML:

o INSERT
o UPDATE
o DELETE
o SELECT

a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of
a table.

Syntax:

INSERT INTO TABLE_NAME (col1, col2,.... col N) VALUES (value1, value2, .... valueN);

Or

INSERT INTO TABLE_NAME VALUES (value1, value2, .... valueN);

Example: NSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");

b. UPDATE: This command is used to update or modify the value of a column in the
table.

Syntax:

UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE


CONDITION]

Example:UPDATE students SET User_Name = 'Sonoo' WHERE Student_Id = '3'

c. DELETE: It is used to remove one or more row from a table.


24
Syntax: DELETE FROM table_name [WHERE condition];

Example: DELETE FROM javatpoint WHERE Author="Sonoo";

d. SELECT: This is the same as the projection operation of relational algebra. It is used to
select the attribute based on the condition described by WHERE clause.

Syntax:

SELECT expressions FROM TABLES WHERE conditions;

Example: SELECT emp_name FROM employee WHERE age > 20;

iii). Data Control Language

DCL commands are used to grant and take back authority from any database user.

Here are some commands that come under DCL:

o Grant
o Revoke

o COMMIT
o ROLLBACK

a. Grant: It is used to give user access privileges to a database.

Example

GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;

b. Revoke: It is used to take back permissions from the user.

Example

REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

c. Commit: Commit command is used to save all the transactions to the database.

Syntax: COMMIT;

Example:

25
DELETE FROM CUSTOMERS WHERE AGE = 25;

Output :COMMIT;

d. Rollback: Rollback command is used to undo transactions that have not already been
saved to the database.

Syntax: ROLLBACK;

Example: DELETE FROM CUSTOMERS WHERE AGE = 25;

Output: ROLLBACK;

5) SQL Operators
There are various types of SQL operator: They are

SQL Arithmetic Operators:

There are 5 types of arithmetic operators .

They are + , - , * , / , % (remainder after division operator)

SQL Comparison Operators:

There are 5 types of comparison operators.They are

 == (equall to)
 != (not equall to)
 > (greater than)
 >= (greater than or equall to )
 < (less than )
 <= (less than or euqall to)

SQL Logical Operators

There is the list of logical operator used in SQL:

26
Operator Description

AND It allows the existence of multiple conditions in an SQL statement.

BETWEEN It is used to search for values that are within a set of values.

IN It compares a value to that specified list value.

NOT It reverses the meaning of any logical operator.

OR It combines multiple conditions in SQL statements.

EXISTS It is used to search for the presence of a row in a specified table.

LIKE It compares a value to similar values using wildcard operator.

NOT IN It is used display search records are not in lit

ISNULL Used to check whether an attribute value is null

ALL It compares a value to all values in another value set.

ANY It compares the values in the list according to the condition.

NOT EXITS It is opposite of EXIST

6) Views in SQL
o Views in SQL are considered as a virtual table. A view also contains rows and
columns.
o To create the view, we can select the fields from one or more tables present in the
database.
o A view can either have specific rows based on certain condition or all the rows of a
table.

27
Student_Detail

STU_ID NAME ADDRESS

1 Stephan Delhi

2 Kathrin Noida

3 David Ghaziabad

4 Alina Gurugram

Creating view

A view can be created using the CREATE VIEW statement. We can create a view from a
single table or multiple tables.

Syntax:

CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name


WHERE condition;

Creating View

In this example, we create a View named DetailsView from the table Student_Detail.

Query:

CREATE VIEW DetailsView AS SELECT NAME, ADDRESS FROM Student_Details


WHERE STU_ID < 4;

Just like table query, we can query the view to view the data.

SELECT * FROM DetailsView;

28
Output:

NAME ADDRESS

Stephan Delhi

Kathrin Noida

David Ghaziabad

Deleting View

A view can be deleted using the Drop View statement.

Syntax: DROP VIEW view_name;

Example: If we want to delete the View MarksView, we can do this as:

DROP VIEW MarksView;

Advantages and Disadvantages of Views

Advantages:
1. Views don't store data in a physical location.
2. The view can be used to hide some of the columns from the table.
3. Views can provide Access Restriction, since data insertion, update and deletion
is not possible with the view.

Disadvantages:
1. When a table is dropped, associated view become irrelevant.
2. Since the view is created when a query requesting data from view is triggered,
its a bit slow.
3. When views are created for large tables, it occupies more memory.

29
7) SQL Index
o Indexes are special lookup tables. It is used to retrieve data from the database very
fast.
o An Index is used to speed up select queries and where clauses. But it shows down
the data input with insert and update statements. Indexes can be created or
dropped without affecting the data.
o An index in a database is just like an index in the back of a book.

Create Index:

It is used to create an index on a table. It allows duplicate value.

Syntax: CREATE INDEX index_name ON table_name (column1, column2, ...);

Example: CREATE INDEX idx_name ON Persons (LastName, FirstName);

Drop Index :

It is used to delete an index in a table.

Syntax: DROP INDEX index_name;

Example :DROP INDEX websites_idx;

8) SQL Sub Query


A Subquery is a query within another SQL query and embedded within the WHERE clause.

Rules:

o A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.
o You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
o A subquery is a query within another query. The outer query is known as the main
query, and the inner query is known as a subquery.
o Subqueries are on the right side of the comparison operator.
o A subquery is enclosed in parentheses.

30
o In the Subquery, ORDER BY command cannot be used. But GROUP BY command
can be used to perform the same function as ORDER BY command.

Syntax

SELECT column_name FROM table_name WHERE column_name expression operator


( SELECT column_name from table_name WHERE ... );

Example: Consider the EMPLOYEE table have the following records:

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

The subquery with a SELECT statement will be:

SELECT * FROM EMPLOYEE WHERE ID IN (SELECT ID FROM EMPLOYEE


WHERE SALARY > 4500);

Output:

ID NAME AGE ADDRESS SALARY

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

7 Jackson 25 Mizoram 10000.00

31
9) Aggregate Functions
o SQL aggregation function is used to perform the calculations on multiple rows of a
single column of a table. It returns a single value.
o It is also used to summarize the data.

Types of SQL Aggregation Function :

There are 5 types of aggregate functions . They are

COUNT( )
o COUNT function is used to Count the number of rows in a database table. It can
work on both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.

Syntax: COUNT(*) or COUNT( columnname)

Example: select count(* ) from PRODUCT_MAST;

SUM( )

Sum function is used to calculate the sum of all selected columns. It works on numeric
fields only.

Syntax: SUM()

Example: SELECT SUM(COST) FROM PRODUCT_MAST;

AVG ( )

The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.

Syntax: AVG()

Example: SELECT AVG(COST) FROM PRODUCT_MAST;

MAX( ):

MAX function is used to find the maximum value of a certain column. This function
determines the largest value of all selected values of a column.

32
Syntax: MAX()

Example: SELECT MAX(RATE) FROM PRODUCT_MAST;

MIN( ):

MIN function is used to find the minimum value of a certain column. This function
determines the smallest value of all selected values of a column.

Syntax: MIN()

Example: SELECT MIN(RATE) FROM PRODUCT_MAST;

10) SQL Clauses


The following are the various SQL clauses:

GROUP BY:

o SQL GROUP BY statement is used to arrange identical data into groups. The GROUP
BY statement is used with the SQL SELECT statement.
o The GROUP BY statement follows the WHERE clause in a SELECT statement and
precedes the ORDER BY clause.
o The GROUP BY statement is used with aggregation function.

Syntax

SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY;

33
HAVING

o HAVING clause is used to specify a search condition for a group or an aggregate.


o Having is used in a GROUP BY clause. If you are not using GROUP BY clause then
you can use HAVING function like a WHERE clause.

Syntax:

SELECT column1, column2 FROM table_name WHERE conditions


GROUP BY column1, column2 HAVING conditions

Example:

SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY


HAVING COUNT(*)>2;

ORDER BY:

o The ORDER BY clause sorts the result-set in ascending or descending order.


o It sorts the records in ascending order by default. DESC keyword is used to sort the
records in descending order.

Syntax:

SELECT column1, column2 FROM table_name WHERE condition


ORDER BY column1, column2... ASC|DESC;

Where

ASC: It is used to sort the result set in ascending order by expression.

DESC: It sorts the result set in descending order by expression.

SELECT * FROM CUSTOMER ORDER BY NAME;

SELECT * FROM CUSTOMER ORDER BY NAME DESC;

34
11) Integrity Constraints
Constraints are the rules that we can apply on the type of data in a table.
The available constraints in SQL are:

i) NOT NULL

If we specify a field in a table to be NOT NULL. Then the field will never accept null value.

CREATE TABLE Student(ID number(6) NOT NULL,NAME varchar(10),ADDRESS


varchar(20));

ii) UNIQUE

This constraint helps to uniquely identify each row in the table. i.e. for a particular
column, all the rows should have unique values.

CREATE TABLE Student(ID number(6)UNIQUE,NAME varchar(10), ADDRESS varchar(20));

iii) PRIMARY KEY

Primary Key is a field which uniquely identifies each row in the table.

i.e combination of NOT NULL and UNIQUE constraints..

CREATE TABLE Student(ID Number(6) Primary key, NAME varchar(10),


ADDRESS varchar(20);

iv) FOREIGN KEY

Foreign Key is a field in a table which uniquely identifies each row of a another table. That
is, this field points to primary key of another table. This usually creates a kind of link
between the tables.

Syntax:
CREATE TABLE studentmarks(ID Number(6) foreign key reference student(ID),marks1
number(3), marks2 number(3));

35
v) CHECK

Using the CHECK constraint we can specify a condition for a field, which should be
satisfied at the time of entering values for this field.

CREATE TABLE Student(ID number(6),NAME varchar(10),AGE number(2) CHECK (AGE


>= 18));

vi) DEFAULT

This constraint is used to provide a default value for the fields.

CREATE TABLE Student(ID number(6),NAME varchar(10),AGE number(3) DEFAULT (18));

12) CREATE Database


The SQL CREATE DATABASE statement is used by a developer to create a database.

Let's see the syntax of SQL CREATE DATABASE DATABASE_NAME:

CREATE DATABASE database_name;

If you want to add tables in that database, you can use CREATE TABLE statement.

SQL DROP Database

If you want to delete or drop an existing database in a SQL schema, you can use SQL
DROP DATABASE.

DROP DATABASE database_name;

ADVANCED SQL
PL/SQL is a block structured language. The programs of PL/SQL are logical blocks that can
contain any number of nested sub-blocks. Pl/SQL stands for "Procedural Language
extension of SQL" that is used in Oracle.

PL/SQL Functionalities

 PL/SQL includes procedural language elements like conditions and loops.


 It allows declaration of constants and variables, procedures and functions, types
and variable of those types and triggers.

36
 It can support Array and handle exceptions (runtime errors).

 You can create PL/SQL units like procedures, functions, packages, types and
triggers, etc. which are stored in the database for reuse by applications.

 With PL/SQL, you can use SQL statements to manipulate Oracle data and flow of
control statements to process the data.

 PL/SQL is not case sensitive so you are free to use lower case letters or upper case
letters except within string and character literals.

 A line of PL/SQL text contains groups of characters known as lexical units. It can be
classified as follows:

o Delimeters
o Identifiers
o Literals
o Comments

1) Declarations and Assignments of Variables


A variable is a meaningful name which facilitates a programmer to store data temporarily
during the execution of code. It is nothing except a name given to a storage area.

Syntax for declaring variable:

variable_name datatype := value;]

Naming rules for PL/SQL variables

o The variable_name should not exceed 30 characters.


o The PL/SQL is not case sensitive so it could be either lowercase or uppercase
o It allows after the first character, it may be any number, underscore (_) or dollar
sign ($).

Write a Simple PL/SQL Program to find sum and average 3 of numbers.

DECLARE
a integer := 30;

37
b integer := 40;
c integer;
f real;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
f := 100.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;
OUTPUT: Value of c: 70 Value of f: 33.3

2) Constants
A constant is a value used in a PL/SQL block that remains unchanged throughout the
program. It is a user-defined literal value. It can be declared and used instead of actual
values.

Syntax : constant_name CONSTANT datatype := VALUE;

3 Literals
Literals are the explicit numeric, character, string or boolean values which are not
represented by an identifier.

There are following kinds of literals in PL/SQL:

Literals Examples

Numeric 75125, 3568, 33.3333333 etc.

Character 'A' '%' '9' ' ' 'z' '('

String Hello JavaTpoint!

Boolean TRUE, FALSE, NULL etc.

Date and Time '26-11-2002' , '2012-10-29 12:01:01'

38
4) Control Statements

If Statement

PL/SQL supports the programming language features like conditional statements and
iterative statements. Its programming constructs are similar to how you use in
programming languages like Java and C++.

There are different syntaxes for the IF-THEN-ELSE statement

i) IF-THEN statement

IF condition THEN
Statements
END IF;

This syntax is used when you want to execute statements only when condition is TRUE.

ii) IF-THEN-ELSE statement

IF condition THEN
statements
ELSE
statements
END IF;

This syntax is used when you want to execute one set of statements when condition is
TRUE or a different set of statements when condition is FALSE.

iii) IF-THEN-ELSIF statement

IF condition1 THEN
statements
ELSIF condition2 THEN
statements
END IF;

This syntax is used when you want to execute one set of statements when condition1 is
TRUE or a different set of statements when condition2 is TRUE.

39
iv) IF-THEN-ELSIF-ELSE statement

IF condition1 THEN
statements
ELSIF condition2 THEN
statements
ELSE
Statements
END IF;
It is the most advance syntax and used if you want to execute one set of statements
when condition1 is TRUE, a different set of statement when condition2 is TRUE or a
different set of statements when both the condition1 and condition2 are FALSE.

Case Statement

A CASE statement is evaluated from top to bottom. If it get the condition TRUE, then the
corresponding THEN clause is executed and the execution goes to the END CASE clause.

CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END

DECLARE
grade char(1) := 'A';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Good');
else dbms_output.put_line('Failed');
END CASE;
END;
Output: Excellent

40
Loop

The PL/SQL loops are used to repeat the execution of one or more statements for
specified number of times. These are also known as iterative control statements.

Types of PL/SQL Loops:

There are 3types of PL/SQL Loops.

1. Basic Loop
2. While Loop
3. For Loop

Basic Loop

PL/SQL exit loop is used when a set of statements is to be executed at least once before
the termination of the loop. There must be an EXIT condition specified in the loop,
otherwise the loop will get into an infinite number of iterations. After the occurrence of
EXIT condition, the process exits the loop.

Syntax of basic loop:

1. LOOP
2. Sequence of statements;
3. END LOOP;

Write a PL/SQL program to print First 10 Numbers.

DECLARE
i NUMBER := 1;
BEGIN
LOOP
EXIT WHEN i>10;
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
Output: 1 2 3 4 5 6 7 8 9 10

41
While Loop

PL/SQL while loop is used when a set of statements has to be executed as long as a
condition is true, the While loop is used. The condition is decided at the beginning of each
iteration and continues until the condition becomes false.

Syntax:

WHILE <condition>
LOOP statements;
END LOOP;
Write a PL/SQL program to print First 10 Numbers
DECLARE
i INTEGER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
Output: 1 2 3 4 5 6 7 8 9 10

FOR Loop

The loop is iterated between the start and end integer values. The counter is always
incremented by 1 and once the counter reaches the value of end integer, the loop ends.

Syntax:

FOR counter IN initial_value .. final_value LOOP


LOOP statements;
END LOOP;
Write a PL/SQL program to print First 10 Numbers
BEGIN
FOR k IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(k);
END LOOP;
END;
Output: 2 3 4 5 6 7 8 9 10

42
GOTO Statement

In PL/SQL, GOTO statement makes you able to get an unconditional jump from the GOTO
to a specific executable statement label in the same subprogram of the PL/SQL block.

Syntax: GOTO label_name;

Here the label declaration which contains the label_name encapsulated within the << >>
symbol and must be followed by at least one statement to execute.

GOTO label_name;
..
..
<<label_name>>
Statement;

5) PL/SQL Procedure
The PL/SQL stored procedure or simply a procedure is a PL/SQL block which performs one
or more specific tasks. It is just like procedures in other programming languages.

The procedure contains a header and a body.

o Header: The header contains the name of the procedure and the parameters or
variables passed to the procedure.
o Body: The body contains a declaration section, execution section and exception
section similar to a general PL/SQL block.

There is three ways to pass parameters in procedure:

1. IN parameters
2. OUT parameters
3. INOUT parameters

Syntax:

CREATE [OR REPLACE] PROCEDURE procedure_name


[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN

43
executable_section
[EXCEPTION
exception_section]
END [procedure_name];

Create procedure example

In this example, we are going to insert record in user table. So you need to create user
table first.

Table creation:

create table user(id number(10) primary key,name varchar2(100));

Now write the procedure code to insert record in user table.

Procedure Code:

create or replace procedure "INSERTUSER" (id IN NUMBER, name IN VARCHAR2) is

begin
insert into user values(id,name);
end;
/

Output:

Procedure created.

PL/SQL program to call procedure

BEGIN
insertuser(101,'Rahul');
dbms_output.put_line('record inserted successfully');
END;
/ Output:

ID Name

101 Rahul

44
PL/SQL Drop Procedure

Syntax : DROP PROCEDURE procedure_name;

6) PL/SQL Function
The PL/SQL Function is very similar to PL/SQL Procedure. The main difference between
procedure and a function is, a function must always return a value, and on the other hand
a procedure may or may not return a value.

Syntax:

CREATE [OR REPLACE] FUNCTION function_name [parameters]


[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];

Example

create or replace function adder(n1 in number, n2 in number)


return number
is
n3 number(8);
begin
n3 :=n1+n2;
return n3;
end;
/
Now write another program to call the function.
DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END; /

45
Output:

Addition is: 33
Statement processed.
0.05 seconds

7) Recursive Function
Defination: The function calls itself is called as recursion.

Write a PL/SQL Program to find factorial of given number

DECLARE

num number;

factorial number;

FUNCTION fact(x number)


RETURN number
IS
f number;
BEGIN
IF x=0 THEN
f := 1;
ELSE
f := x * fact(x-1);
END IF;
RETURN f;
END;

BEGIN
num:= 6;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
END;
/
Output:
Factorial 6 is 720

46
Drop Function:

Syntax: DROP FUNCTION function_name;

8) Exception Handling
An error occurs during the program execution is called Exception.

PL/SQL facilitates programmers to catch such conditions using exception block in the
program and an appropriate action is taken against the error condition.

There are two exceptions

o System-defined Exceptions
o User-defined Exceptions

Syntax for exception handling:

Following is a general syntax for exception handling:

DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
---
END;

9)Trigger
Triggers are stored programs, which are automatically executed or fired when some event
occurs.

Triggers are written to be executed in response to any of the following events.

o A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).

47
o A database definition (DDL) statement (CREATE, ALTER, or DROP).
o A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).

Advantages of Triggers

o Trigger generates some derived column values automatically


o Enforces referential integrity
o Event logging and storing information on table access
o Imposing security authorizations
o Preventing invalid transactions

Syntax:

CREATE [OR REPLACE ] TRIGGER trigger_name

{BEFORE | AFTER | INSTEAD OF }


{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 20000

2 Suresh 22 Kanpur 22000

48
3 Mahesh 24 Ghaziabad 24000

4 Chandan 25 Noida 26000

Create trigger:

CREATE OR REPLACE TRIGGER display_salary_changes


BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
Output: Trigger created.

10) Cursor
When an SQL statement is processed, Oracle creates a memory area known as context
area. A cursor is a pointer to this context area. It contains all information needed for
processing the statement. In PL/SQL, the context area is controlled by Cursor. A cursor
contains information on a select statement and the rows of data accessed by it.

There are two types of cursors:

o Implicit Cursors
o Explicit Cursors

1) Implicit Cursors

The implicit cursors are automatically generated by Oracle while an SQL statement is
executed, if you don't use an explicit cursor for the statement.

These are created by default to process the statements when DML statements like
INSERT, UPDATE, DELETE etc. are executed.

49
The following table specifies the status of the cursor with each of its attribute.

Attribute Description

%FOUND Its return value is TRUE if DML statements like INSERT, DELETE
and UPDATE affect at least one row or more rows or a SELECT
INTO statement returned one or more rows. Otherwise it returns
FALSE.

%NOTFOUND It opposite of %FOUND.

%ISOPEN It always returns FALSE for implicit cursors, because the SQL
cursor is automatically closed after executing its associated SQL
statements.

%ROWCOUNT It returns the number of rows affected by DML Statements

Example

Create customers table and have records:

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 20000

2 Suresh 22 Kanpur 22000

3 Mahesh 24 Ghaziabad 24000

4 Chandan 25 Noida 26000

DECLARE
total_rows number(2);
BEGIN
UPDATE customers
50
SET salary = salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
/

Output: 6 customers updated

select * from customers;

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 25000

2 Suresh 22 Kanpur 27000

3 Mahesh 24 Ghaziabad 29000

4 Chandan 25 Noida 31000

2) Explicit Cursors

The Explicit cursors are defined by the programmers to gain more control over the context
area. It is created on a SELECT statement which returns more than one row.

Following is the syntax to create an explicit cursor:


Syntax: CURSOR cursor_name IS select_statement;;

Steps:

You must follow these steps while working with an explicit cursor.

1. Declare the cursor to initialize in the memory.

CURSOR name IS SELECT statement;

51
2. Open the cursor to allocate memory.

Syntax:: OPEN cursor_name;

3. Fetch the cursor to retrieve data

Syntax : FETCH cursor_name INTO variable_list;

4. Close the cursor to release allocated memory

Syntax : Close cursor_name;

11 ) Subprograms
A PL/SQL subprogram is a named PL/SQL block that can be invoked repeatedly. If the subprogram
has parameters, their values can differ for each invocation.

A subprogram is either a procedure or a function. Typically, you use a procedure to perform an action
and a function to compute and return a value.

12) Package
A package is a schema object that groups logically related PL/SQL types, variables, constants,
subprograms, cursors, and exceptions. A package is compiled and stored in the database, where many
applications can share its contents.

A package always has a specification, which declares the public items that can be referenced from
outside the package.

52
UNIT-III
Entity Relation Ship Modeling
1) ER model
o ER model stands for an Entity-Relationship model. It is a high-level data model.
This model is used to define the data elements and relationship for a specified
system.
o It develops a conceptual design for the database. It also develops a very simple and
easy to design view of data.
o In ER modeling, the database structure is portrayed as a diagram called an entity-
relationship diagram.

For example: Suppose we design a school database. In this database, the student will be
an entity with attributes like address, name, id, age, etc. The address can be another
entity with attributes like city, street name, pin code, etc and there will be a relationship
between them.

2)Notation of ER diagram
In ER diagram, many notations are used to express the cardinality. These notations are as
follows:

Rectangle: Represents Entity sets.


Ellipses: Attributes
53
Diamonds: Relationship Set
Lines: They link attributes to Entity Sets and Entity sets to Relationship Set
Double Ellipses: Multivalued Attributes
Dashed Ellipses: Derived Attributes
Double Rectangles: Weak Entity Sets
Double Lines: Total participation of an entity in a relationship set

3)Components of a ER Diagram

1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity can be
represented as rectangles.

Consider an organization as an example- manager, product, employee, department etc.


can be taken as an entity.

Types of Entities:

a. Weak Entity

An entity that depends on another entity called a weak entity. The weak entity doesn't
contain any key attribute of its own. The weak entity is represented by a double rectangle.
54
b. Strong Entity: An entity that not depends on another entity called a Strong entity.

c. Associative Entity:
If a Relation is also used as entity, that is if it is acting as both as relation and entity it is
known as Associative enity. It is represented by diamond symbol in rectangle box.

2. Attribute
The property (characteristic) of an entity is known as Attribute. Eclipse is used to
represent an attribute.

For example, id, age, contact number, name, etc. can be attributes of a student.

55
Types of Attributes:

a. Key (Identifying) Attribute:

The key attribute is used to represent the main characteristics of an entity. It represents a
primary key. The key attribute is represented by an ellipse with the text underlined.

b. Composite Attribute:

An attribute that composed of many other attributes is known as a composite attribute.


The composite attribute is represented by an ellipse, and those ellipses are connected with
an ellipse.

c. Multivalued Attribute:

An attribute can have more than one value. These attributes are known as a multivalued
attribute. The double oval is used to represent multivalued attribute.

For example, a student can have more than one phone number.

56
d. Derived Attribute

An attribute that can be derived from other attribute is known as a derived attribute. It
can be represented by a dashed ellipse.

For example, A person's age changes over time and can be derived from another
attribute like Date of birth.

3. Relationship:

A relationship is used to describe the relation between entities. Diamond or rhombus is


used to represent the relationship.

Types of relationship:

a. One-to-One Relationship

When only one instance of an entity is associated with the relationship, then it is known as
one to one relationship.

For example, A female can marry to one male, and a male can marry to one female.

57
b. One-to-many relationship

When only one instance of the entity on the left, and more than one instance of an entity
on the right associates with the relationship then this is known as a one-to-many
relationship.

For example, Scientist can invent many inventions, but the invention is done by the only
specific scientist.

c. Many-to-one relationship

When more than one instance of the entity on the left, and only one instance of an entity
on the right associates with the relationship then it is known as a many-to-one
relationship.

For example, Student enrolls for only one course, but a course can have many students.

d. Many-to-many relationship

When more than one instance of the entity on the left, and more than one instance of an
entity on the right associates with the relationship then it is known as a many-to-many
relationship.

For example, Employee can assign by many projects and project can have many
employees.

58
4) Degree of a relationship:
The number of different entity sets participating in a relationship set is called as
degree of a relationship set.

There are 3 types of relationships. They are

1. Unary Relationship:

When a relation is having only one entity is known as unary relation. The degree of
relationship is ONE.

For example, one person is married to only one person.

2. Binary Relationship:

When there are TWO entities set participating in a relation, the relationship is
called as binary relationship. The Degree of relationship is TWO.

For example, Student is enrolled in Course.

3. Ternary Relationship:

When a relation is having more than two entities is known asTernary relation. The
degree of relationship is THREE or FOUR.

59
5) Attributes on Relationships
In ER model, When a relationships can also have attributes associated to them is known
as Attributes on relationships. That is like as entity is having an attribute.
Generally it is not recommended to give attributes to the relationships, because it is very
complex to convert ER diagram into table.

6) Structural Constraints (Mapping Constraints)

constraint specifies the minimum number of relationship instances that each entity can
participate in, and is sometimes called the minimum cardinality constraint.

the cardinality ratio and participation constraints, taken together, as the structural
constraints of a relationship type.

o A mapping constraint is a data constraint that expresses the number of entities to


which another entity can be related via a relationship set.
o It is most useful in describing the relationship sets that involve more than two
entity sets.
o For binary relationship set R on an entity set A and B, there are four possible
mapping cardinalities. These are as follows:
1. One to one (1:1)
2. One to many (1:M)
3. Many to one (M:1)
4. Many to many (M:M)

60
Problems with E-R
Models: (Fan Traps &
Chasm Traps):
In E-R model, due to
misinterpretation
(misunderstanding) of the
meaning of
61
certain relationships, a
problem known as
connection traps results.
There are
mainly two types of
connection traps.
1) Fan traps
2) Chasm traps
1) Fan Trap:
 A model represents a
relationship between
62
entity types but the
pathway
between certain entity
occurrences is ambiguous
(confusing).
 It occurs when two
relationship sets, having
mapping cardinality one-
to-

63
many converge (come
together) to single entity
set. I.e. two or more one-
to-many relationships fan
out from the same entity
set.
For example consider the
following E-R diagram
7) Problems with ER Models
There are several problems that may arise when designing a conceptual data model.
These are known as connection traps.

There are two main types of connection traps:

1. fan traps
2. chasm traps

Fan traps

64
A fan trap occurs when a model represents a relationship between entity types, but the
pathway between certain entity occurrences is ambiguous. It occurs when 1:m
relationships fan out from a single entity.

Fan Trap

The fan trap is resolved by restructuring the original ER model to represent the correct
association.

Resolved Fan Trap

Chasm traps

A chasm trap occurs when a model suggests the existence of a relationship between entity
types, but the pathway does not exist between certain entity occurrences.

It occurs where there is a relationship with partial participation, which forms part of the
pathway between entities that are related.

Chasm Trap

You need to therefore be careful when you remove relationships which you consider to be
redundant.

Resolved Chasm Trap

65
Problems with E-R
Models: (Fan Traps &
Chasm Traps):
In E-R model, due to
misinterpretation
(misunderstanding) of the
meaning of
certain relationships, a
problem known as
connection traps results.
There are
66
mainly two types of
connection traps.
1) Fan traps
2) Chasm traps
1) Fan Trap:
 A model represents a
relationship between
entity types but the
pathway

67
between certain entity
occurrences is ambiguous
(confusing).
 It occurs when two
relationship sets, having
mapping cardinality one-
to-
many converge (come
together) to single entity
set. I.e. two or more one-

68
to-many relationships fan
out from the same entity
set.
For example consider the
following E-R diagram
Problems with E-R
Models: (Fan Traps &
Chasm Traps):
In E-R model, due to
misinterpretation

69
(misunderstanding) of the
meaning of
certain relationships, a
problem known as
connection traps results.
There are
mainly two types of
connection traps.
1) Fan traps
2) Chasm traps
1) Fan Trap:
70
 A model represents a
relationship between
entity types but the
pathway
between certain entity
occurrences is ambiguous
(confusing).
 It occurs when two
relationship sets, having
mapping cardinality one-
to-
71
many converge (come
together) to single entity
set. I.e. two or more one-
to-many relationships fan
out from the same entity
set.
For example consider the
following E-R diagram
Enhanced Entity Relationship Model (EER Model)
Whenever ER model is adding the feature of Generalization / Specialization that model is
known as EER Model.

1) Generalization

Generalization is a process in which the common attributes of more than one entities
form a new entity. This newly formed entity is called generalized entity or Super Entity.

72
o Generalization is like a bottom-up approach in which two or more entities of lower
level combine to form a higher level entity if they have some attributes in common.
o In generalization, an entity of a higher level can also combine with the entities of
the lower level to form a further higher level entity.
o Generalization is more like subclass and super class system, but the only difference
is the approach. Generalization uses the bottom-up approach.
o In generalization, entities are combined to form a more generalized entity, i.e.,
subclasses are combined to make a super class.

Example:

Lets say we have two entities Student and Teacher.


Attributes of Entity Student are: Name, Address & Grade
Attributes of Entity Teacher are: Name, Address & Salary

These two entities have two common attributes: Name and Address, we can make a
generalized entity with these common attributes. Lets have a look at the ER model after
generalization.

73
2)Specialization:

Specialization is a process in which an entity is divided into sub-entities. It is reverse


process of generalization.

o Specialization is a top-down approach, and it is opposite to Generalization. In


specialization, one higher level entity can be broken down into two lower level
entities.
o Specialization is used to identify the subset of an entity set that shares some
distinguishing characteristics.
o Normally, the superclass is defined first, the subclass and its related attributes are
defined next, and relationship set are then added.

Example: Consider an entity employee which can be further classified as sub-


entities Technician, Engineer & Accountant because these sub entities have some
distinguish attributes.

74
All of these three employee types have few attributes common such as name & salary
which we had left associated with the parent entity “Employee” as shown in the above
diagram.

3) Attribute Inheritance:
Deriving (taking) the attributes from General entity to Sub entity is known as attribute
Inheritance.

Example: above diagram is the example for attribute Inheritance.

4) Aggregation
Aggregation is a process in which a single entity alone is not able to make sense in a
relationship so the relationship of two entities acts as one entity.

In aggregation, the relation between two entities is treated as a single entity. In


aggregation.

75
In real world, we know that a manager not only manages the employee working under
them but he has to manage the project as well.

In such scenario if entity “Manager” makes a “manages” relationship with either


“Employee” or “Project” entity alone then it will not make any sense because he has to
manage both.

In these cases the relationship of two entities acts as one entity. In our example, the
relationship “Works-On” between “Employee” & “Project” acts as one entity that has a
relationship “Manages” with the entity “Manager”.

5. Functional Dependency
A Constraint between two attributes or two sets of attributes is called as functional
dependency.

6.Partial functional dependency:

A Functional dependency in which one or more than one non key attributes are
functionally depend on part (but not all) of the primary key is called partial functional
dependency.

7.Transitive Dependency:
A Functional dependency between two or more non key attributes is called as transitive
dependency.

8.Mutivalued Dependency:

76
This type of dependency exists when thre are three attributes, For Example X,Y and Z in a
Relation with a well defied set of Y and Z values for each X value , but those Y and Z
values are independent of each other is called as multivalued dependency.

9. Join Dependency
If a table can be recreated by joining multiple tables and each of this table have a subset of the
attributes of the table, then the table is in Join Dependency. It is a generalization of Multivalued
Dependency
Join Dependency can be related to 5NF, wherein a relation is in 5NF, only if it is already in
4NF and it cannot be decomposed further.

6.Anomalies:
Anomalies are problems (Errors) that can occur in poorly planned, un-normalised
databases where all the data is stored in one table.

Generally there are three types of anomalies. They are

 Insertion Anomaly: The nature of a database may be such that it is not possible
to add a required piece of data unless another piece of unavailable data is also
added.

Example: A library database that cannot store the details of a new member until
that member has taken out a book.

 Deletion Anomaly: A record of data can legitimately be deleted from a database,


and the deletion can result in the deletion of the only instance of other, required
data.

Example: Deleting a book loan from a library member can remove all details of the
particular book from the database such as the author, book title etc.

 updation Anomaly: Incorrect data may have to be changed, which could involve
many records having to be changed, leading to the possibility of some changes
being made incorrectly.

Normalization

Definition: Converting a Relation into well structured Relation is called as Normalization.


That is Converting a Relation into without anomalies are called as normalization.

1.The purpose of Normalization


77
There are three main reasons to normalize a database. The first is to minimize duplicate
data, the second is to minimize or avoid data modification issues, and the third is to
simplify queries.

o Normalization is the process of organizing the data in the database.


o Normalization is used to minimize the redundancy from a relation or set of
relations. It is also used to eliminate the undesirable characteristics like Insertion,
Update and Deletion Anomalies.
o Normalization divides the larger table into the smaller table and links them using
relationship.
o The normal form is used to reduce redundancy from the database table.
o Data redundancy happens when the same piece of data is held in two separate
place.

2.How Normalization Supports Database Design


Normalization during the design phase of a database is a technique used for two
things:

a. saving space
b. reducing the chances on errors

o During the normalization phase, the database designer will look at which data is
stored where, and in what way. And he will try to reduce the number of times the
same data is stored.
o Normalization is the part of design process. Therefore you should be aware of
good design .
o Principles and procedures, as well as normalization on procedures.
o Normalization is very useful to support to developing an ER diagrams. For
example 2NF represents a macro view of entities within the ER diagram.

3. The Process of Normalization:


The process of normalization will done by sever types of normal forms.

Types of Normal Forms: There are the four types of normal forms:

78
Normal Description
Form

1NF A relation is in 1NF if it contains an atomic value.

2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are
fully functional dependent on the primary key.

3NF A relation will be in 3NF if it is in 2NF and no transition dependency


exists.

BCNF A relation will be in 3NF no any reaming insertion, deletion and


updation anomalies exit.

4NF A relation is in Boyce Codd normal form and has no multi-valued


dependency exit.

First Normal Form (1NF)


o If a no Multivalued attribute then the relation is in 1NF.
o It must hold all are single-valued attribute.
o Example: Relation EMPLOYEE is not in 1NF because of multi-valued attribute
EMP_PHONE.

EMPLOYEE table:

EMP_ID EMP_NAME EMP_PHONE EMP_STATE

14 Raju 7272826385, UP
9064738238

20 Hari 8574783832 Bihar


79
12 Shyam 7390372389, Punjab
8589830302

The decomposition of the EMPLOYEE table into 1NF has been shown below:

EMP_ID EMP_NAME EMP_PHONE EMP_STATE

14 Raju 7272826385 UP

14 Raju 9064738238 UP

20 Hari 8574783832 Bihar

12 Shyam 7390372389 Punjab

12 Shyam 8589830302 Punjab

Second Normal Form (2NF)


In the 2NF, relational must be in 1NF and any must satisfy any one of the following
condition THREE Conditions.

Condition 1: If exist only one Primary key attribute.

Conditions 2: Evert Non key attribute is functionally dependent on the full set of
Primary key attribute.(i.e Partial functional dependency exit)

Condition 3: No Non key attribute exists in the Relation (i.e all attributes are keys)

Example: EMPLOYEE Relation

Emp_id Name Dept Name Salary Course_title Date of Completed

80
In the above Example Employee Relation is in 1NF but not in 2NF. So Convert this relation
into 2NF to decompose the into new relations like as follows

a.EMPOLYEE1 (Emp-Id, Course –Title, Date of Completed)

This relations satisfies condition 2 above and also in 1NF , So it is is in 2 NF.

b.EMPLOYEE2 ( Emp-Id, Name , Dept_Name, Salary)

This Relation satisfies condition 1 above and also in 1 NF , So it is in 2 NF.

Third Normal Form (3NF)


A relation will be in 3NF if it is in 2NF and not contain any transitive dependency
exists in the relation.3NF is used to reduce the data duplication.

Example: WORKER

Worker-Id Skill Type Rate

101 Plumbing 3.17


102 Electrician 4.80
103 Plumbing 3.17
104 Carpenter 5.30

In the above Relation Worker is in 2 NF but not in in 3NF, because transitive dependency
exited between two non keys i.e SkillType and Rate.

To convert the WORKER relation into 3NF like as follows.

a. WORKER1 (Worker_Id, SkillType)


This relation satisfies condition3 of 2NF and no transitive dependency, So it is in
3NF.
b. WORKER2 (Skill Type, Rate)
This relation satisfies condition1 of 2NF and no transitive dependency, So it is in
3NF.

Boyce Codd normal form (BCNF)


o BCNF is the advance version of 3NF. It is stricter than 3NF.

81
o If a Relation is in 3NF and it is not having any anomalies (i.e Insertion, deletion and
Updation) then the relation is in BCNF.

Example: WORKER Relation

Above example is WORKER Relation is in 3 NF and there is no any Insertion,


deletion and updation anomalies, So it is also in BCNF.

4.The Database Design Methodology for Relational


Databases
Design methodology is an approach taken in designing or building things and it serves as
a guideline on how things are done.

Normally a design methodology is broken down into phases or stages and for each phase
the detailed steps are outlined, and appropriate tools and techniques are specified.

A design methodology is able to support and facilitate designers in planning, modeling,


and managing a database development project in a structured and systematic.

There are THREE main phases of database design methodology for relational
databases.

a. conceptual Phase : The conceptual design phase focuses on building a conceptual


data model which is independent of software and hardware implementation details.

b. logical Phase: The logical design phase maps the conceptual model on to a logical
model of a specific data model, but independent of the software and physical
consideration

c. physical database design Phase: The physical design phase is tailored to a specific
DBMS and very focused to the hardware requirements

5.Data Redundancy:
 Storing the same data in more than one place is called as data redundancy.
 Data redundancy is a condition created within a database or data storage
technology in which the same piece of data is held in two separate places.
 Whenever data is repeated, if any loss we can recovery from another place.
 But is May chance to go inconsistency.
 Length and cost will increase.
 Complexity to maintain data.
UNIT-IV
Transaction Management
82
Definition: A transaction can be defined as a group of tasks. A single task is the
minimum processing unit which cannot be divided further.
Example of a simple transaction: Suppose a bank employee transfers Rs 500 from A's
account to B's account. This very simple and small transaction involves several low-level
tasks.
A’s Account
Open_Account(A)
Old_Balance = A.balance
New_Balance = Old_Balance - 500
A.balance = New_Balance
Close_Account(A)
B’s Account
Open_Account(B)
Old_Balance = B.balance
New_Balance = Old_Balance + 500
B.balance = New_Balance
Close_Account(B)

1.Properties of Transaction (ACID Properties)

A transaction is a very small unit of a program and it may contain several low level tasks.
A transaction in a database system must maintain Atomicity, Consistency, Isolation,
and Durability − commonly known as ACID properties − in order to ensure accuracy,
completeness, and data integrity.
 Atomicity − This property states that a transaction must be treated as an atomic
unit, that is, either all of its operations are executed or none. No transaction is left
partially completed.
 Consistency − If the database was in a consistent state before the execution of a
transaction, it must remain consistent after the execution of the transaction as
well.
 Durability − The database should be durable enough to hold all its latest updates
even if the system fails or restarts. If a transaction commits but the system fails
before the data could be written on to the disk, then that data will be updated once
the system springs back into action.
 Isolation − The property of isolation states that all the transactions will be carried
out and executed as if it is the only transaction in the system. No transaction will
affect the existence of any other transaction.

2.Serializability

83
When multiple transactions are being executed by the operating system in a
multiprogramming environment, there are possibilities that instructions of one
transactions are interleaved with some other transaction.
 Schedule − A chronological execution sequence of a transaction is called a
schedule. A schedule can have many transactions in it, each comprising of a
number of instructions/tasks.
 Serial Schedule − Transactions are ordered one after the other. This type of
schedule is called a serial schedule, as transactions are executed in a serial
manner.
3.Concurrency Control
In a multiprogramming environment where multiple transactions can be executed
simultaneously, it is highly important to control the concurrency of transactions.
Concurrency control protocols can be broadly divided into two categories −

i) Lock based protocols


ii) Time stamp based protocols

i) Lock-based Protocols

Database systems equipped with lock-based protocols use a mechanism by which any
transaction cannot read or write data until it acquires an appropriate lock on it. Locks are
of two kinds −
 Binary Locks − A lock on a data item can be in two states; it is either locked or
unlocked.
 Shared/exclusive − If a lock is acquired on a data item to perform a write
operation, it is an exclusive lock. Allowing more than one transaction to write on
the same data item would lead the database into an inconsistent state. Read locks
are shared because no data value is being changed.
There are four types of lock protocols available −

a.Simplistic Lock Protocol


Simplistic lock-based protocols allow transactions to obtain a lock on every object before
a 'write' operation is performed.
Transactions may unlock the data item after completing the ‘write’ operation.

b.Pre-claiming Lock Protocol


Pre-claiming protocols evaluate their operations and create a list of data items on which
they need locks. If all the locks are granted, the transaction executes and releases all the
locks when all its operations are over. If all the locks are not granted, the transaction
rolls back and waits until all the locks are granted.

84
c.Two-Phase Locking (2PL) Protocol
This locking protocol divides the execution phase of a transaction into three parts.
In the first part, when the transaction starts executing, it seeks permission for the locks
it requires.
The second part is where the transaction acquires all the locks. As soon as the
transaction releases its first lock, the third phase starts. In this phase, the transaction
cannot demand any new locks; it only releases the acquired locks.

Two-phase locking has two phases, one is growing, where all the locks are being
acquired by the transaction; and the second phase is shrinking, where the locks held by
the transaction are being released.
To claim an exclusive (write) lock, a transaction must first acquire a shared (read) lock
and then upgrade it to an exclusive lock.

d. Strict Two-Phase Locking


The first phase of Strict-2PL is same as 2PL.
After acquiring all the locks in the first phase, the transaction continues to execute
normally.
But in contrast to 2PL, Strict-2PL does not release a lock after using it. Strict-2PL holds
all the locks until the commit point and releases all the locks at a time.

85
Strict-2PL does not have cascading abort as 2PL does.

ii).Timestamp-based Protocols

The most commonly used concurrency protocol is the timestamp based protocol. This
protocol uses either system time or logical counter as a timestamp.
Lock-based protocols manage the order between the conflicting pairs among transactions
at the time of execution, whereas timestamp-based protocols start working as soon as a
transaction is created.
Every transaction has a timestamp associated with it, and the ordering is determined by
the age of the transaction.
A transaction created at 0002 clock time would be older than all other transactions that
come after it. For example, any transaction 'y' entering the system at 0004 is two
seconds younger and the priority would be given to the older one.
In addition, every data item is given the latest read and write-timestamp. This lets the
system know when the last ‘read and write’ operation was performed on the data item.

4.Multi-version Timestamp Ordering Protocol


The timestamp-ordering protocol ensures serializability among transactions in their
conflicting read and write operations. This is the responsibility of the protocol system that
the conflicting pair of tasks should be executed according to the timestamp values of the
transactions.

5. The Need of Concurrency Control


If transactions are executed serially, i.e., sequentially with no overlap in time, no
transaction concurrency exists. However, if concurrent transactions with interleaving
operations are allowed in an uncontrolled manner, some unexpected, undesirable result
may occur, such as:

 The lost update problem: A second transaction writes a second value of a data-
item (datum) on top of a first value written by a first concurrent transaction, and
the first value is lost to other transactions running concurrently which need, by

86
their precedence, to read the first value. The transactions that have read the
wrong value end with incorrect results.

 The dirty read problem: Transactions read a value written by a transaction that
has been later aborted. This value disappears from the database upon abort, and
should not have been read by any transaction (“dirty read”). The reading
transactions end with incorrect results.

 The incorrect summary problem: While one transaction takes a summary over the
values of all the instances of a repeated data-item, a second transaction updates
some instances of that data-item.

6. DEAD LOCK
In a multi-process system, deadlock is an unwanted situation that arises in a shared
resource environment, where a process indefinitely waits for a resource that is held by
another process.
For example, assume a set of transactions {T 0, T1, T2, ...,Tn}. T0 needs a resource X to
complete its task. Resource X is held by T 1, and T1 is waiting for a resource Y, which is
held by T2. T2 is waiting for resource Z, which is held by T 0. Thus, all the processes wait
for each other to release resources.
In this situation, none of the processes can finish their task. This situation is known as a
deadlock.
Deadlocks are not healthy for a system. In case a system is stuck in a deadlock, the
transactions involved in the deadlock are either rolled back or restarted.

Deadlock Avoidance

Deadlock avoidance mechanisms can be used to detect any deadlock situation in


advance.
Methods like "wait-for graph" are available but they are suitable for only those systems
where transactions are lightweight having fewer instances of resource. In a bulky system,
deadlock prevention techniques may work well.

Wait-for Graph
This is a simple method available to track if any deadlock situation may arise. For each
transaction entering into the system, a node is created. When a transaction T i requests
for a lock on an item, say X, which is held by some other transaction T j, a directed edge
is created from Ti to Tj. If Tj releases item X, the edge between them is dropped and
Ti locks the data item.
The system maintains this wait-for graph for every transaction waiting for some data
items held by others. The system keeps checking if there's any cycle in the graph.

87
6. Database Recovery
It is the method of restoring the database to its correct state in the event of a failure at
the time of the transaction or after the end of a process.
Earlier, you have been given the concept of database recovery as a service that should be
provided by all the DBMS for ensuring that the database is dependable and remains in a
consistent state in the presence of failures.
In this context, dependability refers to both the flexibility of the DBMS to various kinds of
failure and its ability to recover from those failures. In this chapter, you will gather a brief
knowledge of how this service can be provided. To gain a better understanding of the
possible problems you may encounter in providing a consistent system, you will first learn
about the need for recovery and its types of failure, which usually occurs in a database
environment.

7. The Need of Recovery


The storage of data usually includes four types of media with an increasing amount of
reliability: the main memory, the magnetic disk, the magnetic tape, and the optical disk.
Many different forms of failure can affect database processing and/or transaction, and
each of them has to be dealt with differently. Some data failures can affect the main
memory only, while others involve non-volatile or secondary storage also. Among the
sources of failure are:

 Due to hardware or software errors, the system crashes, which ultimately resulting in
loss of main memory.
 Failures of media, such as head crashes or unreadable media that results in the loss of
portions of secondary storage.
 There can be application software errors, such as logical errors that are accessing the
database that can cause one or more transactions to abort or fail.
 Natural physical disasters can also occur, such as fires, floods, earthquakes, or power
failures.
 Carelessness or unintentional destruction of data or directories by operators or users.

88
 Damage or intentional corruption or hampering of data (using malicious software or files)
hardware or software facilities.

8. Recovery Facilities
Every DBMS should offer the following facilities to help out with the recovery mechanism:

 Backup mechanism makes backup copies at a specific interval for the database.
 Logging facilities keep tracing the current state of transactions and any changes made to
the database.
 Checkpoint facility allows updates to the database for getting the latest patches to be
made permanent and keep secure from vulnerability.
 Recovery manager allows the database system for restoring the database to a reliable
and steady-state after any failure occurs.

9.Optimistic Technique
In systems with low conflict rates, the task of validating every transaction for
serializability may lower performance. In these cases, the test for serializability is
postponed to just before commit. Since the conflict rate is low, the probability of aborting
transactions which are not serializable is also low. This approach is called optimistic
concurrency control technique.
In this approach, a transaction’s life cycle is divided into the following three phases −
 Execution Phase − A transaction fetches data items to memory and performs
operations upon them.
 Validation Phase − A transaction performs checks to ensure that committing its
changes to the database passes serializability test.
 Commit Phase − A transaction writes back modified data item in memory to the
disk.

Database Security
Database security is the technique that protects and secures the database against
intentional or accident
al threats. Security concerns will be relevant not only to the data resides in an
organization's database: the breaking of security may harm other parts of the system,
which may ultimately affect the database structure. Consequently, database security
includes hardware parts, software parts, human resources, and data. To efficiently do the
uses of security needs appropriate controls, which are distinct in a specific mission and
purpose for the system. The requirement for getting proper security while often having
89
been neglected or overlooked in the past days; is now more and more thoroughly checked
by the different organizations.
We consider database security about the following situations:

 Theft and fraudulent.


 Loss of confidentiality or secrecy.
 Loss of data privacy.
 Loss of data integrity.
 Loss of availability of data.

1.Threats
Any situation or event, whether intentionally or incidentally, can cause damage, which can
reflect an adverse effect on the database structure and, consequently, the organization. A
threat may occur by a situation or event involving a person or the action or situations that
are probably to bring harm to an organization and its database.
The degree that an organization undergoes as a result of a threat's following which
depends upon some aspects, such as the existence of countermeasures and contingency
plans. Let us take an example where you have a hardware failure that occurs corrupting
secondary storage; all processing activity must cease until the problem is resolved.
2. Computer-Based Controls
The different forms of countermeasure to threats on computer systems range from
physical controls to managerial procedures. In spite of the range of computer-based
controls that are preexisting, it is worth noting that, usually, the security of a DBMS is
merely as good as that of the operating system, due to the close association among them.
Most of the computer-based database security are listed below:

 Access authorization.
 Access controls.
 Views.
 Backup and recovery of data.
 Data integrity.
 Encryption of data.
 RAID technology.

90
3. Access Controls
The usual way of supplying access controls to a database system is dependent on the
granting and revoking of privileges within the database. A privilege allows a user to create
or access some database object or to run some specific DBMS utilities. Privileges are
granted users to achieve the tasks required for those jobs.
The database provides various types of access controls:

 Discretionary Access Control (DAC)


 Mandatory Access Control (MAC)

4.Backup and Recovery

Every Database Management System should offer backup facilities to help with the
recovery of a database after a failure. It is always suitable to make backup copies of the
database and log files at the regular period and for ensuring that the copies are in a
secure location. In the event of a failure that renders the database unusable, the backup
copy and the details captured in the log file are used to restore the database to the latest
possible consistent state.

5. Integrity

Data integrity is the overall completeness, accuracy and consistency of data. This can be
indicated by the absence of alteration between two instances or between two updates of a
data record, meaning data is intact and unchanged. Data integrity is usually imposed
during the database design phase through the use of standard procedures and rules. Data
integrity can be maintained through the use of various error-checking methods and
validation procedures.

6. Encryption and Decryption

Database encryption is the process of converting data, within a database, in plain text
format into a meaningless cipher text by means of a suitable algorithm.
Database decryption is converting the meaningless cipher text into the original
information using keys generated by the encryption algorithms.
Database encryption can be provided at the file or column level.

91
7. Redundant Array of Independent Disks(RAID)
RAID or Redundant Array of Independent Disks, is a technology to connect multiple
secondary storage devices and use them as a single storage media.
RAID consists of an array of disks in which multiple disks are connected together to
achieve different goals. RAID levels define the use of disk arrays.

RAID 0
In this level, The data is broken down into blocks and the blocks are distributed among
disks. Each disk receives a block of data to write/read in parallel.

RAID 1

When data is sent to a RAID controller, it sends a copy of data to all the disks in the
array. RAID level 1 is also called mirroring and provides 100% redundancy in case of a
failure.

RAID 2
RAID 2 records Error Correction Code using Hamming distance for its data, striped on
different disks. Like level 0, each data bit in a word is recorded on a separate disk and
ECC codes of the data words are stored on a different set disks.

92
RAID 3
RAID 3 stripes the data onto multiple disks. The parity bit generated for data word is
stored on a different disk. This technique makes it to overcome single disk failures.

RAID 4
In this level, an entire block of data is written onto data disks and then the parity is
generated and stored on a different disk. Note that level 3 uses byte-level striping,
whereas level 4 uses block-level striping.

RAID 5
RAID 5 writes whole data blocks onto different disks, but the parity bits generated for
data block stripe are distributed among all the data disks rather than storing them on a
different dedicated disk.

RAID 6
RAID 6 is an extension of level 5. In this level, two independent parities are generated
and stored in distributed fashion among multiple disks. Two parities provide additional
fault tolerance. This level requires at least four disk drives to implement RAID.

93

You might also like