good example of how you can use enhanced entity-relationship (eer) diagrams in MysQL Workbench to document your schema.
As there is a relatively large number of objects, they will be split into five groups (each of the table groups, views, and stored routines) when discussing the schema. The first group is the customer-related data with the tables shown in Figure 4-5.
Figure 4-4. Overview of the sakila database
62
There are four tables with data related to the customers. The customer table is the main table, and the address information is stored in the address, city, and country tables.
There are foreign keys between the customer and business groups with a foreign key from the customer table to the store table in the business group. There are also four foreign keys from tables in the business group to the address and customer tables. The business group is shown in Figure 4-6.
Figure 4-5. The tables with customer data in the sakila database
ChapTer 4 TesT DaTa
The business tables contain information about the stores, staff, rentals, and payments. The store and staff tables have foreign keys in both directions with staff belonging to a store and a store having a manager that is part of the staff. Rentals and payments are handled by a staff member and thus indirectly linked to a store, and payments are for a rental.
The business group of tables is the one with the most relations to other groups.
The staff and store tables have foreign keys to the address table, and the rental and payment tables reference the customer. Finally, the rental table has a foreign key to the inventory table which is in the inventory group. The diagram for the inventory group is shown in Figure 4-7.
Figure 4-6. The tables with business data in the sakila database
64
Figure 4-7. The tables with inventory data in the sakila database
ChapTer 4 TesT DaTa
The main table in the inventory group is the film table which contains the metadata about the films the stores offer. Additionally, there is the film_text table with the title and description with a full text index.
There is a many-to-many relationship between the film and the category and actor tables. Finally, there is a foreign key from the inventory table to the store table in the business group.
That covers all the tables in the sakila database, but there are also some views as shown in Figure 4-8.
Figure 4-8. The views in the sakila database
The views can be used like reports and can be divided into two categories. The film_list, nicer_but_slower_film_list, and actor_info views are related to the films stored in the database. The second category contains information related to the stores in the sales_by_store, sales_by_film_category, staff_list, and customer_list views.
To complete the database, there are also the stored functions and procedures shown in Figure 4-9.
66
The film_in_stock() and film_not_in_stock() procedures return a result set consisting of the inventory ids for a given film and store based on whether the film is in stock or not. The total number of inventory entries found is returned as an out parameter. The rewards_report() procedure generates a report based on minimum spends for the last month.
The get_customer_balance() function returns the balance for a given customer on a given data. The two remaining functions check the status of an inventory id with inventory_held_by_customer() returning customer id of the customer currently
renting that item (and NULL if no customer is renting it), and if you want to check whether a given inventory id is in stock, you can use the inventory_in_stock() function.
Installation
The downloaded file expands into a directory with three files, of which two create the schema and data and the last file contains the ETL diagram in the format used by MySQL Workbench.
Note This section and the examples later in the book use the copy of the sakila database that is downloaded from MysQL’s homepage.
Figure 4-9. The stored routines in the sakila database
ChapTer 4 TesT DaTa
The files are
• sakila-data.sql: The INSERT statements needed to populate the tables as well as the trigger definitions.
• sakila-schema.sql: The schema definition statements.
• sakila.mwb: The MySQL Workbench ETL diagram. This is similar to that shown in Figure 4-4 with details in Figures 4-5 to 4-9.
You install the sakila database by first sourcing the sakila-schema.sql file and then the sakila-data.sql file. For example, the following is using MySQL Shell:
MySQL [localhost+ ssl] SQL> \source sakila-schema.sql MySQL [localhost+ ssl] SQL> \source sakila-data.sql
Add the path to the files if they are not located in the current directory.
Common for the three data sets thus far is that they contain little data. While this is in many cases a nice feature as it makes it easier to work with, in some cases you need a bit more data to explore the difference in query plans. The employees database is an option with more data.