[[MySQL]]

~
*ウェブアプリケーション用DB作成の手順 [#z2c16d0c]
1.MySQLへログイン~
2.DBの作成~
3.ユーザーの追加~
4.ユーザーが追加されたか確認~
5.完了~

*MySQL基本操作 [#z255f36a]
***ログイン [#qafd35bc]
 # mysql -u root -p パスワード

***データベース作成 [#sb2e4472]
 mysql>create database データベース名 ;

***作成済みデータベース表示 [#i1b57cd9]
 mysql>show databases ;

***データベース削除 [#a51f35b3]
 mysql>drop datatbase test_db ;

***データベースの表示 [#a4ab5bb4]
 mysql>use データベース名;   <--データベースを指定
 mysql>show tables;       <--作成されているテーブル名を表示
 mysql>describe テーブル名;   <--テーブルにあるカラム名を表示

*テーブル操作 [#pb7c7955]
***テーブル作成 [#me8d298a]
 mysql> create table テーブル名(フィールド名01 タイプ,フィールド名02 タイプ);
 ※例
 mysql> create table test01(id text);

***テーブルの確認 [#u6de41e4]
 mysql> show tables

***テーブルの状態確認 [#ba9f3103]
 mysql> show table status;

*フィールド操作 [#aa28667b]
***フィールドの設定確認 [#w5a000af]
 mysql> show columns from テーブル名; 

***既存テーブルへフィールドの追加 [#fafe604b]
 mysql> alter table t_name add request_time text;

***指定したフィールドで並べ替え表示 [#occ3d48d]
 mysql> select * from t_name order by request_time;

***フィールドのデータ型を確認 [#g346b979]
 mysql> describe test01;
 +--------------+------+------+-----+---------+-------+
 | Field        | Type | Null | Key | Default | Extra |
 +--------------+------+------+-----+---------+-------+
 | id           | text | YES  |     | NULL    |       |
 | name         | text | YES  |     | NULL    |       |
 | request_time | text | YES  |     | NULL    |       |
 +--------------+------+------+-----+---------+-------+

***フィールドのデータ型を変更 [#k5c3f499]
 mysql> alter table t_name change column request_time request_time char(28); <-- change column 既存フィールド名 新フィールド名 データ型
 Query OK, 13 rows affected, 13 warnings (0.02 sec)
 Records: 13  Duplicates: 0  Warnings: 13
 
 mysql> describe t_name;
 +--------------+----------+------+-----+---------+-------+
 | Field        | Type     | Null | Key | Default | Extra |
 +--------------+----------+------+-----+---------+-------+
 | id           | text     | YES  |     | NULL    |       |
 | name         | text     | YES  |     | NULL    |       |
 | request_time | char(28) | YES  |     | NULL    |       |
 +--------------+----------+------+-----+---------+-------+
 3 rows in set (0.00 sec)


*レコード操作 [#lac9657e]
***レコードの挿入 [#g66fa6dc]
 mysql> insert into t_name (id,name,request_time) values ('02','suzuki',' [29/Aug/2012:21:08:12 +0900]');
 
 mysql> update t_name set id = '13' where id = '04' and request_time like '%2012:22%';
 Query OK, 1 row affected (0.00 sec)
 Rows matched: 1  Changed: 1  Warnings: 0
 
 mysql> select * from t_name;
 +------+-----------+------------------------------+------------+
 | id   | name      | request_time                 | time_stamp | 
 +------+-----------+------------------------------+------------+
 | 13   | sato      |  [30/Aug/2012:22:10:12 +0900 |       NULL |
 +------+-----------+------------------------------+------------+

***レコードの更新(複数カラム) [#h0488505]
 mysql> update t_name set id = '04',time_stamp = '1347200000' where id = '4';

*ユーザー操作 [#f6e17554]
***ユーザーの作成&データベースへの関連付け [#yd31613a]
 mysql>GRANT ALL PRIVILEGES ON データベース名.* TO ユーザー名@localhost IDENTIFIED BY ' ********';
 mysql>FLUSH PRIVILEGES;

***ユーザー権限の一覧表示 [#rf38c1c1]
 mysql> show grants for kororo@localhost;

*VIEW操作 [#w342f796]
***VIEWの一覧表示 [#pdc9c343]
 mysql>select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_COMMENT = 'VIEW';
 
 または、対象データベースを選択して
 mysql>show tables;

***VIEWの権限確認 [#z0617108]
 mysql> show create view hoge.tables;

*検索 [#p17cd8b3]
***曖昧検索 [#ea9f7781]
 mysql> select * from t_name where id = '04' and request_time like '2012:22';
 or
 mysql> select * from t_name where id = '04' and request_time '%2012:22%';
 
 +------+------+------------------------------+------------+
 | id   | name | request_time                 | time_stamp |
 +------+------+------------------------------+------------+
 | 04   | sato |  [30/Aug/2012:22:10:12 +0900 |       NULL |
 +------+------+------------------------------+------------+
 1 row in set (0.00 sec)

*その他 [#be24e31f]
***最初から10件を表示 [#z462d280]
 mysql> select name from t_name limit 10

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS