• Aucun résultat trouvé

Creating Databases and Tables

Dans le document PHP 6 (Page 145-148)

The first logical use of SQL will be to create a database. The syntax for creating a new database is simply

CREATE DATABASE databasename

That’s all there is to it (as I said, SQL is easy to learn)!

TheCREATEterm is also used for making tables:

CREATE TABLE tablename ( column1name description, column2name description

…)

As you can see from this syntax, after nam-ing the table, you define each column within parentheses. Each column-description pair should be separated from the next by a comma.

Should you choose to create indexes at this time, you can add those at the end of the creation statement, but you can add indexes at a later time as well. (Indexes are more for-mally discussed in Chapter 6, “Advanced SQL and MySQL,” but Chapter 4, “Introduction to MySQL,” introduced the topic.)

In case you were wondering, SQL is case-insensitive. However, I strongly recommend making it a habit to capitalize the SQL key-words as in the preceding example syntax and the following steps. Doing so helps to contrast the SQL terms from the database, table, and column names.

To create databases and tables:

1. Access MySQL using whichever client you prefer.

Chapter 4 shows how to use two of the most common interfaces—the mysql client and phpMyAdmin—to communi-cate with a MySQL server. Using the steps in the last chapter, you should now con-nect to MySQL.

Throughout the rest of this chapter, most of the SQL examples will be entered using the mysql client, but they will work just the same in phpMyAdmin or any other client tool.

2. Create and select the new database (Figure 5.1).

CREATE DATABASE sitename;

USE sitename;

This first line creates the database (assum-ing that you are connected to MySQL as a user with permission to create new data-bases). The second line tells MySQL that you want to work within this database from here on out. Remember that within the mysql client, you must terminate every SQL command with a semicolon, although these semicolons aren’t techni-cally part of SQL itself. If executing mul-tiple queries at once within phpMyAdmin, they should also be separated by semi-colons (Figure 5.2). If running only a single query within phpMyAdmin, no semicolons are necessary.

If you are using a hosting company’s MySQL, they will probably create the database for you. In that case, just con-nect to MySQL and select the database.

Crea ting D a t aba ses and T ables

3. Create the userstable (Figure 5.3).

CREATE TABLE users (

user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,

first_name VARCHAR(20) NOT NULL, last_name VARCHAR(40) NOT NULL, email VARCHAR(60) NOT NULL, pass CHAR(40) NOT NULL,

registration_date DATETIME NOT NULL, PRIMARY KEY (user_id)

);

The design for the userstable is developed in Chapter 4. There, the names, types, and attributes of each column in the table are determined based upon a num-ber of criteria (see that chapter for more information). Here, that information is placed within the CREATEtable syntax to actually make the table in the database.

Because the mysql client will not run a query until it encounters a semicolon, you can enter statements over multiple lines as in Figure 5.3 (by pressing Return or Enter at the end of each line). This often makes a query easier to read and debug. In phpMyAdmin, you can also run queries over multiple lines, although they will not be run until you click Go.

Introduction to SQL

Crea ting D a t aba ses and T ables

Figure 5.1A new database, called sitename, is created in MySQL. It is then selected for future queries.

Figure 5.2The same commands for creating and selecting a database can be run within phpMyAdmin’s SQL window.

Figure 5.3This CREATESQL command will make the userstable.

continues on next page

4. Confirm the existence of the table (Figure 5.4).

SHOW TABLES;

SHOW COLUMNS FROM users;

TheSHOWcommand reveals the tables in a database or the column names and types in a table.

Also, you might notice in Figure 5.4 that the default value for user_idisNULL, even though this column was defined as NOT NULL. This is actually correct and has to do with user_idbeing an automatically incremented primary key. MySQL will often make minor changes to a column’s definition for better performance or other reasons.

In phpMyAdmin, a database’s tables are listed on the left side of the browser window, under the database’s name (Figure 5.5).

Click a table’s name to view its columns (Figure 5.6).

Tips

The rest of this chapter assumes that you are using the mysql client or comparable tool and have already selected the site-namedatabase with USE.

The order you list the columns when cre-ating a table has no functional impact, but there are stylistic suggestions for how to order them. I normally list the primary-key column first, followed by any foreign-key columns (more on this subject in the next chapter), followed by the rest of the columns, concluding with any date columns.

When creating a table, you have the option of specifying its type. MySQL sup-ports many table types, each with its own strengths and weaknesses. If you do not specify a table type, MySQL will automatically create the table using the default type for that MySQL installation.

Chapter 6 discusses this in more detail.

When creating tables and text columns, you have the option to specify its colla-tionandcharacter set. Both come into play when using multiple languages or languages not native to the MySQL server.

Chapter 14, “Making Universal Sites,”

covers these subjects.

DESCRIBE tablenameis the same state-ment as SHOW COLUMNS FROM tablename.

Crea ting D a t aba ses and T ables

Figure 5.4Confirm the existence of, and columns in, a table using the

SHOWcommand.

Figure 5.6phpMyAdmin shows a table’s definition on this screen (accessed by clicking the table’s name in the left-hand column).

Dans le document PHP 6 (Page 145-148)