Linux Memo/MySQL文法
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
*MySQL文法 [#i8c60410]
RIGHT:更新日&lastmod();
**アクセスlog情報 [#s3abe148]
logは/var/lib/mysqlのmysql-bin.xxxxにあるがバイナリなため...
# mysqlbinlog mysql-bin.000002
#080124 18:05:01 server id 2 end_log_pos 127 Query ...
exec_time=0 error_code=0
SET TIMESTAMP=1201165501;
INSERT INTO `foo` VALUES (1,'Okada'),(2,'Okumura'),(3,'Y...
のようなログが見えいる
mysqlbinlog: unknown variable 'default-character-set=utf8'
のようなエラーが出たらmy.cnfの
[client]
#default-character-set = utf8 <=コメントアウトする
''ログファイルの種類''
|ログの種類 |オプション |デフォルトのファイル名 |設定ファ...
|エラーログ |--log-error |ホスト名.err |log-error=ファイ...
|一般クエリーログ |--log |ホスト名.log |log=ファイル名 |...
|スロークエリーログ |--log-slow-queries |ホスト名-slow.lo...
|バイナリログ |--log-bin |ホスト名-bin.数値 |log-bin=ファ...
|InnoDBのログ |デフォルトで生成 |ib_logfile数値 |- |InnoD...
**テーブル名の変更・複製 [#y94408d0]
''テーブル名の変更''
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 元のテーブ...
**Select文いろいろ [#a4c33429]
''重複キーを一度だけ取り出す方法''
distinct key1のように指定するとkey1が重複しているときは1...
mysql> select distinct ktaiid from t_gps;
''レコードを指定して取得''
はじめのレコードから3レコード取得
mysql> select * from ism_uriage limit 0,3;
3レコード目から4レコード取得
mysql> select * from ism_uriage limit 2,4;
''入れ子表現''
入れ子は()で囲む
テーブルism_shouhinのすべてのIDフィールドの値と等しい、テ...
mysql> select * from ism_uriage where ID in (select ID f...
+----+---------------------------+------+-----------+---...
| ID | date | uid | shouhinid | ur...
+----+---------------------------+------+-----------+---...
| 1 | 2008/09/27 (Sat) 23:41:05 | 3 | 14 | ...
| 2 | 2008/09/27 (Sat) 23:41:05 | 3 | 16 | ...
| 3 | 2008/09/27 (Sat) 23:41:05 | 3 | 18 | ...
| 5 | 2008/09/27 (Sat) 23:45:02 | 3 | 11 | ...
(略)
| 33 | 2008/09/28 (Sun) 01:48:04 | 2 | 16 | ...
| 34 | 2008/09/28 (Sun) 01:50:02 | 2 | 20 | ...
| 35 | 2008/09/28 (Sun) 01:50:35 | 2 | 23 | ...
+----+---------------------------+------+-----------+---...
32 rows in set (0.01 sec)
''フィールドの合計を別名で表示''
mysql> select uid, uriagesuu, uid+uriagesuu as sum from ...
+------+-----------+------+
| uid | uriagesuu | sum |
+------+-----------+------+
| 3 | 2 | 5 |
| 2 | 2 | 4 |
| 2 | 4 | 6 |
''フィールドの文字の連結''
mysql> select uid, uriagesuu, concat(uid, uriagesuu) as ...
+------+-----------+--------+
| uid | uriagesuu | gousei |
+------+-----------+--------+
| 3 | 2 | 32 |
| 2 | 2 | 22 |
| 2 | 4 | 24 |
''数値比較して結果を表示''
mysql> select uid, uriagesuu, strcmp(uid, uriagesuu) as ...
+------+-----------+--------+
| 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 ...
+------+---------------+------+--------+------+---------...
| 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,...
+------+-------------------+------+--------+------------...
| ID | name | grp | kakaku | url | statu...
+------+-------------------+------+--------+-----+------...
| 9 | ビール 350ml | 201 | 250 | | ...
| 11 | 発泡酒 350ml | 201 | 134 | | ...
| 13 | 焼酎 (芋) | 201 | 1048 | | ...
| 14 | 焼酎 (麦) | 201 | 1148 | | ...
| 16 | ワイン (白) | 201 | 650 | | ...
+------+-------------------+------+--------+-----+------...
***テーブル名の一時変更 [#rc32bbe6]
mysql> select ID,name as 名前,kakaku as 単価 from ism_sh...
+------+---------------+------+
| ID | 名前 | 単価 |
+------+---------------+------+
| 10 | ビール 500ml | 360 |
| 12 | 発泡酒 500ml | 164 |
| 15 | ワイン (ロゼ) | 680 |
+------+---------------+------+
3 rows in set (0.00 sec)
***テーブル結合 [#c3927a8d]
select ism_uriage.* , ism_shouhin.* from ism_uriage join...
ism_uriage.shouhinid=ism_shouhin.ID where ism_uriage.uid...
ism_uriage.ID desc limit 3;
+-----+---------------------------+------+-----------+--...
----------------------------+------+--------+-----------...
--------------------------------------------+--------+--...
| ID | date | uid | shouhinid | u...
name | grp | kakaku | url | s...
+-----+---------------------------+------+-----------+--...
----------------------------+------+--------+-----------...
--------------------------------------------+--------+--...
| 160 | 2008/10/02 (Thu) 15:52:20 | 2 | 1 | ...
冷蔵庫 AB-12345(H) | 101 | 45000 | | ...
| 159 | 2008/10/02 (Thu) 15:52:20 | 2 | 18 | ...
カクテルバー(レモン) | 201 | 121 | | ...
| 158 | 2008/10/02 (Thu) 15:52:20 | 2 | 21 | ...
コンパクトカメラ(Leica C2) | 301 | 37590 | | ...
+-----+---------------------------+------+-----------+--...
----------------------------+------+--------+-----------...
--------------------------------------------+--------+--...
3 rows in set (0.01 sec)
***テーブル作成・削除時のテーブル存在の確認 [#f0e44c4d]
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, dat...
Query OK, 0 rows affected (0.02 sec)
***フィールド名および型の変更 [#k3a975c5]
''型の変更''(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 <フィールド名> <新フィー...
***入力されるデータの範囲越えのとき [#ae1c5409]
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)
&color(red){フィールドの型を変更すると矛盾すると値がなく...
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)
***フィールドの追加・削除 [#h116c978]
''追加''
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について [#uc99aae9]
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)
終了行:
*MySQL文法 [#i8c60410]
RIGHT:更新日&lastmod();
**アクセスlog情報 [#s3abe148]
logは/var/lib/mysqlのmysql-bin.xxxxにあるがバイナリなため...
# mysqlbinlog mysql-bin.000002
#080124 18:05:01 server id 2 end_log_pos 127 Query ...
exec_time=0 error_code=0
SET TIMESTAMP=1201165501;
INSERT INTO `foo` VALUES (1,'Okada'),(2,'Okumura'),(3,'Y...
のようなログが見えいる
mysqlbinlog: unknown variable 'default-character-set=utf8'
のようなエラーが出たらmy.cnfの
[client]
#default-character-set = utf8 <=コメントアウトする
''ログファイルの種類''
|ログの種類 |オプション |デフォルトのファイル名 |設定ファ...
|エラーログ |--log-error |ホスト名.err |log-error=ファイ...
|一般クエリーログ |--log |ホスト名.log |log=ファイル名 |...
|スロークエリーログ |--log-slow-queries |ホスト名-slow.lo...
|バイナリログ |--log-bin |ホスト名-bin.数値 |log-bin=ファ...
|InnoDBのログ |デフォルトで生成 |ib_logfile数値 |- |InnoD...
**テーブル名の変更・複製 [#y94408d0]
''テーブル名の変更''
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 元のテーブ...
**Select文いろいろ [#a4c33429]
''重複キーを一度だけ取り出す方法''
distinct key1のように指定するとkey1が重複しているときは1...
mysql> select distinct ktaiid from t_gps;
''レコードを指定して取得''
はじめのレコードから3レコード取得
mysql> select * from ism_uriage limit 0,3;
3レコード目から4レコード取得
mysql> select * from ism_uriage limit 2,4;
''入れ子表現''
入れ子は()で囲む
テーブルism_shouhinのすべてのIDフィールドの値と等しい、テ...
mysql> select * from ism_uriage where ID in (select ID f...
+----+---------------------------+------+-----------+---...
| ID | date | uid | shouhinid | ur...
+----+---------------------------+------+-----------+---...
| 1 | 2008/09/27 (Sat) 23:41:05 | 3 | 14 | ...
| 2 | 2008/09/27 (Sat) 23:41:05 | 3 | 16 | ...
| 3 | 2008/09/27 (Sat) 23:41:05 | 3 | 18 | ...
| 5 | 2008/09/27 (Sat) 23:45:02 | 3 | 11 | ...
(略)
| 33 | 2008/09/28 (Sun) 01:48:04 | 2 | 16 | ...
| 34 | 2008/09/28 (Sun) 01:50:02 | 2 | 20 | ...
| 35 | 2008/09/28 (Sun) 01:50:35 | 2 | 23 | ...
+----+---------------------------+------+-----------+---...
32 rows in set (0.01 sec)
''フィールドの合計を別名で表示''
mysql> select uid, uriagesuu, uid+uriagesuu as sum from ...
+------+-----------+------+
| uid | uriagesuu | sum |
+------+-----------+------+
| 3 | 2 | 5 |
| 2 | 2 | 4 |
| 2 | 4 | 6 |
''フィールドの文字の連結''
mysql> select uid, uriagesuu, concat(uid, uriagesuu) as ...
+------+-----------+--------+
| uid | uriagesuu | gousei |
+------+-----------+--------+
| 3 | 2 | 32 |
| 2 | 2 | 22 |
| 2 | 4 | 24 |
''数値比較して結果を表示''
mysql> select uid, uriagesuu, strcmp(uid, uriagesuu) as ...
+------+-----------+--------+
| 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 ...
+------+---------------+------+--------+------+---------...
| 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,...
+------+-------------------+------+--------+------------...
| ID | name | grp | kakaku | url | statu...
+------+-------------------+------+--------+-----+------...
| 9 | ビール 350ml | 201 | 250 | | ...
| 11 | 発泡酒 350ml | 201 | 134 | | ...
| 13 | 焼酎 (芋) | 201 | 1048 | | ...
| 14 | 焼酎 (麦) | 201 | 1148 | | ...
| 16 | ワイン (白) | 201 | 650 | | ...
+------+-------------------+------+--------+-----+------...
***テーブル名の一時変更 [#rc32bbe6]
mysql> select ID,name as 名前,kakaku as 単価 from ism_sh...
+------+---------------+------+
| ID | 名前 | 単価 |
+------+---------------+------+
| 10 | ビール 500ml | 360 |
| 12 | 発泡酒 500ml | 164 |
| 15 | ワイン (ロゼ) | 680 |
+------+---------------+------+
3 rows in set (0.00 sec)
***テーブル結合 [#c3927a8d]
select ism_uriage.* , ism_shouhin.* from ism_uriage join...
ism_uriage.shouhinid=ism_shouhin.ID where ism_uriage.uid...
ism_uriage.ID desc limit 3;
+-----+---------------------------+------+-----------+--...
----------------------------+------+--------+-----------...
--------------------------------------------+--------+--...
| ID | date | uid | shouhinid | u...
name | grp | kakaku | url | s...
+-----+---------------------------+------+-----------+--...
----------------------------+------+--------+-----------...
--------------------------------------------+--------+--...
| 160 | 2008/10/02 (Thu) 15:52:20 | 2 | 1 | ...
冷蔵庫 AB-12345(H) | 101 | 45000 | | ...
| 159 | 2008/10/02 (Thu) 15:52:20 | 2 | 18 | ...
カクテルバー(レモン) | 201 | 121 | | ...
| 158 | 2008/10/02 (Thu) 15:52:20 | 2 | 21 | ...
コンパクトカメラ(Leica C2) | 301 | 37590 | | ...
+-----+---------------------------+------+-----------+--...
----------------------------+------+--------+-----------...
--------------------------------------------+--------+--...
3 rows in set (0.01 sec)
***テーブル作成・削除時のテーブル存在の確認 [#f0e44c4d]
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, dat...
Query OK, 0 rows affected (0.02 sec)
***フィールド名および型の変更 [#k3a975c5]
''型の変更''(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 <フィールド名> <新フィー...
***入力されるデータの範囲越えのとき [#ae1c5409]
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)
&color(red){フィールドの型を変更すると矛盾すると値がなく...
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)
***フィールドの追加・削除 [#h116c978]
''追加''
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について [#uc99aae9]
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)
ページ名: