既存のテーブルにあるカラムにdefault設定を追加・削除する

MySQLのデフォルト設定の追加・削除は以下のように行います。

デフォルト設定の追加

mysql> show fields from site;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id     | int(11)      | NO   | MUL | NULL    |                |
| url         | varchar(500) | NO   | MUL | NULL    |                |
| name        | varchar(500) | NO   | MUL | NULL    |                |
| link_url    | varchar(500) | NO   |     | NULL    |                |
| category_id | int(11)      | NO   | MUL | NULL    |                |
| description | text         | NO   |     | NULL    |                |
| published   | int(11)      | NO   |     | 0       |                |
| created_at  | datetime     | NO   |     | NULL    |                |
| updated_at  | datetime     | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
10 rows in set (0.02 sec)

mysql> alter table site alter link_url set default 'http://example.com/';
Query OK, 0 rows affected (0.38 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show fields from site;
+-------------+--------------+------+-----+---------------------+----------------+
| Field       | Type         | Null | Key | Default             | Extra          |
+-------------+--------------+------+-----+---------------------+----------------+
| id          | int(11)      | NO   | PRI | NULL                | auto_increment |
| user_id     | int(11)      | NO   | MUL | NULL                |                |
| url         | varchar(500) | NO   | MUL | NULL                |                |
| name        | varchar(500) | NO   | MUL | NULL                |                |
| link_url    | varchar(500) | NO   |     | http://example.com/ |                |
| category_id | int(11)      | NO   | MUL | NULL                |                |
| description | text         | NO   |     | NULL                |                |
| published   | int(11)      | NO   |     | 0                   |                |
| created_at  | datetime     | NO   |     | NULL                |                |
| updated_at  | datetime     | NO   |     | NULL                |                |
+-------------+--------------+------+-----+---------------------+----------------+
10 rows in set (0.00 sec)

デフォルト設定を削除

mysql> alter table site alter link_url drop default;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show fields from site;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id     | int(11)      | NO   | MUL | NULL    |                |
| url         | varchar(500) | NO   | MUL | NULL    |                |
| name        | varchar(500) | NO   | MUL | NULL    |                |
| link_url    | varchar(500) | NO   |     | NULL    |                |
| category_id | int(11)      | NO   | MUL | NULL    |                |
| description | text         | NO   |     | NULL    |                |
| published   | int(11)      | NO   |     | 0       |                |
| created_at  | datetime     | NO   |     | NULL    |                |
| updated_at  | datetime     | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
10 rows in set (0.04 sec)

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>