Outils pour utilisateurs

Outils du site


issue66:tutoriel_-_libreoffice_p._19

Ceci est une ancienne révision du document !


Table des matières

intro

Many people collect things like sports cards, books, comic books, or butterflies. Sometimes, it is helpful to catalog these collections, so you create a catalog. You decide which characteristic about the items you want to track, you determine that some items share some of these characteristics in common, and you create a system for identifying each item uniquely. Finally, you begin to build your catalog. You can think of a database as a catalog of similar items. You have something you want to track, and what better way to track it than through your computer. Base is the database module for LibreOffice. Base is not a database engine, but a front end for interacting with databases. By default, Base uses the HSQL database engine, which is an open source engine, but you can connect to other engines like MySQL or Oracle. You can even use a spreadsheet as the basis for a database, as we did in part 7 of this series (see Full Circle issue 52). When creating a database, you get better results by sitting down and taking some time to plan out how your database will look and behave. You map out the characteristics you want to track, determine the common relationships, and create a unique way to identify each of the items in the collection. Taking the time to plan will save you time and effort later in the process when you begin to build reports and searches for your database.

Beaucoup de gens collectionnent des choses comme les cartes de sport, les livres, les bandes dessinées, ou les papillons. Parfois, il est utile d'inventorier ces collections, donc vous créez un catalogue. Vous décidez quelle caractéristique s'applique aux éléments que vous souhaitez suivre, vous constatez que certains éléments ont certaines de ces caractéristiques en commun, et vous créez un système pour identifier chaque élément de manière unique. Enfin, vous commencez à construire votre catalogue. On peut penser une base de données comme étant un catalogue d'articles similaires. Vous avez quelque chose à suivre, et quelle meilleure façon de le suivre qu'à travers votre ordinateur ?

Base est le module de base de données pour LibreOffice. Base n'est pas un moteur de base de données, mais une interface de présentation pour interagir avec les bases de données. Par défaut, Base utilise le moteur de base de données HSQL, qui est un moteur open source, mais vous pouvez vous connecter à d'autres moteurs comme MySQL ou Oracle. Vous pouvez même utiliser un tableur comme socle de base de données, comme nous l'avons fait dans la partie 7 de cette série (voir le numéro 52 du Full Circle).

Lors de la création d'une base de données, vous obtiendrez de meilleurs résultats en vous asseyant et en prenant le temps de concevoir votre base de données et de son comportement. Vous cartographiez les caractéristiques que vous souhaitez suivre, déterminez les relations communes, et créez un moyen unique d'identifier chacun des éléments de la collection. Prendre le temps de concevoir vous permettra d'économiser du temps et des efforts plus tard dans le processus lorsque vous commencerez à créer des rapports et des recherches pour votre base de données.

Database

What Makes a Database Before we get into the actual planning of a database, let's talk about the parts making up a database. The smallest element of a database is the field. Think of a field as a single characteristic of the object we are defining in the database. A collection of fields is a record. A record defines all the characteristics of a single object we are collecting. We create tables to hold records. Tables define the fields for each record and contains the datum for each field in the records. Think of a database table as a spreadsheet in Calc. Across the top, you have columns. The columns are the fields. Down the side, you have rows. The rows are records. The entire spreadsheet, containing all the data, is the table. Planning Our Example Database Through this series of articles on Base, we will use a database I created for tracking my book collection. I kept the database fairly simple, but including many elements to show the nature and aspects of relational databases, mostly the relational part. In this part, we will track the steps I took for planning the creation of the database. We will use the steps I have mentioned.

build

What Characteristics to Include When I began planning my Books database, I knew I didn't want a big complicated thing with information I would never use. I knew I needed the basics, title and author. However, I decided on including the year of publication, too. With all the different ways to “read” books today, I decided I needed to track the different types of media as well. So, in the end, I decided on these characteristics: • Title • Author • Publication year • Media type • Relationships Relationships put the “relational” in relational databases. When we first look at our characteristics list, we might think we just need a table with four fields. However, we would run into trouble when we have a book that has more than one author, or we own a book in more than one format. We could just stuff multiple authors in one field, but that would make searching for books by a single author difficult. We could create multiple fields for multiple authors, but how many do you create? If it is an anthology, the book could have many authors. The same is true for the media types. The answer is relationships. Relationships help us link data in different tables to each other. There are three different relationships defined for relational databases. One to One – For every one characteristic, you have one matching characteristic. As an example, for every one person, you can have one spouse. One to Many – For every one characteristic, you have many matching characteristics. In our case, for every book, you can have multiple authors. Many to Many – For many characteristics, you have many matching characteristics. As an example, in a school, you have many students who have many different teachers. For our database, we have two one-to-many relationships. For every one book, we can have multiple authors, and for every one book, we can have multiple media types. We will take these relationships into consideration as we begin to map our database.

DB

Mapping the Database It is a good idea to lay out your database on paper, or using a diagram program, before you begin to work in Base. I used the open source program Dia, because it has a Unified Modeling Language (UML) module designed just for programming diagrams. Dia is available in the Ubuntu repositories. You don't need to know a lot about UML to lay out a database. I will walk you through the process in this section. The main table for our database is the Books table. We know we need the fields Title and Published, but we also need a unique field to identify each record. Since two books could potentially have the same title, we will create an auto generated field named BookID. For the Authors table, we need a field for the author's name (Name) and a unique auto generated field (AuthorID). Two fields for the Media table, too: MediaID and Type.

Now that we have our three tables, we need to link them together. Linking is done by what is known as foreign keys. A foreign key is a field used to create a relationship with a record in another table. Since both of our relationships are one-to-many, we can't just stick a field in the Books table to reference authors and media types. We will use intermediate tables to link the authors and media types together. These intermediate tables will contain foreign keys for the IDs to create the link. We will need two intermediate tables. We will name them BooksAuthors and BooksMedia. BooksAuthors will have two fields named the BookID and the AuthorID, which link back the the ID fields in the Books and Authors tables. We do the same with the BooksMedia table. Two fields named BookID and MediaID, linking to the IDs in Books and Media.

UML

I created a UML diagram showing the relationships between our five tables. Each box contains a table. The name of the table appears at the top of the box. Underneath, we list all the fields in the table and their types. We will discuss types in the next part of this tutorial. The lines between the boxes show the relationships from one table to the next. The notation 1..1 shows that field has a one-to-one relationship with the field in the other table. The notation 1..n shows that field has a one-to-many relationship with the field in the other table. For example, BookID in the Books table is connected to the BookID in the BooksAuthors tables. On the Books table BookID, the notation is 1..n, meaning this book can reference more than one record in the BooksAuthors table. On the booksAuthors, BookID has a notation of 1..1, meaning this is a reference to one specific record in Books. With all this planning, we can easily create our database without having to make many changes. We know what tables we need and how they will relate to each other. While this may seem like a lot of work, it saves us a lot of time in the end, because we have actually put thought into how we will construct our database and how it will work. Next time, we will build our tables and create the relationships in LibreOffice Base. Because of our planning, the process is quick and easy.

issue66/tutoriel_-_libreoffice_p._19.1352741153.txt.gz · Dernière modification : 2012/11/12 18:25 de frangi