<<<back

MySQLについて 参

データベースの作成と削除
 準備が整ったところで、新たにデータベースを作成してみましょう。
 データベースはcreateコマンドで作成します。

create database web_db;

と入力すると、web_dbという名前のデータベースが作成されます。
 ちなみに、MySQLでデータベース名やテーブル名やフィールド名として使用できる文字は以下のようなものです。
 先頭に数字や記号は使わない方が良いです。
  • 英数字
  • _(アンダーバー)
  • $(ダラーマーク)
 作成後、早速、確認してみましょう。
 また、できたてのデータベースは空ですので

show tables from web_db;

と入力してもEmpty setと返されます。

mysql> create database web_db;[Enter Key]
Query OK, 1 rows affected (0.00 sec)

mysql> show databases;[Enter Key]

+----------+
| Database |
+----------+
| mysql    |
| test     |
| web_db   |
+----------+
3 rows in set (0.00 sec)

mysql> show tables from web_db;[Enter Key]
Empty set (0.00 sec)

mysql>

 一方、データベースを削除するにはdropコマンドを使います。
 デフォルトで用意されているtestデータベースはとくに必要ないので捨てておきましょう。

drop database test;

と入力するとtestデータベースは削除されます。

mysql> drop database test;[Enter Key]
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;[Enter Key]

+----------+
| Database |
+----------+
| mysql    |
| web_db   |
+----------+
2 rows in set (0.00 sec)

テーブルの作成と削除
 次に、空のデータベースの中にテーブルを作成します。
 テーブルを作成する際には、フィールドの定義をしておかなければなりません。
 ここでは、以下のような10のフィールドを持つ書誌データベース用のテーブルを設計しました。

フィールド名 データ型 備考
id int ユニークなID, 主キー, 必須
title varchar(100) タイトル, 必須
author varchar(100) 著者名
publisher varchar(50) 出版者
py int(4) 出版年
page varchar(50) ページ数
isbn bigint 標準番号
price varchar(50) 価格
series varchar(50) シリーズ名
note text 注記

 int型は4バイト整数、bigintは8バイト整数、varchar型は最大255バイトまでの可変長文字列、text型は最大65535バイトまでの可変長文字列をそれぞれ格納できます。
 intやbigintなどの数値型の場合、バイト数を指定してもしなくても構いませんが、varchar型は必ずバイト数を指定しなければなりません。
 この他にも、MySQLには様々なデータ型があります。
 主要なデータ型を以下に示します。

データ型 内容 データ範囲
int 4バイト整数 -2147483648〜2147483647
int unsigned 4バイト整数 0〜4294967295
bigint 8バイト整数 -9223372036854775808〜
9223372036854775807
float 不動小数点数 -3.402823466E+38〜
-1.175494351E-38
double 倍精度不動小数点数 -1.7976931348623157E+308〜
-2.2250738585072014E-308
char( ) 固定長文字列 最大長255バイト
varchar( ) 可変長文字列 最大長255バイト
text 可変長文字列 最大長65535バイト
date 日付 1000-1-1〜9999-12-31
enum(' ', ' ', ...) 列挙 最大65535個の固有値
set(' ', ' ', ...) リスト 最大64個の要素

 さて、これまではテーブルを操作する際、mysql.userなどとデータベース名とともに指定していましたが、useコマンドを使って、予めデータベースを選択しておくと、データベース名を逐一指定することなく、直接テーブル名を入力するだけでOKです。
 ここではweb_dbデータベースを使いますので、

use web_db;

と入力します。
 すると、Database changedと表示されます。

mysql> use web_db;[Enter Key]
Database changed
mysql>

 テーブルを作成するのもcreateコマンドです。
 基本となる構文は、

create table テーブル名 (フィールド名 データ型 オプション);

です。
 上述のテーブルを作成するには
create table web_tb (id int , title varchar(100), author varchar(100), publisher varchar(50), py int(4), page varchar(50), isbn bigint, price varchar(50), series varchar(50), note text);

と入力します。

mysql> create table web_tb (id int , title varchar(100), author varchar(100), publisher varchar(50), py int(4), page varchar(50), isbn bigint, price varchar(50), series varchar(50), note text);[Enter Key]
Query OK, 0 rows affected (0.00 sec)

mysql>

 showコマンドでweb_tbテーブルの情報を見ると以下のようになっています。

mysql> show fields from web_tb[Enter Key]
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id        | int(11)      | YES  |     | NULL    |       |
| title     | varchar(100) | YES  |     | NULL    |       |
| author    | varchar(100) | YES  |     | NULL    |       |
| publisher | varchar(50)  | YES  |     | NULL    |       |
| py        | int(4)       | YES  |     | NULL    |       |
| page      | varchar(50)  | YES  |     | NULL    |       |
| isbn      | bigint(20)   | YES  |     | NULL    |       |
| price     | varchar(50)  | YES  |     | NULL    |       |
| series    | varchar(50)  | YES  |     | NULL    |       |
| note      | text         | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

 設計の段階では、idフィールドとtitleフィールドは必須とされていますが、いまのところ、Nullの欄がYESのままになっている(すなわち、null値を許容している)ことが分かります。
 必須のフィールドにする場合は、オプションでnot nullを指定しなければなりません。
 テーブルの情報に変更を加える場合は、alterコマンドを使用します。
 基本構文は、

alter table テーブル名 change 変更を加えるフィールド名 新規フィールド名 新規データ型 新規オプション;

となります。
 この場合、

alter table web_tb change id id int not null;

ならびに

alter table web_tb change title title varchar(100) not null;

と入力します。

mysql> alter table web_tb change id id int not null;[Enter Key]
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table web_tb change title title varchar(100) not null;[Enter Key]
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql>

 さらに、idフィールドは資料ごとにユニークな番号をとりますので、いちいち入力しなくても、自動的にふってもらった方が楽です。
 そういった場合のためにMySQLでは、auto_incrementというオプションがあります。
 このオプションを指定すると、自動的に番号をふってくれるようになります。
 但し、auto_incrementを指定することができるのは1テーブルに1フィールドだけであり、当然ながら、データ型は数値型でなければなりません。
 さらに、そのフィールドを主キー(primary key)に設定しておく必要があります。
 主キーとは、端的に言えば、他のテーブルとの窓口になるようなフィールドであり、その中では、データの繰り返しやnull値は許されません。
 そこで、auto_incrementオプションを指定する前に、idフィールドを主キーに設定しておかなければなりません。
 この場合も同様に、alterコマンドを使用して、

alter table web_tb add primary key(id);

と入力します。

mysql> alter table web_tb add primary key(id);[Enter Key]
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql>

 さらに、今度はidフィールドにauto_incrementオプションを設定します。

mysql> alter table web_tb change id id int not null auto_increment;[Enter Key]
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql>

 以上の変更を一気に行うには、

alter table change web_tb id id int not null auto_increment primary key;

とします。

mysql> alter table change web_tb id id int not null auto_increment primary key;[Enter Key]
Query OK, 0 rows affected (0.00 sec)

mysql>

 また、主キー、auto_increment、not nullなどの設定をcreate tableの際にやってしまうには、以下のようにすればOKです。

mysql> create table web_tb (id int not null auto_increment primary key, title varchar(100) not null, author varchar(100), publisher varchar(50), py int(4), page varchar(50), isbn bigint, price varchar(50), series varchar(50), note text);[Enter Key]
Query OK, 0 rows affected (0.00 sec)

mysql>

 ここで再びshowコマンドでweb_tbテーブルのフィールド情報を見てみましょう。
 idフィールドとtitleフィールドの設定が先程と変わっていることが分かります。

mysql> show fields from web_tb[Enter Key]
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      |      | PRI | NULL    | auto_increment |
| title     | varchar(100) |      |     |         |                |
| author    | varchar(100) | YES  |     | NULL    |                |
| publisher | varchar(50)  | YES  |     | NULL    |                |
| py        | int(4)       | YES  |     | NULL    |                |
| page      | varchar(50)  | YES  |     | NULL    |                |
| isbn      | bigint(20)   | YES  |     | NULL    |                |
| price     | varchar(50)  | YES  |     | NULL    |                |
| series    | varchar(50)  | YES  |     | NULL    |                |
| note      | text         | YES  |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

 現在はDefault値がNULLになっていますが、Default値を予め規定したければ、create tableやalter tableの際に、オプションで、

default デフォルト値 とすればOKです。
 テーブルの削除の際は、やはりdropコマンドを使います。

drop table web_tb;

と入力すればweb_tbテーブルは削除されます。

mysql> drop table web_tb;[Enter Key]
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;[Enter Key]
Empty set (0.00 sec)

mysql>

 一方、フィールドを削除する際は、

alter table テーブル名 drop フィールド名;

とします。
 テーブルの作成・変更・削除に関わる基本構文を以下に示しておきます。

操作内容 構文
テーブルの作成 create table [table] ([field] [type] [option]);
テーブルの削除 drop table [table];
テーブル名の変更 alter table [table(old)] rename to [table(new)];
フィールドの追加 alter table [table] add [field] [type] [option];
フィールドの削除 alter table [table] drop [field];
フィールドの変更 alter table [table] change [field(old)] [field(new)] [type] [option];
主キーの設定 alter table [table] add primary key([field]);
主キーの削除 alter table [table] drop primary key([field]);