logは/var/lib/mysqlのmysql-bin.xxxxにあるがバイナリなためツールを使用する
# mysqlbinlog mysql-bin.000002
#080124 18:05:01 server id 2 end_log_pos 127 Query thread_id=11 exec_time=0 error_code=0 SET TIMESTAMP=1201165501; INSERT INTO `foo` VALUES (1,'Okada'),(2,'Okumura'),(3,'Yamada');
のようなログが見えいる
mysqlbinlog: unknown variable 'default-character-set=utf8' のようなエラーが出たらmy.cnfの
[client] #default-character-set = utf8 <=コメントアウトする
ログファイルの種類
ログの種類 | オプション | デフォルトのファイル名 | 設定ファイルの出力ファイル名指定 | 内容 |
エラーログ | --log-error | ホスト名.err | log-error=ファイル名 | MySQLサーバー起動時、稼動時、停止時のエラーやメッセージ |
一般クエリーログ | --log | ホスト名.log | log=ファイル名 | 接続や実行クエリーについての情報 |
スロークエリーログ | --log-slow-queries | ホスト名-slow.log | log-slow-queries=ファイル名 | long_query_timeで指定の秒数を超えるクエリー |
バイナリログ | --log-bin | ホスト名-bin.数値 | log-bin=ファイル名 | 更新を伴うクエリーが記録され、バックアップやレプリケーションにしようされる |
InnoDBのログ | デフォルトで生成 | ib_logfile数値 | - | InnoDBのログファイル |
テーブル名の変更
ALTER TABLE 古いテーブル名 RENAME 新しいテーブル名;
mysql> ALTER TABLE ism_test RENAME ism_test2; Query OK, 0 rows affected (0.00 sec)
テーブルの複製
create table 新しいテーブル名 as select * from 元のテーブル名;
mysql> create table ism_test as select * from ism_test2; Query OK, 1 row affected (0.03 sec) Records: 1 Duplicates: 0 Warnings: 0
必要なレコードだけ残しコピーすることも可
create table 新しいテーブル名 as select * from 元のテーブル名 where (条件);
入れ子表現
入れ子は()で囲む
テーブルism_shouhinのすべてのIDフィールドの値と等しい、テーブルism_uriageのIDフィールドを抽出
mysql> select * from ism_uriage where ID in (select ID from ism_shouhin); +----+---------------------------+------+-----------+-----------+------+ | ID | date | uid | shouhinid | uriagesuu | etc | +----+---------------------------+------+-----------+-----------+------+ | 1 | 2008/09/27 (Sat) 23:41:05 | 3 | 14 | 2 | NULL | | 2 | 2008/09/27 (Sat) 23:41:05 | 3 | 16 | 4 | NULL | | 3 | 2008/09/27 (Sat) 23:41:05 | 3 | 18 | 6 | NULL | | 5 | 2008/09/27 (Sat) 23:45:02 | 3 | 11 | 1 | NULL | (略) | 33 | 2008/09/28 (Sun) 01:48:04 | 2 | 16 | 4 | NULL | | 34 | 2008/09/28 (Sun) 01:50:02 | 2 | 20 | 2 | NULL | | 35 | 2008/09/28 (Sun) 01:50:35 | 2 | 23 | 2 | NULL | +----+---------------------------+------+-----------+-----------+------+ 32 rows in set (0.01 sec)
フィールドの合計を別名で表示
mysql> select uid, uriagesuu, uid+uriagesuu as sum from ism_uriage where shouhinid=9; +------+-----------+------+ | uid | uriagesuu | sum | +------+-----------+------+ | 3 | 2 | 5 | | 2 | 2 | 4 | | 2 | 4 | 6 |
フィールドの文字の連結
mysql> select uid, uriagesuu, concat(uid, uriagesuu) as gousei from ism_uriage where shouhinid=9; +------+-----------+--------+ | uid | uriagesuu | gousei | +------+-----------+--------+ | 3 | 2 | 32 | | 2 | 2 | 22 | | 2 | 4 | 24 |
数値比較して結果を表示
mysql> select uid, uriagesuu, strcmp(uid, uriagesuu) as hikaku from ism_uriage where shouhinid=9; +------+-----------+--------+ | uid | uriagesuu | hikaku | +------+-----------+--------+ | 3 | 2 | 1 | | 2 | 2 | 0 | | 2 | 4 | -1 | | 2 | 4 | -1 | | 2 | 4 | -1 |
select in
mysql> select * from ism_shouhin where ID in (10,12,15); +------+---------------+------+--------+------+----------+------+ | ID | name | grp | kakaku | url | status | etc | +------+---------------+------+--------+------+----------+------+ | 10 | ビール 500ml | 201 | 360 | | | | | 12 | 発泡酒 500ml | 201 | 164 | | | | | 15 | ワイン (ロゼ) | 201 | 680 | | 販売終了 | | +------+---------------+------+--------+------+----------+------+ 3 rows in set (0.00 sec)
否定形 [not in]
mysql> select * from ism_shouhin where ID not in (10,12,15); +------+-------------------+------+--------+-----------------+------ | ID | name | grp | kakaku | url | status | etc | +------+-------------------+------+--------+-----+-----------+------ | 9 | ビール 350ml | 201 | 250 | | | | | 11 | 発泡酒 350ml | 201 | 134 | | | | | 13 | 焼酎 (芋) | 201 | 1048 | | | | | 14 | 焼酎 (麦) | 201 | 1148 | | | | | 16 | ワイン (白) | 201 | 650 | | | | +------+-------------------+------+--------+-----+-----------+-------
mysql> select ID,name as 名前,kakaku as 単価 from ism_shouhin where ID in(10,12,15); +------+---------------+------+ | ID | 名前 | 単価 | +------+---------------+------+ | 10 | ビール 500ml | 360 | | 12 | 発泡酒 500ml | 164 | | 15 | ワイン (ロゼ) | 680 | +------+---------------+------+ 3 rows in set (0.00 sec)
select ism_uriage.* , ism_shouhin.* from ism_uriage join ism_shouhin on ism_uriage.shouhinid=ism_shouhin.ID where ism_uriage.uid=2 order by ism_uriage.ID desc limit 3; +-----+---------------------------+------+-----------+-----------+------+------+ ----------------------------+------+--------+----------------------------------- --------------------------------------------+--------+------+ | ID | date | uid | shouhinid | uriagesuu | etc | ID | name | grp | kakaku | url | status | etc | +-----+---------------------------+------+-----------+-----------+------+------+
----------------------------+------+--------+----------------------------------- --------------------------------------------+--------+------+ | 160 | 2008/10/02 (Thu) 15:52:20 | 2 | 1 | 1 | NULL | 1 | 冷蔵庫 AB-12345(H) | 101 | 45000 | | | | | 159 | 2008/10/02 (Thu) 15:52:20 | 2 | 18 | 4 | NULL | 18 | カクテルバー(レモン) | 201 | 121 | | | | | 158 | 2008/10/02 (Thu) 15:52:20 | 2 | 21 | 2 | NULL | 21 | コンパクトカメラ(Leica C2) | 301 | 37590 | | | | +-----+---------------------------+------+-----------+-----------+------+------+ ----------------------------+------+--------+----------------------------------- --------------------------------------------+--------+------+ 3 rows in set (0.01 sec)
mysql> DROP TABLE IF EXISTS ism_test; Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create table if not exists ism_test( ID int8, date varchar(64), etc varchar(64)); Query OK, 0 rows affected (0.02 sec)
型の変更(data)
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | bigint(20) | YES | | NULL | | | date | varchar(64) | YES | | NULL | | | etc | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
mysql> alter table ism_test change date date int8; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | bigint(20) | YES | | NULL | | | date | bigint(20) | YES | | NULL | | | etc | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
名前の変更(data)
mysql> alter table ism_test change date data varchar(64); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | bigint(20) | YES | | NULL | | | data | varchar(64) | YES | | NULL | | | etc | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
書式形式
alter table <テーブル名> change <フィールド名> <新フィールド名> <型>
MySQLでは、定義さている値より多い値を入れようとすると、許容範囲まで入る。
dataが4Byte
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | int(11) | YES | | NULL | | | data | varchar(4) | YES | | NULL | | | etc | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
dataに5byte入力してみると4Byteまで入る
mysql> insert into ism_test values(1,'12345','岡田'); Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from ism_test; +------+------+------+ | ID | data | etc | +------+------+------+ | 1 | 1234 | 岡田 | +------+------+------+ 1 row in set (0.00 sec)
フィールドの型を変更すると矛盾すると値がなくなる (varchar=>int型に変更した場合)
mysql> select * from ism_test; +------+------+------+ | ID | data | etc | +------+------+------+ | 1 | 1234 | 岡田 | +------+------+------+ 1 row in set (0.00 sec)
mysql> alter table ism_test change etc etc int4; Query OK, 1 row affected, 1 warning (0.00 sec) Records: 1 Duplicates: 0 Warnings: 1
mysql> select * from ism_test; +------+------+------+ | ID | data | etc | +------+------+------+ | 1 | 1234 | 0 | +------+------+------+ 1 row in set (0.00 sec)
追加
mysql> show fields from ism_test; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | ID | int(11) | YES | | NULL | | | data | bigint(20) | YES | | NULL | | | etc | int(11) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
mysql> ALTER table ism_test add etc2 varchar(64); Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | int(11) | YES | | NULL | | | data | bigint(20) | YES | | NULL | | | etc | int(11) | YES | | NULL | | | etc2 | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
削除
mysql> ALTER table ism_test drop etc2; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0
varchar(X) Xは255まで文字数を指定Byte数でない
etcをvarchar(4)に指定し、同じ5byteで4文字と5文字で比較
mysql> show fields from t_test; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | No | text | NO | | | | | data | varchar(128) | NO | | | | | etc | varchar(4) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
etcに'abあc'(5Byte,4文字)を入れると入る
mysql> UPDATE t_test SET etc = 'abあc' WHERE No = 2; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t_test; +----+------------+--------+ | No | data | etc | +----+------------+--------+ | 1 | ABCD | あいう | | 2 | あいうえお | abあc | +----+------------+--------+ 2 rows in set (0.00 sec)
etcに'abcde'(5Byte,5文字)を入れると入らない
mysql> UPDATE t_test SET etc = 'abcde' WHERE No = 2; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1
mysql> select * from t_test; +----+------------+--------+ | No | data | etc | +----+------------+--------+ | 1 | ABCD | あいう | | 2 | あいうえお | abcd | +----+------------+--------+ 2 rows in set (0.00 sec)