最近需要用mysql做一个配置, 类似于配置文件, mysql表的列有key,value值, 此时, 可以考虑unique key, 来看看:
mysql> show create table yyy;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| yyy | CREATE TABLE `yyy` (
`cid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`strKey` varchar(64) NOT NULL,
`strValue` varchar(64) NOT NULL,
PRIMARY KEY (`cid`),
UNIQUE KEY `strKey` (`strKey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql>
mysql>
mysql> insert into yyy set strKey='timeout', strValue='200';
Query OK, 1 row affected (0.01 sec)
mysql> insert into yyy set strKey='timeout', strValue='200';
ERROR 1062 (23000): Duplicate entry 'timeout' for key 'strKey'
mysql>
可见, strKey具有唯一性, 如果想去掉这个唯一性, 改表就行: alter table yyy drop key strKey;