1 L'immaginazione è più importante della conoscenza. La conoscenza è limitata, l'immaginazione...

Post on 01-Apr-2015

218 views 2 download

Transcript of 1 L'immaginazione è più importante della conoscenza. La conoscenza è limitata, l'immaginazione...

1

L'immaginazione è più importante della conoscenza. La conoscenza è limitata, l'immaginazione abbraccia il mondo, stimolando il progresso, facendo nascere l'evoluzione. ( A. Einstein )

2

Le BASI DI DATI : le fondamenta dei moderni sistemi Software

Di cosa parleremo

3

Paolo Castelletti– Esperienza: 15 anni maturati in aziende IT in

ambito internazionale e Italiano, sia come libero professionista che come dipendente;

– Ruoli: Responsabile Data Base Administration, DBA, Project Manager.

– paolo.castelletti@aspasiel.it– paolocastle@gmail.com– paolocastle.wordpress.com

Mi presento

4

Chi siamo:Aspasiel è una Società del Gruppo Acciai Speciali Terni. Fornisce la sua competenza nel campo delle soluzioni ICT (Information and Communication Technology) e servizi infrastrutturali per aziende e gruppi industriali. La forza di Aspasiel è nelle capacità e nel know-how del suo personale.

La Mission:I dati e le informazioni costituiscono il patrimonio di un’Azienda.Le minacce informatiche, i guasti, i disastri e gli errori utente mettono a repentaglio la loro sicurezza, Disponibilità, Integrità, Riservatezza. L’obiettivo è supportare le aziende fornendo servizi IT di alta qualità e permettendogli di concentrare tutte le energie nella crescita del loro Business.

La mia ditta

5

Edgar Frank "Ted" Codd

Lawrence Joseph "Larry" Ellison

Personaggi Notevoli

6

Le architetture multitier

7

La diffusione dei RDBMS oggi

8

Chi lavora con il RDBMS

DB Developer

DB Administrat

or

9

Torniamo alla teoria : come progettiamo un DB oggi ?

Modello ER Normalizzazione

Decomposizione dello schema di relazioni

Obiettivi: Eliminare la

ridondanza dati Rappresentare

correttamente tutto lo schema di relazioni

10

Mai sentito parlare di “Forme Normali?”

11

DB non normalizzato

ContactsName Company Address Phone1 Phone2 Phone3 ZipCode

Joe ABC 123 5532 2234 3211 12345

Jane XYZ 456 3421 14454

Chris PDQ 789 2341 6655 14423

12

Prima forma normale

ContactsId Name Compan

yAddress Phone ZipCode

1 Joe ABC 123 5532 12345

1 Joe ABC 123 2234 12345

1 Joe ABC 123 3211 12345

2 Jane XYZ 456 3421 14454

3 Chris PDQ 789 2341 14423

3 Chris PDQ 789 6655 14423

Benefits: Now we can have infinite phone numbers or company addresses for each contact.

Drawback: Now we have to type in everything over and over again. This leads to inconsistency, redundancy and wasting space. Thus, the second normal form…

13

Seconda forma normale

People

Id Name Company Address Zip

1 Joe ABC 123 12345

2 Jane XYZ 456 14454

3 Chris PDQ 789 14423

PhoneNumbers

PhoneID Id Phone

1 1 5532

2 1 2234

3 1 3211

4 2 3421

5 3 2341

6 3 6655

14

Terza forma normale

Elimina i campi che non dipendono dalle PK

People

Id Name AddressID

1 Joe 1

2 Jane 2

3 Chris 3

Address

AddressID Company

Address Zip

1 ABC 123 12345

2 XYZ 456 14454

3 PDQ 789 14423

PhoneNumbers

PhoneID Id Phone

1 1 5532

2 1 2234

3 1 3211

4 2 3421

5 3 2341

6 3 6655

15

Tipi di relazioni

Uno a UnoOne row of a table matches exactly to another

One person, one id number, one address

Uno a MoltiOne row of a table matches many of another

One person, many phone numbers

Molti a MoltiOne row may match many of another or many rows match

one row of another

16

Quarta forma normale

In una relazione molti-a-molti, entità indipendenti non possono essere memorizzate nella stessa tabella.

PhoneNumbers

PhoneID Phone

1 5532

2 2234

3 3211

4 3421

5 2341

6 6655

PhoneRelationsPhoneRelID Id PhoneID

1 1 1

2 1 2

3 1 3

4 2 4

5 3 5

6 3 6

People

Id Name AddressID

1 Joe 1

2 Jane 2

3 Chris 3

Address

AddressID Company Address Zip

1 ABC 123 12345

2 XYZ 456 14454

3 PDQ 789 14423

18

Quindi … perché normalizzare una base dati?

1. Aumenta l’integrità dei dati2. Riduce la ridondanza3. Migliora l’efficienza nell’accesso ai dati4. Migliora la scalabilità dell’applicazione5. Anche se “è un lavoraccio” paga sul

lungo termine

19

Non serve essere “talebani relazionali” basta ricordarsi …

• Tenete a mente il concetto di normalizzazione

• Non replicare i dati in tabelle• Se infrangete le regole, siate

consapevoli del motivo per cui lo fate e fatelo per una buona ragione (la pigrizia non lo è).

20

Fino a che punto infrangere le regole relazionali? Un caso esemplare

21

Fino a che punto infrangere le regole relazionali? Un caso esemplare

22

Fino a che punto infrangere le regole relazionali? Un caso esemplare

OLTP System Online Transaction Processing 

(Operational System)

OLAP System Online Analytical Processing 

(Data Warehouse)

Source of dataOperational data; OLTPs are the original

source of the data.Consolidation data; OLAP data comes from

the various OLTP Databases

Purpose of dataTo control and run fundamental business

tasksTo help with planning, problem solving, and

decision support

What the dataReveals a snapshot of ongoing business

processesMulti-dimensional views of various kinds of

business activities

Inserts and UpdatesShort and fast inserts and updates

initiated by end usersPeriodic long-running batch jobs refresh the

data

QueriesRelatively standardized and simple queries

Returning relatively few recordsOften complex queries involving

aggregations

Processing Speed Typically very fast

Depends on the amount of data involved; batch data refreshes and complex queries may take many hours; query speed can be

improved by creating indexes

Space RequirementsCan be relatively small if historical data is

archived

Larger due to the existence of aggregation structures and history data; requires more

indexes than OLTP

Database Design Highly normalized with many tablesTypically de-normalized with fewer tables;

use of star and/or snowflake schemas

Backup and Recovery

Backup religiously; operational data is critical to run the business, data loss is likely to entail significant monetary loss

and legal liability

Instead of regular backups, some environments may consider simply reloading

the OLTP data as a recovery method

23

R.D.B.M.S.

Dobbiamo sapere :• Le potenzialità del RDBMS• Come sono organizzati i dati• Come viene gestita la concorrenza

24

Sql Server

25

Oracle

26

MySQL

27

Data Manipulation Language

A DML statement is executed when you:Add new rows to a tableModify existing rows in a tableRemove existing rows from a table

A transaction consists of a collection of DML statements that form a logical unit of work.

28

The INSERT Statement

Add new rows to a table by using the INSERT statement.

Only one row is inserted at a time with this syntax.INSERT INTO table [(column [, column...])]VALUES (value [, value...]);

INSERT INTO table [(column [, column...])]VALUES (value [, value...]);

29

Inserting Special Values

The SYSDATE function records the current date and time.

SQL> INSERT INTO emp (empno, ename, job, 2 mgr, hiredate, sal, comm, 3 deptno) 4 VALUES (7196, 'GREEN', 'SALESMAN', 5 7782, SYSDATE, 2000, NULL, 6 10);1 row created.

30

Inserting Specific Date Values

Add a new employee.

SQL> INSERT INTO emp 2 VALUES (2296,'AROMANO','SALESMAN',7782, 3 TO_DATE('FEB 3, 97', 'MON DD, YY'), 4 1300, NULL, 10);1 row created.

• Verify your addition.• Verify your addition.

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO----- ------- -------- ---- --------- ---- ---- ------ 2296 AROMANO SALESMAN 7782 03-FEB-97 1300 10

31

Inserting Values by Using Substitution Variables

Create an interactive script by using SQL*Plus substitution parameters.

SQL> INSERT INTO dept (deptno, dname, loc) 2 VALUES (&department_id, 3 '&department_name', '&location');

Enter value for department_id: 80Enter value for department_name: EDUCATIONEnter value for location: ATLANTA

1 row created.

32

Updating Rows in a Table

Specific row or rows are modified when you specify the WHERE clause.

All rows in the table are modified if you omit the WHERE clause.

SQL> UPDATE emp 2 SET deptno = 20 3 WHERE empno = 7782;1 row updated.

SQL> UPDATE employee 2 SET deptno = 20;14 rows updated.

SQL> UPDATE employee 2 SET deptno = 20;14 rows updated.

33

Specific rows are deleted when you specify the WHERE clause.

All rows in the table are deleted if you omit the WHERE clause.

Deleting Rows from a Table

SQL> DELETE FROM department 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted.

SQL> DELETE FROM department 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted.

SQL> DELETE FROM department;4 rows deleted.

SQL> DELETE FROM department;4 rows deleted.

34

JOIN

35

INNER JOINS

SELECT EE.NAME, DD.NAME , … FROM EMPLOYEES AS EE INNER JOIN DEPARTMENTS AS DDON (EE.DEP_ID = DD.DEP_ID)WHERE …

SELECT EE.NAME, DD.NAME , … FROM EMPLOYEES AS EE , DEPARTMENTS AS DDWHERE(EE.DEP_ID = DD.ID)AND …

SELECT EE.NAME, DD.NAME , … FROM EMPLOYEES AS EE , DEPARTMENTS AS DDUSING (DEP_ID)WHERE …

36

OUTER JOINS

SELECT EE.NAME, DD.NAME , … FROM EMPLOYEES AS EE LEFT OUTER JOIN DEPARTMENTS AS DDON (EE.DEP_ID = DD.DEP_ID)WHERE …

SELECT EE.NAME, DD.NAME , … FROM EMPLOYEES AS EE RIGHT OUTER JOIN DEPARTMENTS AS DDON (EE.DEP_ID = DD.DEP_ID)WHERE …

SELECT EE.NAME, DD.NAME , … FROM EMPLOYEES AS EE FULL OUTER JOIN DEPARTMENTS AS DDON (EE.DEP_ID = DD.DEP_ID)WHERE …

37

SORTING RESULTS , PSEUDOCOLUMNS

SELECT EE.NAME, EE.SURNAME , ROWID FROM EMPLOYEES AS EE WHERE … ORDER BY EE.SURNAME DESC , EE.NAME

SELECT NAME, SURNAME , ROWNUMFROM EMPLOYEESORDER BY SURNAME

SELECT NAME, SURNAME FROM (

SELECT NAME, SURNAME , ROWNUM FROM EMPLOYEESORDER BY SURNAME) QQ

WHERE QQ.ROWNUM >= 10 AND QQ.ROWNUM <= 20;

38

Operatori insiemistici SQL

SELECT product_id FROM order_itemsUNIONSELECT product_id FROM inventories;

SELECT product_id FROM order_itemsUNION ALLSELECT product_id FROM inventories;

SELECT product_id FROM inventoriesINTERSECTSELECT product_id FROM order_items;

SELECT product_id FROM inventoriesMINUSSELECT product_id FROM order_items;

39

SQL aggregate functions

SELECT DEPARTMENT_ID , COUNT (*)

FROM EMPLOYEES WHERE … GROUP BY DEPARTMENT_ID HAVING COUNT(*) > 6 ;

• SUM• MAX• MIN• AVG• SDEV• VARIANCE• PERCENT_RANK

40

Cosa accade a una istruzione SQL in una istanza

41

Transazioni … ACID

Atomicità

Consistenza

Isolamento

Durability (persistenza)

42

Database Transactions

Consist of one of the following statements:DML statements that make up one consistent change to

the dataOne DDL statementOne DCL statement

43

Database Transactions

Begin when the first executable SQL statement is executed

End with one of the following events:COMMIT or ROLLBACK is issuedDDL or DCL statement executes (automatic commit)User exitsSystem crashes

44

DELETE

Controlling Transactions

Transaction

Savepoint A

ROLLBACK to Savepoint B

DELETE

Savepoint B

COMMIT

INSERTUPDATE

ROLLBACK to Savepoint A

INSERTUPDATEINSERT

ROLLBACK

INSERT

45

An automatic commit occurs under the following circumstances:

DDL statement is issuedDCL statement is issuedNormal exit from SQL*Plus, without explicitly issuing

COMMIT or ROLLBACKAn automatic rollback occurs under an abnormal

termination of SQL*Plus or a system failure.

Implicit Transaction Processing

46

State of the Data Before COMMIT or ROLLBACK

The previous state of the data can be recovered.The current user can review the results of the DML operations

by using the SELECT statement.Other users cannot view the results of the DML statements by

the current user.The affected rows are locked; other users cannot change the

data within the affected rows.

47

State of the Data After COMMIT

Data changes are made permanent in the database.The previous state of the data is permanently lost.All users can view the results.Locks on the affected rows are released; those rows are

available for other users to manipulate.All savepoints are erased.

48

Committing Data

SQL> UPDATE emp 2 SET deptno = 10 3 WHERE empno = 7782;1 row updated.

SQL> UPDATE emp 2 SET deptno = 10 3 WHERE empno = 7782;1 row updated.

Make the changes.

• Commit the changes.• Commit the changes.

SQL> COMMIT;Commit complete.

49

State of the Data After ROLLBACK

Discard all pending changes by using the ROLLBACK statement.Data changes are undone.Previous state of the data is restored.Locks on the affected rows are released.

SQL> DELETE FROM employee;14 rows deleted.SQL> ROLLBACK;Rollback complete.

50

Rolling Back Changes to a Marker

Create a marker in a current transaction by using the SAVEPOINT statement.

Roll back to that marker by using the ROLLBACK TO SAVEPOINT statement.

SQL> UPDATE...SQL> SAVEPOINT update_done;Savepoint created.SQL> INSERT...SQL> ROLLBACK TO update_done;Rollback complete.

51

Statement-Level Rollback

If a single DML statement fails during execution, only that statement is rolled back.

The Oracle Server implements an implicit savepoint.All other changes are retained.The user should terminate transactions explicitly by

executing a COMMIT or ROLLBACK statement.

52

Read Consistency

Read consistency guarantees a consistent view of the data at all times.

Changes made by one user do not conflict with changes made by another user.

Read consistency ensures that on the same data:Readers do not wait for writersWriters do not wait for readers

53

Locking

Oracle locks:Prevent destructive interaction between concurrent

transactionsRequire no user actionAutomatically use the lowest level of restrictivenessAre held for the duration of the transactionHave two basic modes:

ExclusiveShare

54

Isolamento,consistenza e concorrenza

Isolation Level Dirty Read Nonrepeatable Read

Phantom Read

Read uncommitted

Possible Possible Possible

Read committed

Not possible Possible Possible

Repeatable read

Not possible Not possible Possible

Serializable Not possible Not possible Not possible

55

Read committed : statement consistency on Oracle

Transaction 2

SQL> update accounts set balance = 100 where account = 1;1 row updated. SQL> update accounts set balance = 500 where account = 30;1 row updated. SQL>

SQL> Commit; Commit complete.

Transaction 1SQL> select balance from accounts where account = 1; BALANCE ---------- 500 SQL> select balance from accounts where account = 2; BALANCE ---------- 200

SQL> select balance from accounts where account = 30; BALANCE ---------- 100

SQL> select balance from accounts where account = 30; BALANCE ---------- 500SQL>

56

Read committed : statement consistency and locks on SQL

Transaction 2

begin transaction update accounts set balance = 100 where account = 1; update accounts set balance = 500 where account = 30; (1 row(s) affected) (1 row(s) affected)

Commit transaction Commit complete.

Transaction 1begin transaction select * from accounts where account = 1;select * from accounts where account = 2; account balance ------------------ ---------------- 1 500 (1 row(s) affected)

account balance ------------------ ---------------- 2 200 (1 row(s) affected)

select * from accounts where account = 30;

account balance ------------------ ---------------- 30 500 (1 row(s) affected)

57

Read committed : exclusive locks on Oracle

Transaction 2

SQL> update accounts set balance = 100 where account = 1;1 row updated. SQL> update accounts set balance = 700 where account = 30;

1 row updated. SQL>

Transaction 1SQL> update accounts set balance = 500 where account = 30;1 row updated. SQL> select balance from accounts where account = 2; BALANCE ---------- 200

SQL> Commit; Commit complete.SQL> select balance from accounts where account = 30; BALANCE ---------- 500SQL>

58

Read committed : exclusive locks on Oracle

Transaction 2

SQL> update accounts set balance = 100 where account = 1;1 row updated. SQL> update accounts set balance = 700 where account = 30;

1 row updated. SQL>

Transaction 1SQL> update accounts set balance = 500 where account = 30;1 row updated. SQL> select balance from accounts where account = 2; BALANCE ---------- 200

SQL> Commit; Commit complete.SQL> select balance from accounts where account = 30; BALANCE ---------- 500SQL>

59

Deadlocks

60

Come fa Oracle ?

61

Gestione sessioni serializable

62

63

Architettura Oracle

64

1 - Oracle Server Architecture Overview

User Process

Server Process

Other Files...Parameter,Password,Archive Logs

PGA

Oracle Database Data files

Redo Log Files

Control Files

DatabaseBufferCache

Shared Pool

RedoLogBuffer

SGA (System Global Area)

DBWR LGWR SMON PMON CKPT RECO

ARCH LCKn Pnnn Dnnn SNPn

Oracle Instance

Java Pool(optional)

Large Pool(optional)

65

Client Oracle

66

Connessione applicazioni

67

Tablespace

68

Oggetti Oracle

• Tablespace• Tabelle (tipi di tabelle) • Constraints ( vincoli )• Sequences• Triggers• Viste• Viste Materializzate• Indici (vari tipi di indice)• Funzioni• Procedure• Packages• DB link• Directories• Tipi• Autorizzazioni : Grants, ruoli e accenni di VPD• Sinonimi• …

Paolo Castelletti
P 553 EOBD

69

Tipi di base Oracle : la regola d’oro

NUMBER(P,S)VARCHAR(S)

DATE

27 15,325,883

32E12

15/3/2013 27/07/1972

31/12/2003 19:30

TestoPaolo

Alfa34

LOB

70

Il Data Dictionary

71

Tabelle

• CREATE TABLE• DDL • ALTER TABLE• DML • INDICI

72

DML

• SELECT …• INSERT …• UPDATE …• DELETE …• MERGE …

INSERT INTO HR.TESTTABLE (AAA,BBB) VALUES (100, 'PROVA');

UPDATE HR.TESTTABLE SET BBB = 'PROVA‘WHERE AAA = 100;

DELETE HR.TESTTABLE WHERE AAA = 100;

73

Bulk operations

• Merge ( DML ) • Insert into select ( DML ) • Create table as ( DDL ) • Truncate ( DDL )

MERGE INTO dest_tab a USING src_tab b ON (a.object_id = b.object_id) WHEN MATCHED THEN UPDATE SET a.status = b.status WHEN NOT MATCHED THEN INSERT (object_id, status) VALUES (b.object_id, b.status);

INSERT INTO dest_tab a (object_id,status ) SELECT (object_id,status ) FROM src_tab b WHERE object_id > 500;

74

High Water Mark

75

Create table

CREATE TABLE RICHIESTA( ID_RICHIESTA NUMBER(7), DATA_RICHIESTA DATE, RICHIEDENTE VARCHAR2(300 BYTE), NOMINATIVO VARCHAR2(300 BYTE), PERIODO_DA DATE, PERIODO_A DATE, SOC_APPARTENENZA VARCHAR2(300 BYTE), LUOGO VARCHAR2(300 BYTE), MOTIVO VARCHAR2(300 BYTE), NR_RDA VARCHAR2(50 BYTE))TABLESPACE ARCHIMEDE

76

Indici

77

Come è fatto un indice ?

78

Creazione indici

Cause problemi lentezza query0

10

20

30

40

50

60

70

80

Bad IndexingStale statsBad SQLNeed parti-tioning/PXOthers

CREATE INDEX emp_ename ON emp(ename,esurname) TABLESPACE users;

79

Bitmap Indexes

80

Viste

CREATE VIEW staff ASSELECT employee_id,last_name,job_id,manager_id,department_idFROM employeesWHERE status=1;

81

Inline views

SELECT * FROM ( SELECT … ) a INNER JOIN ( SELECT … ) b ON (a.object_id = b.object_id)WHERE b.fa IN ( SELECT fa FROM …)AND …

WITHsum_sales AS ( select /*+ materialize */ sum(quantity) all_sales from stores ),number_stores AS ( select /*+ materialize */ count(*) nbr_stores from stores ),sales_by_store AS ( select /*+ materialize */ store_name, sum(quantity) store_sales from store natural join sales )SELECT store_nameFROM store, sum_sales, number_stores, sales_by_storewhere store_sales > (all_sales / nbr_stores);

82

Viste Materializzate

83

Constraints

TIPI DI VINCOLI• Primary Key• Unique• Forign Key• Not Null• Check

84

Contraints ( how to create them )

ALTER TABLE DEPARTMENTS ADD ( CONSTRAINT DEPT_ID_PK PRIMARY KEY (DEPARTMENT_ID));

ALTER TABLE EMPLOYEES ADD ( CONSTRAINT EMP_DEPT_FK FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENTS (DEPARTMENT_ID));

ALTER TABLE EMPLOYEES ADD ( CONSTRAINT EMP_EMP_ID_PK PRIMARY KEY (EMPLOYEE_ID));

ALTER TABLE EMPLOYEES ADD ( CONSTRAINT EMP_EMAIL_UK UNIQUE (EMAIL));

ALTER TABLE EMPLOYEES ADD ( HIRE_DATE CONSTRAINT EMP_HIRE_DATE_NN NOT NULL);

ALTER TABLE EMPLOYEES ADD ( CONSTRAINT EMP_SALARY_MIN CHECK (salary > 0));

85

Distributed DB

86

Programmability

• PL/SQL , T-SQL • Triggers• Functions • Procedures

87

Bakup

88

Posso fermare il DB ?