54
CS157A Lecture 2 DB Mangement Systems Prof. Sin-Min Lee Department of Computer Science San Jose State University

27 fcs157al2

Embed Size (px)

Citation preview

Page 1: 27 fcs157al2

CS157A Lecture 2 DB Mangement Systems

Prof. Sin-Min Lee

Department of Computer Science

San Jose State University

Page 2: 27 fcs157al2

Objectives

Purpose of Database Systems View of Data Data Models Data Definition Language Data Manipulation Language Transaction Management Storage Management Database Administrator Database Users Overall System Structure

In this Lecture you will learn:

Page 3: 27 fcs157al2

1. Database Theory Why use database?

Data is a valuable corporate resource which needs adequate, accuracy, consistency and security controls.

The centralised control of data means that for many applications the data will already exist, and facilitate quicker development.

Data will no longer be related by application programs, but by the structure defined in the database.

Easier to maintain systems

Page 4: 27 fcs157al2
Page 5: 27 fcs157al2

First generation database systems

The network and hierarchical databases of the 1970’s

The first systems to offer DBMS function in a unified system

e.g. CODASYL, IMS

Page 6: 27 fcs157al2

Second generation database systems

relational databases of the 1980’s data independence and non

procedural data manipulation language e.g. DB2, INGRES, NON-STOP SQL,

ORACLE, Rdb/VMS Focused on business data processing

Page 7: 27 fcs157al2

Third generation database systems

Problems with 2nd generation DBS– inadequate for a broader class of applications (than

business data processing)– e.g. CAD, CASE, Hypertext, – storing text segments, graphics, etc is usually difficult in

2nd gen. systems– Does not support complex data (folders)

Most vendors are working on functional enhancements on their 2nd gen. systems

Surprising degree of consensus on these features 3rd gen systems includes the desired capabilities of

next generation database systems

Page 8: 27 fcs157al2

DATABASE AND DBMS CONCEPTS

Database – is a collection of data, typically describing the activities of one or more related organizations.

A Database Management System DBMS) is software designed to assist in maintaining and utilization large collections of data.

The SQL (Structured Query Language), developed by IBM is now the standard query language.

Page 9: 27 fcs157al2

ADVANTAGES OF A DBMS• Data independence

Application programs should not, ideally, be exposed to details of data representation and storage.

• Efficient Data access

A DBMS uses several powerful functions to store and retrieve data efficiently.

Data Integrity and Security

The DBMS enforces integrity constraints to get a kind of protection against prohibited access to data.

Page 10: 27 fcs157al2

ADVANTAGES OF A DBMS• Data Administration

When any users share the data, centralizing the administration of data can offer significant improvements.

• Concurrent Access and Crash Recovery

A DBMS schedules concurrent access to the data in such manner that users can think of the data as being accessed by only one user at a time. DBMS also protects users from the effects of system failures.

• Reduced Application Development Time

DBMS includes several important functions that are common to many applications accessing data in the DBMS. In conjunction with the high-level interface to the data, facilitates quick application development.

Page 11: 27 fcs157al2

DATA MODEL CONCEPT A data model is a collection of high-level data

description constructs that hide many low-level storage details.

Most database management systems today are based on the Relational data model.

Page 12: 27 fcs157al2

THE RELATIONAL MODEL The central data description construct is this model

is a Relation, which can be thought of a set of records.

A Schema for a relation specifies:– Name of relation (e.g. Students)– Name of each field (or attribute or column)– Type of each field.

Schema Sudents (sid:string,age:integer)

Page 13: 27 fcs157al2

Example of an Instance of Students

Sid Age

53-666 28

53-688 35

Students

Schema ------> Students (sid : String, Age : Integer)

Page 14: 27 fcs157al2

Characteristics Each row in the Students relation is a

record that describes any student. Integrity constraints are conditions that

the records must satisfy. E.g. sid is unique.

Oracle uses relational( and also object) data model.

Page 15: 27 fcs157al2

Levels of Abstraction in a DBMS

1. Conceptual Schema : or logical schema describes all relations that are stored in the database.

In the university example, these relations contain information about entities, such as students and faculty, and about relationships, such as students’ enrollment in courses.

Page 16: 27 fcs157al2

Conceptual Schema

For university, a conceptual schema is:

– Students(sid:string,Age:integer)– Faculty (fid: string, salary: real)– Courses (cid: string, cname: string,

credits:integer)

Page 17: 27 fcs157al2

Physical Schema2. Physical Schema : specifies additional

storage details. It summarizes how the relations described

in the conceptual schema are stored on secondary storage devices such as disks and tapes.

Creation of data structures called indexes, to speed up data retrieval operations

Page 18: 27 fcs157al2

Physical SchemaA sample physical schema for the

university:

– Store all relations as unsorted files of records

– Create indexes on the first column of Students, Faculty, and Courses relations.

Page 19: 27 fcs157al2

External Schema3. Each external schema consists of a

collection of one or more views and relations from the conceptual schema.

A view is conceptually a relation, but the records in a view are not stored in the DBMS.

A user creates any view from data already stored.

Page 20: 27 fcs157al2

External Schema For example: we might want to allow

students to find out the names of faculty members teaching courses.– This is the view associated:

Courseinfo (cid:string, fname:string)

– A user can treat a view just like a relation and ask questions about the records in the view, even though the records in the view are not stored explicitly.

Page 21: 27 fcs157al2

Database Management System (DBMS)

•A software package such as Oracle or MS-Access.

•Manages data and relationships in the database.

•Creates a Data Dictionary to store Metadata – data about data.

•Manages all day-to-day transactions.

•Provides user with data independence at application level.

•Transforms logical data requests to match physical data structures.

•Secures access through passwords, restricted user access, and encryption.

•Provides backup and recovery mechanisms.

•Provides export and import utilities.

•Allows sharing of data with locking capabilities.

Page 22: 27 fcs157al2

In the the past as new applications were written they used existing files, or created a new file for their use.

Sometimes several existing files need to be sorted and merged to obtain the new file.Thus, it is probable that several files will contain the same information stored in different ways. In other words, there will be redundant and possibly inconsistent data.

Consider the files for an insurance company

Traditional File Systems

POLICY#

POLICYHOLDER

PREMIUMS data ADDRESS

PREMIUM-PA

PREMIUM-TOTAL

POLICY#

POLICYHOLDER

AGENCY data ADDRESS

AGENT-CODE

RENEWAL-DATE

RENEWAL-AMT

Page 23: 27 fcs157al2

Applications were often considered in relative isolation.

Data that should have been together was not.

The potential for flexible enquiry and reporting was limited.

All validations were in the programs.

Procedures were required to for backup and recovery.

All programmers had access to all records.

There was limited concurrent access.

Traditional File Systems

Page 24: 27 fcs157al2

Basic Definitions

Database: A collection of related data

Data: Known facts that can be recorded

Schema: Some part of the real world about which data is stored in the database.

Database Management System(DBMS):A software package to facilitate the creation and maintenance of a computerised database.

Page 25: 27 fcs157al2

Degrees of Data Independence

· Device Characteristics

· Blocking Factors

· Data Access Organisation

· Physical Record Location

· Logical Views (Local)

· Virtual Data Items

· Virtual Records

· Data Value Characteristics

· Data Element Name Only

Page 26: 27 fcs157al2

Logical vs Physical Data Independence

GLOBALLOGICAL

DATABASEDESCRIPTION

Application

Program

Local Views

Physical

Files

Logical Data

Independence

Physical Data

Independence

Page 27: 27 fcs157al2

Three Schema Architecture

ANSI & ISO suggest that DBMS should have three schemas

· CONCEPTUAL SCHEMA - the global logical model of the data and processing of the enterprise. i.e community user view.

· EXTERNAL SCHEMA(S) - the logical application views of the CS. i.e individual user views.

· INTERNAL SCHEMA - internal level storage view.

Page 28: 27 fcs157al2

External Schema 1

External Schema 2

External Schema n

ConceptualSchema

InternalSchema

Three Schema Architecture

Page 29: 27 fcs157al2

The External Level

· Each user has a language through which they access or see the database.

· For the programmer - COBOL etc, for the end-user a query language or special purpose language.

· All languages will contain a data sub-language which may be tightly or loosely coupled to the host language.

Page 30: 27 fcs157al2

· DSLs generally contain a data definition language DDL and data manipulation language DML.

Page 31: 27 fcs157al2

The Conceptual Level· A representation of the entire information

content of the database.

· Defined with a Conceptual Schema Language which does not represent any storage or access details.

· Should include all security and integrity rules and some suggest the CS should describe the total enterprise including all allowable processing.

Page 32: 27 fcs157al2

The Internal Level· Low level representation of entire database.

· Deals with stored records rather than conceptual or external records.

· Stored records may differ in structure from conceptual records and external records.

· The Internal Schema is still one level away from physical records which are often called pages or blocks

Page 33: 27 fcs157al2

Inter-Related Data

CLAIMS

RENEWALS

AGENCY

DBMS

RENEWALS

CLAIMS AGENCY

QUERY

Data related by structure

Flexible enquiry easier

Page 34: 27 fcs157al2

Multiple Applications

CLAIMSAGENCY

DATABASELOCALVIEWS

RENEWALS

Page 35: 27 fcs157al2

Database Management System (DBMS)

Collection of interrelated data Set of programs to access the data DBMS contains information about a particular enterprise DBMS provides an environment that is both convenient and

efficient to use. Database Applications:

– Universities: registration, grades– Banking: all transactions– Sales: Airlines: reservations, schedules– customers, products, purchases– Manufacturing: production, inventory, orders, supply chain– Human resources: employee records, salaries, tax

deductions Databases touch all aspects of our lives

Page 36: 27 fcs157al2

Purpose of Database System In the early days, database applications were

built on top of file systems Drawbacks of using file systems to store

data:– Data redundancy and inconsistency

Multiple file formats, duplication of information in different files

– Difficulty in accessing data Need to write a new program to carry out each new task

– Data isolation — multiple files and formats– Integrity problems

Integrity constraints (e.g. account balance > 0) become part of program code

Hard to add new constraints or change existing ones

Page 37: 27 fcs157al2

Purpose of Database Systems (Cont.)

Drawbacks of using file systems (cont.) – Atomicity of updates

Failures may leave database in an inconsistent state with partial updates carried out

E.g. transfer of funds from one account to another should either complete or not happen at all

– Concurrent access by multiple users Concurrent accessed needed for performance Uncontrolled concurrent accesses can lead to

inconsistencies– E.g. two people reading a balance and updating it at the same

time

– Security problems Database systems offer solutions to all the

above problems

Page 38: 27 fcs157al2

Levels of Abstraction Physical level describes how a record (e.g.,

customer) is stored. Logical level: describes data stored in

database, and the relationships among the data.

type customer = recordname : string;street : string;city : integer;

end; View level: application programs hide details

of data types. Views can also hide information (e.g., salary) for security purposes.

Page 39: 27 fcs157al2

View of DataAn architecture for a database system

Page 40: 27 fcs157al2

Instances and Schemas Similar to types and variables in programming languages Schema – the logical structure of the database

– e.g., the database consists of information about a set of customers and accounts and the relationship between them)

– Analogous to type information of a variable in a program– Physical schema: database design at the physical level– Logical schema: database design at the logical level

Instance – the actual content of the database at a particular point in time – Analogous to the value of a variable

Physical Data Independence – the ability to modify the physical schema without changing the logical schema– Applications depend on the logical schema– In general, the interfaces between the various levels and

components should be well defined so that changes in some parts do not seriously influence others.

Page 41: 27 fcs157al2

Data Models

A collection of tools for describing – data – data relationships– data semantics– data constraints

Entity-Relationship model Relational model Other models:

– object-oriented model– semi-structured data models– Older models: network model and hierarchical model

Page 42: 27 fcs157al2

Entity-Relationship ModelExample of schema in the entity-relationship model

Page 43: 27 fcs157al2

Entity Relationship Model (Cont.)

E-R model of real world– Entities (objects)

E.g. customers, accounts, bank branch

– Relationships between entities E.g. Account A-101 is held by customer Johnson Relationship set depositor associates customers with

accounts

Widely used for database design– Database design in E-R model usually converted

to design in the relational model (coming up next) which is used for storage and processing

Page 44: 27 fcs157al2

Relational Model Example of tabular data in the relational model

customer-name

Customer-id customer-street

customer-city

account-number

Johnson

Smith

Johnson

Jones

Smith

192-83-7465

019-28-3746

192-83-7465

321-12-3123

019-28-3746

Alma

North

Alma

Main

North

Palo Alto

Rye

Palo Alto

Harrison

Rye

A-101

A-215

A-201

A-217

A-201

Attributes

Page 45: 27 fcs157al2

A Sample Relational Database

Page 46: 27 fcs157al2

Data Definition Language (DDL)

Specification notation for defining the database schema– E.g.

create table account ( account-number char(10), balance integer)

DDL compiler generates a set of tables stored in a data dictionary

Data dictionary contains metadata (i.e., data about data)– database schema – Data storage and definition language

language in which the storage structure and access methods used by the database system are specified

Usually an extension of the data definition language

Page 47: 27 fcs157al2

Data Manipulation Language (DML)

Language for accessing and manipulating the data organized by the appropriate data model– DML also known as query language

Two classes of languages – Procedural – user specifies what data is required

and how to get those data – Nonprocedural – user specifies what data is

required without specifying how to get those data SQL is the most widely used query language

Page 48: 27 fcs157al2

SQL SQL: widely used non-procedural language

– E.g. find the name of the customer with customer-id 192-83-7465

select customer.customer-namefrom customerwhere customer.customer-id = ‘192-83-7465’

– E.g. find the balances of all accounts held by the customer with customer-id 192-83-7465

select account.balancefrom depositor, accountwhere depositor.customer-id = ‘192-83-7465’ and depositor.account-number =

account.account-number Application programs generally access databases through one of

– Language extensions to allow embedded SQL– Application program interface (e.g. ODBC/JDBC) which allow SQL queries to

be sent to a database

Page 49: 27 fcs157al2

Database Users Users are differentiated by the way they expect to

interact with the system Application programmers – interact with system through

DML calls Sophisticated users – form requests in a database query

language Specialized users – write specialized database

applications that do not fit into the traditional data processing framework

Naïve users – invoke one of the permanent application programs that have been written previously– E.g. people accessing database over the web, bank tellers,

clerical staff

Page 50: 27 fcs157al2

Database Administrator Coordinates all the activities of the database

system; the database administrator has a good understanding of the enterprise’s information resources and needs.

Database administrator's duties include:– Schema definition– Storage structure and access method definition– Schema and physical organization modification– Granting user authority to access the database– Specifying integrity constraints– Acting as liaison with users– Monitoring performance and responding to changes in requirements

Page 51: 27 fcs157al2

Transaction Management

A transaction is a collection of operations that performs a single logical function in a database application

Transaction-management component ensures that the database remains in a consistent (correct) state despite system failures (e.g., power failures and operating system crashes) and transaction failures.

Concurrency-control manager controls the interaction among the concurrent transactions, to ensure the consistency of the database.

Page 52: 27 fcs157al2

Storage Management Storage manager is a program module that

provides the interface between the low-level data stored in the database and the application programs and queries submitted to the system.

The storage manager is responsible to the following tasks: – interaction with the file manager – efficient storing, retrieving and updating of data

Page 53: 27 fcs157al2

Overall System Structure

Page 54: 27 fcs157al2

Application Architectures

Two-tier architecture: E.g. client programs using ODBC/JDBC to communicate with a databaseThree-tier architecture: E.g. web-based applications, and applications built using “middleware”