Home  >  Article  >  Backend Development  >  How to use SQL statements to express: create a new field so that every time a new record is inserted, the current time is automatically marked?

How to use SQL statements to express: create a new field so that every time a new record is inserted, the current time is automatically marked?

WBOY
WBOYOriginal
2016-08-18 09:16:111160browse

I found this expression on the Internet and implemented the above function on mysql (version 5.7) provided by local wamp

<code>alter table account.users add column reg_time datetime not null default now();</code>

But I am using the same SQL statement on the server side

<code>alter table users add column reg_time datetime not null default now();</code>

But it prompts: Invalid default value for 'reg_time'
The server mysql version is 5.0
What should I do?

Reply content:

I found this expression on the Internet and implemented the above function on mysql (version 5.7) provided by local wamp

<code>alter table account.users add column reg_time datetime not null default now();</code>

But I am using the same SQL statement on the server side

<code>alter table users add column reg_time datetime not null default now();</code>

But it prompts: Invalid default value for 'reg_time'
The server mysql version is 5.0
What should I do?

<code>ALTER TABLE `users`
ADD COLUMN `reg_time`  timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP</code>

sqlserver uses getdate()
mysql which should correspond to now()

timestamp timestamp type field has two attributes: automatic update insertion time and modification time

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn