But First, a Database…
|
phpMyAdmin Home. |
If all goes well, you should see a new page with the message "Database rescued_pets running on localhost." No tables are in the database yet, though. You can choose to run an SQL query in the text area field to create one and all of its fields. Alternatively, you can let phpMyAdmin make it easier by using the "Create new table on database rescued_pets:" text field. I vote for that, so lets do it. Enter in the name "adoption_list" (without the quotes) and enter 8 as the number of fields. Click the "Go" button.
Create a table. |
The next screen is a thing of beauty. Rather than type in data types and other attributes in the SQL statement (and making typos, and forgetting quotes and semicolons at the end of statements), customizing our fields is as easy as using some drop down menus and text fields. Enter the following attributes and fields:
Field |
Type |
Length |
Attributes |
Null |
Extras |
Radio |
petID |
INT |
11 |
Not null |
Auto_increment |
primary |
|
species |
VARCHAR |
100 |
Not null |
|||
breed |
VARCHAR |
100 |
Not null |
|||
name |
VARCHAR |
100 |
Not null |
|||
age |
TINYINT |
2 |
UNSIGNED |
null |
||
personality |
TEXT |
Not null |
||||
enterDate |
DATETIME |
Not null |
||||
petPic |
VARCHAR |
100 |
Not null |
Remember that an unsigned number is only positive. Know any negative number ages for pets? Also note that we allow the age field to be null in case the age of the animal cannot be determined. The first field, the ID, will be the primary key that identifies each animal. We will allow MySQL to auto increment, or automatically assign this ID each time we make a new entry.
Setting up the fields. |
If you want, you can add a description in the "Table Comments" field. Set the "Table Type" to MyISAM. Now click the "Save" button and let phpMyAdmin do all of the work. The next screen you see displays the raw SQL statement you would have had to type, and below that, a list of your fields and attributes in a more readable format. You'll notice also that you can select the checkbox for each field and drop (delete) it or change it.
Complete database. |
We're most interested in adding some actual data to our table, though. Right now, it is an empty structure. Click on the "Insert" link. Go ahead and play with the actual data. I entered the data "cat," "Himalayan," "Sherpa," "6," "sweet disposition," enter date as automatically added (just like the ID), and "sherpa.jpg" for the image name. Enter a few records, and then click the browse link to view what you've got.
You now have your database, all dressed up and ready to use PHP scripts in a database driven Web page. MySQL is a deep database program, and the SQL language upon which it is based is full of features. You'll want to go beyond the basics as you get more involved with database design. Check out the wonderful and free resource at http://www.sqlcourse.com. Here you'll encounter a logical series of lessons, each with exercises you can practice right in your browser, with the provided online interpreter. You'll want to learn how to join relational databases, index fields for faster queries, and other more advanced topics that we didn't cover here. Also, don't forget the official MySQL Web site at http://www.mysql.org. Finally, when you want to get help with Mac OS X specific MySQL issues, the best place on the Net is Marc Liyanage's site at http://www.entropy.ch/software/macosx/mysql.
With our foundations firmly in place, we can contemplate the upcoming beauty of the dance between our database and the PHP scripting language. Tune into the next Journal to join in the performance, where we'll finally be able to put our techniques into practice.