MySQL database TIMESTAMP sets the default value of several Application Instances

Source: Internet
Author: User
Tags table definition

Previously we introduced:MySQLDatabaseTIMESTAMPSet the default value for flexible use, this next time we will introduce several MySQL database TIMESTAMP set the default value of several application instances, hope to help you.

#1 check the table definition. You can see that column B has an attribute ON UPDATE CURRENT_TIMESTAMP. As a result, the column data is automatically updated even if the column is not involved. On the other hand, the default value of column c is '2017-00-00 00:00:00 ', and the actual insertion has been automatically assigned current_timestamp.

 
 
  1. root@localhost:test >show create table dj1\G  
  2. *************************** 1. row ***************************  
  3. Table: dj1  
  4. Create Table: CREATE TABLE `dj1` (  
  5. `a` char(1) COLLATE utf8_bin DEFAULT NULL,  
  6. `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  
  7. `c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  
  8. UNIQUE KEY `dj1_idx_u1` (`b`)  
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin 
  10. 1 row in set (0.00 sec) 

#2 create a table dj2 and column B does not contain automatic attribute update.

 
 
  1. root@localhost:test >CREATE TABLE `dj2` (  
  2. -> `a` char(1) COLLATE utf8_bin DEFAULT NULL,  
  3. -> `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,  
  4. -> `c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  
  5. -> UNIQUE KEY `dj1_idx_u1` (`b`)  
  6. -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;  
  7. Query OK, 0 rows affected (0.01 sec) 

#3 insert dj2 Test Data

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

#4 View dj2 data

 
 
  1. root@localhost:test >select * from dj2;  
  2. +------+---------------------+---------------------+  
  3. | a | b | c |  
  4. +------+---------------------+---------------------+  
  5. | 1 | 2009-09-09 14:02:55 | 2009-09-09 14:02:55 |   
  6. | 2 | 2009-09-09 14:03:00 | 2009-09-09 14:03:00 |   
  7. +------+---------------------+---------------------+  
  8. 2 rows in set (0.00 sec) 

# Create a unique index on 5dj2

 
 
  1. Root @ localhost: test> create unique index dj2_idx_u1 on dj2 (B );
  2. Query OK, 2 rows affected (0.02 sec)
  3. Records: 2 Duplicates: 0 Warnings: 0
  4. # Data updated successfully
  5. Root @ localhost: test> update dj2 set a = 9;
  6. Query OK, 2 rows affected (0.00 sec)
  7. Rows matched: 2 Changed: 2 Warnings: 0
  8. Root @ localhost: test> select * from dj2;
  9. + ------ + --------------------- +
  10. | A | B | c |
  11. + ------ + --------------------- +
  12. | 9 | 14:02:55 | 14:02:55 |
  13. | 9 | 14:03:00 | 14:03:00 |
  14. + ------ + --------------------- +
  15. 2 rows in set (0.00 sec)

#6 create a table dj3. The default value of Column B is CURRENT_TIMESTAMP. The default value of column c is CURRENT_TIMESTAMP, which includes automatic attribute update. An error is returned and no behavior is allowed.

 
 
  1. root@localhost:test >CREATE TABLE `dj3` (  
  2. -> `a` char(1) COLLATE utf8_bin DEFAULT NULL,  
  3. -> `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,  
  4. -> `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  
  5. -> UNIQUE KEY `dj1_idx_u1` (`b`)  
  6. -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;  
  7. ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause 

#7 create a table dj4. The default value of Column B is CURRENT_TIMESTAMP, and the default value of column c is '2017-00-00 00:00:00 '. attributes are automatically updated. An error is returned and no behavior is allowed.

 
 
  1. root@localhost:test >CREATE TABLE `dj4` (  
  2. -> `a` char(1) COLLATE utf8_bin DEFAULT NULL,  
  3. -> `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,  
  4. -> `c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,  
  5. -> UNIQUE KEY `dj1_idx_u1` (`b`)  
  6. -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;  
  7. ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause 

#8 create a table dj5. The default value of Column B is CURRENT_TIMESTAMP and the attribute is automatically updated. The default value of column c is CURRENT_TIMESTAMP. An error is returned and no behavior is allowed.

 
 
  1. root@localhost:test >CREATE TABLE `dj5` (  
  2. -> `a` char(1) COLLATE utf8_bin DEFAULT NULL,  
  3. -> `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  
  4. -> `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,  
  5. -> UNIQUE KEY `dj1_idx_u1` (`b`)  
  6. -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;  
  7. ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause 

The above are several application instances for setting the default value of TIMESTAMP in the MySQL database. This article will introduce them here. I hope this introduction will help you gain some benefits!

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.