"Strange" rule timestamp field

Some students say the rules timestamp field more "weird." Manual which says more complicated, here briefly explain some of the rules in MySQL timestamp field.

Divided into two parts

Define rules:

1, timestamp field has three properties:

a) whether to allow NULL. The default is not null.

b) the default value. It can be set to default CURRENT_TIMESTAMP or a constant default. If the default is defined for the first occurrence of a timestamp field, default CURRENT_TIMESTAMP, other fields timestamp, default '0000-00-00 00:00:00'. Default can be specified as null, provided that the field allows null.

For example, the statement field2 timestamp null default null.

c) On update CURRENT_TIMESTAMP. Whether to automatically modify the timestamp field to the current timestamp when updating other fields. For the first timestamp field is present, the default is On update CURRENT_TIMESTAMP, other timestamp field, you do not have this behavior.

2, default CURRENT_TIMESTAMP and On update CURRENT_TIMESTAMP a column can be modified at the same time. Both properties are modified in any column is defined as a timestamps_with_niladic, a table can have only a maximum of a column is defined as timestamps_with_niladic. If more than one, then the construction of the table fail.

These are the definition of rules that define the behavior of built form. After the completion of construction of the table show create table xxx can be used to verify the above rules. Once you have defined table, use the rules we look at the timestamp.

=2013-05-05 Modify

In the modified version of MySQL 5.6 above rules, allowing a table may have a plurality of various attributes of the timestamp fields have

Use Rules

1, is provided in a timestamp field insert or update statement is null

a) If the column is not null, the timestamp is set to the current

b) if the column is allowed to null, it is set to null

2, if the default at the time of insert, (the value of the column insert statement is not specified), is equivalent to the assigned default value. Specific value is determined by the default section.

3, if there is a column On update CURRENT_TIMESTAMP attribute, modify this value timestamp column modifications to the current timestamp when the Bank's other columns.

Guess you like

Origin blog.csdn.net/Edu_enth/article/details/85235482