-- My_Biao表增加productIDs字段:
-- 改变内容:+ProductIDs: varchar comment '多个产品id,逗号分隔'
-- SQL语句:
alter table `My_Biao` add column `ProductIDs` varchar(512) NOT NULL DEFAULT '' COMMENT '多个产品id,逗号分隔';
-- My_BiaoShop表增加cityIDs和Anti字段:
-- 改变内容:+CityIDs: varchar comment '多个城市id,逗号分隔'。+Anti: int comment '城市是否反选标志位:0-正选,1-反选'
-- SQL语句:
alter table `My_BiaoShop` add column `CityIDs` mediumtext COMMENT '多个城市id,逗号分隔的字符串';
alter table `My_BiaoShop` add column `Anti` int(11) NOT NULL DEFAULT '0' COMMENT '城市是否反选标志位:0-正选,1-反选';
update My_BiaoShop ap set ap.Type=1 where ap.ProType in (0,1,2,3,4,5,6,7,8,9,10,11,12,14,19);
alter table My_Aux modify column Reason varchar(512);
desc `My_City`;
`Status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '1-有效,0-无效';
alter table `My_City` modify column Status tinyint(4) NOT NULL DEFAULT '1' COMMENT '1-有效,0-无效';
alter table My_All comment '所有城市的数据每周更新';
CREATE TABLE `My_Util` (
`Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`UtilPackage` varchar(256) NOT NULL DEFAULT '' COMMENT '工具包名',
`Util` text DEFAULT NULL COMMENT '工具(英文逗号分隔的字符串)',
`Business` varchar(256) NOT NULL DEFAULT '' COMMENT '适用行业',
`ShopType` text DEFAULT NULL COMMENT '适用频道(一二级频道的JSON串)',
`Status` int(11) NOT NULL DEFAULT '1' COMMENT '1-有效,0-无效',
`AddTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
`UpdateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`Id`),
UNIQUE KEY `UNIQUE_IX_UtilPackage` (`UtilPackage`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `My_Price` (
`Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`ShopType1` int(11) NOT NULL COMMENT '一级频道(缺省时是-1)',
`ShopType2` int(11) NOT NULL COMMENT '二级频道(缺省时是-1)',
`UtilPackageId` int(11) NOT NULL DEFAULT '-1' COMMENT '工具包Id(缺省时是-1)',
`Price` int(11) NOT NULL COMMENT '价格',
`AddTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
`UpdateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`Id`),
KEY `IX_UtilPackageId` (`UtilPackageId`),
UNIQUE KEY `IX_ShopType1_ShopType2_UtilPackageId` (`ShopType1`,`ShopType2`,`UtilPackageId`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='价格配置';
show index from My_City;
create index UNIQUE_IX_Business_ShopType on My_City(Business,ShopType);
drop index UNIQUE_IX_Business_ShopType on My_City;
create index ix_UtilId on My_Util(UtilId);
create index ix_UtilId_TypeId on My_Util(UtilId,TypeId);
drop index ix_UtilId on My_Util;
drop index ix_UtilId_TypeId on ix_UtilId_TypeId;
CREATE TABLE `Class`(
`Id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`ClassId` int NOT NULL COMMENT '班级表对应主键id',
`StudentId` int NOT NULL COMMENT '学生id',
`Type` tinyint NOT NULL COMMENT '类型',
`Comment` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '备注',
`Add_time` timestamp NOT NULL DEFAULT NOW() COMMENT '添加时间',
`Update_time` timestamp NOT NULL DEFAULT NOW() ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`Id`),
UNIQUE KEY `IX_ClassId_StudentId` (`ClassId`,`StudentId`),
KEY idx_update_time(Update_time)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci comment '班级学生表';
ALTER TABLE table_name ADD col_name datatype, ADD col_name2 datatype;
ALTER TABLE table_name ADD [UNIQUE] INDEX index_name (index_col_name, ...), ADD INDEX index_name2 (index_col_name, ...);
ALTER TABLE table_name MODIFY col_name datatype, MODIFY col_name2 datatype;
ALTER TABLE table_name DROP INDEX index_name, DROP INDEX index_name2;
ALTER TABLE table_name DROP col_name, DROP col_name2;
SELECT colName1,colName2
FROM tableName
WHERE where_condition
GROUP BY colName1,colName2
HAVING having_condition
ORDER BY colName1,colName2
LIMIT offset,rowCount;
INSERT INTO tableName(colName1,colName2) values(expr1,expr2), (expr1,expr2);
UPDATE tableName SET colName1=expr1, colName2=expr2 WHERE where_condition;
DELETE FROM tableName WHERE where_condition;
-- 修改字段名:alter table 表名 chang 旧字段名 新字段名 字段类型 alter table tb_name change col_name new_col_name new_col_name_type;
alter table Wind_Emp change seal sal decimal(7,2) not null;
-- 新增一个字段:ALTER TABLE table_name ADD col_name datatype, ADD col_name2 datatype;
alter table Wind_Emp add comm decimal(7,2) not null;
(1)很详细的博客:https://www.cnblogs.com/clsn/p/8087501.html#auto_id_9
(1)https://www.csdn.net/gather_23/MtTaMgxsNjcyMy1ibG9n.html
注:场景描述如下:为了避免数据重复,同时也缓解数据库的压力,需要建立唯一索引来约束数据库字段。但是,这样就会可能产生org.springframework.dao.DuplicateKeyException异常。