HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [254]
Follow these steps:
1. Begin with the INSERT keyword.
Use INSERT to clarify that this instruction is a data insertion command.
2. Specify the table you want to add data to.
In my example, I have only one table, so use INTO contact to specify that’s where the table goes.
3. (Optional) Specify field names.
You can specify a list of field names, but this step is unnecessary if you add data to all fields in their standard order. (Normally, you don’t bother with field names.)
4. Use the VALUES keyword to indicate that a list of field values is coming.
5. Enclose the values within parentheses.
Use parentheses to enclose the list of data values.
6. Put all values in the right order.
Place values in exactly the same order the fields were designated.
7. Place text values within single quotes.
MySQL uses single quotes to specify text values.
8. End the statement with a semicolon, as you do with all SQL commands.
9. Repeat with other data.
Add as many INSERT commands as you want to populate the data table.
Viewing the sample data
Once you’ve created and populated a table, you’ll want to look it over. SQL provides the SELECT command for this purpose. SELECT is amazingly powerful, but its basic form is simplicity itself:
SELECT * FROM contact;
This command simply returns all fields of all records from your database.
Running a Script with phpMyAdmin
phpMyAdmin provides terrific features for working with SQL scripts. You can write your script directly in phpMyAdmin, or you can use any text editor.
Aptana Studio is fine for editing SQL files, but it doesn’t have built-in SQL support, such as syntax checking and coloring. You can download a plugin to add these features (search for eclipse SQL plugins); use another editor like Notepad++ or Komodo Edit, which both support syntax coloring for SQL; or just do without syntax coloring in Aptana.
If you’ve written a script in some other editor, you’ll need to save it as a text file and import it into phpMyAdmin.
To run a script in phpMyAdmin, follow these steps:
1. Connect to phpMyAdmin.
Be sure that you’re logged in and connected to the system.
2. Navigate to the correct database.
Typically, you use a drop-down list to the left of the main screen to pick the database. (If you haven’t created a database, see the instructions in Chapter 1 of this minibook.) Figure 2-1 shows the main phpMyAdmin screen with the xfd database enabled.
3. Activate the SQL pop-up window.
You can do so by clicking the small SQL icon in the left-hand navigation menu. The resulting window looks like Figure 2-2.
Figure 2-1: The xfd database is created and ready to go.
Figure 2-2: The SQL script window.
4. (Optional) Type your SQL code directly into this dialog box.
This shortcut is good for making quick queries about your data, but generally you create and initialize data with prewritten scripts.
5. Move to the Import Files tab.
In this tab, you can upload the file directly into the MySQL server. Figure 2-3 shows the resulting page. Use the Browse button to locate your file and the Go button to load it into MySQL.
If you’ve already created the contact database by following the instructions in Chapter 1 of this minibook, you may be nervous that you’ll overwrite the data. You will, but for this stage in the process, that’s exactly what you want. The point of a script is to help you build a database and rebuild it quickly. After you have meaningful data in the table, you won’t be rebuilding it so often, but during the test and creation stage, this skill is critical.
6. Examine your handiwork.
Look back at the phpMyAdmin page, and you see something like Figure 2-4. It shows your script and, if you ended with a SELECT statement, an output of your table. (Later versions of phpMyAdmin display only the last statement in the script, but all are executed.)
Figure 2-3: Importing an externally defined SQL script.
Figure 2-4: Here’s the script and its results, shown in phpMyAdmin.
Using AUTO_INCREMENT