ENUM型のカラムを持つテーブルを作成する練習 MySQL free41
ENUM型のカラムを持つテーブルを作成する練習。 少しですが文法にも慣れてきたような気がします。 Friday,June,21,2013 SHOW DATABASES; USE sadachika; SHOW TABLES; Tables_in_sadachika sadachika test1 test2 test3 ○文字列のENUM型のカラムを持つテーブルを作成します。 mysql> create table test4(t text, b blob); *一般的なテイブルの作り方 今回は使いません。文法を忘れそうなのでメモした。 mysql> create table test4(col ENUM('one','two','three','four','five','red','blue','yellow','sadachika','ten','eleven','twelve','定近','twenty nine')); ○「test3」テーブルの構造を見る。 カラムの設定が見れます。桁数、NULL値が無い場合の表示方法など mysql> describe test4; mysql> show columns from test4; 同じ結果 Field Type Null Key Default Extra col enum('one','two','three','four','five','red','blue','yellow','sadachika','ten','eleven','twelve','定近','twenty nine') YES NULL *このように長く連なっているのがリストです。これら以外の文字は入力できない。('one','two','three'〜〜〜〜 ○作成したテーブルの定義を確認してみる。 mysql> show create table test4\G *********** 1. row ************** *このように長く連なっているのがリストです。これら以外の文字は入力できない。('one','two','three'〜〜〜〜 Table: test4 *ENUM型のカラムを持つテーブル Create Table: CREATE TABLE 'test4'( 'col' enum('one','two','three','four','five','red','blue','yellow','sadachika','ten','eleven','twelve','定近','twenty nine') DEFAULT NULL *カムラの形に文字コードで一応理解しておくことに。 )ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ○インデックスが自動的に付与されています。 one インデックス 1 *インデックス番号は自動的に割り振られる two インデックス 2 *インデックス番号は自動的に割り振られる three インデックス 3 *インデックス番号は自動的に割り振られる four インデックス 4 *インデックス番号は自動的に割り振られる five インデックス 5 *インデックス番号は自動的に割り振られる red インデックス 6 *インデックス番号は自動的に割り振られる blue インデックス 7 *インデックス番号は自動的に割り振られる yellow インデックス 8 *インデックス番号は自動的に割り振られる sadachika インデックス 9 *インデックス番号は自動的に割り振られる ten インデックス 10 *インデックス番号は自動的に割り振られる eleven インデックス 11 *インデックス番号は自動的に割り振られる twelve インデックス 12 *インデックス番号は自動的に割り振られる 定近 インデックス 13 *インデックス番号は自動的に割り振られる twenty nine インデックス 14 *インデックス番号は自動的に割り振られる[29トゥエンティー ナイン] ○入力してみます。 mysql> insert into test4(col) values(13); *インデックス数字を入力しました。シングルクォーテイション無し mysql> insert into test4(col) values('13'); *数字をシングルクォーテイションでくくってもインデックスを認識している mysql> insert into test4(col) values(定近); *入力に失敗 なぜか漢字が打ち込めませんね。 mysql> insert into test4(col) values('one'); * mysql> insert into test4(col) values('two'); * mysql> insert into test4(col) values(2); *インデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values('three'); * mysql> insert into test4(col) values('three'); * mysql> insert into test4(col) values(3); *インデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values('four'); * mysql> insert into test4(col) values('four'); * mysql> insert into test4(col) values('four'); * mysql> insert into test4(col) values(4); *インデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values('five'); * mysql> insert into test4(col) values('Five'); * mysql> insert into test4(col) values('five '); *後ろ側に半角スペースを入力 mysql> insert into test4(col) values('five'); * mysql> insert into test4(col) values(5); *自動的に割り振られるインデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values('red'); * mysql> insert into test4(col) values('blue'); * mysql> insert into test4(col) values('yellow'); * mysql> insert into test4(col) values(6); *インデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values(7); *インデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values(8); *インデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values(null); *データ無しがを入力 mysql> insert into test4(col) values(null); *データ無しがを入力 mysql> insert into test4(col) values(null); *データ無しがを入力 mysql> insert into test4(col) values('sadachika'); *小文字アルファベット mysql> insert into test4(col) values('SADACHIKA'); *大文字アルファベット mysql> insert into test4(col) values('Sadachika'); *頭が大文字 mysql> insert into test4(col) values('Sadachika '); *後ろ側に半角スペースを入力 頭が大文字 mysql> insert into test4(col) values(9); *自動的に割り振られるインデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values('ten'); * mysql> insert into test4(col) values('ten '); *後ろ側に半角スペースを入力 mysql> insert into test4(col) values(' ten '); *入力に失敗 前後に半角スペースは設けれないようです mysql> insert into test4(col) values(10); *インデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values(11); *インデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values(12); *インデックス数字を入力。シングルクォーテイション無し mysql> insert into test4(col) values('twenty nine'); *文字と文字の半角の空白は認識できる[29トゥエンティー ナイン] mysql> insert into test4(col) values(13); *インデックス数字を入力。 [29トゥエンティー ナイン] mysql> insert into test4(col) values(14); *インデックス数字を入力。シングルクォーテイション無し ○リストに含まれていない値を格納するテスト mysql> insert into test4(col) values('black'); *リストにあがっているもの以外は入力できません。入力に失敗 mysql> insert into test4(col) values('thirty'); *入力に失敗失敗 リストにあがっているもの以外は入力できません。 サーティー mysql> insert into test4(col) values('twenty'); *入力に失敗失敗 リストにあがっているもの以外は入力できません。 トゥエンティー ○テーブルの中身の表示 mysql> select * from test4; 定近 定近 *数字をシングルクォーテイションでくくってもインデックスを認識している。 one two two three three three four four four four five five five five *後ろ側の半角スペースはいかされない。 five red blue yellow red blue yellow NULL NULL NULL sadachika *大文字小文字半角スペースはいかされない sadachika sadachika sadachika sadachika ten ten *後ろ側の半角スペースはいかされない。 ten eleven twelve twenty nine *文字と文字の半角の空白は認識できる 定近 twenty nine ○テーブルの中身の表示 プログラムの文法テスト mysql> select t, b from test2; *文法を忘れるので書きました。今回は使いません。 mysql> select col from test4; 定近 定近 *数字をシングルクォーテイションでくくってもインデックスを認識している。 one two two three three three four four four four five five five five *後ろ側の半角スペースはいかされない five red blue yellow red blue yellow NULL NULL NULL sadachika *大文字小文字半角スペースはいかされない sadachika sadachika sadachika sadachika ten ten *後ろ側の半角スペースはいかされない ten eleven twelve twenty nine *文字と文字の半角の空白は認識できる 定近 twenty nine ○テーブルの中身の表示 プログラムの文法テスト mysql> select concat('(',t,')'), concat('(',b,')')from test2; *文法を忘れるので書きました。今回は使いません。 mysql> select concat('(',col,')')from test4; (定近) (定近) *数字をシングルクォーテイションでくくってもインデックスを認識している。 (one) (two) (two) (three) (three) (three) (four) (four) (four) (four) (five) (five) (five) (five) *後ろ側の半角スペースはいかされない。 (five) (red) (blue) (yellow) (red) (blue) (yellow) (NULL) (NULL) (NULL) (sadachika) *大文字小文字半角スペースはいかされない (sadachika) (sadachika) (sadachika) (sadachika) (ten) (ten) *後ろ側の半角スペースはいかされない。 (ten) (eleven) (twelve) (twenty nine) *文字と文字の半角の空白は認識できる (定近) (twenty nine) ○テーブルの中身の検索? mysql> select concat('(',col,')')from test4 where col=1; concat('(',col,')') (one) mysql> select concat('(',col,')')from test4 where col=2; concat('(',col,')') (two) (two) mysql> select concat('(',col,')')from test4 where col=3; concat('(',col,')') (three) (three) (three) mysql> select concat('(',col,')')from test4 where col=4; concat('(',col,')') (four) (four) (four) (four) mysql> select concat('(',col,')')from test4 where col=5; concat('(',col,')') (five) (five) (five) (five) (five) mysql> select concat('(',col,')')from test4 where col='five'; concat('(',col,')') (five) (five) (five) (five) (five) mysql> select concat('(',col,')')from test4 where col=6; concat('(',col,')') (red) (red) mysql> select concat('(',col,')')from test4 where col=7; concat('(',col,')') (blue) (blue) mysql> select concat('(',col,')')from test4 where col=8; concat('(',col,')') (yellow) (yellow) mysql> select concat('(',col,')')from test4 where col=9; concat('(',col,')') (sadachika) *大文字小文字半角スペースはいかされない (sadachika) (sadachika) (sadachika) (sadachika) mysql> select concat('(',col,')')from test4 where col=10; concat('(',col,')') (ten) (ten) (ten) mysql> select concat('(',col,')')from test4 where col=11; concat('(',col,')') (eleven) mysql> select concat('(',col,')')from test4 where col=12; concat('(',col,')') (twelve) mysql> select concat('(',col,')')from test4 where col=13; concat('(',col,')') (定近) (定近) (定近) mysql> select concat('(',col,')')from test4 where col='定近'; concat('(',col,')') (定近) (定近) *数字をシングルクォーテイションでくくってもインデックスを認識している。 (定近) mysql> select concat('(',col,')')from test4 where col=14; concat('(',col,')') (twenty nine) *文字と文字の半角の空白は認識できる (twenty nine) ○テーブルの中身を表示 mysql> SELECT * FROM test4 WHERE col = 'one'; col one mysql> SELECT * FROM test4 WHERE col = 1; col one mysql> SELECT * FROM test4 WHERE col = 'two'; col two two mysql> SELECT * FROM test4 WHERE col = 2; col two two mysql> SELECT * FROM test4 WHERE col = 'three'; col three three three mysql> SELECT * FROM test4 WHERE col = 3; col three three three mysql> SELECT * FROM test4 WHERE col = 'four'; col four four four four mysql> SELECT * FROM test4 WHERE col = 4; col four four four four mysql> SELECT * FROM test4 WHERE col = 'five'; col five five five five five mysql> SELECT * FROM test4 WHERE col = 5; col five five five five five mysql> SELECT * FROM test4 WHERE col = 6; col red red mysql> SELECT * FROM test4 WHERE col = 7; col blue blue mysql> SELECT * FROM test4 WHERE col = 8; col yellow yellow mysql> SELECT * FROM test4 WHERE col = 9; col sadachika sadachika sadachika sadachika sadachika mysql> SELECT * FROM test4 WHERE col = 10; col ten ten *後ろ側の半角スペースはいかされない。 ten mysql> SELECT * FROM test4 WHERE col = 11; col eleven mysql> SELECT * FROM test4 WHERE col = 12; col twelve mysql> SELECT * FROM test4 WHERE col = 13; col 定近 定近 *数字をシングルクォーテイションでくくってもインデックスを認識している。 定近 mysql> SELECT * FROM test4 WHERE col = 14; col twenty nine *文字と文字の半角の空白は認識できる twenty nine mysql> SELECT * FROM test4 WHERE col = NULL; Empty set (0.00 sec) ○サーバのENUM型のカラムを持つテーブルに格納するデータの形を表示。 mysql> select hex(t), hex(b) from test2; *文法を忘れるので書きました。今回は使いません。 mysql> select hex(col) from test4; hex(col) E5AE9AE8BF91 E5AE9AE8BF91 6F6E65 74776F 74776F 7468726565 7468726565 7468726565 666F7572 666F7572 666F7572 666F7572 66697665 66697665 66697665 66697665 66697665 726564 626C7565 79656C6C6F77 726564 626C7565 79656C6C6F77 NULL NULL NULL 736164616368696B61 736164616368696B61 736164616368696B61 736164616368696B61 736164616368696B61 74656E 74656E 74656E 656C6576656E 7477656C7665 7477656E7479206E696E65 E5AE9AE8BF91 7477656E7479206E696E65 ○テーブルの追加を確認する SHOW TABLES; Tables_in_sadachika sadachika test1 test2 test3 test4 追加されたね
* 参考にしたサイトは→ こちら です。
これは赤色の文字例です。
これは青色の文字例です。