您好,欢迎访问一九零五行业门户网

MySQL表字段设置默认值(图文教程及注意细节)

默认值的设置很重要,比如在插入的时候一些字段是可以省略的,这会带来很多的方便,接下来将要介绍mysql表字段设置默认值感兴趣的你可以千万不要走开啊,希望本文对你有所帮助
环境
mysql 5.1 + 命令行工具
问题
mysql表字段设置默认值
解决
代码如下:
--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 ''
);
mysql获取系统当前时间的函数
其它类似信息

推荐信息