Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 L'immaginazione è più importante della conoscenza. La conoscenza è limitata, l'immaginazione abbraccia il mondo, stimolando il progresso, facendo nascere.

Similar presentations


Presentation on theme: "1 L'immaginazione è più importante della conoscenza. La conoscenza è limitata, l'immaginazione abbraccia il mondo, stimolando il progresso, facendo nascere."— Presentation transcript:

1 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 2 Le BASI DI DATI : le fondamenta dei moderni sistemi Software Di cosa parleremo

3 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 paolo.castelletti@aspasiel.it – paolocastle@gmail.com paolocastle@gmail.com – paolocastle.wordpress.com Mi presento

4 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 5 Edgar Frank "Ted" Codd Lawrence Joseph "Larry" Ellison Personaggi Notevoli

6 6 Le architetture multitier

7 7 La diffusione dei RDBMS oggi

8 8 Chi lavora con il RDBMS DB Developer DB Administrator

9 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 10 Mai sentito parlare di “Forme Normali?”

11 11 DB non normalizzatoContacts NameCompanyAddressPhone1Phone2Phone3ZipCode JoeABC12355322234321112345 JaneXYZ456342114454 ChrisPDQ7892341665514423

12 12 Prima forma normaleContacts IdNameCompanyAddressPhoneZipCode 1JoeABC123553212345 1JoeABC123223412345 1JoeABC123321112345 2JaneXYZ456342114454 3ChrisPDQ789234114423 3ChrisPDQ789665514423 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 13 Seconda forma normalePeople IdNameCompanyAddressZip 1JoeABC12312345 2JaneXYZ45614454 3ChrisPDQ78914423 PhoneNumbersPhoneIDIdPhone 115532 212234 313211 423421 532341 636655

14 14 Terza forma normale Elimina i campi che non dipendono dalle PK People IdNameAddressID 1Joe1 2Jane2 3Chris3 AddressAddressIDCompanyAddressZip 1ABC12312345 2XYZ45614454 3PDQ78914423 PhoneNumbersPhoneIDIdPhone 115532 212234 313211 423421 532341 636655

15 15 Tipi di relazioni Uno a Uno One row of a table matches exactly to another One person, one id number, one address Uno a Molti One row of a table matches many of another One person, many phone numbers Molti a Molti One row may match many of another or many rows match one row of another

16 16 Quarta forma normale In una relazione molti-a-molti, entità indipendenti non possono essere memorizzate nella stessa tabella. PhoneNumbers PhoneIDPhone 15532 22234 33211 43421 52341 66655 PhoneRelationsPhoneRelIDIdPhoneID 111 212 313 424 535 636 PeopleIdNameAddressID 1Joe1 2Jane2 3Chris3 AddressAddressIDCompanyAddressZip 1ABC12312345 2XYZ45614454 3PDQ78914423

17 17 Fifth Normal Form The “very esoteric” one that is probably not required to get the most out of your database. “The original table must be reconstructed from the tables into which it has been broken down.” The rule ensures that you have not created any extraneous columns and all the tables are only as large as they need to be.

18 18 Quindi … perché normalizzare una base dati? 1.Aumenta l’integrità dei dati 2.Riduce la ridondanza 3.Migliora l’efficienza nell’accesso ai dati 4.Migliora la scalabilità dell’applicazione 5.Anche se “è un lavoraccio” paga sul lungo termine

19 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 20 Fino a che punto infrangere le regole relazionali? Un caso esemplare

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

22 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 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 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 OLTP Database 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 23 R.D.B.M.S. Dobbiamo sapere : Le potenzialità del RDBMS Come sono organizzati i dati Come viene gestita la concorrenza

24 24 Sql Server

25 25 Oracle

26 26 MySQL

27 27 Data Manipulation Language A DML statement is executed when you: Add new rows to a table Modify existing rows in a table Remove existing rows from a table A transaction consists of a collection of DML statements that form a logical unit of work.

28 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 INTOtable [(column [, column...])] VALUES(value [, value...]); INSERT INTOtable [(column [, column...])] VALUES(value [, value...]);

29 29 Inserting Special Values The SYSDATE function records the current date and time. SQL> INSERT INTOemp (empno, ename, job, 2mgr, hiredate, sal, comm, 3deptno) 4 VALUES(7196, 'GREEN', 'SALESMAN', 57782, SYSDATE, 2000, NULL, 610); 1 row created.

30 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 31 Inserting Values by Using Substitution Variables Create an interactive script by using SQL*Plus substitution parameters. SQL> INSERT INTOdept (deptno, dname, loc) 2 VALUES (&department_id, 3 '&department_name', '&location'); 80 Enter value for department_id: 80 EDUCATION Enter value for department_name: EDUCATION ATLANTA Enter value for location: ATLANTA 1 row created.

32 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 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 FROMdepartment 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted. SQL> DELETE FROMdepartment 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted. SQL> DELETE FROMdepartment; 4 rows deleted. SQL> DELETE FROMdepartment; 4 rows deleted.

34 34 JOIN

35 35 INNER JOINS SELECT EE.NAME, DD.NAME, … FROM EMPLOYEES AS EE INNER JOIN DEPARTMENTS AS DD ON (EE.DEP_ID = DD.DEP_ID) WHERE … SELECT EE.NAME, DD.NAME, … FROM EMPLOYEES AS EE, DEPARTMENTS AS DD WHERE(EE.DEP_ID = DD.ID) AND … SELECT EE.NAME, DD.NAME, … FROM EMPLOYEES AS EE, DEPARTMENTS AS DD USING (DEP_ID) WHERE …

36 36 OUTER JOINS SELECT EE.NAME, DD.NAME, … FROM EMPLOYEES AS EE LEFT OUTER JOIN DEPARTMENTS AS DD ON (EE.DEP_ID = DD.DEP_ID) WHERE … SELECT EE.NAME, DD.NAME, … FROM EMPLOYEES AS EE RIGHT OUTER JOIN DEPARTMENTS AS DD ON (EE.DEP_ID = DD.DEP_ID) WHERE … SELECT EE.NAME, DD.NAME, … FROM EMPLOYEES AS EE FULL OUTER JOIN DEPARTMENTS AS DD ON (EE.DEP_ID = DD.DEP_ID) WHERE …

37 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, ROWNUM FROM EMPLOYEES ORDER BY SURNAME SELECT NAME, SURNAME FROM ( SELECT NAME, SURNAME, ROWNUM FROM EMPLOYEES ORDER BY SURNAME) QQ WHERE QQ.ROWNUM >= 10 AND QQ.ROWNUM <= 20;

38 38 Operatori insiemistici SQL SELECT product_id FROM order_items UNION SELECT product_id FROM inventories; SELECT product_id FROM order_items UNION ALL SELECT product_id FROM inventories; SELECT product_id FROM inventories INTERSECT SELECT product_id FROM order_items; SELECT product_id FROM inventories MINUS SELECT product_id FROM order_items;

39 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 40 Cosa accade a una istruzione SQL in una istanza

41 41 Transazioni … ACID

42 42 Database Transactions Consist of one of the following statements: DML statements that make up one consistent change to the data One DDL statement One DCL statement

43 43 Database Transactions Begin when the first executable SQL statement is executed End with one of the following events: COMMIT or ROLLBACK is issued DDL or DCL statement executes (automatic commit) User exits System crashes

44 44 DELETE Controlling Transactions Transaction Savepoint A ROLLBACK to Savepoint B DELETE Savepoint B COMMIT INSERT UPDATE ROLLBACK to Savepoint A INSERTUPDATE INSERT ROLLBACK INSERT

45 45 An automatic commit occurs under the following circumstances: DDL statement is issued DCL statement is issued Normal exit from SQL*Plus, without explicitly issuing COMMIT or ROLLBACK An automatic rollback occurs under an abnormal termination of SQL*Plus or a system failure. Implicit Transaction Processing

46 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 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 48 Committing Data SQL> UPDATEemp 2 SET deptno = 10 3 WHEREempno = 7782; 1 row updated. SQL> UPDATEemp 2 SET deptno = 10 3 WHEREempno = 7782; 1 row updated. Make the changes. Commit the changes. SQL> COMMIT; Commit complete.

49 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 FROMemployee; 14 rows deleted. SQL> ROLLBACK; Rollback complete.

50 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 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 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 writers Writers do not wait for readers

53 53 Locking Oracle locks: Prevent destructive interaction between concurrent transactions Require no user action Automatically use the lowest level of restrictiveness Are held for the duration of the transaction Have two basic modes: Exclusive Share

54 54 Isolamento,consistenza e concorrenza Isolation LevelDirty ReadNonrepeatable Read Phantom Read Read uncommitted Possible Read committedNot possiblePossible Repeatable readNot possible Possible SerializableNot possible

55 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 1 SQL> 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 ---------- 500 SQL>

56 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) Commit transaction Commit complete. Transaction 1 begin 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 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 1 SQL> 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 ---------- 500 SQL>

58 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 1 SQL> 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 ---------- 500 SQL>

59 59 Deadlocks

60 60 Come fa Oracle ?

61 61 Gestione sessioni serializable

62 62

63 63 Architettura Oracle

64 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 Database Buffer Cache Shared Pool Redo Log Buffer SGA (System Global Area) DBWRLGWRSMONPMONCKPTRECO ARCHLCKnPnnnDnnnSNPn Oracle Instance Java Pool (optional) Large Pool (optional)

65 65 Client Oracle

66 66 Connessione applicazioni

67 67 Tablespace

68 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 69 Tipi di base Oracle : la regola d’oro 2715,3 25,883 32E12 15/3/2013 27/07/1972 31/12/2003 19:30 Testo Paolo Alfa34

70 70 Il Data Dictionary

71 71 Tabelle CREATE TABLE DDL ALTER TABLE DML INDICI

72 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 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 74 High Water Mark

75 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 76 Indici

77 77 Come è fatto un indice ?

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

79 79 Bitmap Indexes

80 80 Viste CREATE VIEW staff AS SELECT employee_id,last_name,job_id,manager_id,department_id FROM employees WHERE status=1;

81 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 … WITH sum_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_name FROM store, sum_sales, number_stores, sales_by_store where store_sales > (all_sales / nbr_stores);

82 82 Viste Materializzate

83 83 Constraints TIPI DI VINCOLI Primary Key Unique Forign Key Not Null Check TIPI DI VINCOLI Primary Key Unique Forign Key Not Null Check

84 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 85 Distributed DB

86 86 Programmability PL/SQL, T-SQL Triggers Functions Procedures

87 87 Bakup

88 88 Posso fermare il DB ?

89 89 Hot backup policy


Download ppt "1 L'immaginazione è più importante della conoscenza. La conoscenza è limitata, l'immaginazione abbraccia il mondo, stimolando il progresso, facendo nascere."

Similar presentations


Ads by Google