4 Database System
4 Database System
(Computer Science)
SEMESTER - I (CBCS)
DATABASE SYSTEMS
Unit - I
1. Introduction To DBMS 01
2. Data Models 08
5. ER to Table 25
Unit - II
7. Relational Algebra 35
8. DDL Statements 39
9. DML Statements 43
Unit - III
Syllabus
F.Y.B.Sc. (CS) Database System
Semester I (CBCS)
Objectives:
The objective of this course is to introduce the concept of the
DBMS with respect to the relational model, to specify the functional and
data requirements for a typical database application and to understand
creation, manipulation and querying of data in databases Expected
Learning Outcomes
Unit I :
Introduction to DBMS – Database, DBMSܽ Definition, Overview of
DBMS, Advantages of DBMS, Levels of abstraction, Data independence,
DBMS Architecture
Unit II
Schema refinement and Normal forms: Functional dependencies, first,
second, third, and BCNF normal forms based on primary keys, lossless
join decomposition.
I
DDL Statements - Creating Databases, Using Databases, datatypes,
Creating Tables (with integrity constraintsܽ primary key, default, check,
not null), Altering Tables, Renaming Tables, Dropping Tables, Truncating
Tables, Backing Up and Restoring databases
Unit III
Functions - String Functions (concat, instr, left, right, mid, length,
lcase/lower, ucase/upper, replace, strcmp, trim, ltrim, rtrim), Math
Functions (abs, ceil, floor, mod, pow, sqrt, round, truncate) Date Functions
(adddate, datediff, day, month, year, hour, min, sec, now, reverse)
Joining Tables - inner join, outer join (left outer, right outer, full outer)
Text books:
1. Ramez Elmasri & Shamkant B.Navathe, Fundamentals of Database
Systems, Pearson Education, Sixth Edition, 2010
2. Ramakrishnam, Gehrke, Database Management Systems,
McGraw◌ܻHill, 2007
3. Joel Murach, Murach’s MySQL, Murach, 2012
Additional References :
1. Robert Sheldon, Geoff Moes, Begning MySQL, Wrox Press, 2005.
II
Unit-1
1
INTRODUCTION TO DBMS
Unit Structure
1.0 Objectives
1.1 Introduction to DBMS
1.2 Overview of DBMS
1.3 Advantages of DBMS
1.4 Levels of abstraction
1.5 Data independence
1.6 DBMS architecture
1.0 OBJECTIVES
1.1 INTRODUCTION
1
DATABASE MANAGEMENT SYSTEM
A database-management system (DBMS) is a collection of
interrelated data and a set of programs to access those data. This is a
collection of related data with an implicit meaning and hence is a
database.
1.2 OVERVIEW
1.3 ADVANTAGES
2
Concurrent Access and Crash Recovery: A database system allows
several users to access the database concurrently. Answering different
questions from different users with the same (base) data is a central aspect
of an information system. Such concurrent use of data increases the
economy of a system. An example for concurrent use is the travel database
of a bigger travel agency. The employees of different branches can access
the database concurrently and book journeys for their clients. Each travel
agent sees on his interface if there are still seats available for a specific
journey or if it is already fully booked. A DBMS also protects data from
failures such as power failures and crashes etc. by the recovery schemes
such as backup mechanisms and log files etc.
Data Administration:
When several users share the data, centralizing the administration
of data can offer significant improvements. Experienced professionals,
who understand the nature of the data being managed, and how different
groups of users use it, can be responsible for organizing the data
representation to minimize redundancy and fine-tuning the storage of the
data to make retrieval efficient.
Database Administrator:
One of the main reasons for using DBMSs is to have central
control of both the data and the programs that access those data. A person
who has such central control over the system is called a database
administrator (DBA). DBA is responsible for authorizing access to the
database, coordinating and monitoring its use, and acquiring software and
hardware resources as needed.
Naive users:
Naive users are unsophisticated users who interact with the system
by invoking one of the application programs that have been written
previously. For example, a bank teller who needs to transfer $50 from
account A to account B invokes a program called transfer. This program
asks the teller for the amount of money to be transferred, the account from
which the money is to be transferred, and the account to which the money
is to be transferred.
3
Application programmers:
Application programmers are computer professionals who write
application programs. Application programmers can choose from many
tools to develop user interfaces. Rapid application development (RAD)
tools are tools that enable an application programmer to construct forms
and reports without writing a program.
Sophisticated users:
Sophisticated users interact with the system without writing
programs. Instead, they form their requests in a database query language.
They submit each such query to a query processor, whose function is to
break down DML statements into instructions that the storage manager
understands. Analysts who submit queries to explore data in the database
fall in this category.
Specialized users:
Specialized users are sophisticated userswho write specialized
database applications that do not fit into the traditional data-processing
framework.
Hiding certain details of how the data are stored and maintained. A
major purpose of database system is to provide users with an “Abstract
View” of the data. In DBMS there are 3 levels of data abstraction. The
goal of the abstraction in the DBMS is to separate the users request and
the physical storage of data in the database.
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:
This level of data Abstraction describes “What” data are to be
stored in the database and what relationships exist among those
data.
Database Administrators use the logical level of abstraction.
View Level:
It is the highest level of data Abstracts that describes only part of
entire database.
4
Different users require different types of data elements from each
database.
The system may provide many views for the some database.
5
1.7 DBMS ARCHITECTURE
6
Storage Manager:
A storage manager is a program module that provides the interface
between the low-level data stored in the database and the application
programs and queries submitted to the system. The storage manager is
responsible for the interaction with the file manager. The storage manager
translates the various DML statements into low-level file-system
commands. Thus, the storage manager is responsible for storing,
retrieving, and updating data in the database.
7
2
DATA MODELS
Unit Structure
2.0 Introduction
2.1 Types of Data Models
2.0 INTRODUCTION
10
3
ENTITY RELATIONSHIP MODEL
Unit Structure
3.0 Introduction
3.1 Types of Attributes
3.0 INTRODUCTION
11
Aggregation: In aggregation, the relation between two entities is
treated as a single entity. In aggregation, relationship with its
corresponding entities is aggregated into a higher-level entity.
1. Simple Attributes
Simple attributes are atomic attributes with independence meaning
which cannot be further divided. For example, employee’s phone is an
atomic attribute.
2. Composite Attributes:
Composite are made up of more than one attributes. It can divide
into smaller subparts, which represent more basic attributes with
independent meanings. They sometimes form a hierarchy. The value of a
composite attribute is the combination of the values of its components
atomics attributes. For example, the Address attribute of the EMPLOYEE
entity can be subdivided into Street_address, City, State and Zip.
Composite attributes are useful to model situations in which a user
sometimes refers to the composite attribute as a unit but at other times
refers specifically to its components.
Multivalued Attributes:
Multi valued attributes has a group of values for a specific entity.
Multi valued attributes comes with upper and lower limits the number of
values to be specified for an entity. For example, an employee may have
more than one phone number.
12
Stored Attributes: Stored attributes consist of attributes that are fetched
directly from the entity. For Date_of_Birth
Derived Attributes: Data that is derived using the data stored in the
stored attributes set are known as Derived attributes. For example, Age
can be calculated using the stored date_of _birth attribute.
Entity type: an entity type defines a collection or set of entities that have
the same attributes. Each entity type in the database is described by its
name and attributes. For example, a college may want to store similar
information concerning each of the students. Students can be entity types
that share the same attributes, but each entity has its own values for each
attribute.
Entity Set: The collection of all entities of a particular entity type in the
database at any point in time is called an entity set. The entity set is
usually referred to using the same name as the entity type. For example
student refers to both a type of entity as well as the current set of all
student entities in the database.
Relationship: An association among several entities is known as
Relationship.
RELATIONSHIP
Relationship set:
A relationship set is a set of relationship of the same type. It is a
mathematical relation on n>=2 entity sets. Diamonds represents the relationship
sets. If E1,E2,E3…….En are entity set then a relationship set R is a subset
of {(e1,e2….en) | e1 ∈E1, e2 ∈E2…..en∈En where (e1,e2,……en) is a
relationship.
13
databases is often binary. Some relationships that appear to be non-binary
could actually be better represented by several binary relationships.
Mapping Cardinality
The relationship set are of one to one, one to many, many to one or many
to many. To distinguish among these types, either a directed line ( ) or
an undirected line ( )between the relationship set and the entity set is
drawn.
One to one:
A directed line is drawn from the relationship set advisor to both
entity sets instructor and student. This indicates that an instructor
may advise at most one student, and a student may have at most
one advisor.
One to many:
A directed line is drawn from the relationship set advisor to the
entry set instructor and an undirected line to the entity set student.
This indicates that an instructor may advise many students, but a
student may have at most one advisor.
Many to one:
An undirected line drawn from the relationship set advisor to the
entity set instructor and a directed line to the entity set student.
This indicates that an instructor may advise at most one student,
but a student may have many advisors.
Many to many:
an undirected line drawn from the relationship set advisor to both
entity sets instructor and student. This indicates that an instructor
may advise many students, and a student may have many advisors.
14
Figure 3.4: mapping cardinalities
Key constraint:
A Key or uniqueness constraint on the attributes of entities helps to
identify relationship uniquely, and thus distinguish relationship from each
other. No two entities are allowed to have exactly the same.
Specialization
Specialization is a process of creating sub parts of an entity type.
Generalization is a bottom-up approach, while Specialization is a top-
down approach. One higher level entity can be broken down into two
lower-level entities by specialisation. The term "specialization" refers to a
subset of an entity set that shares certain common characteristics.
Normally, the superclass is described first, followed by the subclass and
its related attributes, and finally the relationship set. For example, In an
15
employee management system, EMPLOYEE entity can be specialized as
TESTER or DEVELOPER based on what role they play in the company.
Generalization
It’s a reverse process of abstraction, where in the difference
amongst the entity sets are suppressed and they are generalized together
into a single entity type. Distinctions are made explicitly in case of
generalization with top-down approach. Commonality is defined using
generalization and expressed using containment relationship. It creates a
relationship between higher-level entities set to successive hierarch of
subclass entity set. The design process may also proceed in a bottom-up
manner, in which multiple entities sets are synthesized into a higher-level
entity set on the basis of common features. For example, Faculty and
Student entities can be generalized and create a higher-level entity Person.
16
Figure 3.6: Generalization
AGGREGATION
In aggregation, the relation between two entities is treated as a
single entity. In aggregation, relationship with its corresponding entities is
aggregated into a higher-level entity. For example, Center entity offers the
Course entity act as a single entity in the relationship which is in a
relationship with another entity visitor. In the real world, if a visitor visits
a coaching center then he will never enquiry about the Course only or just
about the Center instead he will ask the enquiry about both.
17
Summary of Notation in ER diagram
Entity Vs Attributes
While identifying the attributes of an entity set, it is sometimes not
clear whether a property should be modelled as an attribute or as an entity
set (and related to the first entity set using a relationship set). For example,
consider adding address information to the Employees entity set. One
option is to use an attribute address. This option is appropriate if we need
to record only one address per employee, and it suffices to think of an
18
address as a string. An alternative is to create an entity set called
Addresses and to record associations between employees and addresses
using a relationship.
Entity vs Relationship
The nature of ER modelling can thus make it difficult to recognize
underlying entities, and we might associate attributes with relationships
rather than the appropriate entities. In general, such mistakes lead to
redundant storage of the same information and can cause many problems.
19
4
RELATIONAL DATA MODEL
Unit Structure
4.0 Introduction
4.1 Relation
4.2 Attribute Types
4.3 Domain
4.4 Properties of Relations
4.5 Relational Model Notation
4.6 Characteristics of Relation
4.0 INTRODUCTION
4.1 RELATION
20
attributes
(or columns)
tuples
(or rows)
The set of allowed values for each attribute is called the domain of
the attribute. Attribute values are (normally) required to be atomic; that is,
indivisible. The special value null is a member of every domain, indicated
that the value is “unknown”. The null value causes complications in the
definition of many operations. An attribute or a combination of attributes
that is used to identify the records uniquely is known as super key.
Candidate key is defined as minimal super key or irreducible super key;
used to identify the records uniquely. A candidate key that is used by the
database designer for unique identification of each row in a table is known
as primary key. A primary Key can consist of one or more attributes of a
table, known as composite key. The candidate key not chosen by database
designer as a primary key is known as alternate key. A foreign key is an
attribute or combination of attribute in one table that points to the primary
key of another table.
4.3 DOMAIN
21
4.4 PROPERTIES OF RELATIONS
RELATIONAL CONSTRAINTS
The meaning of constraint is Restriction. There are generally many
restrictions or constraints on the actual values in a database state.
Constraints on databases can generally be divided into five main
categories.
1. Domain constraint
2. Tuple Uniqueness constraint
3. Key constraint
4. Entity Integrity constraint
5. Referential Integrity constraint
1. Domain Constraint
Domain constraint defines the domain or set of values for an
attribute. It specifies that the value taken by the attribute must be the
atomic value from its domain. 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.
3. Key constraint
Key constraint specifies that in any relation-All the values of
primary key must be unique. The value of primary key must not be null.
24
5
ER TO TABLE
Unit Structure
5.1 Rules for converting ER to Table
For example:
25
There are the following steps which need to be considered before
developing table:
26
Figure 5.2: table structure of given ER diagram
27
Unit II
6
SCHEMA REFINEMENT AND NORMAL
FORMS
Unit Structure
6.0 Objectives
6.1 Functional dependencies
6.2 Normalization
6.3 Types of Normal forms
6.4 Lossless join decomposition
6.0 OBJECTIVES
6.1FUNCTIONALDEPENDENCIES
6.2 NORMALIZATION
29
SECOND NORMAL FORM:
A relation schema R is in 2NF, if it satisfies 1NF and if every non-
prime attribute A in R is fully functionally dependent on primary key of R.
2NF is based on the concept of full functions dependency. A functional
dependency X→Y is a full functional dependency if removal of any
attribute A from X means that the dependency does not hold any more.
Let's assume, a school can store the data of teachers and the subjects they
teach. In a school, a teacher can teach more than one subject.
30
THIRD NORMAL FORM
If a relation is in 2NF and does not have any transitive partial
dependency, it is in 3NF. The 3NF algorithm is used to decrease data
duplication. It's also used to ensure data consistency. The relation must be
in third normal form if there is no transitive dependency for non-prime
characteristics. For every non-trivial function dependency X Y, a relation
is in third normal form if it meets at least one of the following conditions.
1. X is a super key
2. Y is a prime property, which means that each of its elements is part
of a candidate key.
3.
31
BCNF (BOYCE CODD NORMAL FORM)
The advanced form of 3NF is BCNF. It's more stringent than
3NF.A table is in BCNF if every functional dependency X → Y, X is the
super key of the table.The table should be in 3NF for BCNF, and LHS is
super important for every FD.Consider the following scenario: a
corporation with workers who work in multiple departments.
32
Candidate keys:
33
6.4 LOSSLESS JOIN DECOMPOSITION
34
7
RELATIONAL ALGEBRA
Unit structure:
7.1 Introduction
7.2 Selection
7.3 Projection
7.4 Set operations
7.5 Joins
7.6 Equi join and natural joins
7.1 INTRODUCTION
Union operation:
Assume you have two tuples, R and S. All tuples that are either in R or S,
or both in R and S, are included in the union operation. It eliminates the
duplicate tuples. It is denoted by ∪.
Notation: R ∪ S
The following conditions must be met by a union operation:
The attribute of the same number must be shared by R and S.
Duplicate tuples are eliminated automatically.
Consider two relations, BORROW and DEPOSITOR.
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
Set Operation:
Assume you have two tuples, R and S. All tuples in both R and S
are included in the set intersection operation.It is denoted by intersection
∩.
Notation: R ∩ S
For example:
Using the above DEPOSITOR table and BORROW table.
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DE
POSITOR)
Set Difference:
Assume you have two tuples, R and S. All tuples that are in R but
not in S are included in the set intersection operation. It is denoted by
intersection minus (-).
Notation: R - S
36
Example: Using the above DEPOSITOR table and BORROW table
∏ CUSTOMER_NAME (BORROW) -
∏ CUSTOMER_NAME (DEPOSITOR)
Cartesian product:
Each row in one table is combined with each row in the other table
using the Cartesian product. A cross product is another name for it. It is
denoted by X.
Notation: E X D
Rename Operation:
The output relation is renamed using the rename method. Rho (ρ)
is the symbol for it.For example, we can use the rename operator to
rename STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)
7.5 JOINS:
Types of Joins:
Natural Join:
A natural join is the set of tuples of all combinations in R and S that are
equal on their common attribute names.It is denoted by ⋈.Let's use the
EMPLOYEE and SALARY tables.
∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
Outer Join:
The join operation is extended by the outer join operation. It's utilised to
deal with information that's lacking.
(EMPLOYEE ⋈ FACT_WORKERS)
An outer join is of three types. Left outer join, right outer join and
full outer join.
37
Right outer join:
The set of tuples in R and S that are equivalent on their shared
attribute names is called the right outer join.The set of tuples in R and S
that are equivalent on their shared attribute names is called the right outer
join.It is denoted by ⟕.Using the above EMPLOYEE table and
FACT_WORKERS Relation.
EMPLOYEE ⟕ FACT_WORKERS
Full outer join:
The full outer join is like a left or right join, except it includes all
rows from both tables.Tuples in R that have no matching tuples in S and
tuples in S that have no matching tuples in R in their common attribute
name are used in full outer join.It is denoted by ⟕.Using the above
EMPLOYEE table and FACT_WORKERS table.
EMPLOYEE ⟕ FACT_WORKERS
7.6 EQUI-JOIN:
An inner join is another name for it. It's the most prevalent type of
connection. It is based on data that has been matched according to the
equality criteria. The comparison operator is used in the equi join.
CUSTOMER ⋈ PRODUCT
38
8
DDL STATEMENTS
Unit structure:
8.1 Creating Databases
8.2 Using Databases
8.3 Creating Tables with integrity constraints
8.4 Altering Tables
8.5 Renaming Tables
8.6 Dropping Tables
8.7 Backing Up and Restoring databases
SQL DDL commands are used to create schemas and tables and
gives an overview of basic data types used in creating a database. SQL
uses some terms such as table, row and column which are knows as
relation, tuple and attribute respectively. The basic command is CREATE
command. It can not only create table but also schemas, domains and
views.
CREATE DATABASE Database_Name;
39
DATATYPES:
CHAR(Size) It is used to specify a fixed length string that can
contain numbers, letters, and special characters.
Its size can be 0 to 255 characters. Default is 1.
40
8.3 CREATING TABLES WITH INTEGRITY
CONSTRAINTS:
DROP TABLE statement is used to remove a table definition and all the
data, indexes, triggers, constraints and permission specifications for that
table.
41
8.6 BACKING UP AND RESTORING DATA:
Reasons of Failure in a Database
There can be multiple reasons of failure in a database because of
which a database backup and recovery plan is required. Some of these
reasons are:
User Error - Normally, user error is the biggest reason of data
destruction or corruption in a database. To rectify the error, the
database needs to be restored to the point in time before the error
occurred.
Hardware Failure - This can also lead to loss of data in a database.
The database is stored on multiple hard drives across various locations.
These hard drives may sometimes malfunction leading to database
corruption. So, it is important to periodically change them.
Catastrophic Event - A catastrophic event can be a natural calamity
like a flood or earthquake or deliberate sabotage such as hacking of the
database. Either way, the database data may be corrupted, and backup
may be required.
Methods of Backup
The different methods of backup in a database are:
Full Backup - This method takes a lot of time as the full copy of the
database is made including the data and the transaction records.
Transaction Log - Only the transaction logs are saved as the backup
in this method. To keep the backup file as small as possible, the
previous transaction log details are deleted once a new backup record
is made.
Differential Backup - This is similar to full back up in that it stores
both the data and the transaction records. However only that
information is saved in the backup that has changed since the last full
backup. Because of this, differential backup leads to smaller files.
Database Recovery
There are two methods that are primarily used for database
recovery. These are:
Log based recovery - In log-based recovery, logs of all database
transactions are stored in a secure area so that in case of a system
failure, the database can recover the data. All log information, such
as the time of the transaction, its data etc. should be stored before
the transaction is executed.
Shadow paging - In shadow paging, after the transaction is
completed, its data is automatically stored for safekeeping. So, if
the system crashes in the middle of a transaction, changes made by
it will not be reflected in the database.
42
9
DML STATEMENTS
Unit Structure
9.1 DML Commands
9.2 Conditional select
9.3 In clause (Set membership Test)
9.4 Between clause (Range Test)
9.5 Order By clause
9.6 Group By
9.7Aggregate functions
Syntax:
INSERT INTO TABLE_NAME
(col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);
For example:
INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");
Syntax:
UPDATE table_name SET [column_name1= value1,...column_na
meN = valueN] [WHERE CONDITION]
43
For example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
Syntax:
DELETE FROM table_name [WHERE condition];
For example:
DELETE FROM javatpoint
WHERE Author="Sonoo";
SELECT statement is used to fetch the data from a database table which
returns this data in the form of a result table. These result tables are called
result-sets.
The basic syntax of the SELECT statement with the WHERE clause is
as shown below.
SELECT column1, column2, columnN
FROM table_name
WHERE [condition]
You can specify a condition using the comparison or logical operators like
>, <, =, LIKE, NOT, etc.
44
The following code is an example which would fetch the ID, Name and
Salary fields from the CUSTOMERS table, where the salary is greater
than 2000 −
SQL> SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY >2000;
This would produce the following result −
| ID | NAME | SALARY |
| 4 | Chaitali| 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
45
9.5 ORDER BY clause:
46
If you want to know the total amount of the salary on each
customer, then the GROUP BY query would be as follows.
1. COUNT FUNCTION
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.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( [ALL|DISTINCT] expression )
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It
works on numeric fields only.
Syntax: SUM() or SUM( [ALL|DISTINCT] expression )
Example: SUM()
SELECT SUM(COST) FROM PRODUCT_MAST;
47
3. AVG function
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() or AVG( [ALL|DISTINCT] expression )
Example:
SELECT AVG(COST)
FROM PRODUCT_MAST;
4. MAX Function
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. Syntax:
MAX() or MAX( [ALL|DISTINCT] expression )
Example:
1. SELECT MAX(RATE)
2. FROM PRODUCT_MAST;
5. MIN Function
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.
SyntaxMIN() or MIN( [ALL|DISTINCT] expression )
Example:
SELECT MIN(RATE)
FROM PRODUCT_MAST;
48
Unit - III
10
FUNCTINS, JOIN, SUBQUERIES, VIEWS,
DATA PROTECTION
AND DCL STATEMENTS
Unit Structure
10.0 Objectives
10.1 Introduction
10.2 Functions
10.2.1 String Functions
10.2.2 Math Functions
10.2.3 Date Functions
10.3 Join
10.3.1 Equi joins
10.3.2 Non-Equi joins
10.4 Subqueries
10.4.1 Nested subqueries, subqueries with IN
10.4.2 subqueries with ALL
10.4.3 subqueries with ANY
10.4.4 correlated subqueries
10.4.5 subqueries withEXISTS
10.4.6 subqueries restrictions
10.5 Database Protection
10.5.1 Security Issues
10.5.2 Threats to Databases
10.5.3 Security Mechanisms
10.5.4 Role of DBA
10.6 Views
10.6.1 Create Views
10.6.2 DropViews
10.6.3 Update Views
10.7 DCL Statements
10.7.1 Privileges introduction
10.7.2 Granting/revoking privileges,
10.7.3 Viewing privileges,
10.8 List of References
49
10.9 Bibliography
10.10 Unit End Exercises
10.0 OBJECTIVES: -
10.1 INTRODUCTION: -
Views can join and simplify multiple tables into a single virtual table.
Views can act as aggregated tables, where the database engine aggregates
data (sum, average, etc.) and presents the calculated results as part of the
data. Views can hide the complexity of data.
SQL functions are sub-programs, which are commonly used and re-used
throughout SQL database applications for processing or manipulating
data. All SQL database systems have DDL (data definition language) and
DML (data manipulation language) tools to support the creation and
maintenance of databases.
A JOIN clause is used to combine rows from two or more tables, based on
a related column between them.
10.2 FUNCTIONS: -
CONCAT:
It merges two or more strings or a string and a data value together
Example:
SELECT CONCAT('summer ','18') FROM DUAL;
50
INSTR:
The INSTR() function returns the position of the first occurrence of a
string in another string.
Example:
SELECT INSTR("RamSham.com", "3") AS MatchPosition;
LEFT:
This function returns the leftmost n characters from the string str. If the
string is empty, it returns NULL.
Example:
SELECT LEFT('RamSham', 4);
RIGHT:
This function returns the rightmost n characters from the string str. If the
string is empty, it returns NULL.
Example:
SELECT RIGHT('RamSham', 5);
MID:
The MID() function extracts a substring from a string (starting at any
position).
Example:
SELECT MID("SQL Tutorial", 5, 3) AS ExtractString;
LENGTH:
Find outs the length of given string.
Example:
SELECT LENGTH (‘abcd’) FROM DUAL
LOWER:
Converts a string to all lowercase characters.
Example:
SELECT LOWER (‘ABCD’) FROM DUAL
UPPER:
Converts a string to all uppercase characters.
Example:
SELECT UPPER (‘abcd’) FROM DUAL
REPLACE:
It returns character string with each occurrence of search string replaced
with [repstring]
Example:
SELECT REPLACE (‘Tick and Tock’, ‘T’,’C’) FROM DUAL
STRCMP:
This function compares both the strings str1 and str2. It returns 0 if both
strings are equal, 1 if str1 is greater than str2 and -1 if if str2 is greater than
str1.
51
Example:
SELECT STRCMP('HARRY', 'HARRY');
TRIM:
The TRIM() function removes the space character OR other specified
characters from the start or end of a string.
Example:
SELECT TRIM('#! ' FROM ' #SQL Tutorial! ') AS TrimmedString;
LTRIM:
Removesleading spaces from a string
Example:
SELECT LTRIM (‘abcd) FROM DUAL;
RTRIM:
Removes trailing spaces from a string
Example:
SELECT RTRIM (‘abcd’) FROM DUAL;
ABS:
This function returns the absolute value of X.
Example:
Select abs(-6);
CEIL:
This returns the smallest integer value that is either more than X or equal
to it.
Example:
SELECT CEIL(5.7);
FLOOR:
This returns the largest integer value that is either less than X or equal to
it.
Example:
SELECT FLOOR(5.7);
MOD:
The variable X is divided by Y and their remainder is returned.
Example:
SELECT MOD(9,5);
POW:
This function returns the value of x raised to the power of Y
Example:
SELECT POWER(2,5);
52
SQRT:
This function returns the square root of X.
Example:
SELECT SQRT(9);
ROUND:
This function returns the value of X rounded off to the whole integer that
is nearest to it.
Example:
SELECT ROUND(5.7);
ADDDATE:
ADDDATE() is a synonym for DATE_ADD().
Example:
SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);
DATEDIFF:
DATEDIFF() returns expr1 . expr2 expressed as a value in days from one
date to the other. Both expr1 and expr2 are date or date-and-time
expressions. Only the date parts of the values are used in the calculation.
Example :-
SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');
DAY:
The DAY() is a synonym for the DAYOFMONTH() function. Returns the
day of the month for date, in the range 0 to 31.
Example:
SELECT DAYOFMONTH('1998-02-03');
MONTH:
Returns the month for date, in the range 0 to 12.
Example:
SELECT MONTH('1998-02-03')
YEAR:
Returns the year for date, in the range 1000 to 9999, or 0 for the .zero.
date.
Example:
SELECT YEAR('98-02-03');
HOUR:
Returns the hour for time. The range of the return value is 0 to 23 for time-
of-day values. However, the range of TIME values actually is much larger,
so HOUR can return values greater than 23.
Example:
SELECT HOUR('10:05:03');
53
MIN:
Returns the minute for time, in the range 0 to 59.
Example:
SELECT MINUTE('98-02-03 10:05:03');
SEC:
Returns the second for time, in the range 0 to 59.
Example:
SELECT SECOND('10:05:03');
NOW:
Returns the current date and time as a value in 'YYYY-MM-DD
HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether
the function is used in a string or numeric context. This value is expressed
in the current time zone.
Example:
SELECT NOW();
REVERSE:
The REVERSE() function reverses a string and returns the result.
Example:
SELECT REVERSE('SQL Tutorial');
10.3 JOINS
Example:
If the first table has 20 rows and the second table has 10 rows, the result
will be 20 * 10, or 200 rows.
This query takes a long time to execute.
Let us use the below two tables to explain the sql join conditions.
54
Database table "product";
Joins can be classified into Equi join and Non Equi join.
1. SQL Equi joins
2. SQL Non equi joins
Example:
We can get Information about a customer who purchased a product and
the quantity of product.
Example:
To display the product information for each order the query will be as
given below.
Since retrieving the data from two tables, you need to identify the
common column between these two tables, which is the product_id.
55
QUERY:
SELECT order_id, product_name, unit_price, supplier_name,
total_units
FROM product, order_items
WHERE order_items.product_id = product.product_id;
The columns must be referenced by the table name in the join condition,
because product_id is a column in both the tables and needs a way to be
identified.
Example
Display all the product data along with order items data, with null values
displayed for order items if a product has no order item.
QUERY
SELECT p.product_id, p.product_name, o.order_id, o.total_units
FROM order_items o, product p
WHERE o.product_id (+) = p.product_id;
Output:
Product_id product_name order_id total_units
100 Camera
101 Television 5103 10
102 Refrigerator 5101 5
103 IPod 5102 25
It is necessary to ensure that the join statement defines an alias for both
copies of the table to avoid column ambiguity.
Example
SELECT a.sales_person_id, a.name, a.manager_id, b.sales_person_id,
b.name
FROM sales_person a, sales_person b
WHERE a.manager_id = b.sales_person_id;
56
10.3.2 SQL NON-EQUI JOIN:
A Non Equi Join is a SQL Join whose condition is established using all
comparison operators except the equal (=) operator.
Like >=, <=, <, >
Example:
Find the names of students who are not studying either Economics, the sql
query would be like, (let’s use Employee table defined earlier.)
QUERY:
SELECT first_name, last_name, subject FROM Employee
WHERE subject != 'Economics'
Output:
first_name last_name subject
Anajali Bhagwat Maths
Shekar Gowda Maths
Rahul Sharma Science
Stephen Fleming Science
10.4 SUBQUERIES
SELECT *
FROM product
WHERE id IN (SELECT product_idFROM provider_offer WHERE
provider_id = 156);
a table
SELECT MAX(average.average_price)
FROM ( SELECTproduct_category,
AVG(price) AS average_price FROM product GROUP BY
product_category) average;
or a value
SELECT id
FROM purchase
WHERE value >( SELECT AVG(value) FROM purchasec);
57
10.4.1 NestedSubqueries: -
Nested subqueries are subqueries that don’t rely on an outer query. In
other words, both queries in a nested subquery may be run as separate
queries.
SELECT
FROM
WHERE [NOT] IN (subquery)
SELECT *FROM clientWHERE city IN (SELECT city FROM
provider);
The example subquery returns all clients that are FROM the same city as
the product providers.
The IN operator checks if the value is within the table and retrieves the
matching rows.
For example, the following query returns all of the models and producers
of bikes that have a price greater than the most expensive headphones.
The following query returns all of the models and producers of bikes that
have a price greater than at least one of the headphones.
58
AND price >ANY(SELECT price FROM product WHERE
product_category = 'headphones');
10.4.4 CorrelatedSubqueries:-
Subqueries are correlated when the inner and outer queries are
interdependent, that is, when the outer query is a query that contains a
subquery and the subquery itself is an inner query. Users that know
programming concepts may compare it to a nested loop structure.
The inner query calculates the average value and returns it. In the outer
query’s WHERE clause, we filter only those purchases which have a value
greater than the inner query’s returned value.
SELECT id
FROM purchase p1
WHERE date > '2013-07-15'
AND value >( SELECT AVG(value) FROM purchase p2 WHERE
p1.date = p2.date );
The query returns purchases after 15/07/2014 with a total price greater
than the average value FROM the same day.
59
SELECT p1.id
FROM purchase p1, purchase p2
WHERE p1.date = p2.date
AND p1.date> '2013-07-15'
GROUP BY p1.idHAVING p1.value > AVG(p2.value);
The subquery returns the table that contains the average value for each
purchase for each day. We join this result with the Purchase table on
column ‘date’ to check the condition date > ’15/07/2014'.
SELECT id
FROM
purchase,
(
SELECT date, AVG(value) AS average_value
FROM purchase
WHERE date > '2013-07-15'
GROUP BY date
) average
WHERE purchase.date = average.date
AND purchase.date> '2013-07-15'
AND purchase.value>average.average_value;
60
before the results FROM the subquery are used. If a query that contains a
subquery can be written using a join, it should be done this way. Joins
usually allow the query optimizer to retrieve the data in a more efficient
way.
61
The where clause of a subquery can contain an aggregate function
only if the subquery is in a having clause of an outer query and the
aggregate value is a column from a table in the from clause of the
outer query.
The result expression from a subquery is subject to the same limits
as for any expression. The maximum length of an expression is
16K.
Now there is greater emphasis on database security than in the past as the
amount of data stored in corporate database is increasing and people are
depending more on the corporate data for decision-making, customer
service management, supply chain management and so on. Any loss or
unavailability to the corporate data will cripple today's organization and
will seriously affect its performance. Now the unavailability of the
database for even a few minutes could result in serious losses to the
organization.
Database security is a broad area that addresses many issues, including the
following:
The account level. At this level, the DBA specifies the particular
privileges that each account holds independently of the relations in
the database.
The relation (or table) level. At this level, the DBA can control
the privilege to access each individual relation or view in the
database.
References privilege on R. This gives the account the capability
to reference (or refer to) a relation R when specifying integrity
64
constraints. This privilege can also be restricted to specific
attributes of R.
Notice that to create a view, the account must have the SELECT privilege
on all relations involved in the view definition in order to specify the
query that corresponds to the view.
3. Revoking of Privileges
In some cases it is desirable to grant a privilege to a user temporarily. For
example, the owner of a relation may want to grant the SELECT privilege
to a user for a specific task and then revoke that privilege once the task is
completed. Hence, a mechanism for revoking privileges is needed. In SQL
a REVOKE command is included for the purpose of cancelling privileges.
Role of DBA
A person having who has central control over data and programs that
access the data is called DBA. Following are the functions of the DBA.
65
Schema definition: DBA creates database schema by executing
Data Definition Language (DDL) statements.
Storage structure and access method definition
Schema and physical organization modification: If any changes
are to be made in the original schema, to fit the need of your
organization, then these changes are carried out by the DBA.
Granting of authorization for data access: DBA can decide
which parts of data can be accessed by which users. Before any
user access the data, DBMS checks which rights are granted to the
user by the DBA.
Routine maintenance: DBA has to take periodic backups of the
database, ensure that enough disk space is available to store new
data, ensure that performance of DBMS ix not degraded by any
operation carried out by the users.
Performance monitoring: Here DBMS should respond to
changes in requirements, i.e.,changing details of storage and access
thereby organising the system so as to get the performance that is `
best for the enterprise'.
10.6 VIEWS: -
Definition:
A view is a virtual table that consists of columns from one or more
tables.
A virtual table is like a table containing fields but it does not
contain any data. In run time it contains the data and after that it
gets free.
But table stores the data in database occupy some space.
Just like table, view contains Rows and Columns which is fully
virtual based table.
Base Table -The table on which view is defined is called as Base
table.
10.6.1 Creating a VIEW
This statement is used to create a view.
Syntax:
CREATE VIEW view_name
The CREATE statement assigns a name to the view and also gives
the query which defines the view.
To create the view, one should must have privileges to access all of
the base tables on which view is defined.
66
The create view can change name of column in view as per
requirements.
Horizontal View
A Horizontal view will restrict the user’s access to only a few rows of the
table.
Example:
Define a view for Sue (employee number 1004) containing only orders
placed by customers assigned to her.
CREATE VIEW SUEORDERS AS SELECT *
FROM ORDERS WHERE CUST IN
(SELECT CUST_NUM FROM CUSTOMERS WHERE
CUST_REP=1004)
Vertical View
A vertical view restricts a user’s access to only certain columns of a table.
Ex:
CREATE VIEW EMP_ADDRESS AS
SELECT EMPNO, NAME, ADDR1, ADDR2, CITY FROM
EMPLOYEE
ROW/COLUMN SUBSET VIEW.
Views can be used to restrict a user to access only selected set of rows
and columns of a table in a database.
This view generally helps us to visualize how view can represent the
base table.
This type of view is combination of both horizontal and vertical views.
Ex:
CREATE VIEW STUDENTS_PASSED AS SELECT ROLLNO,
NAME, PERCENTAGE
FROM STUDENTS
WHERE RESULT =’PASS’
Grouped View
A grouped view is one in which query includes GROUPBY
CLAUSE.
It is used to group related rows of data and produce only one result
row for each group.
Ex:
Find summary information of Employee Salaries in sales Department.
CREATE VIEW Summary_Empl_Sal (
67
Total_Employees, Minimum_salary, Maximum_Salary,
Average_salary, Total_salary)
AS
SELECT COUNT(EmpID),
Min(Salary), Max(Salary), Avg(Salary), SUM(Salary), FROM
Employee
GROUP BY Department HAVING Department=’Sales’;
View Call
SELELCT *FROM Summary_Empl_Sal
The above Query will give,
Total No. Of Employees in sales Department, Minimum Salary in sales
Department.
Maximum Salary in sales Department. Average Salary in sales
Department.
Total Salary of Employees in sales Department.
Joined Views
A Query based on more than one base table is called as Joined
View.
It is also called as Complex View
This gives a way to simplify multi table queries by joining two or
more table query in the view definition that draws its data from
multiple tables and presents the query results as a single view.
The view once it is ready we can retrieve data from multiple tables
without joining any table simply by accessing a view created.
Ex:
Company database find out all EMPLOYEES for respective
DEPARTMENTS.
CREATE VIEW Emp_Details As
Select Employee,EmpID, Department, DeptID, Department,
DeptName From
Where Employee.DeptID=Department.DeptID;
View Call
SELECT * FROMEmp_Details
68
Syntax:
DROP VIEW <VIEW NAME> [CASCADE/RESTRICT]
CASCADE: It deletes the view with all dependent view on original view.
RESTRICT: It deletes the view only if they’re in no other view depends
on this view.
Example:
Consider that we have view VABC and VPQR. ViewVPQR depends on
VABC.
Query:
DROP view VABC
If we drop VABC, then cascading affect takes place and view VPQR is
also dropped.
Thus, default option for dropping a view is CASCADE. The CASCADE
option tells DBMS to delete not only the named view, but also query
views that depend on its definition. But,
QUERY:
DROP view VABC RESTRICT
Here, the query will fail because of RESTRICT option tells DBMS to
remove the view only if no other views depend on it. Since VPQR
depends on VABC, will cause an error.
The WHERE clause must not include a subquery; only simple row-
by-row search conditions may appear.
The query must not include a GROUP BY or a HAVING clause.
69
The following code block has an example to update the age of Ramesh.
UPDATE CUSTOMERS_VIEW
SET AGE = 35
WHERE name = 'Ramesh';
This would ultimately update the base table CUSTOMERS and the same
would reflect in the view itself.
10.7 DCLSTATEMENTS: -
1) System Privileges:
System privileges are privileges given to users to allow them to perform
certain functions that deal with managing the database and the server
e.g Create user, Create table, Drop table etc.
2) Object Privileges:
Object privileges are privileges given to users as rights and
restrictions to change contents of database object – where database
objects are things like tables, stored procedures, indexes, etc.
Ex. Select,insert,delete,update,execute,references etc
Syntax:
Revoke<object privileges>on<object name>from<username> ;
Example:
REVOKE SELECT, UPDATE ON student FROM BCA, MCA;
www.w3school.com
www.tutorialspoint.com
wielyIndia.com or DreamtechPress.com
www.williamstannings.com
10.9 BIBLIOGRAPHY
Database System Concepts(4th Edition) by Korth, Tata McGraw Hill
Introduction to Database Management Systems by ISRD Group, Tata
McGraw Hill
SQL ,PL/SQL the Programming language of Oracle by Ivan
Bayross(4th edition), BPB.
Advanced Database Management System by Dasgupta Chakrabarti,
Dreamtech
71
10.10 EXERCISES
72