LoginSignup
5
3

More than 5 years have passed since last update.

MySQLのPrimaryKey(id)を振り直す

Posted at

MySQLのPrimaryKey(id)を振り直す

PrimaryKeyを振り直す時のメモ

ALTER table [table_name] drop column id;
ALTER table [table_name] add id int(11) primary key not null auto_increment first;
ALTER TABLE [table_name] AUTO_INCREMENT =1

解説

1行目

指定したテーブルのカラム名idをドロップ(削除)する。

2行目

指定したテーブルにカラム名idのカラムを追加する。

下記の指定は重要
int(11) primary key not null auto_increment first

3行目

AUTO_INCREMENTを1から始める

5
3
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
5
3