Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

To keep your database from growing too large, you should choose the

smallest data type possible for each field. For example, the SMALLINT

data type stores integer values between –32,768 and 32,767 and

occupies 2 bytes of storage space, regardless of how small the value

Is. In comparison, the bigint data type stores integer values between

–9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 and occu-

pies 8 bytes of storage space, no matter how small the value. If you

know that a value you assign to a field will always be between –32,768

and 32,767, you should use the SMALLINT data type instead of the

BIGINT data type, which saves 6 bytes per record. For a single record,

this is not a huge savings, but it could be for a database table with

thousands or even millions of records.

Creating Tables

To create a table, you use the CREATE TABLE statement, which specifies

the table and column names and the data type for each column. The

syntax for the CREATE TABLE statement is as follows:

CREATE TABLE table_name (column_name TYPE, ...);

The following statement creates the vehicles table in the

vehicle_fleet database. The first three columns in the table are

VARCHAR data types; the license field can be a maximum of 10 char-

acters, the make field can be a maximum of 25 characters, and the

model field can be a maximum of 50 characters. The miles field is a

FLOAT data type. The last field, assigned_to, is also a VARCHAR data

type, with a maximum of 40 characters.

mysql> CREATE TABLE vehicles (license VARCHAR(10),[ENTER ]

-> make VARCHAR(25), model VARCHAR(50), miles FLOAT,[ENTER ]

-> assigned_to VARCHAR(40));[ENTER ]

After you create a table, you can use the DESCRIBE statement to dis-

play how the table is structured. The following DESCRIBE statement

displays the structure of the vehicles table below the command:

mysql> DESCRIBE vehicles;[ENTER ]

+-------------+-------------+------+-----+---------+-------+

| Field| Type| Null | Key | Default | Extra |

+-------------+-------------+------+-----+---------+-------+

| license| varchar(10) | YES || NULL||

| make| varchar(25) | YES || NULL||

| model| varchar(50) | YES || NULL||

| miles| float| YES || NULL||

| assigned_to | varchar(40) | YES || NULL||

+-------------+-------------+------+-----+---------+-------+

5 Rows in set (0.00 sec)

You can exe-

cute the SHOW

TABLES

statement to

display a list

of the tables in the cur-

rent database.

The result of the preceding DESCRIBE vehicles; command showed

six columns, not just the two for Field and Type. The other four


Defining Database Tables

columns show special characteristics or restrictions on the fields

that can be defined in MySQL. The values for these columns can be

specified in the CREATE TABLE statement. The Null column indicates

whether the field can be left empty or not. The Key field indicates

which type of key, if any, is defined for the field (PRIMARY, UNIQUE,

or INDEX). The Default column shows the value that will be inserted

automatically in the field if no value is specified. The Extra col-

umn indicates any special features about the field, such as an auto-

increment value.

Next, you will create a table named pagevisits in the sitevisitors

database. The table will contain detailed information about every

visit to a Web page on your site. The table will contain seven

fields: page_filename, visit_date, visit_time, previous_page,

request_method, remote_host, and remote_port. The visit_date

field will be a DATE data type, the visit_time field will be a TIME data

type, the remote_port field will be an INTEGER data type, and the

rest of the fields will be VARCHAR data types. Note that the TIME data

type can be used to store a specific time or a measure of time. In the

pagevisits table, the visit_time field will contain the duration of

each visit. For the DATE data type, dates must be entered in the for-

mat YYYY-MM-DD; for the TIME data type, times must be entered in the

format HH:MM:SS.

To create the pagevisits table:

1.

2.

405

Return to MySQL Monitor.

Enter the following command to select the sitevisitors

database:

mysql> USE sitevisitors;[ENTER ]

3.

Enter the following command to create the pagevisits table:

mysql> CREATE TABLE pagevisits (page_filename

VARCHAR(250), visit_date DATE,[ENTER ]

-> visit_time TIME, previous_page

VARCHAR(250),[ENTER ]

-> request_method VARCHAR(10), remote_host

VARCHAR(250), remote_port INT);[ENTER ]

4.

After you see the “Query OK” message, enter the following

command to view the structure of the new table. Your screen

should look like Figure 7-9.

mysql> DESCRIBE pagevisits;[ENTER ]


CHAPTER 7

Working with Databases and MySQL

406

Figure 7-9

DESCRIBE statement displaying the structure of the pagevisits table

Altering Tables

Over time, the original table definition may not be sufficient for your

needs. New fields may be needed, existing fields may become obso-

lete, or the field modifiers may no longer be appropriate. To modify

the structure of a table, you use the ALTER TABLE statement. The state-

ment has several different syntaxes, depending on the change being

made.

The keyword

COLUMN is

required for

standard SQL,

but not for

MySQL. When adding a

single column, the paren-

theses around the list of

column_name and

column_type specifiers

are optional.

Adding Columns

To add fields to a table with the ALTER TABLE statement, use the

following syntax:

ALTER TABLE table_name ADD [COLUMN] (column_name column_

type [, column_name column_type ...]);

The following statement adds a new column of type INT named

model_year to the vehicles table in the vehicle_fleet database:

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]