202 Chapter 8 Creating Your Web Database Next Now that you know how to create users, databases, and tables, you can concentrate on interacting with the database. In the next chapter, we’ll look at how to put data in the tables, how to update and delete it, and how to query the database. 11 525x ch08 1/24/03 3:39 PM Page 202 9 Working with Your MySQL Database IN THIS CHAPTER ,WE’ LL DISCUSS Structured Query Language (SQL) and its use in querying databases.We’ll continue developing the Book-O-Rama database by seeing how to insert, delete, and update data, and how to ask the database questions. To pics we will cover include n What is SQL? n Inserting data into the database n Retrieving data from the database n Joining tables n Updating records from the database n Altering tables after creation n Deleting records from the database n Dropping tables We’ll begin by talking about what SQL is and why it’s a useful thing to understand. If you haven’t set up the Book-O-Rama database, you’ll need to do that before you can run the SQL queries in this chapter. Instructions for doing this are in Chapter 8, “Creating Your Web Database.” What Is SQL? SQL stands for Structured Query Language. It’s the standard language for accessing relational database management systems (RDBMS). SQL is used to store and retrieve data to and from a database. It is used in database systems such as MySQL, Oracle, PostgreSQL, Sybase, and Microsoft SQL Server among others. 12 525x ch09 1/24/03 3:37 PM Page 203 204 Chapter 9 Working with Your MySQL Database There’s an ANSI standard for SQL, and database systems such as MySQL generally strive to implement this standard.There are some subtle differences between standard SQL and MySQL’s SQL. Some of these are planned to become standard in future ver- sions of MySQL, and some are deliberate differences.We’ll point out the more important ones as we go.A complete list of the differences between MySQL’s SQL and ANSI SQL at any given version can be found in the MySQL online manual.You can find this page at this URL and in many other locations: http://www.mysql.com/doc/en/Compatibility.html You might have heard the phrases Data Definition Languages (DDL), used for defining databases, and Data Manipulation Languages (DML), used for querying databases. SQL covers both of these bases. In Chapter 8, we looked at data definition (DDL) in SQL, so we’ve already been using it a little.You use DDL when you’re initially setting up a data- base. You will use the DML aspects of SQL far more frequently because these are the parts that we use to store and retrieve real data in a database. Inserting Data into the Database Before you can do a lot with a database, you need to store some data in it.The way you will most commonly do this is with the SQL INSERT statement. Recall that RDBMSs contain tables, which in turn contain rows of data organized into columns. Each row in a table normally describes some real-world object or relation- ship, and the column values for that row store information about the real-world object. We can use the INSERT statement to put rows of data into the database. The usual form of an INSERT statement is INSERT [INTO] table [(column1, column2, column3, )] VALUES (value1, value2, value3, ); For example, to insert a record into Book-O-Rama’s Customers table, you could type insert into customers values (NULL, "Julie Smith", "25 Oak Street", "Airport West"); You can see that we’ve replaced table with the name of the actual table we want to put the data in, and the values with specific values.The values in this example are all enclosed in double quotes. Strings should always be enclosed in pairs of single or double quotes in MySQL. (We will use both in this book.) Numbers and dates do not need quotes. There are a few interesting things to note about the INSERT statement. The values we specified will be used to fill in the table columns in order. If you want to fill in only some of the columns, or if you want to specify them in a different order, you can list the specific columns in the columns part of the statement. For example, 12 525x ch09 1/24/03 3:37 PM Page 204 205 Inserting Data into the Database insert into customers (name, city) values ("Melissa Jones", "Nar Nar Goon North"); This approach is useful if you have only partial data about a particular record, or if some fields in the record are optional.You can also achieve the same effect with the following syntax: insert into customers set name="Michael Archer", address="12 Adderley Avenue", city="Leeton"; You’ll also notice that we specified a NULL value for the customerid column when adding Julie Smith and ignored that column when adding the other customers.You might recall that when we set the database up, we created customerid as the primary key for the Customers table, so this might seem strange. However, we specified the field as AUTO_INCREMENT.This means that, if we insert a row with a NULL value or no value in this field, MySQL will generate the next number in the autoincrement sequence and insert it for us automatically.This is pretty useful. You can also insert multiple rows into a table at once. Each row should be in its own set of brackets, and each set of brackets should be separated by a comma. We ’ve put together some simple sample data to populate the database.This is just a series of simple INSERT statements that use this multirow insertion approach.The script that does this can be found on the CD accompanying this book in the file \chapter9\book_insert.sql. It is also shown in Listing 9.1. Listing 9.1 book_insert.sql—SQL to Populate the Tables for Book-O-Rama use books; insert into customers values (NULL, "Julie Smith", "25 Oak Street", "Airport West"), (NULL, "Alan Wong", "1/47 Haines Avenue", "Box Hill"), (NULL, "Michelle Arthur", "357 North Road", "Yarraville"); insert into orders values (NULL, 3, 69.98, "2000-04-02"), (NULL, 1, 49.99, "2000-04-15"), (NULL, 2, 74.98, "2000-04-19"), (NULL, 3, 24.99, "2000-05-01"); insert into books values ("0-672-31697-8", "Michael Morgan", "Java 2 for Professional Developers", 34.99), ("0-672-31745-1", "Thomas Down", "Installing Debian GNU/Linux", 24.99), ("0-672-31509-2", "Pruitt, et al.", "Teach Yourself GIMP in 24 Hours", 24.99), ("0-672-31769-9", "Thomas Schenk", "Caldera OpenLinux System Administration 12 525x ch09 1/24/03 3:37 PM Page 205 206 Chapter 9 Working with Your MySQL Database Unleashed", 49.99); insert into order_items values (1, "0-672-31697-8", 2), (2, "0-672-31769-9", 1), (3, "0-672-31769-9", 1), (3, "0-672-31509-2", 1), (4, "0-672-31745-1", 3); insert into book_reviews values ("0-672-31697-8", "Morgan's book is clearly written and goes well beyond most of the basic Java books out there."); You can run this script by piping it through MySQL as follows: >mysql -h host -u bookorama -p < book_insert.sql Retrieving Data from the Database The workhorse of SQL is the SELECT statement. It’s used to retrieve data from a database by selecting rows that match specified criteria from a table.There are a lot of options and different ways to use the SELECT statement. The basic form of a SELECT is SELECT items FROM tables [ WHERE condition ] [ GROUP BY group_type ] [ HAVING where_definition ] [ ORDER BY order_type ] [ LIMIT limit_criteria ] ; We’ll talk about each of the clauses of the statement. First of all, though, let’s look at a query without any of the optional clauses, one that selects some items from a particular table.Typically, these items are columns from the table. (They can also be the results of any MySQL expressions.We’ll discuss some of the more useful ones later in this section.) This query lists the contents of the name and city columns from the Customers table: select name, city from customers; This query has the following output, assuming that you’ve entered the sample data from Listing 9.1 and the other two sample INSERT statements: Listing 9.1 Continued 12 525x ch09 1/24/03 3:37 PM Page 206 . order_items values (1, " 0-6 7 2-3 169 7-8 ", 2), (2, " 0-6 7 2-3 176 9-9 ", 1), (3, " 0-6 7 2-3 176 9-9 ", 1), (3, " 0-6 7 2-3 150 9-2 ", 1), (4, " 0-6 7 2-3 174 5-1 ", 3); insert. "200 0-0 4-0 2"), (NULL, 1, 49.99, "200 0-0 4-1 5"), (NULL, 2, 74.98, "200 0-0 4-1 9"), (NULL, 3, 24.99, "200 0-0 5-0 1"); insert into books values (" 0-6 7 2-3 169 7-8 ",. Your MySQL Database There’s an ANSI standard for SQL, and database systems such as MySQL generally strive to implement this standard.There are some subtle differences between standard SQL and MySQL s