AF_IC02_ Introducció a les BBDD. AF Unit 1. Relational data base elements. INDEX 1. Introduction ...........................................................................................................2. 2. LibreOffice Base ...................................................................................................3. 3. File types in LibreOffice Base ............................................................................. 4. 4. Objects Data Base ................................................................................................6. 4.1. Requirements ..........................................................................................7. 4.2. The information model ...........................................................................8. 4.3. Data types ................................................................................................8. 4.4. The field key ............................................................................................8. 4.5. The relations ............................................................................................8. 5. Data Base types ....................................................................................................9. 5.1. Integer ......................................................................................................9. 5.2. Floating-Point ........................................................................................10. 5.3. Text.........................................................................................................10. 5.4. Date/Time ...............................................................................................10. 5.5. Other Types ...........................................................................................10. AF_IC02_Introducció a les BBDD / AF 1 1. Introduction. A database can be defined as a set of homogeneous information maintaining an ordered structure, and which can easily be maintained and updated. A clear example of a database would be the agenda of a company in which certain information (name, address , phone, etc..) related to each of its customers, suppliers or employees is stored. To manage this information it would be necessary to create the database "Agenda". Another example of a database would be the set of data and information related to a video club ( movie data , customers, distributors, gender ... ) whereby, through a specific program for these tasks a Database data Manager would have to create a database "video club" . Another example of a database would be an educational institution where all necessary files to control students, teachers, courses, prices, etc ... are available. The database could be called the "Academy". Often the idea of a database table is misinterpreted. A table is a structure of rows and columns that contain information or data of a particular nature, such as a table of data (code, name, address, activity... ) of the 12345 clients, for example, of a company; This would be called a customer table. Each row would have all the data for each customer, which would in terms of database be called a record: It was previously called tab. In each column you would have all the data related to an item in this table (all names, all activities... ).This is called a field. However, a database ( all information and management elements of information regarding the same subject ) can contain multiple tables ( customers, vendors, items, families, discounts ) and other items that you can work with in the data tables. In LibreOffice Base, these other elements are called queries, forms and reports. Therefore, within a database, there may be elements of different natures as we will see throughout this unit. AF_IC02_Introducció a les BBDD / AF 2 2. LibreOffice Base. From version 2.0 of OpenOffice.org a new application called Base, a manager of database with its own SQL engine was implemented. LibreOffice Base, aspires to be a successful LibreOffice equivalent to the popular Microsoft Access. With LibreOffice Base files we can create databases: Directly manage data with its engine HSQLDB relational database. Establish a connection to an external database. In LibreOffice you can record almost any type of database in different formats: spreadsheet, CSV text, dBase, Access, Oracle, and in general, any database accessible via ODBC or JDBC. Once the database is registered we can use the data to create mailings or analyse the pilot data spreadsheet ( PivotTables ). In either cases, tables LibreOffice Base can be used to design, build relationships, create views and queries, and create forms and reports, depending on the capabilities of the data base accessed. TO KNOW MORE Wiki LibreOffice AF_IC02_Introducció a les BBDD / AF 3 3. File types in LibreOffice Base. If you have studied a word processor such as the LibreOffice Writer program, you know that with this program, the information is managed (text mainly) and archived in files .ODT. For example, if you create a letter with information, typed, the necessary extension will be stored in a file such as "letter.odt". If we examine a spreadsheet program such as LibreOffice Calc, we know that with this program, the information (numerical data and formulas basically) is managed and archived in files to the program itself with the extension ODS. We create a file Calc spreadsheet to prepare budgets, information entered, data, formulae, functions, etc.. which be stored in a file such as "budget.ods". Similarly, all information pertaining to the same area of running a business (agenda, video club, academy...) can be managed using a database manager program such as LibreOffice Base, and is stored in a file that the program itself has, the . ODB extension to,(LibreOffice Data Base), leaving the database files stored, for example, in the hard drive computer with the names "clients.odb " or "video club.odb " or " academy.odb ". AF1-1: FORUM in classroom TASK: Think about one real situation where it would be necessary to create a database to store information and then compare and contrast this information with other classmates. Within . ODB ( for Base), all data files on a particular topic are stored in a table. In the above example, you could store the data for customers in a table, the references to suppliers in another, the articles in another, etc.., Therefore, as we can see a database can hold multiple tables. The information in each table is arranged in rows and columns. Each row in a table is called a record, and each column a field . Continuing with the example, each row or record containing all information related to each of the customers of the company , and each column or field, each individual data of each customer. Number 1234 1235 1236 1237 1238 Name Charly Mary Adrian Maria Carla Address Av Catalunya, 3 C/ Palma, 4 C/ Riba, 22 4º-B C/ St Pere, 3 4º C/ Bajo, 9 ... ... ... ... ... ... RECORDS FIELDS AF_IC02_Introducció a les BBDD / AF 4 Therefore, every record is made up of diverse fields, and every field, contains the same type of information in each of the records. If any information of the data base is compared with a typical agenda, every card of the agenda would correspond with every row of the table, that is to say with a record. In all the cards of the agenda the same information will keep each of them corresponding to a field. The set of cards of the agenda would constitute a table of a base of information. In the following picture, you have a comparison between the different office applications of Microsoft Office and LibreOffice as for the extension (format) of the files generated with every program. Microsoft Word .doc .docx Writer .odt Microsoft Excel .xls .xlsx Calc .ods Microsoft Access .mdb .accbd Base .odb Microsoft PowerPoint .ppt .pptx Impress .odp TO KNOW MORE LibreOffice Base vs Microsoft Access. AF_IC02_Introducció a les BBDD / AF 5 4. Objects Data Base. LibreOffice Bases possesses a series of tools and objects to store and to manage information. Inside a file of database, a file .odb, different elements called objects, each with their different natures exist: Tables: They are the managers of storing the information. The storage information is carried out thanks to the structure of the tables. A good structure of the tables is indispensable in order to carry out all necessary operations efficiently. The tables are the main elements. If there are no tables, the rest of elements of a database cannot exist. In other words, If you don't have the main stores of information, that is to say the tables, it's not possible to create a database. Queries: They allow to give response to questions that could appear with regard to the information stored in the tables. In addition, they allow you to make calculations and operations with the stored information. From this form we can obtain the information to work exclusively with those that are needed in every moment. You can save these criteria, in order to use them in future occasions. That is a query. Forms: They take charge presenting the information proceeding from tables or consultations with a personalized and "elegant" format. From this form they present the information in an attractive form, being able at the same time to carry out other operations with records: to add, to modify, to eliminate, etc. It is better to work with the information of the tables seeing them across a form instead of in the shape of table of rows and columns. This is the interface that works the best. Reports: They create summaries and present the information of tables and consultations with a personalized format, fundamentally in order "to extract them" for printing (though not always). The possibilities for it are immense. AF_IC02_Introducció a les BBDD / AF 6 TABLES REPORTS LibreOffice BASE QUERIES FORMS 4.1. Requirements. Before you can create a new database document, you need to know your specific needs. You might want to try to identify your requirements so you can be sure that you find a solution that will fit all of your current and future needs. To construct a database, first you have to know that it will contain information. It is important to analyze your needs before starting the database The analysis can be carried out in different ways but one way consists of elaborating a list of fields that you wish to include in the database. The fields are where the information is placed into the table. When you have the fields of the information, you must group them, at the end, you are left with the tables of the database. An example might be: Name Surname Partner number Partner type Address Telephone number City Zip code Description partner type Inscription date AF_IC02_Introducció a les BBDD / AF 7 4.2. The information model. When you have ended the analysis you can start grouping the fields. If we continue with the previous example. Partner Partner number Partner type Inscription date Name Surname Address Zip code Partner types Partner type Description partner type Zip codes Zip code City If you organize the information, be sure to avoid repeated information. 4.3. Data types. The information that you can stored in a database can be of text type, numerical type, a date, an answer (yes / no), a note or another type of information. The types of information indicate the type of information that the field will have. 4.4.The key field. One of the fields of every table has to be defined as a key field. This field will serve to identify a record of the only form. For example, if two partners have the same name and the same surname and live at the same address, you will have to distinguish them in the database. The key field could be a number of partner because it is unique. 4.5.The relations. The tables of the database can be interrelated by logical relations with other tables. When you create a database, you use separate tables for different types of entities. Some examples are: employees, departments, contracts, etc. Sometimes you have to work with multiple tables that have relationships with each other. However you need to have relationships between these tables. For example, employees is employed at a department, employees have a contract, etc.. There are several types of database relationships: One to One Relationships: This type of relationship occurs when each entry in the first table has one, and only one, counterpart in the second table. AF_IC02_Introducció a les BBDD / AF 8 One to Many or Many to One Relationships: This type of relationship occurs when each record in the first table corresponds to one or more records in the second table but each record in the second table corresponds to only one record in the first table. Many to Many Relationships: This type of relationship occurs when each record in the first table corresponds to one or more records in the second table and each record in the second table corresponds to one or more records in the first table. 5. Data Base types. By default LibreOffice Base uses the HSQLDB database management system. Like any database system, HSQLDB has a rich set of data types to choose from when building your database. An understanding of the types and when you might, if ever, use them will help you as you create your tables for your database projects. HIGHLIGHTS Before you create your first table, you need to think about the common database field types. When you select a type for a field, you are presented with many options for the type. Many of the types are identical, and are there for compatibility reasons. Below you will have a look at five most important categories of data types in LibreOffice base: integers, text, time, and others. 5.1 Integers An integer is a whole number. There are four important integer types, and their differences are mostly related to size. TINYINT – the smallest of the integer types. Its size is 28. That is 1 to 256 if there are no negatives, and -128 to 127 if there are negatives. You shouldn’t use this type unless you can guarantee the numbers will always fall into this range. SMALLINT – with a size of 216, this integer type gives you a little more breathing room. If you don’t need negatives, you get 65,536, but if you need negatives, the range is 32,768 to 32,767. Like the TINYINT, make sure your numbers will fall into this range. INTEGER or INT – the most commonly used integer type, with a size of 232. That’s 4,294,967,296 without negatives and -2,147,483,648 to 2,147,483,647 with negatives. This is the type usually used for recording identities. You are pretty safe using this type for most of your integer needs. The one exception is when you need extremely large integer numbers. BIGINT – rarely used. Size is 264 = big, enormous numbers (i.e. 1.84467440737 X 1019). Unless you are using some extremely big numbers (calculating the distance to the next parallel world) or extremely small numbers (the size of a pit on an electron), you will not ever need to use this type. AF_IC02_Introducció a les BBDD / AF 9 5.2. Floating-Point. Floating-point numbers are numbers with decimals, or real numbers. They are made of a whole part and a partial part separated by a decimal point. There are four floating-point types. The basic difference between them is the level of accuracy. DECIMAL and NUMERIC – These types have an unlimited range. When you define them, you specify the total numbers of digits (or places) allowed and the number of those digits which will fall after the decimal point, i.e. “number” DECIMAL(10, 2) would mean a field named number that has 10 places with two after the decimal point so that the largest number for this definition is 99,999,999.99. The accuracy for DECIMAL and NUMERIC is nearly perfect. DOUBLE or REAL – These have a more limited range. A max of 15 decimal places. Accuracy is not so good. If you just need an approximate number, these are okay. For more precise calculations and better rounding, use DECIMAL or NUMERIC. 5.3. Text. Text is a string of characters, not meant to represent a number or other type, enclosed in quotes. There are four different text data types, and they differ mostly in how they consume space. TEXT or VARCHAR –a variable length string of characters. With VARCHAR, you define the max number of characters for the field, i.e. “name” VARCHAR(30) for a variable named "name" that can have up to 30 characters. If you do not use all 30 characters, the field will only use the needed space in the database. 5.4. Date/Time These are types for defining fields that track the date and time. There are three types, and they differ in the content they contain. DATE – contains a date value. The format for date entry is YYYY-MM-DD, i.e. 2013-1029. (the exact format is location specific) TIME – contains a clock time. The format for time uses a 24 hour clock, as HH:MM:SS, ie 14:25:36 for 2:25:36 PM. TIMESTAMP or DATETIME – a combination of both the date and the time. Once again the time is in 24 hour clock format, as YYYY-MM-DD HH:MM:SS, i.e. 2013-10-29 14:25:36. 5.5. Other Types. The types in this category are not really related. They just don’t fit in any of the above categories. YES/NO or BOOLEAN – Think of a BOOLEAN as a three-state switch. You can turn it on, TRUE, you can turn it off, FALSE, or you can put it in neutral, NULL. Though you will find few needs for the NULL state, TRUE and FALSE states are handy for saying an option is available or not, turned on or off. AF_IC02_Introducció a les BBDD / AF 10 BINARY, VARBINARY, LONGVARBINARY – These type are for storing binary data like images. We won’t go into them too much today, but will revisit them at some point in the future. OTHER or OBJECT – You may not find much use for this type when working in LibreOffice, but for completeness, OTHER and OBJECT are used to store serialized Java objects. There you have it, the data types of the HSQLDB database engine used by LibreOffice by default. In later articles, we will use some of these data types to create tables for database collections. ACTIVITY: Web. TASK: Start to practice the unit vocabulary with this activity 2.0. If you have the time, create your own set of cards adding any additional information such as Catalan/Spanish translations or images. Database vocabulary. Unit 1 AF_IC02_Introducció a les BBDD / AF 11