By George Reese, Randy Jay Yarger, Tim King
With Hugh E. Williams
Book Price: $39.95 USD
£28.50 GBP
PDF Price: $31.99
Cover | Table of Contents | Colophon
http://www.mysql.com/downloads/os-solaris.html.
http://www.mysql.com/downloads or at one of
the mirror sites. Windows installation is simply a matter of
downloading the
mysql
-version.
zip,
unzipping it, and running the set-up program.
http://www.mysql.com/documentation for more
information.
|
Server name
|
Description
|
|---|---|
|
mysqld
|
Debug binary with memory allocation checking, symbolic link support,
and transactional table support (InnoDB and DBD)
|
|
mysqld-opt
|
Optimized binary with no support for transactional tables
|
|
mysqld-nt
|
Optimized binary with support for NT named pipes
|
SELECT name FROM people WHERE name LIKE 'Stac%'
SELECT name FROM people WHERE name LIKE 'Stac%'
SHOW
DATABASES
command. Upon installation of MySQL
3.23.40, the following tables already exist:
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.37 sec)mysql, is
MySQL's system database, which you will learn more
about in Chapter 5. The second database,
test, is a play database you can use to learn
MySQL and run tests against. You may find other databases on your
server if you are not dealing with a clean installation. For now,
however, we want to create a new database to illustrate the use of
the MySQL CREATE
statement:
CREATE DATABASE TEMPDB;
TEMPDB:USE TEMPDB;
DROP
DATABASE
command:
DROP DATABASE TEMPDB;
CREATE
statement and destroy things using the DROP
statement, just as we used them here.
test database that comes with MySQL or your own
play database. Using the SHOW command, you can
display a list of tables in the current database the same way you
used it to show databases. In a brand new installation, the
test database has no tables. The following shows
the output of the SHOW
TABLES
command when connected to the
mysql system database:
mysql> USE mysql; Database changed mysql> SHOW TABLES; +-----------------+ | Tables_in_mysql | +-----------------+ | columns_priv | | db | | func | | host | | tables_priv | | user | +-----------------+ 6 rows in set (0.00 sec)
DESCRIBE
command:
mysql> DESCRIBE db;
+-----------------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-----------------+------+-----+---------+-------+
| Host | char(60) binary | | PRI | | |
| Db | char(64) binary | | PRI | | |
| User | char(16) binary | | PRI | | |
| Select_priv | enum('N','Y') | | | N | |
| Insert_priv | enum('N','Y') | | | N | |
| Update_priv | enum('N','Y') | | | N | |
| Delete_priv | enum('N','Y') | | | N | |
| Create_priv | enum('N','Y') | | | N | |
| Drop_priv | enum('N','Y') | | | N | |
| Grant_priv | enum('N','Y') | | | N | |
| References_priv | enum('N','Y') | | | N | |
| Index_priv | enum('N','Y') | | | N | |
| Alter_priv | enum('N','Y') | | | N | |
+-----------------+-----------------+------+-----+---------+-------+
13 rows in set (0.36 sec)DATE that will be useful to everyday users. You
could store a DATE type in a more basic numeric
type, but having a type specifically dedicated to the nuances of date
processing adds to SQL's ease of use—one of
SQL's primary goals.
|
Data type
|
Description
|
|---|---|
INT
|
An integer value. MySQL allows an
INT to be either
signed or unsigned.
|
REAL
|
A floating-point value. This type offers a greater range and more
precision than the
INT type, but it does not have
the exactness of an INT.
|
CHAR
(length)
|
A fixed-length character value. No
CHAR fields can
hold strings greater in length than the specified value. Fields of
lesser length are padded with spaces. This type is the most commonly
used in any SQL implementation.
|
VARCHAR
(length)
|
CREATE INDEX index_name ON tablename (column1, column2, ..., columnN)
CREATE TABLE material (id INT NOT NULL,
name CHAR(50) NOT NULL,
resistance INT,
melting_pt REAL,
INDEX index1 (id, name),
UNIQUE INDEX index2 (name))index1—consists of both
the id and name fields. The
second index includes only the name field and
specifies that values for the name field must
always be unique. If you try to insert a field with a
name held by a row already in the database, the
insert will fail. Generally, you should declare all fields in a
unique index as NOT NULL
.
name by
itself, we did not create an index for just id. If
we did want such an index, we would not need to create it—it is
already there. When an index contains more than one column (for
example: name, rank, and
serial_number), MySQL reads the columns in order
from left to right. Because of the structure of the index MySQL uses,
any subset of the columns from left to right are automatically
created as indexes within the
"main" index. For example,
INSERT syntax:
INSERT INTO table_name (column1, column2, ..., columnN) VALUES (value1, value2, ..., valueN)
INSERT INTO addresses (name, address, city, state, phone, age)
VALUES('Irving Forbush', '123 Mockingbird Lane', 'Corbin', 'KY',
'(800) 555-1234', 26)\, by default—enables you to
escape single quotes and other literal instances of the escape
character:
# Insert info for the directory Stacie's Directory which
# is in c:\Personal\Stacie
INSERT INTO files (description, location)
VALUES ('Stacie\'s Directory', 'C:\\Personal\\Stacie')CREATE
call. If you want to use the default values for a column, however,
you must specify the names of the columns for which you intend to
insert nondefault data. For example, if the earlier
files table had contained a column called
size, the default value would be used for
Stacie's Directory. MySQL
allows you to specify a custom default value in the
table's CREATE call. If you do
not have a default value set up for a column, and that column is
NOT NULL, you must include that column in the
INSERT statement with a
non-NULL value.
INSERT call for inserting multiple rows at once:
SELECT
, enables you to view the data in the
database. This action is by far the most common action performed in
SQL. While data entry and modifications do happen on occasion, most
databases spend the vast majority of their lives serving up data for
reading. The general form of the SELECT statement
is as follows:
SELECT column1, column2, ..., columnN FROM table1, table2, ..., tableN [WHERE clause]
SELECT statement enables you
to identify the columns you want from one or more tables. The
WHERE clause identifies the rows with the data you
seek.
SELECT
syntax in Chapter 15.) The simplest form
is:
SELECT 1;
mysql> SELECT DATABASE( );
+------------+
| DATABASE( ) |
+------------+
| test |
+------------+
1 row in set (0.01 sec)DATABASE(
)
is a MySQL function that returns the
name of the current database. (We will cover functions in more detail
later in the chapter.) Nevertheless, you can see how simple SQL can
provide a quick-and-dirty way of finding out important information.
SELECT statement enumerates the
columns you wish to retrieve. You may specify a *
to say that you want to select all columns. The
FROM
clause specifies which tables those
columns come from. The
WHERE
clause identifies the specific rows to be
used and enables you to specify how to join two tables.
= operator for the obvious task
of verifying that two values in a WHERE clause
equal one another. Other fairly basic operations include
<>, >,
<, <=, and
>=. Note that MySQL allows you to use either
<> or != for
"not equal." Table 3-6 contains a full set of simple SQL operators.
|
Operator
|
Context
|
Description
|
|---|---|---|
+
|
Arithmetic
|
Addition
|
-
|
Arithmetic
|
Subtraction
|
*
|
Arithmetic
|
Multiplication
|
/
|
Arithmetic
|
Division
|
=
|
FULLTEXT
index. It specifically enables you to
do something like:
INSERT INTO Document (url, page_text )
VALUES ('index.html', 'The page contents.');
SELECT url FROM Document WHERE MATCH ( page_text ) AGAINST ('page');INSERT adds a row to a Document
table containing the URL of a web page and its text content.
SELECT then looks for the URLs of all documents
with the word page embedded in their text.
FULLTEXT index. The
CREATE
statement for the
Document table might look like this:
CREATE TABLE Document (
url VARCHAR(255) NOT NULL PRIMARY KEY,
page_text TEXT NOT NULL,
FULLTEXT ( page_text )
);FULLTEXT index enables you to search the index
using words or phrases that will not match exactly and then weigh the
relevance of any matches. As with other indexes, you can create
multicolumn FULLTEXT indexes:
CREATE TABLE Document (
url VARCHAR(255) NOT NULL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
page_text TEXT NOT NULL,
FULLTEXT ( title, page_text )
);title and page_text together
with this table, but you cannot look for words that exist only in the
title unless you create a separate FULLTEXT index
on it alone. Your combined search will look like the
following: