When MySQL builds a table, you need to add two time columns to record the creation and modification time of the current record, respectively. Good. The following is the build table statement:
[SQL]View PlainCopy
- DROP TABLE IF EXISTS ' mytesttable ';
- CREATE TABLE ' mytesttable ' (
- ' ID ' int (one) is not NULL,
- 'name ' varchar (255) DEFAULT NULL,
- ' Createtime ' datetime DEFAULT current_timestamp,
- ' UpdateTime ' datetime DEFAULT current_timestamp on UPDATE current_timestamp,
- PRIMARY KEY (' id ')
- ) Engine=innodb DEFAULT CHARSET=GBK;
After execution completes, insert the record in the table. Then modify a record:
[SQL]View PlainCopy
- Update mytesttable set name = ' Wer ' where id = 2
View the data in the table again: you can see that createtime represents the time that this record was created, and UpdateTime records the time that the current record was modified.
MySQL Add time column (for record creation time and modified time)