Definizione delle relazioni

From Apache OpenOffice Wiki
Jump to: navigation, search






Pagina in traduzione - translation in progress





Ora che le tabelle sono state create, quali sono le relazioni tra di esse? E' giunto il momento di definirle basandoci sulla serie di domande e di risposte che ci eravamo posti all'inizio.

Quando siamo in vacanza, vogliamo inserire ogni giorno tutte le nostre spese in una volta sola. La maggior parte di queste spese quindi starà nella tabella Vacanze, ma il carburante che abbiamo acquistato no. Così dovremo mettere in relazione queste due tabelle usando i campi Data. Può accadere che la tabella Carburante abbia più di un record nella medesima data, quindi tra la tabella Vacanze e quella Carburante abbiamo una relazione da "uno a molti" (contrassegnata con 1:n).

La tabella Vacanze contiene anche diversi campi per il tipo di pagamento usato. Per ogni campo che contiene il tipo di pagamento, c'è solo una voce corrispondente nella tabella Tipo Pagamento. Questa è una relazione di tipo "uno a uno"; da un campo in una tabella ad un altro di un'altra tabella (contrassegnata con 1:1). Anche altre tabelle contengono campi riguardanti il tipo di pagamento; le relazioni tra tali campi e la tabella Tipo Pagamento sono sempre di tipo 1:1.

Poiché la tabella Tipo Pagamento fornisce solo un elenco statico, non possiamo definire una relazione tra essa ed i campi delle altre tabelle che utilizzano le sue voci. Questo si farà attraverso la creazione dei formulari.

Le tabelle Carburante e Manutenzione non hanno realmente una relazione anche se utilizzano dei campi simili, come Data e Chilometraggio. A meno che una persona si trovi ad acquistare regolarmente carburante e servizi per le sue automobili, le voci di queste tabelle non hanno nulla in comune tra di loro.


Suggerimento
Al momento di creare il vostro database , è importante determinare quali tabelle sono in relazione tra loro e come.
  1. Cominciamo ora a definire le relazioni attraverso Strumenti > Relazioni. Si apre la finestra Automobile – OpenOffice.org Base: struttura relazione (Figura Errore: sorgente del riferimento non trovata). Le icone che useremo sono Aggiungi tabelle e Nuova Relazione.
[[Image:|thumb|<center>
Figura 1: Finestra Struttura relazione]]</center>
  1. Fate clic sull'icona Aggiungi tabelle; si apre la finestra Aggiungi tabelle.
  2. Usate uno di questi modi per aggiungere una tabella alla finestra Struttura relazione:
    • Fate doppio clic sul nome della tabella che volete aggiungere (nel nostro caso, fatelo sulle tabelle Vacanze e Carburante).
    • Oppure, selezionate il nome della tabella e quindi fate clic sul pulsante Aggiungi per ogni tabella.
  1. Fate clic su Chiudi quando avete aggiunto le tabelle che desiderate (Figura 2).
[[Image:|thumb|Figura 2: Aggiunta elenchi tabella]]
  1. Definizione della relazione tra le tabelle Vacanze e Carburante.

Esistono due modi per eseguire questa operazione:

    • Selezionate e trascinate il campo Data dalla tabella Carburante sul campo Data della tabella Vacanze. Quando rilascerete il pulsante del mouse, una linea di collegamento apparirà tra i due campi data (Figura 3).
[[Image:|thumb|Figura 3: Attribuzione di una relazione di tipo 1:n ]]
    • Oppure fate clic sull'icona Nuova relazione: si aprirà la finestra Relazioni (Figura 4). Le nostre due tabelle sono elencate nella sezione Tabelle considerate.
      • Nella sezione Campi considerati, fate clic sull'elenco a tendina sotto l'etichetta Carburante.
[[Image:|thumb|Figura 4: Impostazione delle relazioni tra tabelle]]
    • Selezionate Data dall'elenco della tabella Carburante.
    • Fate clic nella cella a destra dell'elenco a scomparsa. Si aprirà un elenco a tendina per la tabella Vacanze.
    • Selezionate Data dall'elenco della tabella Vacanze. Ora il quadro dovrebbe assomigliare alla Figura 5.
    • fate clic su OK.
[[Image:|thumb|Figura 5: Campi selezionati in una relazione]]
  1. Modifica della sezione Opzioni di aggiornamento e Opzioni di eliminazione della finestra Relazioni.
    1. Fate clic con il tasto destro sulla linea che collega i campi Data delle due tabelle elencate per aprire il menu contestuale.
    2. Selezionate Modifica per aprire la finestra Relazioni (Figura 6).
    3. Selezionate Aggiorna cascata.
    4. Selezionate Elimina cascata.
[[Image:|thumb|Figura 6: Sezione Opzioni di aggiornamento e Opzioni di eliminazione]]

Queste opzioni non sono assolutamente necessarie, ma costituiscono un valido aiuto; la loro attivazione vi permette di aggiornare una tabella che ha una relazione con un'altra. Vi permettono inoltre di cancellare un campo dalla tabella.






Now that the tables have been created, what are the relationships between our tables? This is the time to define them based upon the questions we asked and answered in the beginning.

When on vacation, we want to enter all of our expenses all at one time each day. Most of these expenses are in the Vacations table, but the fuel we buy is not. So, we will relate these two tables using the Date fields. Since the Fuel table may have more than one entry per date, this relationship between the Vacations and Fuel tables is one to many. (It is designated 1:n.)

The Vacations tables also contains several fields for the type of payment used. For each field listing the payment type, there is only one entry from the Payment Type table. This is a one to one relationship: one field in one table to one entry from the other table. (It is designated 1:1.) Other tables also contain fields for the type of payment. The relationship between these fields of those tables and the Payment Type table are also 1:1.

Since the Payment Type table only provides a static list, we will not be defining a relationship between the Payment Type table and the fields of the other tables which use the entries of the Payment Type table. That will be done when the forms are created.

The Fuel and Maintenance tables do not really have a relationship even though they share similar fields: Date, and Odometer. Unless a person is in a habit of regularly getting fuel and having their vehicle serviced, the entries in these tables do not share anything in common.

Template:Documentation/Tip

  1. We begin defining relationships by Tools > Relationships. The Automobile - OpenOffice.org Base: Relation design window opens. The icons we will use are Add Tables and New Relation.
  2. Relation design window.
  3. Click the Add Tables icon. The Add Tables window opens.
  4. Use one of these ways to add a table to the Relation design window:
    • Double-click the name of the table. In our case, do this for both Vacations and Fuel.
    • Or, click the name of the table and then click Add.
  5. Click Close when you have added the tables you want.
  6. Added table lists.

Defining the relationship between the Vacations and Fuel tables

Two ways exist to do this:

  • Click and drag the Date field in the Fuel table to the Date field in the Vacations table. When you release the mouse button, a connecting line forms between the two date fields.
  • Designation for a 1:n relationship.
  • Or, click the New Relation icon. This opens the Relations window. Our two tables are listed in the Tables involved section.
    1. In the Fields involved section, click the dropdown list under the Fuel label.
    2. Setting the relationship between tables.
    3. Select Date from the Fuel table list.
    4. Click in the cell to the right of this dropdown list. This opens a dropdown list for the Vacations table.
    5. Select Date from the Vacations table list. It should now look like the figure below.
    6. Click OK.
    7. Selected fields in a relationship.

Modifying the Update options and Delete options section of the Relation window

  1. Right-click the line connecting the Date fields in the two table lists to open a context menu.
  2. Select Edit to open the Relation window.
  3. Select Update cascade.
  4. Select Delete cascade.
Update options and Delete options section.

While these options are not absolutely necessary, they do help. Having these options selected permits you to update a table that has a relationship defined with another table. It also permits you to delete a field from the table.

Top of page


Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY).
Personal tools