Вы находитесь на странице: 1из 12

Implementation of Database Exercise 1

Tanmaya Mahapatra Matriculation Number : 340959 tanmaya.mahapatra@rwth-aachen.de

Bharath Rangaraj Matriculation Number : 340909 bharath.rangaraj@rwth-aachen.de

Manasi Jayapal Matriculation Number : 340892 manasi.jayapal@rwth-aachen.de

October 28, 2013

1 Exercise 1.1 : Database Architecture

1.1 Name each of the five layers in the database architecture specified in the lecture, explain the concepts handled in each layer, and the interfaces between layers.

The five layers in the Database Architecture are :

Logical Data Structures

Logical Access Paths/Structures

Storage Structures

Propagation Control/Page Assignment

File Services/Memory Assignment Structures

The important concepts handled in each layer together with t he interfaces between each layer is shown in the Table 1

1

ture specified in the lecture, explain the concepts handled

in each layer, and the interfaces between layers.

1

1.1

EXERCISE 1.1 : DATABASE ARCHITECTURE

Name each of the five layers in the database architec-

2

Table 1: Concepts & Interfaces in Different Layers of Database.

Layer Name

Concepts Handled

Interface Between Layers

Logical Data Structure

Roles Played

Interface

Prepares Query Execution Plan : Translates & Optimize Queries.

Users interact using a Set-oriented Interface (SQL)

Addressing Units

 

Tables

 

Views

Tuples

Auxiliary Structures

 

External

Schema

 

Description

Addressing Units

 

External Records

Sets

 

Keys

Access Paths

 

Continued on next page

ture specified in the lecture, explain the concepts handled

in each layer, and the interfaces between layers.

1

1.1

EXERCISE 1.1 : DATABASE ARCHITECTURE

Name each of the five layers in the database architec-

3

Table 1 – continued from previous page

Layer Name

 

Concepts Handled

Interface Between Layers

Logical

Access

Path-

Roles Played

Interface

s/Structures

Manage Cursor

The Higher Layer interacts using a Record Oriented DB Interface

 

Sort Components

Manages Dictionary

Storage Structures

 

Roles Played

Interface

 

Manage Records & Index

Internal Record Interface ( Stores Records in B * Trees)

Consists of Auxiliary struc- tures like Page Indexes.

Consists of Addressing Units like Pages & Segments

 
 

Continued on next page

ture specified in the lecture, explain the concepts handled

in each layer, and the interfaces between layers.

1

1.1

EXERCISE 1.1 : DATABASE ARCHITECTURE

Name each of the five layers in the database architec-

4

Table 1 – continued from previous page

Layer Name

Concepts Handled

Interface Between Layers

Propagation

Control/-

Roles Played

Interface

Page Assignment

Manages Buffer & Segments

DB Buffer Interface

Consists of Auxiliary struc- tures like Page Tables, Block Tables etc.

Consists of Addressing Units like Blocks & Files.

 

Continued on next page

ture specified in the lecture, explain the concepts handled

in each layer, and the interfaces between layers.

1

1.1

EXERCISE 1.1 : DATABASE ARCHITECTURE

Name each of the five layers in the database architec-

5

Table 1 – continued from previous page

Layer Name

Concepts Handled

Interface Between Layers

File Services/Memory As- signment Structures

Roles Played

Interface

Manages Files & External Memory.

Consists of Addressing Units like tracks, cylinders & chan- nels.

Memory Assignment Structures

 

File Interface (Manages read/write Block)

Consists of Auxiliary struc- tures like File Catalogues, Free-Placement etc.

1

EXERCISE 1.1 : DATABASE ARCHITECTURE

6

1.2 The following tasks belong to different layers, sort them so that they match the architecture top-down

.

1. Buffering

2. Logical Relation and Cursor Management

3. Media Access

4. Access Path Management

5. View Formulation and Management

Solution

1. Buffering =Propagation Control/Page Assignment

2. Logical Relation and Cursor Management =Logical Access Path- s/Structures

3. Media Access =File Services/Memory Assignment Structures

4. Access Path Management =Storage Structures

5. View Formulation and Management =Logical Data Structures

Tasks sorted so that they match the architecture (top-down)

1. View Formulation and Management

2. Logical Relation and Cursor Management

3. Access Path Management

4. Buffering

5. Media Access

1.3 What does data independence mean ? Why is it an important feature of database systems ? Give examples for how data independence is achieved in the five-layered architecture!

Data Independence

Data Independence means application programs are insulate d from changes in the way the data is structured and stored. Data independen ce is achieved through use of the three levels of data abstraction; in parti cular, the concep- tual schema and the external schema provide distinct benefit s in this area. There are 2 types of Data Independence :

1

EXERCISE 1.1 : DATABASE ARCHITECTURE

7

Logical Data Independence The ability to change the logical schema without changing the external schema is called as Logical Data In- dependence.

Physical Data Independence The ability to change the physical schema without changing the logical schema is called as Physical Data Inde- pendence.

Important Feature

Data Independence is regarded as an important feature of Database Systems because :

It facilitates improvement of various layers and changes to various layers without affecting/impacting other layers.

Applications are insulated from how data are structured and stored.

It provides protection from changes in logical structure of Data.

It provides protection from changes in physical structure of Data.

Examples

1. Data independence is achieved by abstracting each higher level to its lower lever that is layer i+1 is the abstraction of layer i and data representation is unique to each layer and the data of each layer is hidden from the other layers.

2. The layers communicate by invoking the abstract methods and while communication the data must be converted to the layer specifi c form since each layer consists of unique data representation and data rep- resentation of each layer is hidden from other layers.

Layers

Abstraction Provided

Logical Data Structures

Position indicator and explicit relations in the table.

Logical access path

Number and kind of physical access paths and internal rep- resentation of tables.

Storage Structures

Management of Buffers and Logging.

Page Assignment Structures

File Maping and Indirect Page Assignment.

Memory Assignment Structures

Technical features and techni- cal details of external media.

1

EXERCISE 1.1 : DATABASE ARCHITECTURE

8

Scenario To understand how Data Independence is achieved in the 5 laye r model we consider a scenario in which a user/program wants some specific information about a particular Book and Author.

1. The User interacts with the top most layer of the DB (L5) wit h the help of a SQL. The user has no idea of where the data or how the data is actually stored.

SELECT B.Title A.Author from Books B, Author A where B.aid = A.aid;

2. It prepares a Query Execution plan and the operations of it are directly invoked at L4 Interface.

3. The L4 layer plans a sort/merge join for the query evaluati on. Sorted objects have to be created explicitly in L4, before the join c an be processed.

4. The Index-scans, which deliver the records to be sorted, f etch them via physical access paths and storage structures managed by L3.

5. L3 offers a variety of storage structures which physically embody the indexes or other types of access paths.

6. The functionality provided by L3 needs to refer to the phys ical Data of DB. The DB Buffer acts as an interface to the DB on external devi ces and provides access to pages based on logical page reference s.

7. L1 encapsulates number, type and location of external dev ices. It together with the OS file Management process actually fetche s the Data stored somewhere on the storage Disk.

The different levels perform different operations without ac tually knowing what other layers are actually doing. The user submitting th e query does not know that the simple SQL is broken into QEP and other complicated stuff finally leading to very low level data access mechanism. This demonstrates ”Data Independence” in the 5 layered Database Model.

The addition or removal of new entities, attributes or relat ionships to the conceptual schema should be possible without having to change existing external schemas or having to rewrite existing app lication pro- grams.

A change to the internal schema such as using a different file or ga- nizations or storage structures, storage devices or indexi ng strategy should be possible without having to change the conceptual or exter- nal schema

2

EXERCISE 1.2 : QUERY LANGUAGES

9

2 Exercise 1.2 : Query Languages

The following relations are given:

lives(pname,city,street) which contains for every person the location(s) he lives,

works(pname,cname,salary) which contains for every person the name of the company that the person works for, as well as his salary,

located(cname,city) which contains the locations for ever y company (i.e. a company can be located in more than one city),

boss(pname,mname), which contains the persons that are sup ervised by a manager.

Formulate the following queries as expressions in relational algebra, tuple relational calculus,domain relational calculus and SQL :

2.1 Find the names of all persons who are working in the same company as their boss and get in this company a higher salary than their boss.

2.1.1 Relational Algebra

a higher salary than their boss. 2.1.1 Relational Algebra 2.1.2 Tuple Relational Calculus { w.pname |

2.1.2 Tuple Relational Calculus

{w.pname | works ( w ) ∧ ∃ ( b ) ( boss ( b ) b.pname = w.pname w.salary < {w.salary | work ( w ) ∧ ∃ ( b ) ( boss ( b ) b.mname = w.pname } ∧ w.cname = {w.cname | works ( w ) ∧ ∃ ( b ) ( boss ( b )) b.mname = w.pname }}

2.1.3 Domain Relational Calculus

{pname | ( cname )( salary )( name 1)( name 2)( < pname, cname, salary > works < name 1, name 2 > boss pname = name 1 salary < {salary | ( cname )( pname )( name 1)( name 2)( < pname, cname, salary > works < name 1, name 2 > boss name 2 = pname ) } ∧ cname = {cname | ( pname )( salary )( name 1)( name 2)( < pname, cname, salary > works < name 1, name 2 > boss name 2 = pname ) }) }

2

EXERCISE 1.2 : QUERY LANGUAGES

10

2.1.4 SQL

1

SELECT PNAME FROM (BOSS NATURAL JOIN WORKS J ) WHERE

3

SALARY > ( SELECT MAX(SALARY) FROM WORKS W WHERE PNAME=MNAME and J .CNAME = W.CNAME)

AND

5

CNAME IN ( SELECT CNAME FROM WORKS WHERE PNAME=MNAME) ;

7

/ ANOTHER METHOD /

9

SELECT EPNAME FROM WORKS W INNER JOIN

11

( SELECT PNAME AS EPNAME, CNAME AS ECNAME,

SALARY AS ESALARY,

MNAME as EMNAME FROM WORKS NATURAL JOIN BOSS) ON W.PNAME=EMNAME AND W.CNAME=ECNAME AND W.SALARY<ESALARY;

Listing 1: SQL Query for finding the names of persons who are working in the same company as their Boss & earn more salary than Boss

2.2 Find the names of all persons, who work for at least two different companies ( 2)

2.2.1

Relational Algebra

1.

Taking the Cartesian Product of “works” table and storing t he result into a table called “Companies” and renaming the fields to avoid duplication :

2.

Persons working for at least 2 different companies:

π

pname1( σ ( pname1=pname2) ( cname1

= cname2) ( companies ))

2.2.2

Tuple Relational Calculus

ρ (companies(1 pname1,2 cname1,3 salary1,4 pname2,5 cname2,6 salary2),works × works))

{w.pname | works ( w ) ∧ {COUNT ( x.pname ) | works ( x ) } ≥ 2}

2.2.3 Domain Relational Calculus

{< pname > | ( cname )( salary )( < pname, cname, salary > works ∧ {COUNT ( pname ) | ( cname )( salary )( < pname, cname, salary > works ) } ≥ 2) }

2.2.4 SQL

2

EXERCISE 1.2 : QUERY LANGUAGES

11

 

SELECT T.PNAME

2

FROMWORKS T GROUP BY T.PNAME HAVING COUNT(T.PNAME) >=2;

Listing 2: SQL Query for finding names of all person working for at least 2 companies

2.3

Find the names of the persons with the highest salary (Note: There might be several persons with the same salary).

2.3.1

Relational Algebra

1.

Find all the salaries less than the others :

π works.salary ( σ works.salary<d.salary ( works × ρ d ( works )) )

2.

Find the largest salary:

π salary ( works ) π works.salary ( ρ works.salary<d.salary ( works ) × ρ d ( works ))

2.3.2

Tuple Relational Calculus

{w.pname | works ( w ) w.salary = {MAX ( x.salary ) | works ( x ) }}

2.3.3

Domain Relational Calculus

{pname | ( cname )( salary )( < pname, cname, salary > works salary ≥ {MAX ( salary ) | ( pname ) ( cname ) ( < pname, cname, salary > works ) }) }

2.3.4

SQL

1

SELECT PNAME FROM WORKS W WHERE

3

(W.SALARY IN ( SELECT MAX(SALARY) FROM WORKS) ) ;

Listing 3: SQL Query for finding the names of the persons with t he highest salary.

2

EXERCISE 1.2 : QUERY LANGUAGES

12

2.4 Find the names of all companies that are located in cities in which ’IBM’ is not located.

2.4.1

Relational Algebra

1.

Choose the city corresponding to the city in which “IBM” is located :

σ cname= IBM ( located )

2.

Select the companies located in cities where IBM is not loc ated:

π cname ( π cname,city σ cname= IBM ( located ))

2.4.2

Tuple Relational Calculus

{l.name | located ( l ) l.city = ¬ {l.city | located ( l ) l.cname = IBM }}

2.4.3 Domain Relational Calculus

{cname | ( city )( < cname, city > located city = ¬ {city | ( cname ) ( < cname, city > located cname = IBM ) }) }

2.4.4 SQL

1

SELECT CNAME FROM LOCATED L WHERE L . CITY NOT IN ( SELECT CITY FROM LOCATED WHERE CNAME = ’IBM ’ ) ;

3

/The Below Query i s CaseI n s e n s i t i v e /

5

SELECT CNAME FROM LOCATED L

7

WHERE (LOWER( TRIM (L . CITY) ) NOT IN

9

( SELECT LOWER( TRIM (CITY) ) FROM LOCATED WHERE LOWER( TRIM ( CNAME) ) = LOWER( TRIM ( ’IBM ’ ) ) ) ) ;

Listing 4: SQL Query for finding the names of all companies that are located in cities in which ’IBM’ is not located.