灵活设置默认值

/*
在建表时一般会设置默认值,但如果默认值随着条件的变化有可能更改,比如录入员工资料时默认工资为500,下一年度涨工资后默认工资为600。
或者有很多表中的很多列有相同的默认值时,可以考虑用一个变量来设置,就要自定义数据类型一样。
SQL提供有这方面的功能,就是Create Default。
参考联机帮助: Create Default;  sp_bindefault;  sp_unbindefault
下面是测试( 测试环境SQL2000)

*/


/*创建一个测试用表*/
Create Table Employee (
 EmpID char(3),
 Salary numeric(10,2)
)

--未绑定默认值时insert两条记录看看
insert into Employee (EmpID) values('001') 
insert into Employee (EmpID) values('001') 
go

/*创建一个默认值,值为500*/
Create Default DF_salary AS 500
GO

/*将默认值绑定到该表的Salary列*/
Exec sp_bindefault DF_salary, 'Employee.Salary'
go

--绑定默认值后再insert两条记录看看
insert into Employee (EmpID) values('101')
insert into Employee (EmpID) values('102')

--取消绑定
EXEC sp_unbindefault 'Employee.Salary'
go
--删除默认值
Drop Default DF_salary
go
--新建一个同样的默认值,值改为另一个数
Create Default DF_salary AS 600
GO
--再次绑定
Exec sp_bindefault DF_salary, 'Employee.Salary'
go
--用新默认值后再insert两条记录看看
insert into Employee (EmpID) values('201')
insert into Employee (EmpID) values('202')
--查询结果
select * from Employee


/* 以下是结果
EmpID Salary      
----- ------------
001   NULL
001   NULL
101   500.00
102   500.00
201   600.00
202   600.00

(所影响的行数为 6 行)
*/

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot中,可以使用@ConfigurationProperties注解来设置默认值。通过在配置类中使用该注解,并指定prefix属性,可以将配置文件中的属性值绑定到对应的属性上。如果配置文件中未设置某个属性的值,则可以通过在属性上使用@Value注解,并指定默认值设置默认值。例如,可以在配置类中使用@ConfigurationProperties注解,并指定prefix属性为"my",然后在属性上使用@Value注解,并指定默认值为"defaultValue",这样如果配置文件中未设置my.property的值,则my.property将被赋值为defaultValue。具体代码如下所示: ``` @ConfigurationProperties(prefix ="my") public class MyProperties { @Value("${my.property:defaultValue}") private String property; // getter and setter } ``` 这样,在配置文件中未设置my.property的值时,property属性将被赋值为defaultValue。这样就可以在不改动Java代码的情况下,通过配置文件来设置默认值,实现灵活的配置。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [springboot 注解设置默认值](https://blog.csdn.net/weixin_35752645/article/details/129074139)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [SpringBoot @Value 设置默认值](https://blog.csdn.net/libertine1993/article/details/80765886)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txlicenhe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值