MySQL unique 注意
刚才修改表结构:
1 alter table room add CONSTRAINT roomname_unique UNIQUE(roomname);
结果提示如下错误:
1 ERROR 1071 : Specified key was too long; max key length is 767 bytes
搞了一会没明白,上网搜了。
原因如下:
1 +----------+---------------+------+-----+---------+----------------+ 2 | Field | Type | Null | Key | Default | Extra | 3 +----------+---------------+------+-----+---------+----------------+ 4 | roomid | int(10) | NO | PRI | NULL | auto_increment | 5 | roomname | varchar(1024) | NO | | | | 6 | capacity | int(10) | NO | | 8 | | 7 | cost | decimal(10,0) | NO | | 0 | | 8 | count | decimal(10,0) | NO | | 0 | | 9 | status | int(1) | NO | | 0 | | 10 | comment | varchar(1024) | YES | | | | 11 | deleted | int(1) | NO | | 1 | | 12 +----------+---------------+------+-----+---------+----------------+
我的roomname字段长度设定的是varchar(1024),1024长于767bytes。
database维护索引是需要开销的,所以数据库不会使它过大。Mysql数据库的varchar长度被限制在0~65535之间。