MySQL默认值约束

1、默认值约束
特点:
(1)一个表可以有很多的默认值约束
(2)默认值约束只能针对某一个字段来说
(3)默认值约束意味着,该字段如果没有手动赋值,会按默认值处理

2、如何在建表时指定默认值约束?

create table 【数据库名.】表名称(
	字段名1 数据类型  primary key,
	字段名2 数据类型 【unique key】【not nulldefault 默认值,
	字段名3 数据类型 default 默认值,
	。。。。
);

例如:

create table test.t_stu(
	sid int primary key,
	sname varchar(20) not null,
	gender char default '男'
);
insert into t_stu values(1,'张三');
ERROR 1136 (21S01): Column count doesn't match value count at row 1 ' 列数与值数量

不匹配

insert into t_stu values(1,'张三',default);
insert into t_stu(sid,sname) values(2,'李四');

3、建表后如何指定某个字段有默认值呢?

alter table 【数据库名.】表名称 modify 字段名 数据类型 default 默认值;
create table test.t_stu(
	sid int primary key,
	sname varchar(20) not null,
	gender char 
);
alter table t_stu modify gender char default '男';

提醒:如果某个字段既要非空,又要默认值,那么alter 语句后面两个都要写。

create table test.t_stu(
	sid int primary key,
	sname varchar(20) not null,
	gender char not null
);

增加gender还有默认值

alter table t_stu modify gender char default '男' not null;

4、如何取消某个字段的默认值约束

alter table 【数据库名.】表名称 modify 字段名 数据类型 【not null; #不写默认值约束
insert into t_stu values (1,'张三','女');
update t_stu set gender = default where sid = 1;
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值