66
56

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でユニークキーや外部キーを確認する

Last updated at Posted at 2018-04-05

MySQLでテーブルの情報を確認する場合は、

DESC table_name;

とすることが多いですが、これだとユニークキーや外部キー制約などは確認できません

このような場合は

SHOW CREATE TABLE table_name;

とすると、

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL DEFAULT '',
  `name` varchar(255) DEFAULT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `index_users_on_email` (`email`),
  KEY `index_users_on_id` (`id`),
  FOREIGN KEY (parent_id) 
      REFERENCES parent(id)
      ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8

上記のように確認できます。

指定したテーブルをTABLE CREATEするための情報がそのまま出力されるので、DB設定を網羅的に確認できて便利です。

バージョン

MySQL:5.6.38

参考リンク

66
56
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
66
56

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?