MySQL下如何给一个日期字段赋默认值

P_ZLJ 2005-07-11 02:18:11
各位大侠:请问MySQL下如何给一个日期字段赋默认值 ,也就是创建表的时候,default 后面怎么写,
我写了 create table (id int AUTO_INCREMENT primary key,Mydate datetime default now())怎么老提示出错呢?求教!!!
...全文
580 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rardge 2005-09-21
  • 打赏
  • 举报
回复
楼主的第二个问题。
楼主是不是希望id成为这种样子?
1
2
3
1
2
3
……
如果是这样,不能用auto_increment,用alter table命令去掉它。
然后,这个字段你不能使用 primary key(auto_increment 用这个) 或者 unique 之类的唯一索引,否则,mysql是不允许你插入一样的记录(就是说你第二个“1”是无法添加到数据库中的)。
所以,这个字段的插入和判断应该要用程序来控制了。
rardge 2005-09-21
  • 打赏
  • 举报
回复
即使他指定表名,这个语句还是错误的!
有些东西不是想当然的可以做到的,default 后面要使用常量,而不能使用函数或者表达式。
看看手册吧。
The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column as of MySQL 4.1.2.
志_祥 2005-09-20
  • 打赏
  • 举报
回复
可能是版本问题
mituzhishi 2005-09-18
  • 打赏
  • 举报
回复
create table (id int AUTO_INCREMENT primary key,Mydate datetime default now())

出错是因为你没有指定表格名称。
sender 2005-07-12
  • 打赏
  • 举报
回复
去掉auto_increment可以用alter命令。
P_ZLJ 2005-07-11
  • 打赏
  • 举报
回复
那比如我上表中的id自增列能不能通过语句把AUTO_INCREMENT去掉,让它不是自增列,或者怎么能让自增列ID值增加到一定的数之后,我又让ID种子从1开始增加呢?
mathematician 2005-07-11
  • 打赏
  • 举报
回复
好像不行吧,需要插入数据的时候赋值。

56,675

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧