81
85

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 キー着脱メモ

Posted at

テーブルに付いているキーを調べる

SHOW INDEX FROM table_name

Key_nameカラムがキーの名称です。

プライマリキー

Create文

CREATE TABLE table_name (
	column1 int(11) PRIMARY KEY
)
CREATE TABLE table_name (
	column1 int(11),
	column2 int(11),
	PRIMARY KEY (column1, column2)
)

追加

ALTER TABLE table_name
  ADD PRIMARY KEY (column1, column2);

削除

ALTER TABLE table_name
 DROP PRIMARY KEY

ユニークキー

Create文

CREATE TABLE table_name (
	column1 int(11),
	column2 int(11),
	UNIQUE KEY uk_name (column1, column2)
)

追加

ALTER TABLE table_name
  ADD UNIQUE uk_name (column1, column2);

削除

ALTER TABLE table_name
 DROP INDEX uk_name

インデックス

Create文

CREATE TABLE table_name (
	column1 int(11),
	column2 int(11),
	KEY idx_name (column1, column2)
)

追加

ALTER TABLE table_name
  ADD INDEX idx_name (column1, column2);

削除

ALTER TABLE table_name
 DROP INDEX idx_name
81
85
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
81
85

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?