mysql中设置字段默认值_mysql中设置字段的默认值

2009-12-29 回答

--sql:

create table test(

i_a int not null default 1,

ts_b timestamp not null default now(),

c_c char(2) not null default '1'

);

--以下sql不合法

--time_d time not null default curtime(),

--date_e date not null default curdate(),

--datetime_f datetime not null default now(),

总结

int类型:默认值也得是整型,并且default后边不要()括号。

char类型:默认值使用单引号。

datetime类型:now()函数以'yyyy-mm-dd

hh:mm:ss'返回当前的日期时间,可以直接存到datetime字段中。不支持使用系统默认值。

date类型:curdate()以'yyyy-mm-dd'的格式返回今天的日期,可以直接存到date字段中。不支持使用系统默认值。

time类型:curtime()以'hh:mm:ss'的格式返回当前的时间,可以直接存到time字段中。不支持使用系统默认值。

参考资料

mysql表字段默认值

用sql语句创建表时,给表字段默认值出错。

比如:mssql中

create table dnt_forums(

aa int not null default (''),

bb date not null default (getdate()),

cc char(50) not null default (null)

}

请问上述的sql语句要如何修改在mysql中才能使用

aa 是 int 类型,默认值也得是整型,并且default后边不要()括号

bb date类型不支持使用系统默认值,改成timestamp,能过now()取系统时间

cc 已经不允许为空(not null)所以不能默认为 null ,可以改成空字符串

create table dnt_forums(

aa int not null default 2,

bb timestamp not null default now(),

cc char(50) not null default ''

);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值