mysql 笔记

1. 备份一张表或条件表

# 备份 rs_measure
create table rs_measure_on_2021_01_28
as (select * from rs_measure);
create table if not exists rs_cdk_service_cat_rel
(
    relationId int auto_increment comment '主键'
        primary key,
    rsServiceCatId int null comment '乐家服务大类ID',
    rsServiceCatName varchar(50) not null comment '乐家服务大类名称',
    cdkServiceCatId varchar(50) null comment 'CDK服务大类ID',
    cdkServiceCatName varchar(50) not null comment 'CDK服务大类名称'
)
    comment '乐家CDK服务大类对应关系表';

2. 删除一张表

truncate table table_name;

3. 更新表:更新多个字段时用逗号:注意全表更新要有索引

update mall_items a
left join a_temp_mall_item_sub_service temp on temp.mallItemId = a.itemId
set a.subServiceId = temp.subServiceId, a.subServiceName = temp.subServiceName;
update mall_items a
set subServiceId = (
    select subServiceId
    from a_temp_mall_item_sub_service sub
    where sub.subServiceId = a.subServiceId
) where a.itemId = 12149677;

4. 用navicat 的导入向导可以导入excel数据,在生产环境新建临时表,导入进临时表,在关联临时表和正式表进行更新数据

5. 很重要:createTime 插入时更新,updateTime 更新时才更新:

ALTER TABLE rs_sub_service_cat
    MODIFY COLUMN createTime timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    MODIFY COLUMN updateTime timestamp NULL DEFAULT null ON UPDATE CURRENT_TIMESTAMP;

你可能感兴趣的:(mysql)