1chap22 and Chap 24
1chap22 and Chap 24
1
What is a database
A database is any organized collection of
data. Some examples of databases you
may encounter in your daily life are:
– a telephone book
– T.V. Guide
– airline reservation system
– motor vehicle registration records
– papers in your filing cabinet
– files on your computer hard drive.
Why do we need a database?
• Keep records of our:
– Clients
– Staff
– Volunteers
• To keep a record of activities and
interventions;
• Keep sales records;
• Develop reports;
• Perform research
What is the ultimate purpose of a
database management system?
Is to transform
7
Terminology
Database
• A collection of integrated and related files
File
• A collection of related records
Record
• A collection of related fields
Field
• A group of characters
Character
• Basic building block of information, represented
by a byte
8
Data Entities, Attributes, and
Keys
Entity
• A generalized class of people, places, or things
(objects) for which data are collected, stored, and
maintained E.g., Customer, Employee
Attribute
• A characteristic of an entity; something the entity
is identified by E.g., Customer name, Employee
name
Keys
• A field or set of fields in a record that is used to
identify the record E.g, A field or set of fields
that uniquely identifies the record
9
The Traditional Approach
• The traditional approach…
– Separate files are created and stored for each
application program
10
Drawbacks
• Data redundancy
– Duplication of data in separate files
• Lack of data integrity
– The degree to which the data in any one file is
accurate
• Program-data dependence
– A situation in which program and data organized for
one application are incompatible with programs and
data organized differently for another application
11
Database Approach
• The database approach…
– A pool of related data is shared by multiple
application programs
– Rather than having separate data files, each
application uses a collection of data that is
either joined or related in the database
12
Advantages
– Improved strategic use of corporate data
– Reduced data redundancy
– Improved data integrity
– Easier modification and updating
– Data and program independence
– Better access to data and information
– Standardization of data access
– A framework for program development
– Better overall protection of the data
– Shared data and information resources
13
Disadvantages
– Relatively high cost of purchasing and
operating a DBMS in a mainframe operating
environment
– Increased cost of specialized staff
14
Data Modeling and
Database Models (2)
• Data model
– A map or diagram of entities and their relationships
• Enterprise data modeling
– Data modeling done at the level of the entire
organization
• Entity-relationship (ER) diagrams
– A data model that uses basic graphical symbols to
show the organization of and relationships between
data
15
Hierarchical Database Model
• Hierarchical database model
– A data model in which data are organized in a
top-down, or inverted tree structure
16
Project 1
Project 1 Project 2
Schema
Column Field
SQL
• Structured Query Language, abbreviated
SQL
– Usually pronounced “sequel” but also “ess-cue-ell”)
– The common language of client/server database
management systems.
– Standardized – you can use a common set of SQL
statements with all SQL-compliant systems.
– Defined by E.F. Codd at IBM research in 1970.
– Based on relational algebra and predicate logic
Queries
• Queries are the information retrieval
requests you make to the database
• Your queries are all about the information
you are trying to gather
Reports
• If the query is a question...
...then the report is its answer
• Reports can be tailored to the needs of the
data-user, making the information they
extract much more useful
Data Dictionary
• Data Dictionary
– A detailed description of all data used in the
database
Data Dictionary Features
– Provide a standard definition of terms and
data elements
– Assist programmers in designing and writing
programs
– Simplify database modification
– Reduce data redundancy
– Increase data reliability
– Faster program development
– Easier modification of data and information
Data Definition Language
• Data Definition Language (DDL)
– A collection of instructions and commands
used to define and describe data and data
relationships in a specific database
Database Transactions
1. DML
2. DDL
3. DCL
Transaction starts with an execution of SQL
statements and ends with one of the following:
– COMMIT or ROLLBACK
– DDL or DCL (automatic commit)
– user exits or quits
– system crashes
31
• A cell is also called a FIELD.
• A number of such field placed in
horizontal plane is called a RECORD or a
row.
• To create a cell in which a user can store
and maintain data,the DBA requires a
minimum of three parameters by user.
• Cell name, Cell length, Cell data type.
• These parameters are passed to the DBA
via its natural language,SQL.
• Various Datatypes:
• Char(max len 255)
• Varchar(max len 2000)
• Number
• Date(dd/mm/yy)
• Long(len upto 65,535)
Choosing Column Names
• Define a column for each piece of data
• Allow plenty of space for text fields
• Avoid using spaces in column names
• For the members of an organization:
Column Name Type Remarks
Member_ID int Primary key
First_Name varchar(40)
Last_Name varchar(40)
Phone varchar(30)
Email varchar(50)
Date_Joined smalldatetime Date only, no time values
Meetings_Attended smallint
Officer Yes/No True/False values
22.4 SQL (Structured Query
Language)
SQL keyword Description
SELECT Selects (retrieves) columns from one or more tables.
FROM Specifies tables from which to get columns or delete
rows. Required in every SELECT and DELETE
statement.
WHERE Specifies criteria that determine the rows to be
retrieved.
INNER JOIN Joins rows from multiple tables to produce a single
set of rows.
GROUP BY Specifies criteria for grouping rows.
ORDER BY Specifies criteria for ordering rows.
INSERT Inserts data into a specified table.
UPDATE Updates data in a specified table.
DELETE Deletes data from a specified table.
CREATE Creates a new table.
DROP Deletes an existing table.
COUNT Returns the number of records that satisfy given
search criteria.
Fig. 22.12 SQL keywords.
22.4.1 Basic SELECT Query
• SELECT * FROM tableName
– SELECT * FROM Authors
– SELECT authorID, lastName FROM
Authors
22.4.1 Basic SELECT Query
authorID lastName authorID lastName
1 Deitel 7 Sadhu
2 Deitel 8 McPhie
3 Nieto 9 Yaeger
4 Steinbuhler 10 Zlatkina
5 Santry 11 Wiedermann
6 Lin 12 Liperi
Fig. 22.13 authorID and lastName from the Authors table.
• The create table command:
CREATE TABLE tablename
(columnname datatype(size),
columnname datatype(size));
• Eg:
CREATE TABLE client(client_no
varchar2(6),name varchar2(20),add
varchar2(30),pincode number(6),bal_due
number(10,2));
insertion
• INSERT INTO tablename
[(columnname,columnname)]
Values(expression,expression);
• INSERT INTO client
• (client_no,name,add,Pincode)
VALUES(‘c123’,’lovely’,’phagwara’,
144444);
NOTE:character expression in single quotes
Updation
• UPDATE tablename SET
columnname=expression,
columnname=expression WHERE
columnname=expression;
• UPDATE client SET
name=‘university’,add=jalandhar’
Where client_no=‘c123’;
Deletion
• DELETE FROM tablename;
• DELETE FROM client;
• DELETE FROM client WHERE
client_no=‘c123’;
Controlling Transactions
An automatic commit occurs under the
following circumstances:
– DDL statement is issued
– DCL statement is issued
– normal exit from SQL, without explicitly
issuing COMMIT or ROLLBACK
– Abnormal Termination
42
COMMIT & ROLLBACK
• INSERT, DELETE, & UPDATE data
from a database can be reversed
(ROLLBACK) or committed (COMMIT)
• Show AUTOCOMMIT
• ROLLBACK
• COMMIT
• SET AUTOCOMMIT ON
43
State of Data Before COMMIT
or ROLLBACK
• The previous state of the data can be recovered
• The current user can review the result of the
DML operation by using SELECT
• Other user cannot view the result of the DML
statements by the current user
• The affected rows are locked (other users can not
change the data within the affected rows)
44
State of the Data after COMMIT
• Data changes are made permanent in DB
• The previous state of the data is
permanently lost
• All users can view the results
• Locks are released
• All saved points are erased
45
Committing Data
UPDATE student
SET major = ‘COSC’
WHERE id = 111;
COMMIT;
46
State of the Data after ROLLBACK
• Data changes are undone
• Previous state of the data is restored
• Locks on the rows are released
47
Controlling Transactions
Transaction
COMMIT Point A Point B
Rollback
Rollback
Rollback
Rollback
48
Roll Back Changes to a Marker
• SAVEPOINT PointA;
…………..
• SAVEPOINT PointB;
…………………..
• ROLLBACK TO PointB;
• ROLLBACK ;
49
Distributed Databases
• Distributed database…
– A database in which the actual data may be
spread across several smaller databases
connected via telecommunications devices
Data Warehouse
• Data warehouse
– A relational database management system designed
specifically to support management decision making
– Current evolution of Decision Support Systems
(DSSs)
• Data mart
– A subset of a data warehouse for small and medium-
size businesses or departments within larger
companies
Schematic
Relational
databases
Data
Hierarchical extraction
databases process
Network
Data
databases
cleanup
process
Flat files
Spreadsheets Data
End user access wharehouse
Query and
analysis
tools
Data Mining Applications
• Data mining
– The automated discovery of patterns and relationships
in a data warehouse
• Data mining applications
• Market segmentation
• Customer queries
• Fraud detection
• Direct marketing
• Market basket analysis
• Trend analysis
Object-Relational Database
Management Systems (ORDBMS)
– Object-relational database management system (ORDBMS)
• A DBMS capable of manipulating audio, video, and graphical data.
– Hypertext
• Users can search and manipulate alphanumeric data in an
unstructured way
– Hypermedia
• Allows businesses to search and manipulate multimedia forms of data
– Spatial data technology
• Use of an object-relational database to store and access data
according to the location it describes and to permit spatial queries and
analysis
THAT’S ALL
56