• Aucun résultat trouvé

MySQL Test

Dans le document Beginning JSP, JSF and Tomcat (Page 172-176)

Listing 6-1 shows an SQL script to create a simple database, which we will use in the E-shop application you first encountered in Chapter 3. For your reference, I have written a summary of the SQL language in Appendix B.

Listing 6-1. shop_create.sql 01 drop database shop;

02 create database shop;

03 create table shop.categories (

04 category_id integer not null auto_increment unique, 05 category_name varchar(70),

07 primary key (category_id) 08 );

09 create table shop.books (

10 book_id integer not null auto_increment unique, 11 title varchar(70),

17 create index category_id_key on shop.categories (category_id);

18 create index book_id_key on shop.books (book_id);

19 alter table shop.books add index category_id (category_id), 20 add constraint category_id foreign key (category_id) 21 references shop.categories (category_id)

22 ;

Line 01 removes the database. It reports an error when you use it the first time, because there is no database to delete, but it also lets you re-run the script to re-create the database from scratch. It isn’t something you would normally do in a non-test environment.

Line 02 creates a blank database named shop.

Lines 03 to 08 create a table to store book categories.

Lines 09 to 16 create a table to store book records.

Line 17 creates an index to speed up the search of categories.

Line 18 creates an index to speed up the search of books when selected by their IDs.

Lines 19 to 22 create an index to speed up the search of books when selected by their categories.

To execute the SQL script, you can use either the Command Line Client or the Workbench. You will find the script in the software package for this chapter.

To use the Command Line Client, click on "Start" and select "Programs h MySQL h MySQL Server 5.5 h MySQL 5.5 Command Line Client". This opens a command-line window where you will first have to type the password to access the server. If you have removed the password as I suggested, you only need to hit Enter. The Client will respond by displaying the "mysql> " prompt. Open shop_create.sql with a text editor, copy everything into the clipboard, and paste it onto the Command Line Client.

Listing 6-2 shows what you will get.

Listing 6-2. Log of shop_create.sql Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> drop database shop;

ERROR 1008 (HY000): Can't drop database 'shop'; database doesn't exist mysql> create database shop;

Query OK, 1 row affected (0.00 sec) mysql> create table shop.categories (

-> category_id integer not null auto_increment unique, -> category_name varchar(70),

-> primary key (category_id) -> );

Query OK, 0 rows affected (0.13 sec) mysql> create table shop.books (

-> book_id integer not null auto_increment unique, -> title varchar(70),

Query OK, 0 rows affected (0.19 sec)

mysql> create index category_id_key on shop.categories (category_id);

Query OK, 0 rows affected (0.37 sec) Records: 0 Duplicates: 0 Warnings: 0

mysql> create index book_id_key on shop.books (book_id);

Query OK, 0 rows affected (0.36 sec) Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table shop.books add index category_id (category_id), -> add constraint category_id foreign key (category_id) -> references shop.categories (category_id)

-> ;

Query OK, 0 rows affected (0.33 sec) Records: 0 Duplicates: 0 Warnings: 0

If you repeat the operation, you’ll see that something like "Query OK, 2 rows affected (0.56 sec)"

will replace the "ERROR 1008" message.

You can achieve the same result with the Workbench. After launching it, double-click on the link

"Local MySQL55" that appears under the heading "Open Connection to Start Querying". This will open a window as shown in Figure 6-1.

Figure 6-1. MySQL Workbench

If you have already created the shop database from the command line, you will see the

corresponding entry in the Object Browser. If you right-click it, you will be able to choose the default database, although it is not necessary for you to do so.

In any case, to load the SQL script, click on the folder icon in the menu bar of the central pane. You can then open shop_create.sql or paste its content into the central pane. To execute it, click on the lightning icon. Figure 6-2 shows what happens when you do so.

Figure 6-2. Creating the shop database with the Workbench

Now that the database is in place, insert book categories and book records by executing the SQL script shown in Listing 6-3.

Listing 6-3. shop_populate.sql USE shop;

INSERT INTO categories ( category_id

, category_name )

VALUES

(1,'Web Development') , (2,'SF')

, (3,'Action Novels') ;

INSERT INTO books ( book_id

, title , author , price , category_id )

VALUES

(1,'Pro CSS and HTML Design Patterns','Michael Bowers',44.99,1) , (2,'Pro PayPal E-Commerce','Damon Williams',59.99,1)

, (3,'The Complete Robot','Isaac Asimov',8.95,2) , (4,'Foundation','Isaac ASimov',8.95,2)

, (5,'Area 7','Matthew Reilly',5.99,3) , (6,'Term Limits','Vince Flynn',6.99,3) ;

Note that you only need the "USE shop;" command if you execute the script from the command line.

After populating the database, you can look at the book records by typing a SELECT command in the central pane of Workbench (or at the "mysql> " prompt of the Command Line Client). For example,

"select * from books;" will list all the books you have inserted, as shown in Figure 6-3.

Figure 6-3. List of all books

Dans le document Beginning JSP, JSF and Tomcat (Page 172-176)