Semplice introduzione ai DBMS e al linguaggio SQL (con attenzione verso pgsql). Ispirato da Appunti di Informatica Libera - http://informaticalibera.net/
Introduzione al linguaggio SQL con riferimento allo standard Mysql.
Creazione e modifica tabelle, inserimento istanze, funzioni di manipolazione, funzioni di aggregazione.
Ready for the fame and fortune you deserve? Set yourself up for success and create a marketing plan to share your online portfolio. Gain new clients and grow your career as a creative professional
Semplice introduzione ai DBMS e al linguaggio SQL (con attenzione verso pgsql). Ispirato da Appunti di Informatica Libera - http://informaticalibera.net/
Introduzione al linguaggio SQL con riferimento allo standard Mysql.
Creazione e modifica tabelle, inserimento istanze, funzioni di manipolazione, funzioni di aggregazione.
Ready for the fame and fortune you deserve? Set yourself up for success and create a marketing plan to share your online portfolio. Gain new clients and grow your career as a creative professional
This document summarizes key concepts from Chapter 55 of Biology, Seventh Edition regarding conservation biology and restoration ecology. It discusses how conservation biology aims to conserve biodiversity by integrating various fields of study. It also explains how restoration ecology applies ecological principles to return degraded ecosystems to more natural states. The document then covers threats to biodiversity like habitat destruction and introduced species, as well as approaches to conserving populations, landscapes, and ecosystems.
The document discusses PL/SQL triggers. It provides an example of creating a trigger on a books table to log changes to a books_hist table. The trigger checks for insert, update, or delete events on the books table and inserts corresponding records into the books_hist table. It also discusses trigger events, the ability to apply triggers at the row level or with a condition, and data dictionary views for viewing trigger information.
This document summarizes key concepts from Chapter 55 of Biology, Seventh Edition regarding conservation biology and restoration ecology. It discusses how conservation biology aims to conserve biodiversity by integrating various fields of study. It also explains how restoration ecology applies ecological principles to return degraded ecosystems to more natural states. The document then covers threats to biodiversity like habitat destruction and introduced species, as well as approaches to conserving populations, landscapes, and ecosystems.
The document discusses PL/SQL triggers. It provides an example of creating a trigger on a books table to log changes to a books_hist table. The trigger checks for insert, update, or delete events on the books table and inserts corresponding records into the books_hist table. It also discusses trigger events, the ability to apply triggers at the row level or with a condition, and data dictionary views for viewing trigger information.
The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. In the example code, abstract factories like ContinentFactory define interfaces for creating herbivores and carnivores, while concrete factories like AfricaFactory and AmericaFactory implement the interfaces and produce animal objects appropriate for their continent. A client like AnimalWorld uses the factories to populate ecosystems with different sets of animals.
Procedures and functions are subprograms in PL/SQL. Functions return a value while procedures do not return a value. Functions are called and return a value, like sales_for_1995 := tot_sales (1504, 'C'). Procedures are called by name and can pass parameters, like apply_discount( new_company_id, 0.15 ). Parameters can be passed into functions and procedures as IN, OUT or IN OUT parameters. An example shows a function that returns the number of days in a month based on the month number and year passed in as parameters.
1. 22/07/04 Ing. Ronchi Sergio 1
SQL : Ricerca di dati univoci
Questa query visualizza solo i dati distinti
SELECT DISTINCT COGNOME,NOME, DATA
FROM ANAGRAFICA;
Questa può essere usata per eliminare i duplicati:
INSERT INTO NewAnagrafica (................)
SELECT DISTINCT ........... FROM ANAGRAFICA;
ovviamente sarebbe stato meglio creare un indice univoco:
CREATE UNIQUE INDEX index_unico ON
ANAGRAFICA (COGNOME,NOME,DATA);
2. 22/07/04 Ing. Ronchi Sergio 2
Query di Selezione
La clausola where estrae solo i record che rispettano la condizione:
SELECT COGNOME, NOME
FROM ANAGRAFICA
WHERE PROVINCIA='MI'
Ovviamente sono possibili tutti i tipi di confronti logici, i vari operatori logici
AND OR NOT e l'uso di funzioni o operatori aritmetici:
SELECT COGNOME, NOME, ALTEZZA
FROM ANAGRAFICA
WHERE PROVINCIA!='MI' AND ALTEZZA>1.70
AND YEAR(DATA)>1980 AND YEAR(DATA)<1990;
3. 22/07/04 Ing. Ronchi Sergio 3
Query di Selezione
Uso di Between...and
SELECT COGNOME, NOME FROM ANAGRAFICA
WHERE DATA BETWEEN '1980-01-01' AND '1990-12-31'
Ordinamento del risultato di una query:
SELECT COGNOME, NOME, ALTEZZA FROM ANAGRAFICA
WHERE PROVINCIA!='MI' ORDER BY COGNOME, NOME;
Ovviamente è possibile invertire l'ordine utilizzando DESC:
SELECT COGNOME, NOME, ALTEZZA FROM ANAGRAFICA
ORDER BY ALTEZZA DESC;
Nell'order by è possibile utilizzare anche gli ALIAS:
SELECT COGNOME, PESO-ALTEZZA*100 SOVRAPPESO
FROM ANAGRAFICA
ORDER BY SOVRAPPESO;
4. 22/07/04 Ing. Ronchi Sergio 4
Query di Raggruppamento
Permettere di ottenere totalizzazioni su tutta la tabella o
su dati congruenti e raggruppati.
Se si vuole calcolare un valore senza raggruppare i dati:
SELECT AVG(ALTEZZA) FROM ANAGRAFICA; MEDIA
Altre funzioni di raggruppamento sono
SUM() somma
COUNT() conteggio
MIN() minimo
MAX() massimo
STDDEV() deviazione standard (anche STD())
è possibile ovviamente utilizzare la clausola WHERE
5. 22/07/04 Ing. Ronchi Sergio 5
Query di Raggruppamento
Se si vuole calcolare un valore per raggruppamenti:
SELECT PROVINCIA, AVG(ALTEZZA)
FROM ANAGRAFICA GROUP BY PROVINCIA;
SELECT PROVINCIA, SESSO, AVG(ALTEZZA)
FROM ANAGRAFICA GROUP BY PROVINCIA,SESSO;
Uso di WHERE – GROUP BY – ORDER BY:
SELECT PROVINCIA, SESSO, AVG(ALTEZZA) MEDIA
FROM ANAGRAFICA WHERE PESO>50
GROUP BY PROVINCIA, SESSO
ORDER BY MEDIA;
da notare l'uso dell'alias nel ORDER BY
SELECT PROVINCIA, SESSO, AVG(ALTEZZA) MEDIA
FROM ANAGRAFICA
GROUP BY PROVINCIA, SESSO
HAVING PROVINCIA!=‘EE’
ORDER BY MEDIA;
6. 22/07/04 Ing. Ronchi Sergio 6
Query con Join tra tabelle
JOIN NATURALE
SELECT COGNOME, ANAGRAFICA.NOME, PROVINCE.NOME
FROM ANAGRAFICA, PROVINCE
WHERE ANAGRAFICA.PROVINCIA=PROVINCE.SIGLA
vengono visualizzati solo i record che hanno il codice (sigla)
corrispondente.
In alternativa si può usare la sintassi:
SELECT COGNOME, ANAGRAFICA.NOME, PROVINCE.NOME
FROM ANAGRAFICA INNER JOIN PROVINCE
ON ANAGRAFICA.PROVINCIA=PROVINCE.SIGLA
7. 22/07/04 Ing. Ronchi Sergio 7
Query con Join tra tabelle
OUTER JOIN
- LEFT JOIN
SELECT COGNOME, PROVINCIA.NOME
FROM ANAGRAFICA LEFT JOIN PROVINCE
ON ANAGRAFICA.PROVINCIA=PROVINCE.SIGLA
mostra tutti i record della tabella di sinistra e solo quelli corrispondenti
della tabella di destra.
- RIGHT JOIN
SELECT COGNOME, PROVINCIA.NOME
FROM ANAGRAFICA RIGHT JOIN PROVINCE
ON ANAGRAFICA.PROVINCIA=PROVINCE.SIGLA
mostra tutti i record della tabella di destra
8. 22/07/04 Ing. Ronchi Sergio 8
Query con Join tra tabelle
AUTO JOIN
in alcuni casi è necessario eseguire una join su campi della stessa tabella:
Ad esempio se volessi trovare tutti coloro che hanno nome e cognome uguali,
ma codici diversi.
SELECT A1.COGNOME, A1.NOME, A1.DATA, A2.DATA
FROM ANAGRAFICA A1 INNER JOIN ANAGRAFICA A2
ON A1.COGNOME=A2.COGNOME AND
A1.NOME=A2.NOME
WHERE A1.CODICE>A2.CODICE;
9. 22/07/04 Ing. Ronchi Sergio 9
Query con Join tra tabelle
AUTO JOIN
Voglio visualizzare cognome e nome di una persona e cognome e nome della madre.
Tutte le persone si trovano nella stessa tabella
SELECT CONCAT(FIGLIO.COGNOME,’, ‘,FIGLIO.NOME) PERSONA,
CONCAT(MADRE.COGNOME,’, ‘, MADRE.NOME) MAMMA
FROM ANAGRAFICA FIGLIO LEFT JOIN ANAGRAFICA MADRE
ON FIGLIO.GENITORE=MADRE.CODICE;
10. 22/07/04 Ing. Ronchi Sergio 10
Query con Join tra tabelle
Tabelle relazionate più volte con la stessa tabella. Ad esempio la relazione tra I Voli
(origine e destinazione) e gli Aeroporti.
SELECT CODVOLO,ORIGINE.NOME, DESTINAZIONE.NOME, DATA
FROM VOLI, AEROPORTI ORIGINE, AEROPORTI DESTINAZIONE
WHERE VOLI.ORIGINE=ORIGINE.CODICE AND
VOLI.DEST=DESTINAZIONE.CODICE;
11. 22/07/04 Ing. Ronchi Sergio 11
Subqueries
Non sono supportate dalla versione 3.23 di MySQL.
Permettono diverse operazioni che altrimenti dovrebbero essere realizzate con
più di una query.
Estrae le persone con altezza superiore alla media
SELECT COGNOME, NOME, ALTEZZA
FROM ANAGRAFICA
WHERE ALTEZZA> (SELECT AVG(ALTEZZA) FROM ANAGRAFICA);
Estrae le persone della lombardia
SELECT COGNOME, NOME, ALTEZZA
FROM ANAGRAFICA WHERE PROVINCIA IN (SELECT SIGLA FROM PROVINCE
WHERE REGIONE='Lombardia');
La stessa fatta con una JOIN
SELECT COGNOME, NOME, ALTEZZA
FROM ANAGRAFICA, PROVINCE WHERE
ANAGRAFICA.PROVINCIA=PROVINCE.SIGLA AND
PROVINCE.REGIONE='Lombardia';
12. 22/07/04 Ing. Ronchi Sergio 12
Subqueries
In alcuni casi sI usano le subqueries anche nel SELECT
Ad esempio se voglio estrarre l'altezza massima
e la persona che ha quell'altezza per ogni provincia
SELECT PROVINCE.NOME, PROVINCE.SIGLA, MAX(ALTEZZA) MASSIMO,
(SELECT NOME FROM ANAGRAFICA
WHERE PROVINCIA=SIGLA AND
ALTEZZA=MASSIMO)
FROM PROVINCE, ANAGRAFICA
WHERE PROVINCIA.SIGLA=ANAGRAFICA.PROVINCIA
GROUP BY PROVINCE.NOME;
13. 22/07/04 Ing. Ronchi Sergio 13
UNION
Permettono di ottenere una vista in cui record provenienti da più
tabelle vengono accodati, purché dello stesso tipo.
SELECT CODICE, NOME FROM LIBRI
UNION
SELECT CODICE, NOME FROM SOFTWARE;