Top Banner
DBMS 3. course
50

DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Dec 24, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

DBMS

3. course

Page 2: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Reminder

• Data independence: logical and physical• Concurrent processing

– Transaction– Deadlock– Rollback– Logging

• ER Diagrams

Page 3: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

• Decision in design– Entity or attribute?– Entity or relationship?– Binary or ternary relationship?

Page 4: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Today

• Relational algebra• Relational calculus• SQL• DDL

Page 5: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Relational Query Languages

• Aim• Relational model supports simple and efficient

QLs• Query language programming language

– Non Turing complete (Turing machine)

Page 6: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Formal relational query languages

• 2 formalisms– Relational algebra– Relational calculus

• They substantiate the implementation level and the concrete query language (e.g. SQL)

Page 7: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Example

• Sailors• Boats• Reserves

R

S

B bid bname color

101 Santa Maria Black

102 Pinta Yellow

103 Nina White

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.0

58 rusty 10 35.0

sid bid day

22 101 25/09/13

58 103 11/09/13

Page 8: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Relational algebra

Page 9: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Goal

• Describes the way of procedure of the relations

• Execution plan

Page 10: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Basic operations

• Union: • Difference: • Descartes multiplication: • Projection: • Selection:

– F: ith and jth element , where

Page 11: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Consequent operations

• Intersection: • Quotient:

– Just if S R

a1 b1

a1 b2

a1 b3

a1 b4

a2 b1

a2 b3

a3 b2

a3 b3

a3 b4

a4 b1

a4 b2

a4 b3

b1

b2

b3

a1

a4

S R

Page 12: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Interpretation of quotient

• E.g. who are the sailors who reserved all the boats?

• b: boat• s: sailor

• : s so that -for all b- there is an {s,b} record in A

Page 13: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

How to get the quotient

• (projection)

• In this case,

Page 14: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Example

a be d

Page 15: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

sno pnos1 p1s1 p2s1 p3s1 p4s2 p1s2 p2s3 p2s4 p2s4 p4

pno p2

pnop2p4

pnop1p2p4

snos1s2s3s4

snos1s4

snos1

𝐴

𝐵1𝐵2

𝐵3

𝐴÷𝐵1 𝐴÷𝐵2 𝐴÷𝐵3

Page 16: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Join

• General join: , that fulfills T• Natural join (inner join): Same values in the same

attributes, • Outer join:

– There can be NULL in the result– R left join S: every line of R comes up at least once (if no

match than NULL for S)– R right join S: every line of S comes up at least once (if no

match than NULL for R)– R full join S: every line of S and R comes up at least once (if

no match than NULL for the other)

Page 17: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

General join

Page 18: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Natural join visually

Page 19: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Practice

• Calculate !

• Reminder: • T: same attributes contain the same values

Page 20: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Practical example

• Cars (C): plate no, manufacturer, type, color• People (P): ID, name, job, address• Owns (O): car, people_id

• Task: query the job of the owners of the blue Audis

Page 21: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Steps

• Find the blue Audis

• Drop all the attributes except the plate no

• Connect to attribute car of relation O

• Drop all the attributes except people_id (projection)

• Connect with table P: • Filter to attribute job: ∏

job

( ∏people ID

( ∏plate no

(𝜎 color = blue t ype = Audi(𝐶))⋈plate   no =car (𝑂 ))⋈𝑃 )

Page 22: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

SQL – 1. blue Audis, projection

• SELECT * FROM C WHEREcolor=’blue’ AND type=’Audi’

• SELECT plate_no FROM C WHEREcolor=’blue’ AND type=’Audi’

Page 23: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

SQL – 2. join with O, projection

• SELECT * FROM (SELECT plate_no FROM C WHERE

color=’blue’ AND type=’Audi’) inner_table1 INNER JOIN O ON (O.car=inner_table1.plate_no)• SELECT people_ID as ID FROM (SELECT plate_no FROM C WHERE

color=’blue’ AND type=’Audi’) inner_table1 INNER JOIN O ON (O.car=inner_table1.plate_no)

Page 24: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

SQL – 3. join with table P

• SELECT * FROM (SELECT people_ID as ID FROM (SELECT plate_no FROM C WHERE

color=’blue’ AND type=’Audi’) INNER JOIN O ON (O.car=C.plate_no) inner_table2 INNER JOIN P USING (ID)

Page 25: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

SQL – 4. projection

• SELECT job FROM (SELECT people_ID as ID FROM (SELECT plate_no FROM C WHERE

color=’blue’ AND type=’Audi’) INNER JOIN O ON (O.car=C.plate_no)) INNER JOIN P USING (ID)

Page 26: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Relational calculus

Page 27: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Goal

• Formalism for the rows of the relation• Declarative• Expression:

– Rows t that satisfy function – E.g. Select … WHERE …

Page 28: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Atomic formulas

1. – row relation 2. – is an operator (e.g. <,>,=,…) that holds

between row ’s th element and row ’s th element

3. – hold between row ’s th element and constant

Page 29: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Formulas

is a formula if1. it is an atomic formula2. an expression made of formulas and logical operators 3. an expression made of formulas and logical operators (

– – there exist , for what holds– – for all , holds

4. expressions can be parenthesized to override precedence ()

5. there is no other formula

Page 30: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Example

Page 31: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Example no. 2

Page 32: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Finally

• Relational algebra and relational calculus can express the same

• Declarative part is user-friendly• The algebra (way of calculation) is the task of

the DB

Page 33: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

SQL

Page 34: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

History of SQL and dialects

• Main advantage: simple and efficient query• DBMS does the evaluation

– Semantics– DBMS can (re)order the operations to optimize– Solution set has to be the same

Page 35: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Development

• SQL-86• SQL-89 (smaller change)• SQL-92 (bigger change)• SQL:1999 (significantly extended)• SQL:2003 (slighly extended)• SQL:2008 (splitted into parts, extended)• SQL:2011 (newest, most of the DBMSs cannot

use)

Page 36: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

20 April 2009

• Oracle buys Sun Microsystems• PostgreSQL (Sun), MySQL ( Sun), Firebird,

Interbase, MSACCESS• Operating systems: Oracle: Linux, Sun: Unix

(Solaris), Microsoft: Windows Server, IBM: Unix (AIX)

• Environment: Sun: Java, Microsoft: .NET, Others with external libraries

• Hardware: Sun: Sparc + Server/Workstation/ Storage, IBM: Datacenter, Server

Page 37: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

27 January 2010• Oracle Corporation (NASDAQ:ORCL) announced today that

it had completed its acquisition of Sun Microsystems, Inc.”• „This time, the difficulty was persuading European antitrust

regulators who were concerned about the fate under Oracle of the open-source MySQL database software business that was part of Sun.”

• „With the addition of servers, storage, Sparc processors, the Solaris operating system, Java, and the MySQL database to Oracle's portfolio of database, middleware, and business applications, we plan to engineer and deliver open and integrated systems -- from applications to disk -- where all the pieces fit and work together out of the box.”

• www.sun.com www.oracle.com/sun/

Page 38: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Sun?

• OpenSolaris (OS): KO ( OpenIndiana)• MySql (DB): „still alive” ( MariaDB)• OpenOffice: KO ( LibreOffice, ApacheOO)• Licence replacements, support contracts

replaced ( obligatory HW support)

Oracle is unavoidable We’re gonna talk about Oracle dialect

Page 39: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Queries

Structured Query Language (SQL) – 4 parts• Data Definition Language (DDL): CREATE and

ALTER tables, views, and indexes• Data Manipulation Language (DML): INSERT,

UPDATE, and DELETE records• Data Control Language (DCL): GRANT and

REVOKE permissions, COMMIT and ROLLBACK transactions

• Query Language: SELECT

Page 40: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

DDL – Data Definition Language

• Tables– CREATE TABLE– ALTER TABLE– DROP TABLE

• Views– CREATE VIEW– DROP VIEW

• Indexes– CREATE [UNIQUE] INDEX– DROP INDEX

Page 41: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Syntaxis

• CREATE TABLE name (attribute description,…keyforeign key(s),constraints (table, column),indexes)

Page 42: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

• CREATE TABLE name(attribute description,…PRIMARY KEY,UNIQUE – unique/secondary key (column)FOREIGN KEY – relationship with other tableCHECK – constraints for columns (attributes),CREATE INDEX …)

Page 43: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Primary key

• Has to be– Unique– Cannot be NULL– In many cases an unsigned integer

• AUTO_INCREMENT– Increased automatically when inserted

Page 44: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Creating relations (tables)

• The domain of theattributes is given

• DBMS validates thedomain constraintson every insertion

CREATE TABLE Students(sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL)

CREATE TABLE Enrolled(sid: CHAR(20), cid: CHAR(20), grade: CHAR(2))

Page 45: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Data types in Oracle

• Everything is stored in tables• char(N) – varchar(N) – varchar2(N)• blob (vs. clob, ~2-4GB)• numeric(N[, M]) / number(N[, M]) / int, float• date / timestamp

Page 46: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Data types in MySQL

• char(N) – varchar(N)• blob vs. text: tiny~, ~, medium~, long~, max. 4GB)• [signed/ unsigned] tinyint (sbyte), smallint

(integer), int, bigint (long), float, double • date / time / datetime / timestamp /year

– DATE: YYYY-MM-DD– DATETIME: YYYY-MM-DD HH:MM:SS– TIMESTAMP: YYYY-MM-DD HH:MM:SS– TIME: HH:MM:SS– YEAR: YYYY

Page 47: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

DROP and ALTER tables

DROP TABLE Students

ALTER TABLE Students ADD firstYear: integer

Page 48: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Example

• CREATE TABLE animals (name VARCHAR2(25),species VARCHAR2(20),gender CHAR(1),born DATE,died DATE);

Page 49: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Giving primary key

• ALTER TABLE animalsADDID INTEGER Unsigned PRIMARY KEY

AUTO_INCREMENT FIRST;

Page 50: DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Thank you for your attention!

• www.uni-obuda.hu/users/varkonyi.teri• OR• elearning.uni-obuda.hu• [email protected]