8月 172013
 

NOT NULLを削除する

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    |                |
| category_id | int(11)      | NO   | MUL | NULL    |                |
| description | text         | NO   |     | NULL    |                |
| published   | int(11)      | NO   |     | 1       |                |
| created_at  | datetime     | NO   |     | NULL    |                |
| updated_at  | datetime     | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
 
mysql> alter table site modify column user_id int;
Query OK, 16 rows affected (0.07 sec)
Records: 16  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)      | YES  | MUL | NULL    |                |
| url         | varchar(500) | NO   | MUL | NULL    |                |
| name        | varchar(500) | NO   | MUL | NULL    |                |
| category_id | int(11)      | NO   | MUL | NULL    |                |
| description | text         | NO   |     | NULL    |                |
| published   | int(11)      | NO   |     | 1       |                |
| created_at  | datetime     | NO   |     | NULL    |                |
| updated_at  | datetime     | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

NOT NULLを設定する

mysql> alter table site modify column user_id int not null;
Query OK, 16 rows affected (0.06 sec)
Records: 16  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    |                |
| category_id | int(11)      | NO   | MUL | NULL    |                |
| description | text         | NO   |     | NULL    |                |
| published   | int(11)      | NO   |     | 1       |                |
| created_at  | datetime     | NO   |     | NULL    |                |
| updated_at  | datetime     | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>