Creating a table is easy—it's the planning that takes some brainpower. To create a simple table, you only need to give it a name. But that would make for a boring table, because it wouldn't contain any columns (fields) and couldn't hold any data. So besides the name, you should know the number of fields and the types of fields you want to have in your table.
All fields in a table are given a particular data type definition. The data type defines the type of data that's allowed in the field. With some data type definitions, you must also define the maximum length you want to allow in the field, but others are assumed to have one specific length for its particular type.
It's very important to define fields appropriately. For example, if you have a field to hold the name of a recording, and it's a 50-character varchar field, yet you try to stuff a 100-character string into the field, your string will truncate at 50 characters.
Not only is it important to define the fields correctly so that the data fits inside the fields, but if you define a field with an incorrect SQL syntax, the table won't be created, period. For example, if you want to use the text data type for a field, you cannot specify a length: it's automatically assumed to have a particular length.
Table 12.1 shows some of the more common types you will use. For a complete list, please read the MySQL manual.
The overall goal of this chapter is to create a table to hold data from your own personal music collection. Take a moment to think about the kinds of things you'd want to know: the title and artist, obviously, and maybe the record label, the date it was acquired, and your own personal notes regarding the recording. I thought about what I wanted for my own table, which I've decided to call my_music, as shown in Table 12.2.
|
Field Name |
Description |
|---|---|
|
id |
Creates a unique ID number for the entry |
|
format |
Is it a CD, cassette, or even an LP? |
|
title |
The title of the recording |
|
artist_fn |
The artist's first name |
|
artist_ln |
The artist's last name or the name of the group |
|
rec_label |
The record label |
|
my_notes |
My own thoughts about the recording |
|
date_acq |
Date acquired |
In the next section, you create a sequence of forms that will take your table information and send it to your MySQL database. In the first step, you submit the name of the table and the number of fields you want to include. The second step will display additional form fields so that you can define the properties of your table columns. A third step will send the request to MySQL, verify that the table was created, and display a "Success!" message.
Using unique ID numbers not only helps you keep track of your data, but also down the road, helps you attempt to establish relationships between multiple tables. In the my_music table, there will be an ID field. Using this field as the unique field, instead of the title field, will allow you to have two recordings in your table that have the same name. For example, if you own the album Strange Fire by the Indigo Girls, you could have two entries in your table: one for the version released in 1987, and one for the version re-released in 1989 (just trust me on that one).
Without using a unique identifier, you would have to pick only one version to put in your table, and your table wouldn't be very accurate. I hope this simple example conveys the importance of having a unique identifier in each record in your table. The usage of the unique identifier will become more apparent throughout the remainder of this book, as you create more database-driven elements.