DROP TABLE IF EXISTS `city_weather_data`; CREATE TABLE `city_weather_data` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主鍵', `city_id` varchar(20) DEFAULT NULL COMMENT '城市id', `weather_condition` varchar(20) DEFAULT NULL COMMENT '天氣情況)', `temp` varchar(10) DEFAULT NULL COMMENT '溫度', `wind_level` varchar(10) DEFAULT NULL COMMENT '風級', `updatetime` varchar(20) DEFAULT NULL COMMENT '更新時間', PRIMARY KEY (`id`), UNIQUE KEY `cityId_updatetime_unique` (`city_id`,`updatetime`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=58 DEFAULT CHARSET=utf8;
其中,設置惟一索引:保證當 city_id
和 updatetime
均相同時的惟一性spring
insert into city_weather_data (city_id,weather_condition,temp, wind_level,updatetime) values ('1101', '多雲','34','3','2018-02-27 12:10:00')
執行兩次,第一次成功,第二次報錯:sql
Error : Duplicate entry 'city_id-updatetime' for key 'cityId_updatetime_unique'
若這段是利用mybatis
進行數據的插入,第二次執行時,將會拋出org.springframework.dao.DuplicateKeyException
異常。mybatis
explain select t.id, t.city_id, t.weather_condition, t.temp, t.wind_level, t.updatetime from city_weather_data t where t.city_id = '1101' and t.updatetime between '2018-02-26 18:40:00' and '2018-02-27 12:20:00';
返回信息:測試
type = range
另外:between
是閉區間,eg:返回的數據包含 2018-02-26 18:40:00
和 2018-02-27 12:20:00
設計