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