8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLで今更知ったこと3つ

Last updated at Posted at 2015-11-20

1.縦表示にするメタコマンド「\G」

MySQLでとりあえず全表示させるとき

SELECT * FROM table_name;

カオス…
カラムの数が多い時とかもう見てられない。

+----+------------+-------------------------------------------------------+-----------------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------+---------------------+---------------------+
| id | date       | k

これを縦に表示してくれるのがあるんです!
「;」を「\G」にしてみな!(\gだとダメー)

SELECT * FROM table_name\G

2.テーブルのステータスを確認するよ!

SHOW TABLE STATUS LIKE 'table_name'\G

Auto_incrementとか表示される。便利!

※でもこういうこともあるので注意な。
[MySQLのSHOW TABLE STATUSに騙された]http://qiita.com/TanukiTam/items/b396598c1e2fe208e978

3.オートインクリメントの値をリセットするよ!

これはrailsのモデルでuserを全削除したのに、
再度ユーザーを作成してもidが1から始まらないよ〜、
というような時に。

まあ、気にしなくてもいいっちゃあいいんですが。

ALTER TABLE table_name AUTO_INCREMENT = 1;

ただし、既にデータが存在する場合、idの一番大きい数値以上にしないとリセットできません。
[MySQL] AUTO_INCREMENTのリセットができない場合の対応方法を参照のこと。

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?