8.1 作用
给某个字段/某列指定默认值,一旦设置默认值,在插入数据时,如果此字段没有显式赋值,则赋值为默认值。
8.2 关键字
DEFAULT
8.3 如何给字段加默认值
(1)建表时
create table 表名称(字段名 数据类型 primary key,字段名 数据类型 unique key not null,字段名 数据类型 unique key,字段名 数据类型 not null default 默认值,);create table 表名称(字段名 数据类型 default 默认值 ,字段名 数据类型 not null default 默认值,字段名 数据类型 not null default 默认值,primary key(字段名),unique key(字段名));说明:默认值约束一般不在唯一键和主键列上加
create table employee(eid int primary key,ename varchar(20) not null,gender char default '男',tel char(11) not null default '' #默认是空字符串);
mysql> desc employee;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| eid | int(11) | NO | PRI | NULL | || ename | varchar(20) | NO | | NULL | || gender | char(1) | YES | | 男 | || tel | char(11) | NO | | | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)
insert into employee values(1,'汪飞','男','13700102535'); #成功
mysql> select * from employee;+-----+-------+--------+-------------+| eid | ename | gender | tel |+-----+-------+--------+-------------+| 1 | 汪飞 | 男 | 13700102535 |+-----+-------+--------+-------------+1 row in set (0.00 sec)
insert into employee(eid,ename) values(2,'天琪'); #成功
mysql> select * from employee;+-----+-------+--------+-------------+| eid | ename | gender | tel |+-----+-------+--------+-------------+| 1 | 汪飞 | 男 | 13700102535 || 2 | 天琪 | 男 | |+-----+-------+--------+-------------+2 rows in set (0.00 sec)
insert into employee(eid,ename) values(3,'二虎');#ERROR 1062 (23000): Duplicate entry '' for key 'tel'#如果tel有唯一性约束的话会报错,如果tel没有唯一性约束,可以添加成功
再举例:
CREATE TABLE myemp(id INT AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(15),salary DOUBLE(10,2) DEFAULT 2000);
(2)建表后
alter table 表名称 modify 字段名 数据类型 default 默认值;#如果这个字段原来有非空约束,你还保留非空约束,那么在加默认值约束时,还得保留非空约束,否则非空约束就被删除了#同理,在给某个字段加非空约束也一样,如果这个字段原来有默认值约束,你想保留,也要在modify语句中保留默认值约束,否则就删除了alter table 表名称 modify 字段名 数据类型 default 默认值 not null;
create table employee(eid int primary key,ename varchar(20),gender char,tel char(11) not null);
mysql> desc employee;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| eid | int(11) | NO | PRI | NULL | || ename | varchar(20) | YES | | NULL | || gender | char(1) | YES | | NULL | || tel | char(11) | NO | | NULL | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)
alter table employee modify gender char default '男'; #给gender字段增加默认值约束alter table employee modify tel char(11) default ''; #给tel字段增加默认值约束
mysql> desc employee;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| eid | int(11) | NO | PRI | NULL | || ename | varchar(20) | YES | | NULL | || gender | char(1) | YES | | 男 | || tel | char(11) | YES | | | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)
alter table employee modify tel char(11) default '' not null;#给tel字段增加默认值约束,并保留非空约束
mysql> desc employee;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| eid | int(11) | NO | PRI | NULL | || ename | varchar(20) | YES | | NULL | || gender | char(1) | YES | | 男 | || tel | char(11) | NO | | | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)
8.4 如何删除默认值约束
alter table 表名称 modify 字段名 数据类型 ;#删除默认值约束,也不保留非空约束alter table 表名称 modify 字段名 数据类型 not null; #删除默认值约束,保留非空约束
alter table employee modify gender char; #删除gender字段默认值约束,如果有非空约束,也一并删除alter table employee modify tel char(11) not null;#删除tel字段默认值约束,保留非空约束
mysql> desc employee;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| eid | int(11) | NO | PRI | NULL | || ename | varchar(20) | YES | | NULL | || gender | char(1) | YES | | NULL | || tel | char(11) | NO | | NULL | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)
