MySQL
MySQLのメモ。
■オートコミットの確認(有効になっている)
mysql> select @@autocommit; +--------------+ | @@autocommit | +--------------+ | 1 | +--------------+ 1 row in set (0.08 sec)
■オートコミットを解除
mysql> set autocommit=0; Query OK, 0 rows affected (0.00 sec)
■オートコミットの確認(無効になっている)
mysql> select @@autocommit; +--------------+ | @@autocommit | +--------------+ | 0 | +--------------+ 1 row in set (0.00 sec)
■テーブル一覧
mysql> show tables; +---------------------+ | Tables_in_at_struts | +---------------------+ | ms_role | | ms_user | | ms_user_role | +---------------------+
■テーブル定義1
mysql> desc ms_user; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | USER_ID | char(10) | NO | PRI | | | | USER_PW | varchar(20) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 2 rows in set (0.02 sec)
■テーブル定義2(show full columns で、より詳細な情報取得できる)
mysql> show columns from ms_user; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | USER_ID | char(10) | NO | PRI | | | | USER_PW | varchar(20) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
■ログインしたユーザ(匿名ユーザの場合は匿名ユーザ名)
mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.01 sec)
■ログインした本当のユーザ
mysql> select current_user(); +----------------+ | current_user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)
■ユーザ情報(付与された権限に関する列もある)
mysql> select user, host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | % | | root | localhost | +------+-----------+
■日付フォーマット
mysql> select date_format('2006-11-10 23:45:12', '%Y年%m月%d日 %H時%i分%S秒'); +-----------------------------------------------------------------+ | date_format('2006-11-10 23:45:12', '%Y年%m月%d日 %H時%i分%S秒') | +-----------------------------------------------------------------+ | 2006年11月10日 23時45分12秒 | +-----------------------------------------------------------------+ 1 row in set (0.02 sec)
■その日を返す日付関数
mysql> select current_date, current_time, now(), sysdate(); +--------------+--------------+---------------------+---------------------+ | current_date | current_time | now() | sysdate() | +--------------+--------------+---------------------+---------------------+ | 2006-11-21 | 23:58:26 | 2006-11-21 23:58:26 | 2006-11-21 23:58:26 | +--------------+--------------+---------------------+---------------------+ 1 row in set (0.00 sec)
■単純な文字列連結
mysql> select concat('うは','おk','www'); +----------------------------+ | concat('うは','おk','www') | +----------------------------+ | うはおkwww | +----------------------------+ 1 row in set (0.02 sec)
■セパレータ付の文字列連結
mysql> select concat_ws('-','うは','おk','www'); +-----------------------------------+ | concat_ws('-','うは','おk','www') | +-----------------------------------+ | うは-おk-www | +-----------------------------------+ 1 row in set (0.00 sec)