Importante
La traduzione è uno sforzo comunitario you can join. Questa pagina è attualmente tradotta al 71.15%.
15.3. Lezione: Aggiungere Dati al Modello
Gli esempi che hai creato ora dovranno essere popolati con i dati necessari.
Obiettivo di questa lezione: Imparare a inserire nuovi dati nel database di esempio.
15.3.1. Inserire dati
How do you add data to a table? The sql INSERT statement provides the
functionality for this:
insert into streets (name) values ('High street');
Alcune cose da segnalare:
After the table name (
streets), you list the column names that you will be populating (in this case only thenamecolumn).After the
valueskeyword, place the list of field values.Strings should be quoted using single quotes.
Note that we did not insert a value for the
idcolumn; this is because it is a sequence and will be auto-generated.If you do manually set the
id, you may cause serious problems with the integrity of your database.
You should see INSERT 0 1 if it is successful.
Puoi vedere il risultato della tua azione di inserimento selezionando tutti i dati nella tabella:
select * from streets;
Risultato:
select * from streets;
id | name
----+-------------
1 | High street
(1 row)
Prova Tu: ★☆☆
Use the INSERT command to add a new street to the streets table.
Soluzione
Il comando SQL che dovresti usare assomiglia a questo (puoi sostituire il nome della strada con un nome di tua scelta):
insert into streets (name) values ('Low Road');
15.3.2. Aggiunta di dati rispettando i vincoli
15.3.3. Prova Tu: ★★☆
Try to add a person object to the people table with the following details:
Name: Joe Smith
House Number: 55
Street: Main Street
Phone: 072 882 33 21
Nota
Ricorda che in questo esempio abbiamo definito i numeri di telefono come stringhe e non come numeri interi.
At this point, you should have an error report if you try to do this without
first creating a record for Main Street in the streets table.
Dovresti aver notato che:
Non puoi aggiungere una strada usando il nome
You can’t add a street using a street
idbefore first creating the street record on thestreetstable
Ricorda che le due tabelle sono collegate tramite una coppia di chiavi primaria/esterna. Ciò significa che nessuna persona può essere inserita senza che vi sia anche un record di strada corrispondente.
Usa queste informazioni per aggiungere una nuova persona al database.
Soluzione
Ecco l’istruzione SQL corretta:
insert into streets (name) values('Main Road');
insert into people (name,house_no, street_id, phone_no)
values ('Joe Smith',55,2,'072 882 33 21');
Se guardi di nuovo la tabella delle strade (usando un’istruzione select come prima), vedrai che il id per la voce Main Road è 2.
Ecco perché potremmo semplicemente inserire il numero 2 sopra. Anche se non vediamo Main Road scritto completamente nella voce di cui sopra, il database sarà in grado di associarlo al valore street_id di 2.
Se hai già aggiunto un nuovo oggetto strada, potresti scoprire che la nuova Main Road ha un id di 3 e non 2.
15.3.4. Seleziona dati
Hai già visto la sintassi per la selezione dei record. Dai un’occhiata ad altri esempi:
select name from streets;
select * from streets;
select * from streets where name='Main Road';
Nelle sessioni successive approfondirai come selezionare e filtrare i dati.
15.3.5. Agggiorna dati
Come fare per cambiare alcuni dati esistenti? per esempio cambiare il nome di una strada:
update streets set name='New Main Road' where name='Main Road';
Be very careful using such update statements - if more than one record matches
your WHERE clause, they will all be updated!
Una soluzione migliore è utilizzare la chiave primaria della tabella per fare riferimento al record da modificare:
update streets set name='New Main Road' where id=2;
It should return UPDATE 1.
Nota
The WHERE statement criteria are case sensitive, Main
Road is not the same as Main road.
15.3.6. Cancella dati
In order to delete an object from a table, use the DELETE command:
delete from people where name = 'Joe Smith';
Dai un’occhiata alla tabelle people:
address=# select * from people;
id | name | house_no | street_id | phone_no
----+------+----------+-----------+----------
(0 rows)
15.3.7. Prova Tu: ★★★
Usa le competenze apprese per aggiungere alcuni nuovi amici al tuo database:
name | house_no | street_id | phone_no
-----------------+----------+-----------+--------------
Joe Bloggs | 3 | 2 | 072 887 23 45
Jane Smith | 55 | 3 | 072 837 33 35
Roger Jones | 33 | 1 | 072 832 31 38
Sally Norman | 83 | 1 | 072 932 31 32
15.3.8. In Conclusione
Ora sai come aggiungere nuovi dati agli esempi che hai creato in precedenza. Ricorda che se vuoi aggiungere nuovi tipi di dati, potresti modificare e/o creare nuovi esempi per contenere tali dati.
15.3.9. Cosa Segue?
Ora che hai aggiunto qualche dato, imparerai come usare le interrogazioni per accedere a questi dati in diverse maniere.