+ All Categories
Home > Documents > Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant...

Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant...

Date post: 31-Dec-2015
Category:
Upload: jeffrey-neil-lamb
View: 217 times
Download: 1 times
Share this document with a friend
Popular Tags:
72
Lecture 25 Lecture 25 E nterprise S ystems D evelopment ( CSC447) COMSATS Islamabad Muhammad Usman, Assistant Professor
Transcript
Page 1: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Lecture 25Lecture 25

Enterprise

Systems

Development( CSC447)

COMSATS Islamabad

Muhammad Usman, Assistant Professor

Page 2: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

7

Service-oriented design: design sub-stepsService-oriented design: design sub-stepsService orientedanalysis

Service orienteddesign

Compose SOA

Design entity-centric business services

Design infrastructure services

step 3

step 2

step 1

...

Design task-centricbusiness services

step 4

Design SO business process

step 5

Page 3: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

8

Entity-centric business servicesEntity-centric business services

Customer

PO

Employee

Invoice

Order

1

**

1

1

1

*

*

1

1

1

*

*

1

*EmailWeekly hours

CustomerHours billed

...

...

...

Goal: entity-centric business service layer + parent orchestration layer

Receive PO document

PO processingservice

Validate PO document

(If PO document is invalid,)send rejection notification (and end process)

Transform PO document into native electronic PO format

<<include>>

<<include>>

...

Page 4: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

9

Infrastructure servicesInfrastructure services

Infrastructure service layer

Business service layer

Orchestration/coordination layer

POprocessingservice

POservice

Notificationservice

VerifyPOservice

Transformservice

Page 5: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

10

Task-centric business servicesTask-centric business services

UML sequence diagram express and refine order of invocations

implicit in the UML use case diagram

Receive PO document

PO processingservice

Validate PO document

(If PO document is invalid,)send rejection notification (and end process)

Transform PO document into native electronic PO format

<<include>>

<<include>>

...

Verify POservice

POservice

Notificationservice

get_PO[PO data]

send_reject

verify

Page 6: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

DATABASE DESIGNDATABASE DESIGN

Page 7: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Database Management System (DBMS)Database Management System (DBMS)

DBMS contains information about a particular enterprise

Collection of interrelated data

Set of programs to access the data

An environment that is both convenient and efficient to use

Database Applications:

Banking: all transactions

Airlines: reservations, schedules

Universities: registration, grades

Sales: customers, products, purchases

Online retailers: order tracking, customized recommendations

Manufacturing: production, inventory, orders, supply chain

Human resources: employee records, salaries, tax deductions

Databases touch all aspects of our lives

Page 8: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Purpose of Database SystemsPurpose of Database Systems

In the early days, database applications were built directly 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 “buried” in program code rather than being stated explicitly

Hard to add new constraints or change existing ones

Page 9: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Purpose of Database Systems (Cont.)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 Example: 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

– Example: Two people reading a balance and updating it at the same time

Security problems Hard to provide user access to some, but not all, data

Database systems offer solutions to all the above problems

Page 10: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Levels of AbstractionLevels 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 = record

customer_id : string; customer_name : string;customer_street : string;customer_city : string;

end;

View level: application programs hide details of data types. Views can also hide information (such as an employee’s salary) for security purposes.

Page 11: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

View of DataView of Data

An architecture for a database system

Page 12: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Instances and SchemasInstances and Schemas

Similar to types and variables in programming languages

Schema – the logical structure of the database

Example: 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 13: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Data ModelsData Models

A collection of tools for describing Data Data relationships Data semantics Data constraints

Relational model

Entity-Relationship data model (mainly for database design)

Object-based data models (Object-oriented and Object-relational)

Semistructured data model (XML)

Other older models: Network model Hierarchical model

Page 14: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Data Manipulation Language (DML)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

Declarative (nonprocedural) – user specifies what data is required without specifying how to get those data

SQL is the most widely used query language

Page 15: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Data Definition Language (DDL)Data Definition Language (DDL)

Specification notation for defining the database schema

Example: create table account ( account_number char(10),

branch_name 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

Specifies the storage structure and access methods used Integrity constraints

Domain constraints Referential integrity (e.g. branch_name must correspond to a

valid branch in the branch table) Authorization

Page 16: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Relational ModelRelational Model

Example of tabular data in the relational modelAttributes

Page 17: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

A Sample Relational DatabaseA Sample Relational Database

Page 18: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

SQLSQL

SQL: widely used non-procedural language

Example: Find the name of the customer with customer-id 192-83-7465select customer.customer_namefrom customerwhere customer.customer_id = ‘192-83-7465’

Example: 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 19: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Database DesignDatabase Design

The process of designing the general structure of the database:

Logical Design – Deciding on the database schema. Database design requires that we find a “good” collection of relation schemas.

Business decision – What attributes should we record in the database?

Computer Science decision – What relation schemas should we have and how should the attributes be distributed among the various relation schemas?

Physical Design – Deciding on the physical layout of the database

Page 20: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

The Entity-Relationship ModelThe Entity-Relationship Model

Models an enterprise as a collection of entities and relationships

Entity: a “thing” or “object” in the enterprise that is distinguishable from other objects

Described by a set of attributes

Relationship: an association among several entities

Represented diagrammatically by an entity-relationship diagram:

Page 21: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Other Data ModelsOther Data Models

Object-oriented data model

Object-relational data model

Page 22: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Database Application ArchitecturesDatabase Application Architectures

(web browser)

Old Modern

Page 23: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Database Management System InternalsDatabase Management System Internals

Storage management

Query processing

Transaction processing

Page 24: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Storage ManagementStorage 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

Issues:

Storage access

File organization

Indexing and hashing

Page 25: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Query ProcessingQuery Processing

1. Parsing and translation

2. Optimization

3. Evaluation

Page 26: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Query Processing (Cont.)Query Processing (Cont.)

Alternative ways of evaluating a given query

Equivalent expressions

Different algorithms for each operation

Cost difference between a good and a bad way of evaluating a query can be enormous

Need to estimate the cost of operations

Depends critically on statistical information about relations which the database must maintain

Need to estimate statistics for intermediate results to compute cost of complex expressions

Page 27: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Transaction ManagementTransaction 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 28: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Overall System Structure Overall System Structure

Page 29: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

History of Database SystemsHistory of Database Systems

1950s and early 1960s:

Data processing using magnetic tapes for storage

Tapes provide only sequential access

Punched cards for input

Late 1960s and 1970s:

Hard disks allow direct access to data

Network and hierarchical data models in widespread use

Ted Codd defines the relational data model

Would win the ACM Turing Award for this work

IBM Research begins System R prototype

UC Berkeley begins Ingres prototype

High-performance (for the era) transaction processing

Page 30: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

History (cont.)History (cont.)

1980s: Research relational prototypes evolve into commercial systems

SQL becomes industry standard Parallel and distributed database systems Object-oriented database systems

1990s: Large decision support and data-mining applications Large multi-terabyte data warehouses Emergence of Web commerce

2000s: XML and XQuery standards Automated database administration Increasing use of highly parallel database systems Web-scale distributed data storage systems

Page 31: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

RELATIONAL MODELRELATIONAL MODEL

Page 32: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Example of a RelationExample of a Relation

Page 33: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Attribute TypesAttribute Types

Each attribute of a relation has a name

The set of allowed values for each attribute is called the domain of the attribute

Attribute values are (normally) required to be atomic; that is, indivisible

E.g. the value of an attribute can be an account number, but cannot be a set of account numbers

Domain is said to be atomic if all its members are atomic

The special value null is a member of every domain

The null value causes complications in the definition of many operations

Page 34: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Relation SchemaRelation Schema

Formally, given domains D1, D2, …. Dn a relation r is a subset of

D1 x D2 x … x Dn

Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai Di

Schema of a relation consists of

attribute definitions

name

type/domain

integrity constraints

Page 35: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Relation InstanceRelation Instance

The current values (relation instance) of a relation are specified by a table

An element t of r is a tuple, represented by a row in a table

Order of tuples is irrelevant (tuples may be stored in an arbitrary order)

JonesSmithCurryLindsay

customer_name

MainNorthNorthPark

customer_street

HarrisonRyeRyePittsfield

customer_city

customer

attributes(or columns)

tuples(or rows)

Page 36: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

DatabaseDatabase

A database consists of multiple relations

Information about an enterprise is broken up into parts, with each relation storing one part of the information

E.g.

account : information about accounts depositor : which customer owns which account customer : information about customers

Page 37: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

The The customer customer RelationRelation

Page 38: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

The The depositor depositor RelationRelation

Page 39: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Why Split Information Across Relations?Why Split Information Across Relations?

Storing all information as a single relation such as bank(account_number, balance, customer_name, ..)results in

repetition of information

e.g.,if two customers own an account (What gets repeated?)

the need for null values

e.g., to represent a customer without an account

Normalization theory deals with how to design relational schemas

Page 40: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

KeysKeys

Let K R

K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R)

by “possible r ” we mean a relation r that could exist in the enterprise we are modeling.

Example: {customer_name, customer_street} and

{customer_name}

are both superkeys of Customer, if no two customers can possibly have

the same name

In real life, an attribute such as customer_id would be used instead of

customer_name to uniquely identify customers, but we omit it to keep

our examples small, and instead assume customer names are unique.

Page 41: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Keys (Cont.)Keys (Cont.)

K is a candidate key if K is minimal

Example: {customer_name} is a candidate key for Customer, since it

is a superkey and no subset of it is a superkey.

Primary key: a candidate key chosen as the principal means of

identifying tuples within a relation

Should choose an attribute whose value never, or very rarely,

changes.

E.g. email address is unique, but may change

Page 42: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Foreign KeysForeign Keys

A relation schema may have an attribute that corresponds to the primary key of another relation. The attribute is called a foreign key.

E.g. customer_name and account_number attributes of depositor are foreign keys to customer and account respectively.

Only values occurring in the primary key attribute of the referenced relation may occur in the foreign key attribute of the referencing relation.

Page 43: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Schema DiagramSchema Diagram

Page 44: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Query LanguagesQuery Languages

Language in which user requests information from the database.

Categories of languages

Procedural

Non-procedural, or declarative

“Pure” languages:

Relational algebra

Tuple relational calculus

Domain relational calculus

Pure languages form underlying basis of query languages that people use.

Page 45: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Relational AlgebraRelational Algebra

Procedural language

Six basic operators

select: project: union: set difference: –

Cartesian product: x

rename: The operators take one or two relations as inputs and produce a new

relation as a result.

Page 46: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Select Operation – ExampleSelect Operation – Example

Relation rA B C D

1

5

12

23

7

7

3

10

A=B ^ D > 5 (r)A B C D

1

23

7

10

Page 47: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Project Operation – ExampleProject Operation – Example

Relation r: A B C

10

20

30

40

1

1

1

2

A C

1

1

1

2

=

A C

1

1

2

A,C (r)

Page 48: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Union Operation – ExampleUnion Operation – Example

Relations r, s:

r s:

A B

1

2

1

A B

2

3

rs

A B

1

2

1

3

Page 49: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Set Difference Operation – ExampleSet Difference Operation – Example

Relations r, s:

r – s:

A B

1

2

1

A B

2

3

rs

A B

1

1

Page 50: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Cartesian-Product Operation – ExampleCartesian-Product Operation – Example

Relations r, s:

r x s:

A B

1

2

A B

11112222

C D

1010201010102010

E

aabbaabb

C D

10102010

E

aabbr

s

Page 51: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Rename OperationRename Operation

Allows us to name, and therefore to refer to, the results of relational-algebra expressions.

Allows us to refer to a relation by more than one name.

Example:

x (E)

returns the expression E under the name X

If a relational-algebra expression E has arity n, then

returns the result of expression E under the name X, and with the

attributes renamed to A1 , A2 , …., An .

)(),...,,( 21E

nAAAx

Page 52: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Composition of OperationsComposition of Operations Can build expressions using multiple operations

Example: A=C(r x s)

r x s

A=C(r x s)

A B

11112222

C D

1010201010102010

E

aabbaabb

A B C D E

122

101020

aab

Page 53: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Banking ExampleBanking Example

branch (branch_name, branch_city, assets)

customer (customer_name, customer_street, customer_city)

account (account_number, branch_name, balance)

loan (loan_number, branch_name, amount)

depositor (customer_name, account_number)

borrower (customer_name, loan_number)

Page 54: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Example QueriesExample Queries

Find all loans of over $1200

Find the loan number for each loan of an amount greater than $1200

amount > 1200 (loan)

loan_number (amount > 1200 (loan))

Find the names of all customers who have a loan, an account, or both, from the bank

customer_name (borrower) customer_name (depositor)

Page 55: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Example QueriesExample Queries

Find the names of all customers who have a loan at the Perryridge branch.

Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank.

customer_name (branch_name = “Perryridge”

(borrower.loan_number = loan.loan_number(borrower x loan))) –

customer_name(depositor)

customer_name (branch_name=“Perryridge”

(borrower.loan_number = loan.loan_number(borrower x loan)))

Page 56: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Example QueriesExample Queries

Find the names of all customers who have a loan at the Perryridge branch.

customer_name(loan.loan_number = borrower.loan_number (

(branch_name = “Perryridge” (loan)) x borrower))

customer_name (branch_name = “Perryridge” (

borrower.loan_number = loan.loan_number (borrower x loan)))

Page 57: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Additional OperationsAdditional Operations

Additional Operations

Set intersection

Natural join

Aggregation

Outer Join

Division

All above, other than aggregation, can be expressed using basic operations we have seen earlier

Page 58: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Set-Intersection Operation – ExampleSet-Intersection Operation – Example

Relation r, s:

r s

A B

121

A B

23

r s

A B

2

Page 59: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Natural Join Operation – ExampleNatural Join Operation – Example

Relations r, s:

A B

12412

C D

aabab

B

13123

D

aaabb

E

r

A B

11112

C D

aaaab

E

s

r s

Page 60: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Notation: r s

Natural-Join OperationNatural-Join Operation

Let r and s be relations on schemas R and S respectively. Then, r s is a relation on schema R S obtained as follows:

Consider each pair of tuples tr from r and ts from s.

If tr and ts have the same value on each of the attributes in R S, add a

tuple t to the result, where

t has the same value as tr on r

t has the same value as ts on s

Example:

R = (A, B, C, D)

S = (E, B, D)

Result schema = (A, B, C, D, E)

r s is defined as:

r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))

Page 61: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Aggregate Functions and OperationsAggregate Functions and Operations

Aggregation function takes a collection of values and returns a single value as a result.

avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values

Aggregate operation in relational algebra

E is any relational-algebra expression

G1, G2 …, Gn is a list of attributes on which to group (can be empty)

Each Fi is an aggregate function

Each Ai is an attribute name

)()(,,(),(,,, 221121E

nnn AFAFAFGGG

Page 62: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Aggregate Operation – ExampleAggregate Operation – Example

Relation r:

A B

C

7

7

3

10

g sum(c) (r) sum(c )

27

Question: Which aggregate operations cannot be expressed using basic relational operations?

Page 63: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Aggregate Operation – ExampleAggregate Operation – Example

Relation account grouped by branch-name:

branch_name g sum(balance) (account)

branch_name account_number balance

PerryridgePerryridgeBrightonBrightonRedwood

A-102A-201A-217A-215A-222

400900750750700

branch_name sum(balance)

PerryridgeBrightonRedwood

13001500700

Page 64: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Aggregate Functions (Cont.)Aggregate Functions (Cont.)

Result of aggregation does not have a name

Can use rename operation to give it a name

For convenience, we permit renaming as part of aggregate operation

branch_name g sum(balance) as sum_balance (account)

Page 65: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Outer JoinOuter Join

An extension of the join operation that avoids loss of information.

Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join.

Uses null values:

null signifies that the value is unknown or does not exist

All comparisons involving null are (roughly speaking) false by definition.

Page 66: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Outer Join – ExampleOuter Join – Example

Relation loan

Relation borrower

customer_name loan_number

JonesSmithHayes

L-170L-230L-155

300040001700

loan_number amount

L-170L-230L-260

branch_name

DowntownRedwoodPerryridge

Page 67: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Outer Join – ExampleOuter Join – Example

Join

loan borrower

loan_number amount

L-170L-230

30004000

customer_name

JonesSmith

branch_name

DowntownRedwood

JonesSmithnull

loan_number amount

L-170L-230L-260

300040001700

customer_namebranch_name

DowntownRedwoodPerryridge

Left Outer Join

loan borrower

Page 68: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Outer Join – ExampleOuter Join – Example

loan_number amount

L-170L-230L-155

30004000null

customer_name

JonesSmithHayes

branch_name

DowntownRedwoodnull

loan_number amount

L-170L-230L-260L-155

300040001700null

customer_name

JonesSmithnullHayes

branch_name

DowntownRedwoodPerryridgenull

Full Outer Join

loan borrower

Right Outer Join

loan borrower

Page 69: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Null ValuesNull Values

It is possible for tuples to have a null value, denoted by null, for some

of their attributes

null signifies an unknown value or that a value does not exist.

The result of any arithmetic expression involving null is null.

Aggregate functions simply ignore null values (as in SQL)

For duplicate elimination and grouping, null is treated like any other

value, and two nulls are assumed to be the same (as in SQL)

Page 70: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Bank Example QueriesBank Example Queries

Find the names of all customers who have a loan and an account at bank.

customer_name (borrower) customer_name (depositor)

Find the name of all customers who have a loan at the bank and the

loan amount

customer_name, loan_number, amount (borrower loan)

Page 71: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

Query 1

customer_name (branch_name = “Downtown” (depositor account ))

customer_name (branch_name = “Uptown” (depositor account))

Bank Example QueriesBank Example Queries

Find all customers who have an account from at least the “Downtown” and the Uptown” branches.

Page 72: Lecture 25 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.

ReferenceReference

Silberschatz, Database System Concepts-5th edition


Recommended