发现两条MYSQL的使用技巧,当然可能很早就已经被人们熟知,但是我是刚刚才发现的
一、timstamp类型的默认值
建表的时候对于表中第一个timestamp类型的字段应该指定default值,否则MYSQL默认会在在建表语句后面增加一个关于Update 的触发器。
比如:
mysql> create table ta ( tt timestamp, k varchar(10));
mysql> show create table ta;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ta | CREATE TABLE `ta` (
`tt` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`k` varchar(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

这样在只update 字段k的时候 ,tt字段会自动被更新为当前时间的。这样就会引起程序的错误了,需要格外关注。
二、关于字符串大小写问题
如果对上表插入两条记录
mysql> insert into ta(k) values("a");
mysql> insert into ta(k) values("A");
那么如下的 SELECT语句会令我们的程序产生错误。
mysql> select * from ta where k = 'a';
+---------------------+------+
| tt | k |
+---------------------+------+
| 2006-08-24 23:19:27 | a |
| 2006-08-24 23:19:29 | A |
+---------------------+------+
mysql> select * from ta where k = BINARY 'a';
+---------------------+------+
| tt | k |
+---------------------+------+
| 2006-08-24 23:19:27 | a |
+---------------------+------+因为WHERE子句没有区分大小写。
如果想区分大小写,需要在建表时对varchar类型 增加BINARY选项,或者查询的时候增加BINARY选项。