MySQL


ウェブアプリケーション用DB作成の手順

1.MySQLへログイン
2.DBの作成
3.ユーザーの追加
4.ユーザーが追加されたか確認
5.完了

MySQL基本操作

ログイン

# mysql -u root -p パスワード

データベース作成

mysql>create database データベース名 ;

作成済みデータベース表示

mysql>show databases ;

データベース削除

mysql>drop datatbase test_db ;

データベースの表示

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

テーブル操作

テーブル作成

mysql> create table テーブル名(フィールド名01 タイプ,フィールド名02 タイプ);
※例
mysql> create table test01(id text);

テーブルの確認

mysql> show tables

テーブルの状態確認

mysql> show table status;

フィールド操作

フィールドの設定確認

mysql> show columns from テーブル名; 

既存テーブルへフィールドの追加

mysql> alter table t_name add request_time text;

指定したフィールドで並べ替え表示

mysql> select * from t_name order by request_time;

フィールドのデータ型を確認

mysql> describe test01;
+--------------+------+------+-----+---------+-------+
| Field        | Type | Null | Key | Default | Extra |
+--------------+------+------+-----+---------+-------+
| id           | text | YES  |     | NULL    |       |
| name         | text | YES  |     | NULL    |       |
| request_time | text | YES  |     | NULL    |       |
+--------------+------+------+-----+---------+-------+

フィールドのデータ型を変更

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)

レコード操作

レコードの挿入

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 |
+------+-----------+------------------------------+------------+

レコードの更新(複数カラム)

mysql> update t_name set id = '04',time_stamp = '1347200000' where id = '4';

ユーザー操作

ユーザーの作成&データベースへの関連付け

mysql>GRANT ALL PRIVILEGES ON データベース名.* TO ユーザー名@localhost IDENTIFIED BY ' ********';
mysql>FLUSH PRIVILEGES;

ユーザー権限の一覧表示

mysql> show grants for kororo@localhost;

VIEW操作

VIEWの一覧表示

mysql>select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_COMMENT = 'VIEW';

または、対象データベースを選択して
mysql>show tables;

VIEWの権限確認

mysql> show create view hoge.tables;

検索

曖昧検索

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)

その他

最初から10件を表示

mysql> select name from t_name limit 10

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-06-30 (木) 01:40:11