BSC It Dbms Notes
BSC It Dbms Notes
BSC It Dbms Notes
com
SYLLABUS
DATABASE MANAGEMENT SYSTEMS
Unit I
Unit- II
Data Models
The importance of data models, Basic building blocks, Business rules, The evolution
of data models, Degrees of data abstraction.
Unit-III
Unit- IV
Unit- V
Unit-VI
Books:
A Silberschatz, H Korth, S Sudarshan, Database System and Concepts, fifth Edition McGraw-Hill ,
Rob, Coronel, Database Systems, Seventh Edition, Cengage Learning.
1)
2)
3)
4)
5)
6)
7)
8)
9)
Design a Database and create required tables. For e.g. Bank, College Database
Apply the constraints like Primary Key , Foreign key, NOT NULL to the tables.
Write a sql statement for implementing ALTER,UPDATE and DELETE
Write the queries to implement the joins
Write the query for implementing the following functions: MAX(),MIN(),AVG(),COUNT()
Write the query to implement the concept of Intergrity constrains
Write the query to create the views
Perform the queries for triggers
Perform the following operation for demonstrating the insertion , updation and deletion
using the referential integrity constraints
10) Write the query for creating the users and their role.
1
INTRODUCTION TO DATABASE
MANAGEMENT SYSTEM
Unit Structure
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
Objectives
Introduction
What is Database Management System
History of Database System
Purpose of Database System
Advantages and Disadvantages of Database System
Summary
Model Questions
1.0
OBJECTIVES
1.1
INTRODUCTION
2
information, retrieve and manage this information in timely
manner.
Today DBMS not only used to insert, update and delete the data
stored in database.
1.2
1.3
With the time passed by, the DBMS system has matured
significantly. As the development of relational DBMS has
reached to larger users and the number of benefits from the
same, it was widely accepted and many corporate houses
started using this system for their day to day activities.
The period between 1980 and 1990s saw many advances in the
field of DBMS system. Several vendors try to build a system
where more stress is given on complex analysis of data within
an enterprise.
It allowed the user to write their queries through Web forms, and
the formatted output is tabulated through mark-up languages
like HTML.
1.4
7
o Also there are several copies of files are stored, if any one of
the file is changed, the different versions of same file may
not be updated which leads to inconsistency of data.
2. Difficulty in accessing the data
o Consider the airline reservation system. If the senior
management of company wants to access the information of
all its customers who are living in the same postal code, it
has to be done manually because current file processing
system does not allow the user to obtain this information.
o So in the above case, there are two options. Either the
application programmer has to write a new application
program to satisfy the unusual request or could get this
information manually.
o In former case, it doesnt guarantee that the same query will
be asked and same application program would be used in
future.
o If a query changes, a new application program should be
written to get the needed information.
3. Data isolation
o One of the major problems with the file system is that the
data is scattered and stored in multiple locations and in
different formats.
o Hence in order to retrieve the needed information from
multiple location and in different formats is a very difficult to
proceed with the help of application program.
4. Concurrent access anomalies
o In order to speed up the performance of the system and
faster response to applications, many systems allow the user
to update the data concurrently.
o Suppose two users located at different locations wants to
book the tickets, there might be situation that both of the
people will be given the same seat because the data is
stored in multiple locations and both of them will be given a
seat from individual copy of the data.
8
o Therefore there should be some protection mechanism to
avoid this concurrent updates.
5. Security problems
o Every user in this system should be able to access the data
which he is allowed to access and not all the data.
o For example, the salesperson in an organization should be
allowed to access the data related to him and should not be
allowed to access data which is used HR team or finance
department in an organization.
o If the new constraints are added to avoid this kind of
unauthorized access, enforcing these constraints is difficult
because the existing application programs are added to the
system in an adhoc manner.
6. Integrity problems
o Data stored in the database should be allowed to satisfy
certain constraint checking.
o For eg, before adding a new employee in the Employee
table, if we check the age of the employee and if we apply
constraint such that only those employee whose age is
greater than 18 years should be allowed to enter in the table
which means that before the new data is inserted the age of
the employee should be calculated.
o When a new constraint such as one which is discussed
above is added, it becomes difficult to change the existing
programs to enforce the new constraints.
7. Atomicity problems
o Every application system is assumed to fail at some point in
near future.
o In many applications, if the system fails, the data should be
rolled back to the state before the failure occurs.
o Consider the customer is withdrawing some cash from the
ATM machine from his own account and if the failure
happens in the system, it should not happen that the amount
is deducted from customer account but the customer is not
getting any cash from the machine.
9
o In simple word the withdrawn should be atomic- it must be
happen in its entirely or not at all.
o Another disadvantage with file processing system is that it
becomes difficult to ensure atomicity.
1.5
OF
wasted.
used.
2. Elimination of Inconsistency
o In the file processing system information is duplicated
throughout the system. So changes made in one file may be
necessary be carried over to another file. This may lead to
inconsistent data. So we need to remove this duplication of
data in multiple file to eliminate inconsistency.
o Let us consider the following example of student.
o Imagine that a particular student has opted for Embedded
system as one of the elective subject in Sem V for TYBScIT
Sem V examination while filling up the examination form.
o If, after getting the hall ticket the student realize that rather
than expecting Embedded system as the choice of elective
10
subject in the hall ticket, if some other subject is highlighted,
it means that the data for that student has not correctly
inserted in the database.
o To avoid the above problem, there is a need to have a
centralize database in order to have this conflicting
information.
o On centralizing the data base the duplication will be
controlled and hence inconsistency will be removed.
3. Better service to the users
o A DBMS is often used to provide better services to the
users. In conventional system, availability of information is
often poor, since it normally difficult to obtain information in a
timely manner because our existing systems are not capable
to produce the same.
o Once several conventional systems are combined to form
one centralized database, the availability of information and
its updateness is likely to improve since the data can now be
shared and DBMS makes it easy to respond to anticipated
information requests.
o Centralizing the data in the database also means that user
can obtain new and combined information easily that would
have been impossible to obtain otherwise.
o Also use of DBMS should allow users that don't know
programming to interact with the data more easily, unlike file
processing system where the programmer may need to write
new programs to meet every new demand.
4. Flexibility of the System is Improved
o Since changes are often necessary to the contents of the
data stored in any system, these changes are made more
easily in a centralized database than in a conventional
system.
o Applications programs need not to be changed on changing
the data in the database.
11
5. Integrity can be improved
o Since data of the organization using database approach is
centralized and would be used by a number of users at a
time, it is essential to enforce integrity-constraints.
o In the conventional systems because the data is duplicated
in multiple files so updating or changes may sometimes lead
to entry of incorrect data in some files wherever it is
applicable.
o For example: - The example of Hall Ticket Generation
system that we have already discussed, since multiple files
are to maintained, as sometimes you may enter a value for
subject which may not exist. Suppose Elective Subjects can
have values (Embedded Systems, Advanced Java, Web
Designing etc) but we enter a value 'Mathematics -I' for it, it
may lead to database inconsistency.
o Even if we centralized the database it may still contain
incorrect data. For example: Salary of full time clerk may be entered as Rs. 1500 rather
than Rs. 4500.
A student may be shown to have borrowed library books
but has no enrollment.
o The above problems can be avoided by defining the
validation procedures whenever any update operation is
attempted.
6. Standards can be enforced
o Standards are easier to enforce in database systems
because all the data in database is access through
centralized DBMS.
o Here standards may relate to the naming of data, structure
of data, format of the data etc.
o Standardizing stored data formats is usually desirable for the
purpose of data interchange or migration between systems.
7. Security can be improved
o In conventional systems, applications are developed in an
adhoc manner.
12
o Often different system of an organization would access
different components of the operational data, in such an
environment enforcing security can be quiet difficult.
o Setting up of a database makes it easier to enforce security
restrictions since data is now centralized.
o It is easier to control who has access to what parts of the
database. Different checks can be established for each type
of access (retrieve, modify, delete etc.) to each piece of
information in the database.
o Consider an example of banking in which the employee at
different levels may be given access to different types of
data in the database.
o For example, a clerk may be given the authority to know only
the names of all the customers who have a loan in bank but
not the details of each loan the customer may have.
o This can be accomplished by giving the privileges to each
employee.
8. Organization's requirement can be easily identified
o All organizations have sections and departments and each
of these units often consider the work of their unit as the
most important and therefore consider their need as the
most important.
o Once a database has been setup with centralized control, it
will be necessary to identify organization's requirement and
to balance the needs of the competition units.
o So it may become necessary to ignore some requests for
information if they conflict with higher priority need of the
organization.
o It is the responsibility of the DBA (Database Administrator) to
structure the database system to provide the overall service
that is best for an organization.
o For example, a DBA must choose best file Structure and
access method to give fast response for the high critical
applications as compared to less critical applications.
13
9. Data Model must be developed
o
14
incurred in purchasing different tools, adopting different
techniques as per the requirement.
5. Initial training required for all programmers and user.
Large amount of human efforts, the time and cost is needed to
train the end users and application programmers in order to get
used to the database systems.
1.6
SUMMARY
15
6. Standards can be enforced.
7. Security can be improved etc.
1.7
MODEL QUESTIONS
1. What is the purpose of building a DBMS system?
2. Explain the history of Database system
3. What is the database system? Explain it with its
advantages and disadvantages
4. Compare between File systems and database systems
5. What are the limitations of File processing systems? How
that can be solved by using Database system?
2
INTRODUCTION TO RELATIONAL
DATABASE MANAGEMENT SYSTEM
Unit Structure
2.0
Objectives
2.1
Introduction to RDBMS
2.2
The Relational Model
2.3
Introduction to SQL
2.4
Working with relations of RDBMS
2.5
Advantages and Disadvantages of
System
2.6
Summary
2.7
Model Questions
Relational Database
1.0
OBJECTIVES
2.1
2
o Although there are many different types of database
management systems, relational databases are by far the most
common.
o Other types include hierarchical databases and network
databases.
3
o Before we proceed with more details on relational model,
consider the following example. We will also define various
terminologies associated with relational model.
o Consider the IDOLSYIT table given below
4
o We need to understand the different types of keys
associated with relational databases as follows
o Primary key: In every relational database, every table has
a particular column or set of columns whose value
uniquely identify each row in the table. Such a column is
called the primary key of the table.
o In our IDOLSYIT table, we can call S_ID as a primary key
because it can uniquely define the values from this table.
o The primary key has a different unique value for each
row in the table, so no two rows of a table with a primary
key are exact duplicates of one another.
o In a table, if every row in a given table is different from all
other rows is called the relation in mathematical terms.
o The term relational databases come because relations
are the base of a relational model.
o A column in one table whose value matches with the
primary key of another table is called as a foreign key of
the table.
2.3
INTRODUCTION TO SQL
6
3. SQL is a database administration language.
It allows the database administrators to define database
structures and can also control the access to the stored
data.
4. SQL is a client/server language.
In the client server architecture, the client programs uses
SQL to communicate through a network to access the
shared data stored in database.
5. SQL is an Internet data access language.
Since SQL is a standard language, many Internet web
servers makes use of SQL to interact with company data
and Internet application servers for accessing companywide databases.
6. SQL is a distributed database language.
Many DDBMS (Distributed Database Management
System) uses SQL to distribute the data across many
connected computer systems. The DBMS software
running on the local systems makes use of SQL to
communicate with other systems by sending request for
data access.
7. SQL is a database gateway language.
SQL is most of the time used as a gateway which allows
one brad of DBMS to communicate with the other brands.
2.4
7
o When the user is creating the above table, the user now
become the owner of the newly created table, which is given the
name specified in the above statement.
o Note that the table name must be a legal SQL name, and it
must not conflict with any of the existing tables.
o A slightly complex create table is discussed below which allows
to create a new table and also to set up the relationship
between different tables
Create table NEWORDERS (ONUM INTEGER NOT
NULL,
ODATE DATE NOT NULL,
CUST INTEGER NOT NULL,
REP INTEGER,
MFR CHAR (3) NOT NULL,
PRODUCT CHAR (5) NOT NULL,
QTY INTEGER NOT NULL,
AMT MONEY NOT NULL,
PRIMARY KEY (ONUM),
CONSTRAINT PLACEDBY
FOREIGN KEY (CUST)
REFERENCES
NEWCUSTOMERS
ON DELETE CASCADE,
CONSTRAINT TAKENBY
FOREIGN KEY (REP)
REFERENCES NEWSALESREPS
ON DELETE SET NULL,
CONSTRAINT ISFOR
FOREIGN KEY (MFR, PRODUCT)
REFERENCES NEWPRODUCTS
ON DELETE RESTRICT)
o In the above example, onum denotes the order number for
every order taken by customer. Since it is having only unique
values and no duplicates are allowed here, we can make this as
primary key.
o In the above example, the column name CUST has made a
foreign key which is currently referencing NEWCUSTOMERS
table. REP column is made a foreign key referencing
NEWSALESREPS table and MFR, PRODUCT is made the
composite foreign key referencing the NEWPRODUCTS table.
o By setting the primary- foreign key relationship (Parent- Child
Relationship), it allows the data to flow easily between the set of
tables define in the Create table statement.
9
o In the above example, an existing table of IDOLSYIT is modified
with a new column is added as subject which was not there
earlier.
2. Alter Table IDOLTYIT
Drop Email
o In the above example, an existing table of IDOLTYIT is modified
with a existing column is removed known as email which was
earlier present in the table.
3. Alter Table NEWOFFICES
Add Constraint Myoffices
Foreign key ( NRegion)
References Regions
o In the above example, the NRegion column in the
NEWOFFICES table is made a foreign key for the newly created
Regions Table.
4. Alter Table NEWSALESREPS
DROP Constraint NWORKSIN
Foreign Key (NewRepOffice)
References NEWOFFICES
Alter Table NEWOFFICES
Drop Primary Key (Office)
3. In the above example, the primary key of the NEWOFFICES
table has been changed. Before changing it, first we need to
drop the reference to foreign key and primary key and then we
need to select a new column from the table and should be made
as a primary key.
10
o Several different types of data integrity constraints are
commonly found in relational databases, includes the
following
1. Required data checking
o There are instances when some columns in a database
must contain a valid data value in every row; they are not
allowed to contain missing or NULL values.
o In the sample database, every order must have an
associated customer who placed the order. The DBMS
can be asked to prevent NULL values in this column.
2. Validity checking
o Every column in a database has a domain, a set of data
values that are legal for that column. The DBMS can be
asked to prevent other data values in these columns.
3. Entity integrity
o The primary key of a table must contain a unique value
in each row, which is different from the values in all other
rows.
o Duplicate values are illegal, because they wouldn't allow
the database to distinguish one entity from another. The
DBMS can be forced to enforce this unique values
constraint.
4. Referential integrity
o A foreign key in a relational database links each row in
the child table containing the foreign key to the row of
the parent table containing the matching primary key
value.
o The DBMS can be asked to enforce this foreign
key/primary key constraint.
5. Other data relationships
o The real-world situation modelled by a database will
often have additional constraints that govern the legal
data values that may appear in the database.
o The DBMS can be asked to check modifications to the
tables to make sure that their values are constrained in
this way.
11
6. Business rules
o Updates to a database may be constrained by business
rules governing the real-world transactions that are
represented by the updates.
o For example, there might be a business rule such as the
new employee should be added only if the age of the
employee is between 18 to 35 years.
2.5
ADVANTAGES
RDBMS
AND
DISADVANTAGES
OF
ADVANTAGES
1. Simple data Structures
o By storing the data in table format, it becomes easier for the
users to understand the structure of database and use it.
o RDBMS provides data access using a natural structure and
organization of the data.
o When the users are writing a queries, database queries cam
search any columns for any matching entries.
2. Multi-user database access monitoring
o RDBMS allows the multiple database users to access a
database simultaneously.
o
12
4. Network Access
o RDBMSs provide access to the database through a server
daemon, a specialized software program that listens for
requests on a network, and allows database clients to
connect to and use the database.
o Users do not need
computer system
convenience for the
database. Network
desktop tools and
databases.
5. Speed
o The relational database model is not the fastest data
structure. RDBMS advantages, such as simplicity, make the
slower speed a fair trade-off.
o Optimizations built into an RDBMS, and the design of the
databases, enhance performance, allowing RDBMSs to
perform more than fast enough for most applications and
data sets.
o Improvements in technology, increasing processor speeds
and decreasing memory and storage costs allow systems
administrators to build incredibly fast systems that can
overcome any database performance shortcomings.
6. Maintenance
o RDBMS feature maintenance utilities that provide database
administrators with tools to easily maintain, test, repair and
back up the databases housed in the system.
o Many of the functions can be automated using built-in
automation in the RDBMS, or automation tools available on
the operating system.
7. Language
o RDBMSs support a generic language called "Structured
Query Language" (SQL).
o The SQL syntax is simple, and the language uses standard
English language keywords and phrasing, making it fairly
intuitive and easy to learn.
13
2.6
SUMMARY
14
3.
4.
5.
6.
Entity integrity
Referential integrity
Other data relationships
Business rules
3
INTRODUCTION TO DATABASE
STRUCTURE
Unit Structure
3.0
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.0
Objectives
Levels of abstraction in DBMS
View of data
Role of Database users
Role of database administrators
Transaction Management
Database Structure
Summary
Model Questions
OBJECTIVES
Introduction
o In traditional system, each collection of application programs
had its own independent master file. The duplication of data
over master files could lead to inconsistent data.
o In early days, efforts were discovered to use a common master
file for a number of application programs resulted in problems of
integrity and security.
2
o The production of new application programs could require
amendments to existing application programs, resulting in
`unproductive maintenance'.
o Data structuring techniques, developed to exploit random
access storage devices, increased the complexity of the insert,
delete and update operations on data.
o As a first step towards a DBMS, packages of subroutines were
introduced to reduce programmer effort in maintaining these
data structures.
o However, the use of these packages still requires knowledge of
the physical organization of the data.
o A database system is a computer-based system to record and
maintain information. The information concerned can be
anything of significance to the organisation for whose use it is
intended.
o The database can hold a variety of different things. The
database concepts are divided into two concepts:
1. Schema
2. Data
o The schema is the structure of the database and the data is the
facts of the database.
o Consider our Salesperson database where we are storing the
facts of the salespeople working in an organization.
o Such facts could include salesperson name, address, date of
birth, and salary. In a database all the information on all
salespeople would be held in a single storage "container", called
a table.
o This table is a tabular object like a spreadsheet page, with
different salespeople as the rows, and the facts (e.g. their
names) as columns. Let us call this table Salesperson, and it
could look something like:
Salesperson
Name
Address
Date of Birth
Salary
Rakesh
M.G. Road
12/12/1960
11000
Dinesh
CST Road
15/11/1978
25000
Sudhir
JN Road
14/02/1985
15000
3
o At times as a database administrator, you want to protect user
from accidently entering wrong data. For example, you dont
want the user to enter the name in date of birth field in
database.
o Protecting the database against rubbish data is one of the most
important database design steps, and is what much of this
course is about. From what we know about the facts, we can
say things like:
1. NAME is a string, and needs to hold at least 12 characters.
2. ADDRESS is a string, and needs to hold at least 12
characters.
3. DOB is a date... The company forbids people over 100 years
old or younger than 18 years old working for them.
4. SALARY is a number. It must be greater than zero.
4
o The three levels of database architecture are
1. External Level: It is concerned with the way individual
user observes the data.
2. Conceptual Level: It can be regarded as a community
user view a formal description of data of interest to the
organisation, independent of any storage considerations.
3. Internal Level: It is concerned with the way in which the
data is actually used.
o Let us discuss this three levels in more detail
1. External Level
o A user is anyone who needs to access some portion of the data
from the database.
o They may range from application programmers to casual users
with complex adhoc queries.
o Each user may use the language according to its own choice.
o The application programmer may use a high level language
(e.g. COBOL) while the casual user will probably use a query
language.
o Regardless of the language used, it will include a data sublanguage (DSL) which is that subset of the language which is
concerned with storage and retrieval of information in the
database and may or may not be apparent to the user.
o A DSL is a combination of two languages:
1. A data definition language (DDL) which provides for the
definition or description of database objects
2. A data manipulation language (DML) which supports the
manipulation or processing of database objects.
o Each user sees the data in terms of an external view which is
defined by an external schema, consisting basically of
descriptions of each of the various types of external record in
that external view, and also a definition of the mapping between
the external schema and the underlying conceptual schema.
2. Conceptual Level
o It defines the logical definition of the database.
o It is also known as the community view.
5
o It is abstract representation of the entire information content of
the database.
o It is in general a view of the data as it actually is, that is, it is a
`model' of the `real world'.
o It consists of multiple occurrences of multiple types of
conceptual record, defined in the conceptual schema.
o In order to achieve data independence, the definitions of
conceptual records is defined in the conceptual schema.
3. Internal Level
o It is concern with the way the data are physically stored on the
hardware.
o Usually the internal level is described using the actual bytes and
machine-level terminology which is taken care by the DBMS
software.
o The internal view is a low-level representation of the entire
database consisting of multiple occurrences of multiple types of
internal (stored) records.
6
o The above figure represents the different levels of data
representation.
o The internal level would describe exactly which bytes contain
the information and how it can be accessed.
o If user1 is the payroll clerk, the external view contains the
information of salesperson.
o If the application programmer1 is designing billing program, he
would need all order information as well as information on the
particular sales representative in the external view.
o Consider a possible schema for a student database. The office
administrator wants immediate access to student information.
o The records clerk needs to be sure all student fees structure is
calculated and stored in database.
Data Independence
o The main advantage of using the data independence is that
both the user program and the database can be altered
independently of each other.
o Data independence is therefore defined as the capacity to
change one level of schema without changing the schema at the
next highest level.
o In a conventional system, applications are data dependent
which means that the way in which the data is organised in
secondary storage and the way in which it is accessed are both
dictated by the requirements of the application, and, moreover,
that knowledge of the data organisation and access technique is
built into the application logic.
o Data independence can be classified into two types
1. Logical data independence
o It is the ability to modify the conceptual schema without
affecting the existing external schemas.
o In logical data independence, the users are shielded from
changes in the logical structure of the data or changes in the
choice of relations to be stored.
7
o The changes to the conceptual schema, such as the addition
and deletion of entities, addition and deletion of attributes, or
addition and deletion of relationships must be possible
without changing existing external schemas or having to
rewrite application programs.
o Only the view definition and the mapping need be changed
in a DBMS that supports logical data independence.
2. Physical data independence
o The ability to modify the internal schema without having to
change the conceptual or external schemas is called
physical data independence.
o In physical data independence, the conceptual schema
insulates the users from changes in the physical storage of
the data.
o The changes to the internal schema, such as using different
file organizations or storage structures, using different
storage devices, modifying indexes or hashing algorithms
must be possible without changing the conceptual or
external schemas.
o In other words, physical data independence indicates that
the physical storage structures or devices used for storing
the data could be changed without necessitating a change in
the conceptual view or any of the external views.
o Note: The Logical data independence is difficult to achieve
than physical data independence as it requires the flexibility
in the design of database and programmer has to anticipate
the future requirements or modifications in the design of the
database.
3.3
8
o Example: Suppose the bank teller wants to transfer the
money after maturity of the fixed deposit amount of a
particular customer, needs to invoke a program called
transfer.
o This program ask the teller for the amount of money to be
transferred, the account to which the money is to be
transferred.
2. Application programmers
o They are the computer professionals who interact with the
system through DML calls, which are embedded in a
program written in a host programming language.
o Since the DML syntax is different from the host language
syntax, DMl calls are usually prefaced by a special character
so that the appropriate code can be generated.
o A special pre-processor, called the DML precompiler,
converts the DML statements to normal procedure calls in
the host language.
o There are special types of programming languages that
combine control structures of Pascal like languages with
control structures for the manipulation of a database object.
3. Sophisticated users
o These users interact with the database using database
query language.
o They submit their query to the query processor.
o Then Data Manipulation Language (DML) functions are
performed on the database to retrieve the data.
o Tools used by these users are OLAP(Online Analytical
Processing) and data mining tools.
4. Specialized users
o These users write specialized database applications to
retrieve data.
o
3.4
3.5
TRANSACTION MANAGEMENT
What is a Transaction?
o A transaction is an event which occurs on the database.
Generally a transaction reads a value from the database or
writes a value to the database.
o Although a transaction can both read and write on the
database, there are some fundamental differences between
these two classes of operations.
o A read operation does not change the image of the database
in any way.
o But a write operation, whether performed with the intention
of inserting, updating or deleting data from the database,
10
changes the image of the database. ie, we may say that
these transactions bring the database from an image which
existed before the transaction occurred (called theBefore
Image or BFIM) to an image which exists after the
transaction occurred (called the After Image or AFIM).
The Four Properties of Transactions
o Every transaction, for whatever purpose it is being used, has
the following four properties. Taking the initial letters of these
four properties we collectively call them the ACID
Properties.
1. Atomicity: This means that either all of the instructions within
the transaction will be reflected in the database, or none of them
will be reflected.
o Say for example, we have two accounts A and B, each
containing Rs 1000/-.
o We now start a transaction to deposit Rs 1000/- from
account A to Account B.
Read A;
A = A 100;
Write A;
Read B;
B = B + 100;
Write B;
The transaction has 6 instructions to extract the amount from
A and submit it to B. The AFIM will show Rs 90000/- in A and Rs
1100/- in B.
o Now, suppose there is a power failure just after instruction 3
(Write A) has been complete. What happens now? After the
system recovers the AFIM will show Rs 900/- in A, but the
same Rs 1000/- in B. It would be said that Rs 100/evaporated in thin air for the power failure. Clearly such a
situation is not acceptable.
o The solution is to keep every value calculated by the
instruction of the transaction not in any stable storage (hard
disc) but in a volatile storage (RAM), until the transaction
completes its last instruction.
o When we see that there has not been any error we do
something known as a COMMIT operation. Its job is to write
every temporarily calculated value from the volatile storage
on to the stable storage.
11
o In this way, even if power fails at instruction 3, the post
recovery image of the database will show accounts A and B
both containing Rs 1000/-, as if the failed transaction had
never occurred.
2. Consistency: If we execute a particular transaction in isolation
or together with other transaction, (i.e. presumably in a multiprogramming environment), the transaction will yield the same
expected result.
o To give better performance, every database management
system supports the execution of multiple transactions at the
same time, using CPU Time Sharing.
o Concurrently executing transactions may have to deal with
the problem of sharable resources, i.e. resources that
multiple transactions are trying to read/write at the same
time.
o For example, we may have a table or a record on which two
transaction are trying to read or write at the same time.
Careful mechanisms are created in order to prevent
mismanagement of these sharable resources, so that there
should not be any change in the way a transaction performs.
o A transaction which deposits Rs 100/- to account A must
deposit the same amount whether it is acting alone or in
conjunction with another transaction that may be trying to
deposit or withdraw some amount at the same time.
3. Isolation: In case multiple transactions are executing
concurrently and trying to access a sharable resource at the
same time, the system should create an ordering in their
execution so that they should not create any anomaly in the
value stored at the sharable resource.
There are several ways to achieve this and the most popular
one is using some kind of locking mechanism.
o Again, if you have the concept of Operating Systems, then
you should remember the semaphores, how it is used by a
process to make a resource busy before starting to use it,
and how it is used to release the resource after the usage is
over.
o Other processes intending to access that same resource
must wait during this time. Locking is almost similar. It states
that a transaction must first lock the data item that it wishes
to access, and release the lock when the accessing is no
longer required.
12
o Once a transaction locks the data item, other transactions
wishing to access the same data item must wait until the lock
is released.
4. Durability: It states that once a transaction has been complete
the changes it has made should be permanent.
o As we have seen in the explanation of the Atomicity
property, the transaction, if completes successfully, is
committed. Once the COMMIT is done, the changes which
the transaction has made to the database are immediately
written into permanent storage.
o So, after the transaction has been committed successfully,
there is no question of any loss of information even if the
power fails. Committing a transaction guarantees that the
AFIM has been reached.
o There are several ways Atomicity and Durability can be
implemented. One of them is called Shadow Copy.
o In this scheme a database pointer is used to point to the
BFIM of the database. During the transaction, all the
temporary changes are recorded into a Shadow Copy, which
is an exact copy of the original database plus the changes
made by the transaction, which is the AFIM.
o
3.6
DATABASE STRUCTURE
o
In a database structure, the DBMS acts as an interface
between the user and the database.
o
o
The user requests the DBMS to perform various operations
such as insert, delete, update and retrieval on the database.
o
o
The components of DBMS perform these requested
operations on the database and provide necessary data to the
users.
13
o
o
14
It also enforces constraints to maintain consistency and
integrity of the data.
It also synchronizes the simultaneous operations performed
by the concurrent users.
It also controls the backup and recovery operations.
4. Data Dictionary
Data Dictionary is a repository of description of data in the
database. It contains information about
Data - names of the tables, names of attributes of each table,
length of attributes, and number of rows in each table.
Relationships between database transactions and data items
referenced by them which are useful in determining which
transactions are affected when certain data definitions are
changed.
Constraints on data i.e. range of values permitted.
Detailed information on physical database design such as
storage structure, access paths, files and record sizes.
Access Authorization which is the description of database
users their responsibilities and their access rights.
Usage statistics such as frequency of query and transactions.
Data dictionary is used to actually control the data integrity,
database operation and accuracy. It may be used as an
important part of the DBMS.
Importance of Data Dictionary
Data Dictionary is necessary in the databases due to following
reasons:
It improves the control of DBA over the information system
and user's understanding of use of the system.
It helps in documentations of the database design process by
storing documentation of the result of every design phase and
design decisions.
It helps in searching the views on the database definitions of
those views.
It provides great assistance in producing a report of which
data elements (i.e. data values) are used in all the programs.
It promotes data independence i.e. by addition or
modifications of structures in the database application program
are not affected.
15
5. Data Files - It contains the data portion of the database.
6. Compiled DML - The DML complier converts the high level
Queries into low level file access commands known as compiled
DML.
7. End Users They are the users of the system who is going to
use the system for their day to day activities.
3.7
SUMMARY
16
o The four properties of transactions are generally denoted by
ACID.
3.8
MODEL QUESTIONS
the
different
role
performed
by
administrator.
7. Write a short note on transaction management.
database
4
INTRODUCTION TO DATA MODELS
Unit Structure
4.0
4.1
4.2
4.3
4.4
4.5
4.6
4.7
Objectives
Introduction
Evolution of Data models
Types of Data Models
Merits and Demerits of Each Model
Business Rules
Summary
Review Questions
5.0 OBJECTIVES
4.1 INTRODUCTION
o A data model is a picture or description which shows how the
data is to be arranged to achieve a given task.
o It is a clear model which specifies how the data items are
arranged in a given model.
o Some data models which gives a clear picture which shows the
manner in which the data records are connected or related
within a file structure. These are called structural data models.
o DBMS organize and structure data so that it can be retrieved
and manipulated by different users and application programs.
2
o The data structures and access techniques provided by a
particular DBMS are called its data model.
o A data model determined both the personality of a DBMS and
the applications for which it is particularly well suited.
3
o As the DBMSs evolved, so did the programming languages. In
1967 Simula, the first object-oriented programming language
was born. Simula was developed to make a foundation to
develop simulation programs, and contained the now familiar
class-concept. Several other programming languages adopted
the class-concept from Simula (e.g. C++, Java, Eifel, and
Smalltalk) and continued to evolve more or less independently
of the DBMSs.
o In the early 1980s research started on another kind of database.
This research was among other things, motivated by the need of
a database system capable of handling complex objects and
structures like those used in CAD systems, CASE and OIS
systems (Zdonik. 1994). To accomplish these tasks the
database had to be able to store classes and objects and the
objects associations and methods, and the object-oriented
DBMS (OODBMS) emerged. In the late 1980s several vendors
had developed OODBMSs (e.g. ObjectDesign, Versant, O2 and
Objectivity).
o OODBMSs were no threat in the late 1980s to the now big
commercial vendors developing and selling hierarchical,
network or relational databases. In 1991 ODMG (Object
Database Management Group) was founded, mainly thanks to
Rick Cattell of JavaSoft, and in 1993 several vendors of
OODBMSs agreed upon an OODBMS standard called ODMG93.
o The relational databases already had its standard - SQL-92,
defined by its ANSI committee and ISO. And so did the network
database vendors as well; CODASYL (defined in 1986 by the
ANSI X3H2 comittee).
o The founding of ODMG and the fact that object-oriented
programming languages became more and more used may well
have been the major driving forces when the ANSI X3H2
committee started its work on SQL3 in 1992. This proposal put
another type of DBMS on the arena - the object relational DBMS
(ORDBMS).
o While all this was happening, more and more programmers
converted from C and other languages to C++. C++ was
becoming the most used object-oriented language, but C++
application was not always that easy to develop and maintain.
Such applications often had memory-leaks, erroneous pointers
and other trivial problems attached to them.
4
o In 1991 Sun's Green Team started the development of a new
programming language which was loosely based on C++. The
language was named Oak after the trees outside the office
window of the language designer - James Gosling.
o In 1992 Sun turned Green Team into a fully owned company,
called First Person Inc. National Center for Supercomputing
introduced Mosaic in 1993, a WWW browser, and the Internet
began to bustle with traffic. Soon other WWW browser followed.
o In 1994 First Person built an Oak-ready browser called
WebRunner and Sun backed the decision to give the language
(Oak) away for free, but first Oak was renamed to Java and
WebRunner to HotJava. Java became available to millions of
people due to Netscapes bundling of Java, and soon others
followed (Bank 1995).
5
o Hierarchical Databases is most commonly used with
mainframe systems.
o It is one of the oldest methods of organizing and storing data
and it is still used by some organizations for making travel
reservations.
o A hierarchical database is organized in pyramid fashion, like the
branches of a tree extending downwards.
o In this model, related fields or records are grouped together so
that there are higher-level records and lower-level records, just
like the parents in a family tree sit above the subordinated
children.
o Based on this analogy, the parent record at the top of the
pyramid is called the root record.
o A child record always has only one parent record to which it is
linked, just like in a normal family tree.
o In contrast, a parent record may have more than one child
record linked to it. Hierarchical databases work by moving from
the top down.
o A record search is conducted by starting at the top of the
pyramid and working down through the tree from parent to child
until the appropriate child record is found. Furthermore, each
child can also be a parent with children underneath it.
4.3.2Network databases
6
o Instead of looking like an upside-down tree, a network database
looks more like a cobweb or interconnected network of records.
In network databases, children are called members and parents
are called owners.
The most important difference is that each child or member
can have more than one parent (or owner).
o Similar to hierarchical databases, network databases are
principally used on mainframe computers.
o Since more connections can be made between different types of
data, network databases are considered more flexible.
However, two limitations must be considered when using this
kind of database.
o Similar to hierarchical databases, network databases must be
defined in advance. There is also a limit to the number of
connections that can be made between records.
4.3.3Relational databases
o Pre-relational models depended upon being able to determine
explicitly where and how individual records were stored.
o Early relational proponents argued that the relational data model
viewed information logically rather than physically, but this is not
quite correct.
o Earlier data models associated the logical and physical aspects
of information together; logically-related information was stored
in physical proximity within a data file. The relational data model
first separated the logical from the physical aspects.
o The relational data model looks
an unordered collection of "relations."
at
information
as
7
tuples and fields. Relations are an abstraction of how data is
stored; tables are just one of many possible implementations.
o Some of the relational terms are crafted to emphasize the
distinction between logical and physical features, to avoid
confusing one concept with another. However, vocabulary
leakage from other disciplines has sprinkled into the
conversation of relational proponents.
o There is a strong tendency to refer to an individual tuple/row as
a "record" because collections of fields in other models are
called records. "Attribute" is often used synonymously with field.
o To be sure, "unordered" implies neither "chaotic" nor "random".
Relations and Fields are named uniquely and identified easily.
Distinguishing between tuples is more subtle since the order is
not pre-defined.
o Rather than depending upon relative (as in hierarchy) or
absolute (as in network) locations, tuples may only be
differentiated according to their contents.
o Consequently, duplicate tuples are not permitted within a single
relation. Even more strongly, distinct tuples must have a unique
"key" (some combination of a relation's named fields).
o The set of minimal keys includes one "primary key"; the rest are
"candidate keys". Within a tuple, references to other tuples are
expressed as a "foreign key," which should contain the values of
the referenced tuple's primary key.
o Relational theory provides a firm mathematical foundation for
data management. Set theory could be applied to relations
using relational algebraic operations (union, intersection, join,
projection, etc.).
o
4.3.4
8
o A core object-oriented data model consists of the following basic
object-oriented concepts:
(1) object and object identifier: Any real world entity is uniformly
modeled as an object (associated with a unique id: used to
pinpoint an object to retrieve).
(2) attributes and methods: Here every object has a state (the set
of values for the attributes of the object) and a behavior (the
set of methods - program code - which operate on the state
of the object). The state and behavior encapsulated in an
object are accessed or invoked from outside the object only
through explicit message passing.
An attribute is an instance variable, whose domain may be
any class: user-defined or primitive. A class composition
hierarchy (aggregation relationship) is orthogonal to the
concept of a class hierarchy. The link in a class composition
hierarchy may form cycles.
(3) class: a means of grouping all the objects which share the
same set of attributes and methods. An object must belong
to only one class as an instance of that class (instance-of
relationship). A class is similar to an abstract data type. A
class may also be primitive (no attributes), e.g., integer,
string, Boolean.
(4) Class hierarchy and inheritance: derive a new class
(subclass) from an existing class (superclass). The subclass
inherits all the attributes and methods of the existing class
and may have additional attributes and methods. single
inheritance (class hierarchy) vs. multiple inheritance (class
lattice).
9
4. Efficiency: It is very efficient because when the database
contains a large number of 1:N relationship and when the user
require large number of transaction.
Disadvantages
1. Implementation complexity: Although it is simple and easy to
design, it is quite complex to implement.
2. Database Management Problem: If you make any changes in
the database structure, then you need to make changes in the
entire application program that access the database.
3. Lack of Structural Independence: there is lack of structural
independence because when we change the structure then it
becomes compulsory to change the application too.
4. Operational Anomalies: Hierarchical model suffers from the
insert, delete and update anomalies, also retrieval operation is
difficult.
4.4.2 Network Model
Advantages
10
2. Operational Anomalies: As discussed earlier in network model
large number of pointers is required so insertion, deletion and
updating more complex.
3. Absence of structural Independence: there is lack of structural
independence because when we change the structure then it
becomes compulsory to change the application too.
4.4.3 Relational Model
Advantages
1. Conceptual Simplicity: We have seen that both the hierarchical
and network models are conceptually simple, but relational model is
simpler than both of those two.
2. Structural Independence: In the Relational model, changes in
the structure do not affect the data access.
3. Design Implementation: the relational model achieves both
data independence and structural independence.
4. Ad hoc query capability: the presence of very powerful, flexible
and easy to use capability is one of the main reason for the
immense popularity of the relational database model.
Disadvantages
1. Hardware overheads: The relational database systems hide the
implementation complexities and the physical data storage details
from the user. For doing this, the relational database system need
more powerful hardware computers and data storage devices.
2. Ease of design can lead to bad design: The relational
database is easy to design and use. The user needs not to know
the complexities of the data storage. This ease of design and use
can lead to the development and implementation of the very poorly
designed database management system.
11
o
12
well-written set of business rules consist of having a unique
identifier, describes one and only one concept, are written in
plain language, are written, and are from a single source.
o In terms of a unique identifier, business rules should come with
an identifier that may consist of the rule number and the
department it affects. And example would be BRacc01. In this
case, this business rule (BR) is directly related to the accounting
department.
o Another important aspect of business rules consist of how the
rules are shared within the company.
o A protocol for business rules that many follow is that the
business rules are written down. However, with many
businesses sharing information directly over the internet, some
are opting to place their business rules online in company blogs,
wikis, and websites.
o This shares the business rules with all employees faster and
easier. In relation to how business rules are shred, it is very
important that business rules are written in plain language.
o If business rules are written at a high level language, there is an
increased chance that not every person will understand what
the business rules cover or what is acceptable and what is not.
4.6 SUMMARY
o A data model is a picture or description which shows how the
data is to be arranged to achieve a given task.
o The data structures and access techniques provided by a
particular DBMS are called its data model.
o In 1964 the first commercial database management system
(DBMS) was developed widely known as Integrated Data Store
(IDS).
o A hierarchical database is organized in pyramid fashion, like the
branches of a tree extending downwards.
o In hierarchical model, the parent record at the top of the pyramid
is called the root record and the leaf node is called the child
record.
o Network databases are similar to hierarchical databases by
also having a hierarchical structure.
o The relational model organizes the records and stores the
records in rows and columns.
13
4.7
REVIEW QUESTIONS
Unit Structure
5.0 Objectives
5.1 Database design
5.2 ER-Model
5.3ER Diagram
5.4 Constraints on relationship
5.5 Relational Schemas
5.0
OBJECTIVES
ER Model
In software Engineering, an entity relational model is an
abstract and conceptual representation of dataEntity-relationship
modeling is a database modeling method, used to produce a type
of conceptual schema or semantic data model of a system, often
arelational database, and its requirements in a top-down fashion.
Diagrams created by this process are called entity-relationship
diagrams, ER diagrams, or ERDs.
In 1976, Entity relationship model developed by Chen,
ER Model is high level Conceptual model which used Conceptual
design of database where as relational model are used to logical
design of database
3
ER Diagram
An Entity set is a set of an entities of all same type that share the
same properties.
Example: set of all persons ,companies,trees, holidays
ER Diagram
4
Strong Entity type
An entity type which has own distinct primary key that used to
identify specific uniquely from another entity type is called as
Strong Entity type
An Entity type which is independent on some other entity type
icalled Strong Entity type
Example
In the Case of Client entity Client_no is the primary key of Client
entity which is used to uniquely identified among the Client s entity
set
In the case of Customer Entity , Customer_id is the primary key of
Customer Entity which is used to uniquely identified among the
Customers entity set
Strong Entity type is represented by rectangle Symbol
5
Some weak entities assign partial identifiers and such partial
identifiers of an weak entity called as discriminator
Weak entity type is represented by double rectangle.
Identify relationship
Strong entity type is link with the weak entity type
6
1) Simple Attributes
Simple attributes is an attributes which can further divided in
to two parts
Or
An Attribute composed of single compoenent with an
independent existence
For an example: Desgination of an staff and Salary of an staff
Simple Attributes
Composite Attribute
Composite Attribute is an attribute which is futher divided
into many parts
Or
An attributed composed of multiple component, each
component has its own independent existence
Example
Address attributes of an Branch entity that can be further
divided in to sub parts i.e street, city and postalcode as an
attributes
Composite Attributes
7
2)
Multivalued Attributes
8
3) Stored and Derived attributes
Stored attributes is an attribute which is used supplied a
value to the related attreibute
Example Date_of_Birth of an staff is a stored attributes
Derived attributes
The value from the derived attribute is derived from the
stored attribute for an example Date_of_Birth is a stored attribute
for an each staff member . The value for an Age can be derived
from the Date_of_ Birth attributes I.e by subtracting the Date_of
_Birth from the Current date, therefore the Stored attributes is used
supplied a value to the related attributes
Null attribute
The attribute which take NULL value when entity does not
have the value to it.
The Null attribute is an attribute their value is unknown,
unassigned and missing information
Key Attributes
This attribute has the unique value for an entity which is used
to identified given row in the table is called as key attribute of an
entity
Example : Staff_ no is an key attribute which has an unique value
which is used to identifies given row in the table
9
Relationships
A set of meaningful relationship among several entities
We used to inidicate the diamond symbol for Relationships among
the several entities, it could read from left to right
Example : Branch has a staff
Degree of relationship
It is the number of entities participated in a particular relational
model
There are two type of degree of relationship.
Binary relationship: A Relationship of degree two is called as
binary relationship
Ternary Relationship: A relationship of degree three is called as
Ternary relationship.
Example
Relationship set
The collection of similar relationship is known as Relationship set
10
Constraints on relationship
1) Mapping Constraints / Cardinalities
The number (or range) of possible entity type that is associated to
another entity type through a particular entity
Cardinalities indicates that a specific number of entity occurrence of
related entity .
Type of Mapping Constraints
One-to-one (1:1)
One-to-many (1:*)
Many- to-one(*:1)
Many-to-many (*:*)
TypeOne-to-one (1:1)
In this type of Mapping Constrant One record of an entity is
related to the one record of an another entity
That is one row on an table is related to an one row of another table
i.e A is associated with at most one entity in B and Bis associated
with at most one entity in A
Example
Each branch is managed by one member of the staff thats means
Branch Manager
A member of staff can manage zero or one branch
11
2)
One To Many
In this type Mapping Constraints , Many records in the one enity is
related to the only one records in the other entity
An entity in A is associated with at least one entity in B . an entity in
B can be associated with any number of entities in A.
Example one vendors has many Goods
purchase by one Vendors
12
Many to Many
In this Mapping Constraints , Many records in the entity is related
Many records in the other entity
An entity in A is associated with any number of entities in B. and an
entity in B is associated with any number of entities in A.
Many Vendors Has Clients and Many Clients has may Vendors
Participation Constraints
13
14
2) Min Max notation
The altermate of notation by specify the pair of integer, that
used to specify the minimum and maximum participation of each
entity type in the form of( min, max)
The Minimum participation of 0 indicate partial participation
where as maximum participation of 1 or more indicates total
participation
15
Sub classes and Super classes
In some case , entity type has numerous sub-grouping of
its entities because that are meaningful way for representation and
need to be explicitly defined because of their importance
The set listed is a subset of the entities that belong to the
staff entity which means that every entity that belongs to one of the
sub sets is also an Staff
An entity type that includes distinct Subclasses that require
to be represented in a data model is called as super class.
A Subclass is an entity type that has a distinct role and is
also a member of the Superclass.
Staff is the super class where as manager, Secretary, sales
personnel is the subclass
16
Type Inheritance
The type of an entity is defined by the attributes it possesses,
and the relationship types it participates in.
Specialization
The process of defining a set of subclasses of super class
The specia;ization is a top down approach of super class and
subclasses
The set of sub classes is based on some distinguishing
characteristic of the super class.
.
17
Attributes that only apply to the sub class are attached to the
rectangle representing the subclass. They are called specific
attributes.
Summary of Specialization
Allows for:
Generalization
18
Attribute Inheritance
An entity in a Subclass may possess subclass specific attributes,
as well as those associated with the Superclass
Description
19
4
The comprehensive
sublanguage rule:
Physical
independence:
20
9
10
Integrity independence:
11
Distribution independence:
12
21
Codds rule in detail
1) The information rule:
I) All the information in the database should be represented in
the term of relational or table.Information should be stored
as an values in a tables
II) Data should be stored in form a table and no other means to
stored the data
III) E.g.If want to stored data of student in the form of
table.Consider name of Table is Students , it has four
field(i.e column name) Roll_no, Firstname ,Lastname and
date_of_birth and Consist five record mans Five rows
Students Table
Roll_no
Firstname
101
Sachin
102
Mahavir
103
Dinesh
104
Yogesh
105
Mahesh
Lastname
Godbole
Jain
Maheshwari
Lad
Thorat
date_of_birth
17/07/1981
04/12/1985
09/10/1987
06/11/1985
07/06/1989
22
and inapplicable information that should be treated as
systematic , distinct from regular values
II) Null values is very important concept is the database ,A null
values must be represented as missin information in the
table , it is not same as the blank space, dash, or zero, hash
or any other representation
III) A null values means that we dont know what information
must be provided or entered in to this field name
IV) Null values must be handled logically and consistent manner
4) Active online catalog based on the relational model:
I)
II) User Tables: The user table contains the data about the table
which is created by any users in the database systems
III)System tables: The system table contains the data about the
structure of the database and database object
IV) Metadata: The data which hold the description of table in the
database, the table structure, database structure , the
relationship among the tables, the queries and on , This data
id often called as metadata , in short term, Metadata is data
about the data
V) The collection of the system tables is known as the system
catalogs or data dictionary
5) The comprehensive data sublanguage rule:
I) The system must support at least one relational language that
through which the data in the database must be accessed
II ) The language must support all the operation of the following
items:
Data definition
View definition
Data manipulation
Integrity constraints
Authorization
Transaction boundaries (begin, commit and rollback)
23
6) The view updating rule:
I) All the view must be theoretically updatable can be updated
by the system
II) There is ambiguity in this rule, the Structured query
lanagauge support a single updation at a time suppose if we
try combine two or more tables a for a complex views and try
to update the views and the DBMS would fail to update the
records to the respective tables, thereby violating this rule.
IV) If that view doesnt include the primary key columns in the
view, then each record in the table cannot be updated, thereby
violating this rule.
Eg. If Roll_no column is not present in the view then it is not
possible update the view of the student table
7 ) High-level insert, update, and delete:
I) This rules states that in the relational model, the structured
query language must performed data manipulation such as
inserting ,updating and deleting record on sets of rows in the
table
II) You expected from the RDBMS, that you can retrieves all
the record from table applying single command on the set of
tables,or by using single query statement, this rules state
that not only retrieves all the record from table but also you
can apply the delete , insert, and update multiple records
should possible by using the single command
III) Considered an example, if you want to delete the record of
the invoices table which are older than six years,you dont
have locate postion each record and delete them individually
, uou should able to delete set of records in the table using
one single command
IV) The same concept can be apply to inserting and updating
the record
8) Physical data independence:
i) Any change made in the data is physically stored in term of
data is stored in the file system through array and link list
must not effect application that access the data structure
24
This rule say that any change is made in the back
end(SQLServer/oracle) must not effect front end
application(Visual basic/Java)
If the database file renamed or database location is change,
then this should not have effect on the application.
9 ) Logical data independence:
This rule state that changes in the logical level(rows
,columns and so on) must not change to the applications
structure
This rule state that it should possible to change the database
design or alter the database design without the user being
aware of it.
Thse change could be to adding a new table in the datable or to
delete the table from the database but the application must
effect for accessing the datastructure
Consider an example if want the performance search the record
in the table, for that reason you have split the Customer table in
to part i.e Customer_India and Customer_Rest,This allows to
search a recor in the Customer_India rapidly, but what about the
exiting user who is referring to the Customer table.In practice it
can be done by creating a view which will combines two table
into the single table with the same name. so that there should
be effect on the application.
10 ) Integrity independence:
Data integrity constraints should be considered as separated
from application program, the structured query language which
defines data integrity constraint must be stored in the
database in term of data in table that is, in the catalog and not
in the application.
Referential integrity and entity integrity is integral part of the
relational database , in more specific term, the following two
integrity should be apply to the relational database.
i) Entity integrity:The column which have the primary key value
should not contain missing values or duplicate value.This
mean the column should contain the null values and unique
value in the each record set
25
ii) Referential integrity:The column which have the foreign key
value, there must exist a matching primary key column value
mean the foreign key column have duplicate value must be
referential to primary key column value.
11) Distribution independence
The rules states that the data can be stored centrally on the
single machine or it can be stored in the various
location(ditributed) on various machine but it should be
invisible to the user i.e the user does not location of data is
stored whether on the single machine(Centrally stored) or the
distributed stored.If the location of database in change then
the existing application must continually access the change
database
One of the important benefits of networking is that it allows
multi-user access to a database; that is, the users can access
the data which is distributed across the network.
However, it is also possible to distribute the data across the
same network.
This rule also state that even if the table moves from one
location to another location the user should aware of it, it
should be transparent to the user, changing in the location
mean that the application should not be rewriiten.
12 Nonsubversion rule
The system must not have features that allow you to subvert
database structure integrity. Basically, the system must not
include back doors that let you cheat the system for features
such as administrative privileges or data constraints.
To understand another way, a user should not be allowed
access the database by means of other way, other than SQL
26
6
RELATIONAL DATABASE MODEL
Unit Structure
6.0 Objectives
27
One row in the table represents a relationships among the
another table in the database.The set values in the one table is
related to the set of values in another tables. Thus the table is
represents a collection of relationship, The relationship among the
tables in the form primary key foreign key relationship
Column
Ro
ws
Table is an abstract entity which does not say how the data
is stored in the physical memory of the computer system
Each table in the database has its own unique name trough
which we can refer the content of the table by the unique name
2.Characteristics of an table
I) A tables in the database must be in the two-dimensional structure
which consist number of rows and columns
II) Each row in table as called as record or tuple can represent as a
single entity which is occur within the entity set i.e Customer record
in the Customer table
III) Each column name in the table is called as attribute and each
row in the table is called as record. Each column name in the table
is unique namei.e no duplicate name in the same table cannot be
repeated.
IV)Each rows/ coloumn interection represent a single data item.
V) All the value in the column must be represent in the same data
format
VI)
Each columns has the specific range of values, and also
refer as the domain attribute
28
VII)
3.Example
There is Customer Table contain all information about the
Customer
Cust_id
Cust_Name
Cust_Age
Cust_Address
Cust_Mobile_No
Cust_Phone_No
Cust_id
Cust_
Name
Cust_
Age
Cust_
Address
Cust_Mobile
_No
Cust_Phone_
No
Yogesh
20
Worli
9892456123
0224672345
Ramesh
23
Bandra
9320896742
0225678894
Ram
18
mahim
9819674534
0224678678
Pramod
24
Khar Road
9821673445
0223456478
Yatin
25
Dadar
9892396735
0222456783
Tushar
26
Matunga
9867458432
0226783452
Attribute
Each column in the above table represent the data item in the
database
Each column in the table represent the attribute in the table
Atleast one column consist in the table
There must be one unique column in the table , this means that
no two columns has the same name in the same table ,it is
possible to have two column with same colmn name but it in the
different table.
The ANSI/SQL Standard does not specify a maximum numbers
of rows and columns in the table.
Eg.
Cust_id
,Cust_Name
,Cust_Age
,Cust_Address
,Cust_Mobile_No, Cust_Phone_No are the attributes of the
Customer Table
29
Records/Tuples
6.3 KEY
Definition
A Column value in the table that uniquely identifies a single
record in the table is called key of an table
A attribute or the set of attribute in the table that uniquely
identifies each record in the entity set is called a key for that entity
set
Types of keys
Simple Key: A key which has the single attribute is known as a
simple key
Composite key: A key which consist two or more attributes is called
a Composite Key.
Example:
Cust_id is a key attribute of Customer Table it is possible to
have a single key for one customer i.e is Cust_id ie Cust_id =1 is
only for the Cust_name =Yogesh please refer to the Customer
Table which is mentioned above.
Types of key
Definition of Key
Super Key
Candidate Key
Primary Key
Secondary Key
Foreign Key
30
Types of key
1 Super Key
A key is called super key which is sufficient to identify the
unique record in the table
Customer Table
Cust_id
Cust_
Name
Cust_
Age
Cust_
Address
Cust_Mobile_
No
Cust_Phone_
No
Yogesh
20
Worli
9892456123
0224672345
Ramesh
23
Bandra
9320896742
0225678894
Ram
18
mahim
9819674534
0224678678
Pramod
24
Khar Road
9821673445
0223456478
Yatin
25
Dadar
9892396735
0222456783
Tushar
26
Matunga
9867458432
0226783452
Example
Here Cust_id attribute of the entity set Customer is uniquely
identify Customer entity from another so The Cust_id is the Super
key. Another way is, the combination of Cust_id attribute and
Cust_Name attribute is the Super key for the Customer Entity set.
Only the Cust_Name is not called the Super Key because several
customer may have the Same Name
2. candidate key
Defination:
A minimal super key is called Candidate key .A super key
has no proper subset of candidate key
Here Minimum attribute of the super key is omitted unwanted
attributed of an table that key is sufficient for identifying the unique
record in the entity set so it is called as Candidate key
The Candidate key is also known as the primary key
31
Example
Cust_id
Cust_
Name
Cust
_Age
Cust_
Address
Cust_Mobile
_No
Cust_Phone_
No
Yogesh
20
Worli
9892456123
0224672345
Ramesh
23
Bandra
9320896742
0225678894
Ram
18
mahim
9819674534
0224678678
Pramod
24
Khar Road
9821673445
0223456478
Yatin
25
Dadar
9892396735
0222456783
Tushar
26
Matunga
9867458432
0226783452
Cust_id
Cust_
Name
Cust_
Age
Cust_
Address
Cust_Mobile
_No
Cust_Phone_
No
Yogesh
20
Worli
9892456123
0224672345
Ramesh
23
Bandra
9320896742
0225678894
Ram
18
mahim
9819674534
0224678678
Ram
20
Khar Road
9821673445
0223456478
Yatin
25
Dadar
9892396735
0222456783
Tushar
26
Matunga
9867458432
0226783452
32
In above table the Customer Age cannot act as primary key
hence the customer age column contain repeated values and
Customer Name also cannot act as primary key because it earlier
state that several customer may have the same name hence
Cust_Name column has the repeated values .
Hence there Cust_id can act as the primary key in the
Customer table this is only column which contain a unique set of
values.
3 Secondary key
Defination
Seconday key of the table consist the column and combination
of the some columns which meant for data retrival purpose.
The secondary key not always required to primary key, other
tah the pimary key there are some attribute which is required to
retrieve data from the customer table using the another attribute
such as Cust_Name and Cust_Age columns
Cust_id
Cust_
Name
Cust_
Age
Cust_
Address
Cust_Mobile_
No
Cust_Phone_
No
Yogesh
20
Worli
9892456123
0224672345
Ramesh
23
Bandra
9320896742
0225678894
Ram
18
mahim
9819674534
0224678678
Ram
20
Khar Road
9821673445
0223456478
Yatin
25
Dadar
9892396735
0222456783
Tushar
26
Matunga
9867458432
0226783452
33
What the referential integrity
Referential integrity say the column which contain foreign
key in one table must be primary key of another table
In general term, Foreign key of Table A must be Primary key of
Table B
Example
Customer Table
Cust_id
Cust_
Name
Cust_
Age
Cust_
Address
Cust_Mobile_
No
Cust_Phone_
No
Account Table
Account Cust_id
Account Balance
Description
No
type
In The above example Cust_id is the primary key for the
customer Table while Cust_id is the foreing key for the Account
table
Here the the datatype assigned to column and Numder of
column in the foreign key is same as to the primary key.
Cust_id
Cust_
Name
Cust_
Age
Cust_
Address
Cust_Mobile_
No
Cust_Phone_
No
Yogesh
20
Worli
9892456123
0224672345
Ramesh
23
Bandra
9320896742
0225678894
Ram
18
mahim
9819674534
0224678678
Ram
20
Khar Road
9821673445
0223456478
Yatin
25
Dadar
9892396735
0222456783
Tushar
26
Matunga
9867458432
0226783452
34
Account Cust_id
No
Account
type
Balance
101
Saving
10,000
102
saving
20,200
103
Saving
20,200
104
Current
11,000
105
Saving
50,000
Description
The primary key must be unique for each row in the table that
is no two primary key having the same value in the same table,
The primary key values must be distinct i.e the value could not
be repeated.
The primary key values should not contain null values, primary
key must be NOT NULL
35
For referential integrity to hold in a relational database, any
field in a table that is declared a foreign key can contain only values
from a parent table's primary key or a candidate key. For instance,
deleting a record that contains a value referred to by a foreign key
in another table would break referential integrity.
Some relational database management systems (RDBMS)
can enforce referential integrity, normally either by deleting the
foreign key rows as well to maintain integrity, or by returning an
error and not performing the delete.
Foreign key
A column or collection of column in one table whose values must
match the primary key in the other table is known as a foreign key
Cust_id
Cust_
Name
Cust
_Age
Cust_
Address
Cust_Mobile_
No
Cust_Phone_
No
Yogesh
20
Worli
9892456123
0224672345
Ramesh
23
Bandra
9320896742
0225678894
Ram
18
mahim
9819674534
0224678678
Ram
20
Khar Road
9821673445
0223456478
Yatin
25
Dadar
9892396735
0222456783
Tushar
26
Matunga
9867458432
0226783452
Account Cust_id
No
Account
type
Balance
Description
101
Saving
10,000
102
saving
20,200
103
Saving
20,200
104
Current
11,000
105
Saving
50,000
36
In above example
Cust_id column of Account Table is foreign key for the Account
table while it is primary key for the Customer Table
3. Other integrity rules
NOT NULL. As the integrity rules states column which specify the
NOT NULL values mean these column must contain some values
which should not contain any NULL values
Unique. In this rules no two record or tuples have same values for
the same attribute
Check. In this rule we can apply own integrity rules by applying
CHECK Constraint.
37
6.5.1 Feature
Normalization
i)
ii)
iii)
iv)
v)
vi)
vii)
of
Good
Relational
Database
Design-
Definition of Normalization
In the Relational Database Design, the process of
organizing data to minimizing redundancy is known as
Normalization
Main aim of the Normalization
1.
Ensure data integrity
i) The correct data should be stored in the database
38
ii) This can be achieved by applying integrity rules in the
database
iii) Integrity rules prevent duplicate values in the database
2.
Cust_Name
Pramod
Pramod
Manish
Manish
Cust_Address
Nerul
Nerul
Vashi
Bhandup
39
Example Consider a table Faculty and Course_code consist
the Column name
Faculty_ID,Faculty_Name,Faculty_Hire_Date,Course_Code
Faculty_ID
Faculty_
Name
Faculty_Hire_ Course_Code
Date
386
Mahesh
Lad
10/06/1994
ENG-207
197
Jayesh
Shinde
12/06/1987
PP-205
197
Jayesh
Shinde
12/06/1987
PP-206
234
Pramod
Bhave
11/07/2005
Thus we can add the record the details of any faculty member
who teaches at least one course, but we cannot record the details
of a newly-hired faculty member who has not yet been assigned to
teach any courses except by setting the Course Code to null. This
known as an insertion anomaly.
In the above Table Until the new faculty member, Pramod
Bhave , is assigned to teach at least one course, his details cannot
be recorded.
An deletion anomaly.
There are circumstances in which the deletion of data
representing certain facts necessitates the deletion of some
unrelated data . The "Faculty and Courses" table suffers from this
type of anomaly, for if a faculty member temporarily ceases to be
assigned to any courses, we must delete the last of the records on
which that faculty member appears, effectively also deleting the
faculty member. This is known as a deletion anomaly.
40
Faculty_
ID
386
197
197
Faculty_Na
me
Mahesh
Lad
Jayesh
Shinde
Jayesh
Shinde
Faculty_Hire_
Date
10/06/1994
Course_Co
de
ENG-207
12/06/1987
PP-205
12/06/1987
PP-206
Delete
Advantage of Normalization
1) Avoids data modification (INSERT/DELETE/UPDATE) anomalies
as each data item lives in One place
2) Greater flexibility in getting the expected data in atomic granular
3) Normalization is conceptually cleaner and easier to maintain and
change as your needs change
4) Fewer null values and less opportunity for inconsistency
5) A better handle on database security
6) Increased storage efficiency
7) The normalization process helps maximize the use of clustered
indexes, which is the most powerful and useful type of index
available. As more data is separated into multiple
tables because of normalization, the more clustered indexes
become
available
to
help
speed
up
data
access.
Disadvantage of Normalization
1) Requires much more CPU, memory, and I/O to process thus
normalized data gives reduced database performance
2) Requires more joins to get the desired result. A poorly-written
query can bring the database down
3) Maintenance overhead. The higher the level of normalization,
the greater the number of tables in the database
41
42
1. There are no duplicated rows in the table.
2. Each cell is single-valued (i.e., there are no repeating groups or
arrays).
3. Entries in a column (attribute, field) are of the same kind.
Let us consider the example
Consider a tableCustomer_Rental consisting the attribute
such as Customer_NO, Cust_Name Property_no,P_Address,
Rent_start, Rent_finish,Rent ,Owner_No,Owner_Name
Customer_
Cust_Name
NO
Property_
P_Address
Rent_start
Rent_finish
Rent
no
CR78
CR98
Mahesh
Lad
PG34
Pramod
Patel
Owner_
Owner_Name
No
Nerul,Navi
Mumbai
Turbhe, Navi
mumbai
1-July-91
30-Oct-95
450
C045
Sanjay More
1-Nov-95
1-Nov-98
500
C093
Mahavir Jain
PG34
Nerul,Navi
Mumbai
1-July-95
30-Oct-98
450
C045
Sanjay More
PG36
Kalyan,Thane
1-Nov-97
1-Nov-99
350
C093
Mahavir Jain
1-july-96
1-Nov-97
450
C093
Mahavir Jain
PG78
Karjat,Raigad
PG78
The above table does not contain the atomaic values in the
Property_no,
P_Address
,
Rent_start,
Rent_finish,Rent
,Owner_No,Owner_Name
Hence it is called un-normalizes
table,we cannot Insert ,update and delete the record from the table
because it is inconsistent state .The above tabe has to be
normimalized
Customer_
Cust_Name
NO
Property_
P_Address
Rent_start
Rent_finish
Rent
Owner_No
Owner_Name
no
CR78
Mahesh
Lad
PG34
Nerul,Navi
Mumbai
1-July-91
30-Oct-95
450
C045
Sanjay More
CR78
Mahesh
Lad
PG78
Nerul,Navi
Mumbai
1-Nov-95
1-Nov-98
500
C093
Mahavir Jain
CR98
Pramod
Patel
PG34
Nerul,Navi
Mumbai
1-July-95
30-Oct-98
450
C045
Sanjay More
CR98
Pramod
Patel
PG36
Kalyan,Thane
1-Nov-97
1-Nov-99
350
C093
Mahavir Jain
CR98
Pramod
Patel
PG78
Karjat,Raigad
1-july-96
1-Nov-97
450
C093
Mahavir Jain
43
The above table show the same set of data as the previous
table however we have eliminated the repeated groups.so the table
shown in the above table to be in First Normal form(1NF)
6.6.2 Second Normal Form
Second Normal Form based on the concept of Full
Functional Dependency and it tries remove the problem of
redundant data in the First normal form.
Defination: A 2NF relation in 1NF and every non-primary key
attritube is fully functionally dependent on the primary key
Converting from 1NF to 2NF:
o Firstly Identify the primary key for the 1NF relation.
o Identify whether the functional dependencies in the relation.
o If partial dependencies exist on the primary key remove them by
placing then in a new relation along with a copy of their
determinant.
Example
Functional Dependency for Customer_Rental Relation
Step 1 : Primary key: Customer_No + Property_no
Step 2 :Full Functional Dependency:
(Customer_No+Property_No)->(Rent-Start, RentFinish)
Step3 Partial Dependency:
(Customer_No+Property_No)->Cust_Name
(Customer_No+Property_No)->(P_Address,
Owner_Name)
Customer
Cust_
Property_
_NO
Name
no
P_Address
Rent_start
Rent,
Rent_finish
Rent
Owner_No,
Owner_
Owner_
No
Name
44
Customer Relation
Customer_NO
CR78
CR98
Cust_Name
Mahesh Lad
Pramod Patel
Rental Relation
Customer_NO
Property_No
Rent_start
Rent_finish
CR78
CR78
CR98
CR98
CR98
PG34
PG78
PG34
PG36
PG78
1-July-91
1-Nov-95
1-July-95
1-Nov-97
1-july-96
30-Oct-95
1-Nov-98
30-Oct-98
1-Nov-99
1-Nov-97
Property_owner Relation
Property_No
P_Address
PG34
Nerul,Navi
Mumbai
Nerul,Navi
Mumbai
Kalyan,Thane
PG78
PG36
Rent
450
Owner_No
C045
Owner_Name
Sanjay More
500
C093
Mahavir Jain
350
C093
Mahavir Jain
45
Property_Owner to 3NF Relations
Property_owner Relation
Property_No
P_Address
Rent
Owner_No
Owner_Name
Transitive Dependency:
(Customer_No+Property_No)->Owner_No
Owner_No ->OName
Property_for_Rent
Property_No
P_Address
PG34
Nerul,Navi
Mumbai
Nerul,Navi
Mumbai
Kalyan,Thane
PG78
PG36
Owner
Owner_No
C045
C093
Owner_Name
Sanjay More
Mahavir Jain
Rent
450
Owner_No
C045
500
C093
350
C093
46
Process of Decomposition
Customer_Rental1NF
Property_Owner 2NF
Customer
Rental
Property_for_Rent
Owner 3NF
47
functional dependencies by placing them in a new relation along
with a copy of their determinant.
Example - 3NF to BCNF Relations
Client_Interview Relation
Client_No
CR76
CR56
CR74
CR56
(Client_No,
Room_No)
Interview_Date
13/05/98
13/05/98
13/05/98
01/06/08
Interview_Date
10.30
12.30
12.30
10.30
Interview_Date)
->
Staff_No
SG5
SG5
SG37
SG5
Room_No
G101
G101
G102
G102
(Interview_Time,
Staff_No,
Client_No
CR76
CR56
CR74
CR56
Staff_No
SG5
SG37
SG5
Interview_Date
13/05/98
13/05/98
13/05/98
01/06/08
Interview_Date
13/05/98
13/05/98
01/06/08
Interview_Date
10.30
12.30
12.30
10.30
Room_No
G101
G102
G102
Staff_No
SG5
SG5
SG37
SG5
7
INTRODUCTION TO UML
Unit Structure
7.0
7.1
Objectives
Introduction
1.0
OBJECTIVES
2
Why Modeling is required and what is the principle of Model?
Analysis the problem domain that is simply reality captures
requirements in the design the model, visualize the system in its
entirety, and specify the structure and / or behavior of the system
Choose your model well
The choice of model such way that it should be through
analysis of the problem and the design of the solution.
Every model in the system can be expressed at different
levels of accuracy - the same model can be scaled up (or down) to
different granularities.
The best models in the system are closer to reality - simplify
the model as much as possible and dont hide important details.
No single model suffices - every nontrivial system has
different set of dimensions to the problem and has much solution
UML is an modeling Langauage but not a methodology or
process , the first concept is developed by Grady Booch , James
Rumbaugh and Ivar Jacobson at Rational Software.
This model is accepted as a standard by the Object
Management Group (OMG), in 1997
3
A class icon is simply a rectangle divided into three
compartments. The topmost compartment contains the name of the
class. The middle compartment contains a list of attributes
(member variables), and the bottom compartment contains a list of
operations (member functions). In many diagrams, the bottom two
compartments are omitted. Even when they are present, they
typically do not show every attribute and operations. The goal is to
show only those attributes and operations that are useful for the
particular diagram.
If two classes are very similar it may be helpful to put the
similarities into a more general class called a superclass. For
example, if you set up a superclass called Student, then Graduate
Student and Undergraduate Student can be subclasses of Student.
7. 2 USECASE DIAGRAM
A use case is a set of scenarios that shows an interaction
between a user and a system. A use case diagram shows the
relationship among actors and use cases.
The two main components of a use case diagram are use
cases and actors.
4
An actor is represents a user or another system that will
interact with the system you are modeling. A use case is an
external view of the system that represents some action the user
might perform in order to complete a task.
7. 3 ACTIVITY DIAGRAMS
Activity
diagrams are
graphical
representations
of workflows of stepwise activities and actions with support for
choice, iteration and concurrency.
In the Unified Modeling Language, activity diagrams can be
used to describe the business and operational step-by-step
workflows of components in a system. An activity diagram shows
the overall flow of control.
Activity diagrams are constructed from a limited number of
shapes, connected with arrows. The most important shape types:
activities;
a black circle represents the start (initial state) of the
workflow;
6
Figure shows how a student successfully gets added to a
course. The student lets call him Mahesh ) fills in some
information and submits the form. The form then talks to the
manager and says add Joe to Mahesh 102 . The manager tells
Math 102 that it has to add a student.
Math 102 says to Section 1 are you open? In this case,
Section 1 replies that they are open, so Math 103 tells section 1 to
add this student. Again, sequence diagrams are great tools in the
beginning because they show you and your customer step-by-step
what has to happen.
7
drawn as an open circle; and one or more termination points, which
are drawn using a circle with a solid circle inside it. To draw a
statechart diagram, begin with a starting point and a transition line
pointing to the initial state of the class. Draw the states themselves
anywhere on the diagram, and then simply connect them using the
state transition lines.
8
has on the other software components (e.g., software libraries) in
the system. The diagram can be shown at a very high level, with
just the large-grain components, or it can be shown at the
component package level.
Modeling a component diagram is best described through
an example. Figure shows four components: Reporting Tool,
Billboard Service, Servlet 2.2 API, and JDBC API. The arrowed
lines from the Reporting Tool component to the Billboard Service,
Servlet 2.2 API, and JDBC API components mean that the
Reporting Tool is dependent on those three components.
8
RELATIONAL ALGEBRA
Unit Structure
8.0 Objectives
-Non-navigational(non-procedural)
The query only specifies what data is wanted, not how to find
it e.g. relational calculus.
8.1 INTRODUCTION
Fundamental operation
o Unary operation
Projection operation()
o Select Operation()
o Rename Operation()
o Binary Operation
SET operation
o Union operation()
o Difference Operation(-)
o Intersection Operation()
Join Operation(
)
Cartesian Product Operation(X)
Division Operation(%)
8.1.1Selection operation
The Selection operator select the row from the table that
satify a given predicate. This operation allows to manipulate data in
the single relation. The Selection operation is defined by the symbol
called sigma(). The predicate is appear at subscript of Sigma
symbol(). The argument relation is present in the parenthesis after
the
Synatx
<predicate><Comparsion_operator><Constant_value>(<input_table_name>)
Where
Comparsion_Operator:=,<,<=,>,=>,<>
3
Example
Select all the student from the student table whos Roll no
is greater than 300
Student
Roll No
134
356
500
Students_Name
Mary
John
Steve
Students_Address
3 Curry Road
4 Dockyard
6 Nepean Sea Road
Students_Name
John
Steve
Students_Address
4 Dockyard
6 Nepean Sea Road
Students_Name
John
Students_Address
4 Dockyard
<attributes>(<Input_Table_Name>)
4
Example
1)
Students_Name
Mary
John
Steve
Students_Address
3 Curry Road
4 Dockyard
6 Nepean Sea Road
RollNo>300(Student))
e.Roll No,
(e(Student))
e.Roll No
134
356
500
2) Find
e.Students_Name,
e.Students_Name
Mary
John
Steve
e.Students_Address
e.Students_Address
3 Curry Road
4 Dockyard
6 Nepean Sea Road
DEPTNAME
Sales
R&D
Marketing
Sales
EMPFIRSTNAME
Jayesh
Preetesh
Ganesh
Pooja
EMPLASTNAME
Shinde
Shinde
lad
Lad
6
2) Project Table
PROJECTNO
P1
P2
P3
DEPTNAME
R&D
Sales
HR
EMPNO
103
104
105
select deptname
Result: The return value would be sales, marketing, R&D and HR.
8.2.2 Intersect Operator
This operator is find out all the tuples that all the Common tn the
result of Relation 1 and in the Result of Relation 2
Intersect operator doesnot eliminate duplicate record from the
database and they prints the result expression
Syntax
R1 R2
Example : Get all the employee's full name that are working on a
project.
from
B
x
y
R2
C
a
b
c
D
s
t
u
R1XR2
A
1
1
1
2
2
2
B
x
x
x
y
y
y
C
a
b
c
a
b
c
D
s
t
u
s
t
u
8
Example
Employee Table
Empno
101
102
103
Empname
Ramesh
Suresh
Rajesh
Deptno
100
200
100
Department table
Deptno
100
200
Deptname
Sales
R &D
e.Empno,e.Empname,e.Deptno,d.Deptno,d.Deptname
( e (Employee) X e (Department))
e.E
mpno
101
101
102
102
103
103
e.Empname
e.Deptno
d.Deptno
d.Deptname
Ramesh
Ramesh
Ramesh
Ramesh
Rajesh
Rajesh
100
100
200
200
101
101
100
200
100
200
100
200
Sales
R &D
Sales
R &D
Sales
R &D
<tablename>
9
Natural Joins
Assume R1 and R2 have attributes A in common. Natural join
is formed by concatenating all tuples from R1 and R2 with same
values for A, and dropping the occurrences of A in R2
R1
R2 = A(C(R1 R2))
where C is the condition that the values for R1 and R2 are the
same for all attributes in A and A is all attributes in R1 and R2
apart from the occurrences of A in R2.
Course Table
CourseId
CS51T
CS52S
CS52T
CS51S
Title
DBMS
OS
Networking
ES
eid
123
345
345
456
Instructor Table
eid
123
345
456
ename
Rao
Allen
Mansingh
Course
CourseId
CS51T
CS52S
CS52T
CS51S
Instructor
Title
DBMS
OS
Networking
ES
CourseId,ename Course
CourseId
CS51T
CS52S
CS51S
eid
123
345
345
456
Instructor
ename
Rao
Allen
Mansingh
ename
Rao
Allen
Allen
Mansingh
10
8.3.2 Inner Join
R <R.primary_key = S.foreign_key> S
Inner joins return rows only when there is at least one row
from both tables that
matches the join condition.
Inner joins eliminate the rows that do not match with a row from the
other table
Student Table
Studid
100
200
300
name
Jayesh
Preetesh
Pramod
course
PH
CM
CM
Course Table
course#
PH
name
Pharmacy
CM
Computing
course
course#
Course.name
100
name
Jayesh
PH
PH
Pharmacy
200
Preetesh
CM
CM
Computing
300
Pramod
CM
CM
Computing
11
<R.primary_key = S.foreign_key>
Student Table
Studid
100
200
400
name
Jayesh
Preetesh
Pramod
Course#
PH
CM
EN
Course Table
course#
PH
Cname
Pharmacy
CM
Computing
CH
Chemisty
e.studid,e.name,e.Course#,c.Course#,c.Cname
( e (Student)= c (Course)
e.studid
. e.course#
c.course#
c.Cname
100
name
Jayesh
PH
PH
Pharmacy
200
Preetesh
CM
CM
Computing
400
Pramod
EN
NULL
NULL
<R.primary_key = S.foreign_key> S
All rows from S are retained and unmatched rows of R are padded
with NULL
Right outer Jointakes all the record form the right relation S that
unmatched any record in the S relation
12
Student Table
Studid
100
200
400
name
Jayesh
Preetesh
Pramod
Course#
PH
CM
EN
Course Table
course#
PH
Cname
Pharmacy
CM
Computing
CH
Chemisty
e.studid,e.name,e.Course#,c.Course#,c.Cname
( e (Student) = c (Course)
e.studid
. e.course#
c.course#
c.Cname
100
name
Jayesh
PH
PH
Pharmacy
200
Preetesh
CM
CM
Computing
NULL
NULL
NULL
CH
Chemisty
R COLB
1
2
3
4
5
S COLB
S COLA
A
C
D
E
1
2
3
4
13
R.ColA = S.SColA
A
D
E
B
F
NULL
1
3
5
2
4
NULL
A
D
E
NULL
NULL
C
1
3
4
NULL
NULL
2
It is denoted as .
B
1
2
2
3
4
B
2
3
Thefore r%s
A
b
a
p
14
B
2
4
2
B
2
4
1
3
1
(R)=
1
3
Sorting
B (R) =[(5,2),(1,3),(3,4)]
B
3
4
2
15
9.4.2 Aggregation Operators
Operators that summarise or aggregate the values in a single
attribute of a relation.
Operators are the same in relational algebra and SQL.
All operators treat a relation as a bag of tuples.
SUM: computes the sum of a column with numerical values.
AVG: computes the average of a column with numerical values.
MIN and MAX: for a column with numerical values, computes the
smallest or largest value, respectively. for a column with string or
character values, computes the lexicographically smallest or largest
values, respectively.
COUNT: computes the number of non-NULL tuples in a column.
In SQL, can use COUNT (*) to count the number of tuples in a
relation.
Grouping operator
L(R) where L is a list of items in the Relation(R) that are either
a) They ate individual attributes or grouping attributes or
b) (A), Where is an aggregation operator and A the attribute in
the relation(R) to which the aggregation operator is to applied
It is computed by:
1. Group R according to all the grouping attributes on list L.
2. Within each group, compute (A), for each element (A) on list L.
3. Result is the relation whose columns consist ofone tuple for each
group. The components of that tuple are the values associated with
each element of L for that group.
Example
Let R =
Mall
R-Mall
Metro Mall
Phoenix Mall
INORBIT MALL
Spykar
Jeans
Killer
Lee
Lives
Killer
Lee
Compute Jeans,AVG(Price)
Price
1500
1700
1800
1900
1400
16
Metro Mall
Spykar
Phoenix Mall
Jeans
Killer
Killer
Lee
Lee
Lives
Price
1500
1900
1700
1400
1800
Price
3400
3100
1800
17
9
RELATIONAL CALCULUS
Unit Structure
9.0 Objectives
18
p (r)= {t | P (t) }
Where as
p (r)= Selection operator on the Relation
t= Set of tuples( as called as variable range over tuples)
p= Predicate indiacte that is true for t
each formula in the relation calculus is consist of Connectivity by
logical operator such as
(), or (v) not ()
Set of comparison operators: (e.g., , , , , , )
Implication (): x y, if x if true, then y is true
x y x v y
19
Set of quantifiers:
t r (Q(t)) there exists a tuple in t in relation r
such that predicate Q(t) is true
t r (Q(t)) Q is true for all tuples t in relation r
Query to select all attributes of the table
Consider a sample database of an Employee
SSN
101
102
103
104
105
FirstName
Jayesh
Preetesh
Sachin
Pravin
Mahesh
LastName
Shinde
Shinde
Tendulkar
Kanetkar
Jadhav
Salary
30000
40000
50000
35000
53000
FirstName
Preetesh
Sachin
Pravin
Mahesh
LastName
Shinde
Tendulkar
Kanetkar
Jadhav
Salary
40000
50000
35000
53000
Query 2 Find the SSN for each Employee whose having salary
more than 30000
SSN (Salary>30000(Employee))={t|
t[Salary]>300000}
SSN
102
103
104
105
Employee[SSN]=t[SSN]
20
9.1.2 SET Operations
In set operation, two or more select statement is
combined together to form as desired result
On other hand the set operation combines rows from
two or more different queries
In select statement, there must be same number of
columns retrieve from the two or more queries
There must be same data tape or compitable type of each
columns in select statement
In tuple realational calculus ,a query can be written as
r s={t|t r or t s}
Where,
t= Set of tuples( as called as variable range over tuples)
p= Predicate indiacte that is true for t
Reserves Table
SID
22
95
BID
101
103
Day
10/10/96
11/12/96
Sailor Table
SID
22
31
95
Sname
Jayesh
Preetesh
Pramod
rating
7
8
3
age
45.0
55.5
63.5
Find the all Sailors ID whose rating is greater than 2 ,here we use
the union operator in the relational algebra,In the relational
calculus we used two exists clause and Connected by or
SID (Reserves) SID (Sailor)={t|s Reserves(t[SID]=s[SID]) v
uSailor (t[SID]=u[SID] rating>2}
SID
22
31
95
21
In This above Example duplicate record is eliminated
Query to select the data using Intersection operator
Find the those Sailors ID whose rating is greater than 7 ,here we
use the Intersect operator in the relational algebra,In the relational
calculus we used two exists clause and Connected by And
SID (Reserves) SID (Sailor)={t|s Reserves(t[SID]=s[SID])
uSailor (t[SID]=u[SID] rating>7}
SID
31
Designation
Lecturer
Assistant Professor
Professor
DepartNumber
E1
C1
E3
DepartName
Electrical
Computer
Electronics
In The tuple Relational calculus , requires two exits clause they are
connected by
22
The Query Can be Written as
EmployeeID
(EmployeeXDepartment)={t|
uDepartment}
EmployeeID
101
102
103
101
102
103
101
102
103
Designation
Lecturer
Assistant Professor
Professor
Lecturer
Assistant Professor
Professor
Lecturer
Assistant Professor
Professor
sEmployee
DepartNumber
E1
E1
E1
C1
C1
C1
E3
E3
E3
DepartName
Electrical
Computer
Electronics
Electrical
Computer
Electronics
Electrical
Computer
Electronics
Example
Retrives data from the two table knowns asEmployee and
Department
EmployeeID
101
102
103
Designation
Lecturer
Assistant Professor
Professor
DepartNumber
E1
C1
E3
DepartNumber
DepartName
E1
Electrical
C1
Computer
E3
Electronics
In The tuple Relational calculus , requires two exits clause they are
connected by
Find the Employee Id whose teaches in the Computer Department
EmployeeID (Employee
t[EmployeeID]=s[EmployeeID]
Department)={t| sEmployee (
23
uDepartment(u[DepartNumber]=s[DepartNumber]
u[DepartName]=Computer))}
EmployeeID
102
Marks
97
100
98
85
98
95
95
Marks
98
where
and
each
Xi is
either
domain
variable
or
constant,
the query is the set of tuples Xi to Xn which makes the DRC formula
true.
This language uses the same operators as tuple calculus, the
logical connectives (and), (or) and (not). The existential
24
quantifier () and the universal quantifier () can be used to bind
the variables.
Its computational expressiveness is equivalent to that of Relational
algebra
Example Domain Relational Calculus
1) Find the names of all Clerks who earn more than RS 10,000.
{fN, lN | (sN, posn, sex, DOB, sal, bN) (Staff (sN, fN, lN, posn, sex,
DOB, sal, bN)
posn = Clerkssal > 10,000)}
2 ) List the staff who manage properties for rent in Mumbai.
{sN, fN, lN, posn, sex, DOB, sal, bN | (sN1,cty)
(Staff(sN,fN,lN,posn,sex,DOB,sal,bN) PropertyForRent(pN, st, cty,
pc, typ, rms, rnt, oN, sN1, bN1) (sN=sN1) cty=Mumbai)
3) List the names of staff who currently do not manage any
properties for rent.
{fN, lN | (sN) (Staff(sN,fN,lN,posn,sex,DOB,sal,bN) (~(sN1)
(PropertyForRent(pN, st, cty, pc, typ, rms, rnt, oN, sN1,
bN1)(sN=sN1))))}
4 )Retrieve
names
of
taught Management345
all
professors
who
have
2) Find all students who have ever taken a course from every
professor who has ever taught a course.
{I | Transcript(S,C,SEM,G1) AND PI Teaching
(Teaching(PI,C2,SEM2) AND Transcript(S,C,SEM,G2)
.ProfId
25
3) Retrieve IDs of students who did not take any courses in
F2001:
{I |Student (I, N, A, S} AND NOT Transcript (I, C, F2001, G)}
4) Find potential student graders for this semester's courses:
{P, C, S| Teaching (P, C, S2002) AND Transcript(S, C, SEM, G
AND SEM<> S2002}
5) Find all loan numbers for loans with an amount greater than
$1200:
{ < l > | a, b ( < l, a, b > loan a > 1200) }
Equivalent Relational Algebra expression
loan_number ( amount > 1200 (loan))
10 ) Find the loan numbers of all loans made jointly to Amit and
Ramesh.
{ < l > | x ( <x, l> borrower x = Amit) x ( <x, l>
borrower x = Ramesh)}
11)Find the names of all customers who have a loan from the Kurla
branch, and find the loan amount.
{ < c,a > | l ( <c, l> borrower
loan
b = Kurla)}
b ( <l, b, a>
26
9.3 RELATIONAL
CALCULUS
ALGEBRA
Relational
algebra
indicates
operation on table that ptoduces a
new tables ar a result
VS
) (
RELATIONAL
Relational Calculus
The tuple Relational
calculus is a nonprocedural language,
Lets users describe
what
they
want,
rather than how to
compute it.
Relation
Calculus
defines a new table
by
providining
representation
in
term of given relation
In Relational Calculus
A query Can be
written as
{t|P(t)}
I.e The set of tuple t
where Predicate P is
true
In relational Calculus
we needs to provides
a formal description
of the information
10
CONSTRAINTS
Unit Structure
10.0
Objectives
10.1
10.2
10.3
Introduction
Types of Constraints
Integrity Constraints
10.0 OBJECTIVES:
10.1 INTRODUCTION
Definition:
Constraints are used to limit the type of data that can go into
a table.
s
Constraints can be specified when a table is created (with
the CREATE TABLE statement) or after the table is created (with
the ALTER TABLE statement).
Syntax:
Create table table_name
{
Column
data_type[column_constraint_Name][Column_constraint],
Column datatype[DEFAULT expr] [column_constraint],
....................
[table_contraint][....]
}
2
Example:
Some attributes in the table are not required so such
columns can be defined as NULL constraint. In the EMPLOYEE
table it is allowed insert row having Phone number column as
NULL.
Create table EMPLOYEE
{
Did varchar(10),
EName varchar(10),
Phone_Number char (100) NULL
}
Data Integrity
Constraints are used to enforce the data integrity. This
ensures the accuracy and reliability of the data in the database.
The following categories of the data integrity exist:
Entity Integrity
Domain Integrity
Referential integrity
User-Defined Integrity
3
Some of the Constraints are listed below:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
4
Unique Key at column level:
CREATE TABLE employee
(E_Id number(5) PRIMARY
KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10) UNIQUE
);
OR
Unique Key at table level:
CREATE TABLE employee
(E_Id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
CONSTRAINT loc_un UNIQUE(location)
);
PRIMARY KEY CONSTRAINTS:
The PRIMARY KEY constraint uniquely identifies each record in a
database table.
Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have
only ONE primary key.
Syntax to define a Primary key at column level:
column name datatype [CONSTRAINT constraint_name] PRIMARY
KEY
Syntax to define a Primary key at table level:
[CONSTRAINT constraint_name] PRIMARY KEY
(column_name1,column_name2,..)
5
For Example: To create an employee table with Primary Key
constraint, the query would be like.
Primary Key at table level:
CREATE TABLE employee
(E_Id number(5) PRIMARY
KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
OR
6
Syntax to define a Foreign key at table level:
[CONSTRAINT constraint_name] FOREIGN KEY(column_name)
REFERENCES referenced_table_name(column_name);
For Example:
1) Lets use the "product" table and "order_items".
Foreign Key at column level:
CREATE TABLE product
( product_id number(5)
CONSTRAINT pd_id_pk
PRIMARY KEY,
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
OR
7
CREATE TABLE employee
(E_Id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
mgr_id number(5) REFERENCES employee(id),
salary number(10),
location char(10)
);
CHECK CONSTRAINTS:
This constraint defines a business rule on a column. All the rows
must satisfy this rule. The constraint can be applied for a single
column or a group of columns.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name] CHECK (condition)
For Example: In the employee table to select the gender of a
person, the query would be like
Check Constraint at column level:
CREATE TABLE employee
(E_Id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
gender char(1) CHECK (gender in ('M','F')),
salary number(10),
location char(10)
);
Check Constraint at table level:
CREATE TABLE employee
(E_Id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
gender char(1),
salary number(10),
location char(10),
CONSTRAINT gender_ck CHECK (gender in ('M','F'))
);
Form of a relationship: Mapping cardinalities like 1:1, 1Many and Many to many.
11
VIEWS
Unit Structure
11.0
Objectives
11.1
Introduction
11.0 OBJECTIVES:
Introduction to views
Data Independence
Security
Updates on views
Comparison between tables and views
11.1 INTRODUCTION
Definition:
2
CREATING A VIEW
This statement is used to create a view.
Syntax:
CREATE VIEW view_name
HORIZONTAL VIEW
A Horizontal view will restrict the users 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 users 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.
3
Ex:
CREATE VIEW STUDENTS_PASSED AS
SELECT ROLLNO, NAME, PERCENTAGE
FROM STUDENTS
WHERE RESULT =PASS
GROUPED VIEW
Ex:
Find summary information of Employee Salaries in sales
Department.
Defining View
CREATE VIEW Summary_Empl_Sal
(
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.
4
Maximum Salary in sales Department.
Average Salary in sales Department.
Total Salary of Employees in sales Department.
JOINED VIEWS
Ex:
Company database find out all EMPLOYEES for respective
DEPARTMENTS.
Schema Definition:
EMPLOYEE-> EmpID, EmpName, Salary, DeptID
DEPARTMENT-> DeptID, DeptName
View Definition
CREATE VIEW Emp_Details
As
Select Employee,EmpID,
Department, DeptID,
Department, DeptName
From
Where Employee.DeptID=Department.DeptID;
View Call
Select * from Emp_Details
DROPPING VIEW
When a view is no longer needed, it can be removed by using
DROP VIEW statement.
Syntax:
DROP VIEW <VIEW NAME> [CASCADE/RESTRICT]
5
CASCADE: It deletes the view with all dependent view on original
view.
RESTRICT: It deletes the view only if theyre in no other view
depends on this view.
Example:
Consider that we have view VABC and VPQR .View VPQR
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.
UPDATING VIEWS
6
Data Independence
A major purpose of a database system is to provide the users with
an abstract view of data.
To hide the complexity from users database apply different levels of
abstraction. The following are different levels of abstraction.
i. Physical Level
ii. Logical Level
iii. View Level
Physical Level
Logical Level
View Level
The details such as data type etc are not available at this
level.
7
Logical Data Independence
The changes in the logical level do not affect the view level.
This is called logical data independence.
ADVANTAGES OF VIEWS
1. Security Each user can be given permission to access the
database only through a small set of views that contain the specific
data the user is authorized to see, thus restricting the users access
to stored data.
2. Query simplicity A view can draw data from several different tables and
present it as a single table, turning multi table queries into single-table
queries against the view.
3.Structural simplicity Views can give a user a personalized view
of the database structure, presenting the database as a set of
virtual tables that make sense for that user.
4.Insulation from change A view can present a consistent,
unchanged image of the structure of the database, even if the
underlying source tables are split, restructured, or renamed. Note,
however, that the view definition must be updated whenever
underlying tables or columns referenced by the view are renamed.
5. Data integrity If data is accessed and entered through a view,
the DBMS can automatically check the data to ensure that it meets
specified integrity constraints.
DISADVANTAGES OF VIEWS
While views provide substantial advantages, there are also three
major disadvantages to using a view instead of a real table:
Performance
Views create the appearance of a table, but the DBMS must still
translate queries against the view into queries against the
underlying source tables.
If the view is defined by a complex multitable query, then even a
simple query against the view becomes a complicated join, and it
may take a long time to complete.
However, the issue isnt because the query is in a viewany poorly
constructed query can present performance problemsthe hazard
is that the complexity is hidden in the view, and thus users are not
aware of how much work the query is performing.
8
Manageability
Like all database objects, views must be managed. If developers
and database users are allowed to freely create views without
controls or standards, the DBAs job becomes that much more
difficult.
This is especially true when views are created that reference other
views, which in turn reference even more views.
The more layers between the base tables and the views, the more
difficult it is to resolve problems attributed to the views.
Update restrictions
When a user tries to update rows of a view, the DBMS must
translate the request into an update on rows of the underlying
source tables.
This is possible for simple views, but more complex views cannot
be updated; they are
read-only.
The fields in a view are fields from one or more real tables in
the database.
When view is called, it does not contain any data. For that, it
goes to memory and fetches data from base table and
displays it.
9
TABLES
EX:
Consider a Employee containing following columns,
EMPLOYEE (Emp_ID, EmpName, Designation, Address, Salary)
12
STRUCTURED QUERY LANGUAGE
Unit Structure
12.0
Objectives
12.1
Introduction
12.0 OBJECTIVES:
Data Definition
Aggregate Functions
Null Values
Nested Sub queries
Joined relations
Triggers
12.1 INTRODUCTION
FEATURES OF SQL
2
SQL COMMANDS:
a. CREATE COMMAND
This statement used to create Database.
Syntax:
CREATE TABLE tablename
(
column_name data_type attributes,
column_name data_type attributes,
)
Table and column names cant have spaces or be reserved
words like TABLE, CREATE, etc.
3
Example:
CREATE TABLE Employee
(
EmpId varchar2(10),
FirstName char(20),
LastName char(20),
Designation char(20),
City char(20)
);
OUTPUT:
Emp_Id FirstName LastName Designation City
b. ALTER COMMAND:
This statement is used to make modifications to the table
structure.
This statement is also used to add, delete, or modify
columns in an existing table
Syntax:
ALTER
TABLE
table_name
ADD column_name datatype
OR
ALTER
TABLE
table_name
DROP COLUMN column_name
OR
ALTER
TABLE
table_name
MODIFY COLUMN column_name
Example:
ALTER
TABLE
ADD DateOfBirth date
Employee
OUTPUT:
EMP_Id FirstName LastName Designation City
1
Raj
Malhotra
Manager
Mumbai
Henna
Setpal
Executive
Delhi
DROP COMMAND:
This statement is used to delete a table.
DateOfBirth
4
Syntax:
DROP TABLE table_name
Example:
DROP TABLE Employee
DATA MANIPULATION LANGUAGE (DML)
DML is set of commands used to,
Insert data into table
Delete data from table
Update data of table.
EMP_Id FirstName LastName Designation City
1
Raj
Malhotra
Manager
Mumbai
Henna
Setpal
Executive
Delhi
Aishwarya Rai
Trainee
Indore
a. INSERT
The INSERT statement is used to insert a new row in a
table.
Syntax:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
Example:
INSERT INTO Employee VALUES (4,'Nihar)
INSERT INTO Employee VALUES (5,'savita)
INSERT INTO Employee VALUES (6,'Diana)
OUTPUT:
Emp_Id FirstName
4
Nihar
Savita
Diana
b. DELETE
The DELETE statement is used to delete records in a table.
Syntax:
DELETE FROM table_name
WHERE some_column=some_value
5
Example:
EMP_Id FirstName LastName Designation City
1
Raj
Malhotra
Manager
Mumbai
Henna
Setpal
Executive
Delhi
Aishwarya Rai
Trainee
Indore
Henna
Setpal
Aishwarya Rai
Executive
Delhi
Trainee
Indore
c. UPDATE
The UPDATE statement is used to update records in a table.
Syntax:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Example:
EMP_Id FirstName LastName Designation City
2
Henna
Setpal
Aishwarya Rai
Nihar
Executive
Delhi
Trainee
Indore
Sarkar
UPDATE Employee
SET Designation='CEO, City='Mumbai'
WHERE LastName='Sarkar AND FirstName='Nihar'
OUTPUT:
EMP_Id FirstName LastName Designation City
2
Henna
3
4
Setpal
Executive
Delhi
Aishwarya Rai
Trainee
Indore
Nihar
CEO
Mumbai
Sarkar
6
SQL BASIC QUERIES
a.
SELECT CLAUSE
This statement is used for various attributes or columns of a table.
SELECT can have 2 options as SELECT ALL OR SELECT
DISTINCT, where SELECT ALL is default select all rows from table
and SELECT DISTINCT searches for distinct rows of outputs.
Syntax:
SELECT * FROM table_name
b. FROM CLAUSE
This clause is used to select a Relation/Table Name in a database.
c. WHERE CLAUSE
This clause is used to put a condition on a query result.
Example:
Ex1: SELECT * FROM Employee
EmpID FirstName LastName Designation City
1
Raj
Malhotra
Manager
Mumbai
Henna
Setpal
Executive
Delhi
Aishwarya Rai
Trainee
Indore
Raj
Malhotra
Manager
Mumbai
Aliases
Example:
To select the first name of all the students, the query would be like:
7
Aliases for columns:
SELECT
FirstName
AS
Name
FROM
or
SELECT FirstName Name FROM Employee;
Employee;
SQL ORDER BY
The ORDER BY clause is used in a SELECT statement to
sort results either in ascending or descending order. Oracle sorts
query results in ascending order by default.
Syntax
SELECT column-list
FROM table_name [WHERE condition]
[ORDER BY column1 [, column2, .. columnN] [DESC]];
8
Database table "Employee";
EmpID Name
City
Raj
Malhotra
Manager
56000
Mumbai
Henna
Setpal
Executive
25000
Delhi
Aishwarya Rai
Trainee
20000
Indore
Example:
To select the entire Employee from the table above, however, we
want to sort the empoyee by their last name.
SELECT * FROM Employee
ORDER BY LastName
Output:
EmpID
Name
LastName
Designation
Salary
City
Raj
Malhotra
Manager
56000
Mumbai
Henna
Setpal
Executive
25000
Delhi
Aishwarya
Rai
Trainee
20000
Indore
FROM
Employee
OUTPUT:
EmpID Name
Aishwarya Rai
Trainee
20000 Indore
Henna
Setpal
Executive
25000 Delhi
Raj
Malhotra
Manager
56000 Mumbai
9
AGGREGATE FUNCTIONS
SQL aggregate functions return a single value, calculated from
values in a column.
Aggregate functions in SQL are as follows:
Example
StudID
Name
Marks
Rahul
90
Savita
90
Diana
80
Heena
99
Jyotika
89
Rubi
88
AVG() Function
The AVG() function returns the average value of a numeric
column.
This function first calculates sum of column and then divide
by total number of rows.
Syntax:
SELECT AVG(column_name) FROM table_name
Example:
Find average Marks of Students from above table.
SELECT AVG(Marks) AS AvgMarks FROM Employees
The result-set will look like this:
AvgMarks
89.3
10
COUNT() Function
The COUNT() function returns the number of rows that matches a
specified criteria.
Syntax:
SELECT COUNT(column_name) FROM table_name
Example
SELECT COUNT(StudID) AS Count FROM Students
Count
6
SUM() Function
The SUM() function returns the total sum of a numeric column.
Syntax
SELECT SUM(column_name) FROM table_name
Example
Find total of marks scored by students.
Select SUM (Marks) as Sum from Students
OutPut:
SUM
536
MIN() Function
The MIN() function returns the smallest value of the selected
column.
Syntax
SELECT MIN(column_name) FROM table_name
Example
Find minimum scored by students
Select MIN(Marks) as Min from Students
Min
80
11
MAX() Function
The MAX() function returns the largest value of the selected
column.
Syntax
SELECT MAX(column_name) FROM table_name
Example
Find maximum scored by students
Select MAX(Marks) as Max from Students
Max
90
NESTED SUB-QUERIES
SYNTAX:
SELECT select_Item
FROM table_name
WHERE expr_Operator(SELECT select_item
FROM Table_name)
Expression operator can be of 2 types:
1. Single Row Operator
2. Multiple-row Operator
Single Row Operator
A single-row subquery is one where the subquery returns
only one value. In such a subquery you must use a single-row
operator such as:
12
Operator Description
=
Equal To
<>
Not Equal To
>
Greater Than
>=
<
Less Than
<=
Example
<>
>
>=
<
<=
13
A multiple row subquery is one where the subquery may
return more than one value. In such type of subquery, it is
necessary to use a multiple-row operator
The table below describes the multiple-row operators that
can be used when writing multiple-row subqueries:
Operator Meaning
IN
ANY
ALL
Query
Example
IN
Retreive
the SELECT
DEPARTMENT_ID,
department
ID, DEPARTMENT_NAME,
department name and LOCATION_ID
location
ID
of FROM
DEPARTMENTS
departments that are WHERE
LOCATION_ID
IN
located in the same (SELECT LOCATION_ID FROM
location ID as a LOCATIONS
WHERE
location in the UK.
COUNTRY_ID='UK')
14
<ANY
Retrieve the first name SELECT
FIRST_NAME
of
employees
whose
FROM
EMPLOYEES
(Less than the
salary is less than the WHERE SALARY < ANY
maximum
SALARY
value returned maximum salary of (SELECT
employees
in
FROM
EMPLOYEES
WHERE
by
the
department 60.
DEPARTMENT_ID=10)
subquery)
EXISTS CLAUSE
Example:
SELECT *
FROM suppliers
WHERE EXISTS
(select *
from orders
where suppliers.supplier_id = orders.supplier_id);
This select statement will return all records from the suppliers table
where there is at least one record in the orders table with the same
supplier_id.
NOT EXISTS CLAUSE
Example:
SELECT *
FROM suppliers
WHERE not exists (select * from orders Where
suppliers.supplier_id = orders.supplier_id);
This will return all records from the suppliers table where there are
no records in the orders table for the given supplier_id.
NULL VALUES
15
"Employee" table:
EmpId FirstName LastName Address City
1
Hussain
Lakdhwala
Elie
Sen
Ranbir
Kapoor
Santacruz
Juhu
Road
Santacruz
Bhayander
LastName
Hussain
Lakdhwala
Ranbir
Kapoor
Address
FROM
Employee
Address
FROM
Empoyee
16
Output:
FirstName LastName Address
Elie
Sen
Juhu Road
JOINS
Product_name
Supplier_name
Unit_price
100
Camera
Nikon
300
101
Television
LG
100
102
Refrigerator
Videocon
150
103
IPod
Apple
75
104
Mobile
Nokia
50
17
Database table "order_items";
order_id
5100
5101
5102
5103
product_id
104
102
103
101
total_units
30
5
25
10
customer
Infosys
Satyam
Wipro
TCS
Joins can be classified into Equi join and Non Equi join.
1) SQL Equi joins
2) SQL Non equi joins
It is a simple sql join condition which uses the equal sign as the
comparison operator. Two types of equi joins are SQL Outer
join and SQL Inner join.
Example:
We can get Information about a customer who purchased a
product and the quantity of product.
An equi-join is classified into two categories:
a) SQL Inner Join
b) SQL Outer Join
a) SQL Inner Join:
All the rows returned by the sql query satisfy the sql join condition
specified.
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.
QUERY:
SELECT order_id, product_name, unit_price, supplier_name,
total_units
FROM product, order_items
WHERE order_items.product_id = product.product_id;
18
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.
product_name
Camera
Television
Refrigerator
IPod
order_id total_units
5103
5101
5102
10
5
25
19
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, (lets use Employee table
defined earlier.)
QUERY:
SELECT first_name, last_name, subject
FROM Employee
WHERE subject != 'Economics'
Output:
first_name
Anajali
Shekar
Rahul
Stephen
last_name
Bhagwat
Gowda
Sharma
Fleming
subject
Maths
Maths
Science
Science
TRIGGERS
A trigger is an operation that is executed when some kind of
event occurs to the database. It can be a data or object change.
Creation of Triggers
Syntax:
CREATE TRIGGER [owner.]trigger_name
ON[owner.] table_name
FOR[INSERT/UPDATE/DELETE] AS
IF UPDATE(column_name)
[{AND/OR} UPDATE(COLUMN_NAME)...]
{ sql_statements }
20
Triggers Types:
a. Row level Triggers
b. Statement Level Triggers
a. Row Level triggersA row level trigger is fired each time the table is affected by
the triggering statement.
Example:
If an UPDATE statement updates multiple rows of a table, a
row trigger s fired once for each row affected by the update
statement.
A row trigger will not run, if a triggering statement affects no
rows.
If FOR EACH ROW clause is written that means trigger is
row level trigger.
b. Statement Level Triggers
A statement level trigger is fired once on behalf of the
triggering statement, regardless of the number of rows in the
table that the triggering statement affects, even If no rows
are affected.
Example:
If a DELETE statement deletes several rows from a table, a
statement level DELETE trigger is fired only once.
Default when FOR EACH ROW clause is not written in
trigger that means trigger is statement level trigger
Rules of Triggers
21
Below are some examples:
Can access both new values and old values in the database
when going to do any insert, update or delete
Disadvantages of Triggers
13
TRANSACTION MANAGEMENT
Unit Structure
13.0 Objectives
13.1 Introduction
TRANSACTION
EXAMPLE:
To illustrate Banking transaction:
PROPERTIES OF TRANSACTION:
Four properties of Transaction: (ACID PROPERTIES)
1. Atomicity= all changes are made (commit), or none
(rollback).
2. Consistency = transaction won't violate declared system
integrity constraints
3. Isolation= results independent of concurrent transactions.
4. Durability= committed changes survive various classes of
hardware failure
ATOMICITY
CONSISTENCY
Example:
Transaction T1 transfers $100 from Account A to Account B. Both
Account A and Account B contains $500 each before the
transaction.
Transaction T1
Read (A)
A=A-100
Write (A)
Read (B)
B=B+10
Consistency Constraint
Before Transaction execution Sum = A + B
Sum = 500 + 500
Sum = 1000
After Transaction execution Sum = A + B
Sum = 400 + 600
Sum = 1000
Before the execution of transaction and after the execution
of transaction SUM must be equal.
4
ISOLATION
DURABILITY
states
in transaction
1. Active
This is the initial state. The transaction stay in this state while it
is executing.
2. Partially Committed
This is the state after the final statement of the transaction is
executed.
3. Failed
After the discovery that normal execution can no longer
proceed.
4. Aborted
The state after the transaction has been rolled back and the
database has been restored to its state prior to the start of the
transaction.
5. Committed
The state after successful completion of the transaction.
We cannot abort or rollback a committed transaction.
TRANSACTION SCHEDULE
When multiple transactions are executing concurrently, then
the order of execution of operations from the various transactions is
known as schedule.
Serial Schedule
Non-Serial Schedule
6
Serial Schedule
Transactions are executed one by one without any
interleaved operations from other transactions.
Non-Serial Schedule
A schedule where the operations from a set of concurrent
transactions are interleaved.
SERIALIZABILITY
What is Serializability?
A given non serial schedule of n transactions is serializable if it is
equivalent to some serial schedule.
i.e. this non serial schedule produce the same result as of the
serial schedule. Then the given non serial schedule is said to be
serializable.
A schedule that is not serializable is called a non-serializable.
Non-Serial Schedule Classification
Serializable
Not Serializable
Recoverable
Non Recoverable
Serializable Schedule Classification
Conflict Serializable
View Serializable
Conflict Serializable Schedule
If a schedule S can be transformed into a schedule S by a
series of swaps of non conflicting instruction then we say that S and
S are conflict equivalent.
A schedule S is called conflict serializable if it is conflict
equivalent to a serial schedule.
View Serializable Schedule
All conflict serializable schedule are view serializable.
But there are view serializable schedule that are not conflict
serializable.
A schedule S is a view serializable if it is view equivalent to a serial
schedule.
7
Recoverable Schedule Classification
Cascade
Cascadeless
To recover from the failure of a transaction Ti, we may have to
rollback several transactions.
This phenomenon in which a single transaction failure leads to a
series of transaction roll back is called cascading roll back.
Avoid cascading roll back by not allowing reading uncommitted
data.
But this lead to a serial schedule.