How can I set the default value of a datetime field to the time when the inserted value is used during MySQL table creation?

Source: Internet
Author: User
Use the timestamp column type,

Column Type display format
Timestamp (14) yyyymmddhhmmss
Timestamp (12) yymmddhhmmss
Timestamp (10) yymmddhhmm
Timestamp (8) yyyymmdd
Timestamp (6) yymmdd
Timestamp (4) yymm
Timestamp (2) YY


It will automatically mark your insert or update operations with the current date and time. If a table has multiple timestamp columns, only the first one is automatically updated.

In the following cases, MySQL automatically updates the first timestamp column as the current system time.

1. When an insert, update, or load data infile statement does not explicitly specify its value.

(Note that setting an Update column as its existing value does not cause the timestamp column to be updated, because if you set a column as its current value, MySQL ignores the changes for efficiency. See appendix)

2. You explicitly set the timestamp column as null.

3. The timestamp column except the first one can also be set to the current date and time, as long as the column is set to null, or now ().

Example:

Create Table CATV. Call (
Callintime timestamp (14 ),
Calldh varchar (10) default '',
Callnm varchar (10) default '',
Calldq varchar (20) not null default '',
Callinnum varchar (14) default '',
Callinvoice varchar (28) default '',
Recallnum varchar (14) default '',
Pass Enum ('true', 'false') not null default 'false ',
Key callinnum (callinnum ),
Key callintime (callintime)
)

Update call set callintime = callintime, pass = "true"

The previous command sets all the record pass fields in the table to true, while other field values will not change!
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.