Post on 25-Feb-2016
description
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 123451 Joe ABC 123 2234 123451 Joe ABC 123 3211 123452 Jane XYZ 456 3421 144543 Chris PDQ 789 2341 144233 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 normalePeople
Id Name Company Address Zip1 Joe ABC 123 123452 Jane XYZ 456 144543 Chris PDQ 789 14423
PhoneNumbersPhoneID Id Phone1 1 55322 1 22343 1 32114 2 34215 3 23416 3 6655
14
Terza forma normale
Elimina i campi che non dipendono dalle PK
PeopleId Name AddressID1 Joe 12 Jane 23 Chris 3
AddressAddressID Compan
yAddress Zip
1 ABC 123 123452 XYZ 456 144543 PDQ 789 14423
PhoneNumbersPhoneID Id Phone1 1 55322 1 22343 1 32114 2 34215 3 23416 3 6655
15
Tipi di relazioni
Uno a UnoOne row of a table matches exactly to another
One person, one id number, one addressUno a Molti
One row of a table matches many of anotherOne person, many phone numbers
Molti a MoltiOne row may match many of another or many rows match
one row of another
16
Quarta forma normaleIn una relazione molti-a-molti,
entità indipendenti non possono essere memorizzate nella stessa tabella.
PhoneNumbers
PhoneID Phone1 55322 22343 32114 34215 23416 6655
PhoneRelationsPhoneRelID Id PhoneID1 1 12 1 2
3 1 34 2 45 3 56 3 6
PeopleId Name AddressID1 Joe 12 Jane 23 Chris 3
AddressAddressID Company Address Zip1 ABC 123 123452 XYZ 456 144543 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 data Operational data; OLTPs are the original source of the data.
Consolidation data; OLAP data comes from the various OLTP Databases
Purpose of data To control and run fundamental business tasks
To help with planning, problem solving, and decision support
What the data Reveals a snapshot of ongoing business processes
Multi-dimensional views of various kinds of business activities
Inserts and Updates Short and fast inserts and updates initiated by end users
Periodic long-running batch jobs refresh the data
Queries Relatively standardized and simple queries Returning relatively few records
Often complex queries involving aggregations
Processing Speed Typically very fastDepends 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 Requirements Can be relatively small if historical data is archived
Larger due to the existence of aggregation structures and history data; requires more
indexes than OLTPDatabase Design Highly normalized with many tables Typically 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...]);
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.
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.
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;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àConsistenzaIsolamento
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.
Make the changes.
• Commit the changes.
SQL> COMMIT;Commit complete.
49
State of the Data After ROLLBACKDiscard 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 PossibleRead committed Not possible Possible PossibleRepeatable read Not possible Not possible PossibleSerializable Not possible Not possible Not possible
55
Read committed : statement consistency on OracleTransaction 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 SQLTransaction 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 OracleTransaction 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 OracleTransaction 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• …
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 tableCREATE 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
1020304050607080
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 viewsSELECT * 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 ?