首页 > 其他分享 >对已有的表字段修改设置默认值,会影响原来数据中为null的字段数据吗?

对已有的表字段修改设置默认值,会影响原来数据中为null的字段数据吗?

时间:2023-05-22 17:14:20浏览次数:49  
标签:null name 中为 male height user 默认值 id delete

1.新建表 t_user

CREATE TABLE `t_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `age` int(11) NOT NULL,
  `male` varchar(100) NOT NULL,
  `height` int(11) NOT NULL,
  `if_delete` tinyint(1),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1425357 DEFAULT CHARSET=utf8;

 2.插入数据

INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (1, '张三', 19, '1', 180, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (2, '张三', 19, '1', 180, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (3, '张三', 19, '1', 180, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (4, '张三', 19, '1', 180, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (5, '张三', 19, '1', 180, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (6, '张三', 19, '1', 180, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (7, '张三', 19, '1', 180, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (8, '张三', 19, '1', 180, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (9, '张三', 19, '1', 180, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (10, 'liuss', 111, '11', 1601, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (11, '1', 1, '1', 1, NULL);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (13, 'liu', 11, '1', 160, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (14, 'liu', 11, '1', 160, 0);
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `male`, `height`, `if_delete`) VALUES (15, 'liu', 11, '1', 160, 1);

3.可以看到id=11的数据 if_delete字段是null
修改表中 if_delete字段的默认值为0:

ALTER TABLE t_user  MODIFY COLUMN if_delete tinyint(1) DEFAULT '0' COMMENT '是否删除 0-否 1-是';

查看表的DDL 

 设置成功!

 

查看t_user表的数据:会发现id=11的if_delete字段依旧为null(由此可得 修改字段的默认值为已有数据没有影响)

 

测试新增一条数据,此字段的默认值是否有:

insert into t_user(id,name,age,male,height) VALUES(16,'test',11,'1',160);

默认值数据生效

 结论:对于已经存在的字段,新增默认值,不会影响原有数据此字段的null值,对于已经设置了了默认值的新增数据,默认值会应用上。

 

那么如何对已存在的数据让让进行默认值生效?

replace into t_user(id,name,age,male,height) values(10,'liuss',111,'11',1601);

 新增字段,且设置默认值,新增列数据会受默认值影响。

 

标签:null,name,中为,male,height,user,默认值,id,delete
From: https://www.cnblogs.com/lxlzoro/p/17421059.html

相关文章

  • Undefined 不是 Null
    在JavaScript中,null用于对象,undefined用于变量,属性和方法。对象只有被定义才有可能为null,否则为undefined。如果我们想测试对象是否存在,在对象还没定义时将会抛出一个错误。错误的使用方式:if(myObj!==null&&typeofmyObj!=="undefined")正确的方式是我们......
  • JavaScript全解析——ES6函数中参数的默认值和解构赋值
    本文为千锋资深前端教学老师带来的【JavaScript全解析】系列,文章内含丰富的代码案例及配图,从0到1讲解JavaScript相关知识点,致力于教会每一个人学会JS!文末有本文重点总结,可以收藏慢慢看~更多技术类内容,主页关注一波!ES6函数中参数的默认值给函数的形参设置一个默认值,当你没有......
  • TypeError: Cannot read properties of null (reading 'name')
    报错如下:错误代码:searchList(){this.entity={}this.edit=nullletquery={}query.traceCode=this.codethis.loading=truecodeApi.queryTraceCode(query).then(res=>{...},err=>{......
  • TS高级类型 Record、Pick、Partial、Required、Readonly、Exclude、Extract、Omit、No
    keyof获取类型内所有的key,即所有属性名,获取的是一个联合类型这里类型指:通过interface或type定义的类型;通过typeofxxx返回的类型等。keyof后面必须是类型,不能是具体的对象interfaceIPeople{name:string,age?:number,sex:string,}ty......
  • 删除docker images中为none的镜像
    查看现有镜像➜safelinedockerimagesREPOSITORYTAGIMAGEIDCREATEDSIZEchaitin/safeline-tengine1.5.125a6146e6aed13hoursago119MBchaitin/safeline-tenginelatest25a6146e6aed13hoursago119M......
  • 关于srand((unsigned)time(NULL))详解
    srand()函数用来设置算法的种子,time(NULL)返回当前的时间,先调用srand(time(NULL))是把当前的时间作为种子,是程序每次运行产生不同的随机数序列。涉及到三个概念:1、随机数:数学上产生的都是伪随机数,真正的随机数使用物理方法产生的。2、随机数种子:随机数的产生是由算术规则产生的......
  • wpf XAML 设计器异常,提示NullReferenceException 未将对象引用设置到对象
     在cs构造函数里手动注册,并且在控件的构造函数里增加判断if(DesignerProperties.GetIsInDesignMode(this)){return;}//在这里才注册Load事件cmbSpeed.Loaded+=cmbSpeed_Loaded;来源:https://www.cnblogs.com/zsx-blog/p/8311633.html ......
  • element 时间日期选择器el-date-picker点击清空按钮报错:TypeError: Cannot read prope
       参考:http://www.taodudu.cc/news/show-805096.html?action=onClick......
  • 非空注解@NonNull与@NotNull使用错误导致传参为空时报NPE
    背景新上线了一个需求后,发现时不时的有一些NullPointerException报出来,跟踪后发现都是在一个地方报出来的org.springframework.beans.BeanInstantiationException:Failedtoinstantiate[net.lingyejun.mall.domain.book.req.BookInfoReqDto]:Constructorthrewexception;......
  • Ubuntu20.04中为python3创建一个名为python的软连接
    问题描述:安装好Ubuntu20.04之后,想使用python,只能输入python3,输入python没有用处,如下图 为了少写一个字符,必须把"python3变成python"1.找到输入命令python3执行的二进制文件whichpython3 2.进入/usr/bin/目录查找与python有关的文件cd/usr/bin/ls-alF 可以看......