Flexible Use of MySQL database TIMESTAMP settings Default Value

Source: Internet
Author: User

MySQLDatabaseTIMESTAMPSetting the default value is what we will introduce in this article. We know that CURRENT_TIMESTAMP: When I update this record, this field of this record will not change.

CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP: When I UPDATE this record, this field of this record will change. Changes instantly to the Update time. 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 .)

If multiple TIMESTAMP columns exist, only the first one is automatically updated.

Next we will look at several practical examples:

#1 create a table dj1 with two timestamp fields.

 
 
  1. root@localhost:test >create table dj1 (a char(1), b timestamp ,c timestamp);   
  2. Query OK, 0 rows affected (0.01 sec) 

#2 insert two rows of data, only assigned to column

 
 
  1. root@localhost:test >insert into dj1 values (1,null,null);  
  2. Query OK, 1 row affected (0.00 sec)  
  3. root@localhost:test >insert into dj1 values (2,null,null);   
  4. Query OK, 1 row affected (0.00 sec) 

#3 query the inserted data. Both columns B and c use current timestamp as the default value.

 
 
  1. root@localhost:test >select * from dj1;  
  2. +------+---------------------+---------------------+  
  3. | a | b | c |  
  4. +------+---------------------+---------------------+  
  5. | 1 | 2009-09-09 13:48:40 | 2009-09-09 13:48:40 |   
  6. | 2 | 2009-09-09 13:48:44 | 2009-09-09 13:48:44 |   
  7. +------+---------------------+---------------------+  
  8. 2 rows in set (0.00 sec) 

#4 update a row of data. It is found that the timestamp of Column B is automatically updated, while the value of column c remains unchanged.

 
 
  1. root@localhost:test >update dj1 set a=9 where a=1;   
  2. Query OK, 1 row affected (0.00 sec)  
  3. Rows matched: 1 Changed: 1 Warnings: 0  
  4. root@localhost:test >select * from dj1;  
  5. +------+---------------------+---------------------+  
  6. | a | b | c |  
  7. +------+---------------------+---------------------+  
  8. | 9 | 2009-09-09 13:49:08 | 2009-09-09 13:48:40 |   
  9. | 2 | 2009-09-09 13:48:44 | 2009-09-09 13:48:44 |   
  10. +------+---------------------+---------------------+  
  11. 2 rows in set (0.00 sec) 

#5 update another column, as shown in Figure #4

 
 
  1. root@localhost:test >update dj1 set a=8 where a=2;   
  2. Query OK, 1 row affected (0.00 sec)  
  3. Rows matched: 1 Changed: 1 Warnings: 0  
  4. root@localhost:test >select * from dj1;  
  5. +------+---------------------+---------------------+  
  6. | a | b | c |  
  7. +------+---------------------+---------------------+  
  8. | 9 | 2009-09-09 13:49:08 | 2009-09-09 13:48:40 |   
  9. | 8 | 2009-09-09 13:49:36 | 2009-09-09 13:48:44 |   
  10. +------+---------------------+---------------------+  
  11. 2 rows in set (0.00 sec) 

#6 create a unique index on column B

 
 
  1. root@localhost:test >create unique index dj1_idx_u1 on dj1(b);  
  2. Query OK, 2 rows affected (0.01 sec)  
  3. Records: 2 Duplicates: 0 Warnings: 0 

#7 Update column a of all rows to report a uniqueness conflict.

 
 
  1. root@localhost:test >update dj1 set a=1;  
  2. ERROR 1062 (23000): Duplicate entry '2009-09-09 13:54:45' for key 'dj1_idx_u1' 

Conclusion:

1. The first timestamp Field in the DEFAULT MySQL table is not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP attribute, which must be explicitly defined to change this behavior.

2. MySQL only allows one timestamp field to have the [DEFAULT CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP] attribute. I understand that either DEFAULT CURRENT_TIMESTAMP or DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

3. Modify Field Property Values

 
 
  1. show create table tbl_ledgerrecord;  
  2. alter table tbl_ledgerrecord change intoStorageDate  intoStorageDate timestamp DEFAULT CURRENT_TIMESTAMP; 

Here is an introduction to the flexible use of MySQL database TIMESTAMP settings for default values. I hope this introduction will be helpful to you!

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.