0
0

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 3 years have passed since last update.

MySQL④

Posted at

後から一意制約を付ける alter table テーブル名 add unique unique_name(カラム);

例えば、usersテーブルのnameフィールドをuniqueにしたい、という場合には以下のようにしてあげるといいでしょう(unique_nameはインデックスの名前なので適当につけてあげればOKです)。

mysql> alter table users add unique unique_name(name);

後から一位制約を外す alter table テーブル名 drop index unique_name;

またこのunique制約を外したい場合は、うえで付けたインデックスの名前を使って次のようにしてあげればOKです。

alter table users drop index unique_name;

テーブルの構造を変える

カラムを追加する  alter table テーブル名 add column カラム名 データ型;

alter table users add column email varchar(255);

追加する場所を指定する

nameカラム のあとに追加したかった場合には

alter table users add column email varchar(255) after name;

カラムを削除する alter table テーブル名 drop column カラム名;

alter table users drop column score;

カラムを変更する alter table テーブル名 change 元カラム名 新カラム名 データ型 オプション;

alter table users change name user_name varchar(80) default 'nobody';

テーブルの構造を確認する

desc users

テーブル名を変える alter table 元テーブル名 rename 新テーブル名;

alter table users rename persons;

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?