The difference between datetime and timestamp in SQL

Source: Internet
Author: User

Beginner Php+mysql, when developing a simple registration process, it is required to add a date field after each newly inserted record to facilitate later query and sorting. So immediately Baidu, found that you can use DateTime or timestamp two types of date to achieve. This is not allowed for me to love the tangled, so immediately to find their differences. But the search is either a lengthy, or no focus, so he summed up the essence of these articles, in order to strive for excellence. DATETIME1, allow null values, you can customize the value, the system does not automatically modify its value. 2. You cannot set a default value, so you must manually specify the value of the DateTime field to successfully insert the data in cases where null values are not allowed. 3. Although you cannot set a default value, you can use the now () variable to automatically insert the current time of the system when you specify the value of a DateTime field.Conclusion: DateTime types are appropriate for recording the original creation time of the data, because no matter how you change the value of the other fields in the record, the value of the DateTime field does not change unless you manually change it. TIMESTAMP1, allow null values, but cannot customize values, so null values do not make any sense. 2, the default value is Current_timestamp (), in fact, is the current system time. 3, the database will automatically modify its value, so when inserting records do not need to specify the name of the timestamp field and the value of the timestamp field, you just need to add a timestamp field when you design the table, the value of the field will automatically become the current system time. 4. The timestamp value of the corresponding record is automatically updated to the current system time when the records in the table are modified at any later time.Conclusion: The timestamp type is suitable for recording the last modification time of the data, because the value of the timestamp field is automatically updated as soon as you change the value of the other fields in the record.

The difference between datetime and timestamp in SQL

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.