列の初期値を設定するには、対象の列でDEFAULT VALUEを定義する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
mysql> CREATE TABLE testtable ( -> product VARCHAR(20), -> price INT DEFAULT 0 -> ); Query OK, 0 rows affected (0.03 sec) mysql> DESC testtable; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | product | varchar(20) | YES | | NULL | | | price | int | YES | | 0 | | +---------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> INSERT INTO testtable (product) -> VALUES ('battery'); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM testtable; +---------+-------+ | product | price | +---------+-------+ | battery | 0 | +---------+-------+ |