如何设置 Mysql中的datetime的默认值
答案:2 悬赏:40
解决时间 2021-01-30 23:46
- 提问者网友:娇妻失忆
- 2021-01-30 05:34
如何设置 Mysql中的datetime的默认值
最佳答案
- 二级知识专家网友:苦柚恕我颓废
- 2021-01-30 06:53
在sqlserver2000中可以这样
create table test(name varchar(10),registerTime datetime default getdate());
设置默认值。但在MySQ中
create table test(name varchar(10),registerTime datetime default now());
ERROR 1064: You have an error in your SQL syntax. Check the manual that corresp
onds to your MySQL server version for the right syntax to use near 'now()
)' at line 2
如何在MySQL中设置datetime型的当前默认值? [Re: jingang] Copy to clipboard
Posted by: wanghb507
Posted on: 2004-03-02 10:25
我看只能,在程序上处理了,因为在
insert into test values('name', now());
中是正确的
如何在MySQL中设置datetime型的当前默认值? [Re: jingang] Copy to clipboard
Posted by: NcitZhang
Posted on: 2004-03-28 18:10
create table test(name varchar(10),registerTime datetime default getdate());
create table test(name varchar(10),registerTime datetime default '0000-00-00');
create table test(name varchar(10),registerTime datetime default getdate());
设置默认值。但在MySQ中
create table test(name varchar(10),registerTime datetime default now());
ERROR 1064: You have an error in your SQL syntax. Check the manual that corresp
onds to your MySQL server version for the right syntax to use near 'now()
)' at line 2
如何在MySQL中设置datetime型的当前默认值? [Re: jingang] Copy to clipboard
Posted by: wanghb507
Posted on: 2004-03-02 10:25
我看只能,在程序上处理了,因为在
insert into test values('name', now());
中是正确的
如何在MySQL中设置datetime型的当前默认值? [Re: jingang] Copy to clipboard
Posted by: NcitZhang
Posted on: 2004-03-28 18:10
create table test(name varchar(10),registerTime datetime default getdate());
create table test(name varchar(10),registerTime datetime default '0000-00-00');
全部回答
- 1楼网友:一只傻青衣
- 2021-01-30 07:42
我们都知道在sqlserver中可以很容易指定日期类型字段的默认值为当前服务器操作系统的日期,getdate()或now()就行了,但是如果在mysql中怎么办??很多朋友发现没办法了。
下面我们可以通过timestamp类型字段解决,timestamp列类型详解(怎样设列的默认值为now())
mysql目前不支持列的default 为函数的形式,
如达到你某列的默认值为当前更新日期与时间的功能,
你可以使用timestamp列类型
下面就详细说明timestamp列类型
timestamp列类型
timestamp值可以从1970的某时的开始一直到2037年,精度为一秒,其值作为数字显示。
timestamp值显示尺寸的格式如下表所示:
:
+---------------+----------------+
| 列类型 | 显示格式 |
| timestamp(14) | yyyymmddhhmmss |
| timestamp(12) | yymmddhhmmss |
| timestamp(10) | yymmddhhmm |
| timestamp(8) | yyyymmdd |
| timestamp(6) | yymmdd |
| timestamp(4) | yymm |
| timestamp(2) | yy |
+---------------+----------------+
“完整”timestamp格式是14位,但timestamp列也可以用更短的显示尺寸创造
最常见的显示尺寸是6、8、12、和14。
你可以在创建表时指定一个任意的显示尺寸,但是定义列长为0或比14大均会被强制定义为列长14。
列长在从1~13范围的奇数值尺寸均被强制为下一个更大的偶数。
列如:
定义字段长度 强制字段长度
timestamp(0) -> timestamp(14)
timestamp(15)-> timestamp(14)
timestamp(1) -> timestamp(2)
timestamp(5) -> timestamp(6)
所有的timestamp列都有同样的存储大小,
使用被指定的时期时间值的完整精度(14位)存储合法的值不考虑显示尺寸。
不合法的日期,将会被强制为0存储
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯