1.1 新增字段
ALTER TABLE shanghai_info ADD created DATETIME;
3.1 修改表名
alter table ts01 rename to ts01_new;
3.2 修改字段
ALTER TABLE shanghai_info MODIFY created DATETIME DEFAULT NULL COMMENT "新增时间";
3.3 替换字段中的某个字符串
UPDATE tableName SET phone = REPLACE(phone,'.000000','')
4.1 清空表数据
4.2 删除某字段
ALTER TABLE table_name DROP COLUMN column_name;
5.1 查询重复
如,查找重复记录是根据单个字段(peopleId)来判断。
SELECT * FROM Tpeople
WHERE peopleId IN ( SELECT peopleId FROM Tpeople GROUP BY peopleId HAVING COUNT(peopleId) > 1)
根据多个字段来判断
SELECT * FROM illegal_info
WHERE (illegal_code, city_id, reason) IN ( SELECT illegal_code, city_id, reason FROM illegal_info GROUP BY illegal_code, city_id, reason HAVING COUNT(*) > 1) ORDER BY illegal_code;
5.2 删除表中多余的重复记录
例如,重复记录是根据单个字段(peopleId)来判断,只保留最先增加的记录,下面是保留 ID 最小的记录
DELETE FROM test WHERE id NOT IN (
SELECT a.id FROM (
SELECT MIN(id) AS id FROM test GROUP BY username HAVING COUNT(username) > 1
)AS a
) AND username IN (
SELECT b.username FROM (
SELECT username FROM test GROUP BY username HAVING COUNT(username) > 1
) AS b
);
根据多个字段进行删除,只留有 rowid 最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
6.1 添加普通索引
ALTER TABLE shanghai_info ADD INDEX (car_number);
ALTER TABLE llegal_info ADD UNIQUE INDEX [索引名](字段1, 字段2, 字段3);
CREATE TABLE `hunan_car` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`car_number` varchar(255) DEFAULT NULL COMMENT '车牌号',
`vin` varchar(255) DEFAULT NULL COMMENT '车架号',
`eng` varchar(255) DEFAULT NULL COMMENT '发动机',
`car_type` varchar(255) DEFAULT NULL COMMENT '车辆类型',
`status` int(11) DEFAULT NULL COMMENT '读取状态,0 未读 / 1 已读',
`success` int(11) DEFAULT NULL COMMENT '读取成功状态 0 爬取失败 / 1 爬取成功',
PRIMARY KEY (`id`) USING BTREE,
INDEX `car_number` (`car_number`)
) ENGINE=InnoDB AUTO_INCREMENT=110280 DEFAULT CHARSET=utf8;
6.2 添加唯一索引
ALTER TABLE shanghai_info ADD UNIQUE (car_number);
CREATE TABLE `hunan_car` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`car_number` varchar(255) DEFAULT NULL COMMENT '车牌号',
`vin` varchar(255) DEFAULT NULL COMMENT '车架号',
`eng` varchar(255) DEFAULT NULL COMMENT '发动机',
`car_type` varchar(255) DEFAULT NULL COMMENT '车辆类型',
`status` int(11) DEFAULT NULL COMMENT '读取状态,0 未读 / 1 已读',
`success` int(11) DEFAULT NULL COMMENT '读取成功状态 0 爬取失败 / 1 爬取成功',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `car_number`` (`car_number``)
) ENGINE=InnoDB AUTO_INCREMENT=110280 DEFAULT CHARSET=utf8;
6.3 创建组合索引
ALTER TABLE illegal_info ADD INDEX idx_reason_money_city_id (reason,money,city_id);
CREATE TABLE `illegal_info` (
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',
`illegal_code` varchar(6) DEFAULT NULL COMMENT '违章代码,截取第二个字符则为扣分,扣分为7则为12',
`reason` varchar(300) DEFAULT NULL COMMENT '违章信息',
`city_id` int(11) DEFAULT '0' COMMENT '城市id',
`money` varchar(10) DEFAULT NULL COMMENT '罚款',
PRIMARY KEY (`id`),
KEY `idx_reason_money_city_id` (`reason`,`money`,`city_id`)
) ENGINE=InnoDB AUTO_INCREMENT=16448 DEFAULT CHARSET=utf8 COMMENT='各地违章信息扣分罚款详情表'
6.4 删除索引
ALTER TABLE shanghai_info DROP INDEX cphm;