<字段名> <数据类型> DEFAULT <默认值>;
其中,“默认值”为该字段设置的默认值,如果是字符类型的,要用单引号括起来。mysql> CREATE TABLE tb_dept3 -> ( -> id INT(11) PRIMARY KEY, -> name VARCHAR(22), -> location VARCHAR(50) DEFAULT 'Beijing' -> ); Query OK, 0 rows affected (0.37 sec) mysql> DESC tb_dept3; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(22) | YES | | NULL | | | location | varchar(50) | YES | | Beijing | | +----------+-------------+------+-----+---------+-------+ 3 rows in set (0.06 sec)以上语句执行成功之后,表 tb_dept3 上的字段 location 拥有了一个默认值 Beijing,新插入的记录如果没有指定部门位置,则默认都为 Beijing。
ALTER TABLE <数据表名>
CHANGE COLUMN <字段名> <数据类型> DEFAULT <默认值>;
mysql> ALTER TABLE tb_dept3 -> CHANGE COLUMN location -> location VARCHAR(50) DEFAULT 'Shanghai'; Query OK, 0 rows affected (0.15 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC tb_dept3; +----------+-------------+------+-----+----------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+----------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(22) | YES | | NULL | | | location | varchar(50) | YES | | Shanghai | | +----------+-------------+------+-----+----------+-------+ 3 rows in set (0.00 sec)
ALTER TABLE <数据表名>
CHANGE COLUMN <字段名> <字段名> <数据类型> DEFAULT NULL;
mysql> ALTER TABLE tb_dept3 -> CHANGE COLUMN location -> location VARCHAR(50) DEFAULT NULL; Query OK, 0 rows affected (0.15 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC tb_dept3; +----------+-------------+------+-----+----------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+----------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(22) | YES | | NULL | | | location | varchar(50) | YES | | NULL | | +----------+-------------+------+-----+----------+-------+ 3 rows in set (0.00 sec)
本文链接:http://task.lmcjl.com/news/6355.html