模板 日期-标识-数据库名称.sql
关注点:
1.插入重复 直接在原insert 语句后面加 ignore关键字,该方式必须表有主键或者唯一索引
2.新增字段、新加索引,新建存储过程,查系统表,需要特别注意的是,新建存储过程这的脚本需要重定义分隔符,因为存储过程的创建是多条语句的,而mysql默认的分隔符是分号,如果不重新定义分隔符,会出现语法问题,另外,创建完存储过程需要恢复分隔符
3.存储过程创建,执行完就删除,以避免冗余
1.新建字段
-- 创建一个存储过程判断字段是否存在
drop
PROCEDURE
if EXISTS add_column;
DELIMITER ;;
create
PROCEDURE
add_column()
BEGIN
-- TABLE_SCHEMA 数据库名
-- table_name 表名
-- COLUMN_NAME 列名
if
not
EXISTS(
select
1
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA=
'cims_db'
AND
table_name=
'on_shelf'
AND
COLUMN_NAME=
'status_type'
)
then
-- 要执行的字段新增
ALTER
TABLE
`cims_db`.`on_shelf`
ADD
COLUMN
`status_type`
VARCHAR
(50)
DEFAULT
'0'
NULL
COMMENT
'状态类型'
AFTER
`status`;
ELSE
select
'exist'
;
end
if;
end
;;
DELIMITER ;
CALL add_column();
-- 结束存储过程创建
-- 简化版
-- 表名,列名,数据类型,备注
-- 注意:表名不可待数据库前缀
call add_column_proc(
'brand'
,
'test1'
,
'varchar(10)'
,
'测试'
);
|
2.新建索引
DROP
PROCEDURE
IF EXISTS index_add;
DELIMITER ;;
CREATE
PROCEDURE
index_add()
BEGIN
DECLARE
CurrentDatabase
VARCHAR
(100);
SELECT
DATABASE
()
INTO
CurrentDatabase;
IF
NOT
EXISTS (
SELECT
*
FROM
information_schema.
statistics
WHERE
table_schema=CurrentDatabase
AND
table_name =
'oms_orderdetail'
AND
index_name =
'index_sku_code_query'
)
THEN
ALTER
TABLE
`order_db`.`oms_orderdetail`
ADD
INDEX
`index_sku_code_query` (`sku_code`);
else
select
'exist'
;
END
IF;
END
;;
DELIMITER ;
CALL index_add();
简化版
-- 表名,索引名,索引类型(可以为空字符串),索引方法(可以为空字符串),列名(支持组合列,逗号分隔)
-- 索引类型可选值:unique,normal,full text
-- 索引方法可选值:btree,hash
-- 注意表名不可待库名前缀
CALL add_index_proc(
'brand'
,
'brand_name'
,
'unique'
,
'btree'
,
'code'
);
CALL add_index_proc(
'brand'
,
'brand_name'
,
''
,
''
,
'code'
);
-- 索引方法、索引名称为空
CALL add_index_proc(
'brand'
,
'brand_name'
,
'unique'
,
'btree'
,
'code,name'
);
-- 组合索引
|
3.插入数据
use pms_db;
insert
ignore
into
`sys_menu` (`id`, `
name
`, `parent_id`, `parent_ids`, `href`, `target`, `icon`, `is_show`, `sort`, `remarks`, `permission`, `sub_system_code`, `create_date`, `create_by`, `last_update_date`, `last_update_by`, `del_flag`)
values
(
'96ca6b17a5174f538fde4da40b2a6927'
,
'采购订单执行明细表'
,
'bec8d0d70f014c818afb45fc342fd20f'
,
'0,bec8d0d70f014c818afb45fc342fd20f,'
,
'/purchaseOrderDetailPart/listView.do'
,
NULL
,
NULL
,
'1'
,
'407'
,
''
,
''
,
'purms'
,
'2016-10-11 11:33:36'
,
'1'
,
'2016-10-11 11:34:29'
,
'1'
,
'0'
);
insert
ignore
into
`sys_menu` (`id`, `
name
`, `parent_id`, `parent_ids`, `href`, `target`, `icon`, `is_show`, `sort`, `remarks`, `permission`, `sub_system_code`, `create_date`, `create_by`, `last_update_date`, `last_update_by`, `del_flag`)
values
(
'22c21ad5e6f64a7d8f3850bc8c79ed3c'
,
'查看'
,
'96ca6b17a5174f538fde4da40b2a6927'
,
'0,bec8d0d70f014c818afb45fc342fd20f,96ca6b17a5174f538fde4da40b2a6927,'
,
''
,
NULL
,
NULL
,
'0'
,
'40702'
,
''
,
'purms:purchaseOrderDetailPart:view'
,
'purms'
,
'2016-10-15 14:09:49'
,
'1'
,
NULL
,
NULL
,
'0'
);
insert
ignore
into
`sys_menu` (`id`, `
name
`, `parent_id`, `parent_ids`, `href`, `target`, `icon`, `is_show`, `sort`, `remarks`, `permission`, `sub_system_code`, `create_date`, `create_by`, `last_update_date`, `last_update_by`, `del_flag`)
values
(
'ce57ff600c584216ba46f28df556b2fe'
,
'采购订单明细导出'
,
'96ca6b17a5174f538fde4da40b2a6927'
,
'0,bec8d0d70f014c818afb45fc342fd20f,96ca6b17a5174f538fde4da40b2a6927,'
,
''
,
NULL
,
NULL
,
'0'
,
'40701'
,
''
,
'purms:purchaseOrderDetailPart:export'
,
'purms'
,
'2016-10-15 14:08:10'
,
'1'
,
'2016-10-15 14:08:56'
,
'1'
,
'0'
);
|
4.新建表
use cims_db;
-- DROP TABLE cims_db.`no_goods_commodity`;
CREATE
TABLE
if
not
exists `no_goods_commodity` (
`id`
varchar
(32)
NOT
NULL
DEFAULT
''
COMMENT
'id'
,
`code`
varchar
(50)
DEFAULT
NULL
COMMENT
'商品编码'
,
`
name
`
varchar
(50)
DEFAULT
NULL
COMMENT
'商品名称'
,
`bar_code`
varchar
(50)
DEFAULT
NULL
COMMENT
'商品条形码'
,
`buy_type`
varchar
(50)
DEFAULT
NULL
COMMENT
'购买类型'
,
`category_name`
varchar
(50)
DEFAULT
NULL
COMMENT
'分类名称'
,
`category_id`
varchar
(50)
DEFAULT
NULL
COMMENT
'分类id'
,
`vendor_id`
varchar
(50)
DEFAULT
NULL
COMMENT
'供应商id'
,
`vendor_code`
varchar
(50)
DEFAULT
NULL
COMMENT
'供应商编码'
,
`vendor_name`
varchar
(50)
DEFAULT
NULL
COMMENT
'供应商名称'
,
`preferential_price`
varchar
(50)
DEFAULT
NULL
COMMENT
'优惠价'
,
`stock_limit`
varchar
(50)
DEFAULT
NULL
COMMENT
'0:WMS决定商品可销售数量;1:自定义商品可销售数量'
,
`area_category`
varchar
(50)
DEFAULT
NULL
COMMENT
' 是否区域性商品(0:是,1:否).'
,
`status`
varchar
(50)
DEFAULT
NULL
COMMENT
'商品状态'
,
`remarks`
varchar
(50)
DEFAULT
NULL
COMMENT
'备注'
,
`create_by`
varchar
(50)
DEFAULT
NULL
COMMENT
'创建者'
,
`create_date`
timestamp
NULL
DEFAULT
NULL
COMMENT
'创建日期'
,
`last_update_by`
varchar
(50)
DEFAULT
NULL
COMMENT
'更新者'
,
`last_update_date`
timestamp
NULL
DEFAULT
NULL
COMMENT
'更新日期'
,
`del_flag`
varchar
(50)
DEFAULT
NULL
COMMENT
'删除标记(0:正常;1:删除;2:审核)'
,
PRIMARY
KEY
(`id`)
) ENGINE=InnoDB
DEFAULT
CHARSET=utf8 COMMENT=
'无货商品汇总表'
;
|
5.通用存储过程
-- 字段新增
drop
PROCEDURE
if EXISTS add_column_proc;
CREATE
PROCEDURE
add_column_proc(t_name
varchar
(100),col_name
varchar
(100),datatype
varchar
(100),remark
varchar
(255))
BEGIN
DECLARE
CurrentDatabase
VARCHAR
(100);
DECLARE
sql_str
VARCHAR
(1000);
SELECT
DATABASE
()
INTO
CurrentDatabase;
if
not
EXISTS(
select
1
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA=CurrentDatabase
AND
table_name=t_name
AND
COLUMN_NAME=col_name)
then
-- 要执行的字段新增
set
@sql_str=concat(
'ALTER TABLE '
,t_name ,
' ADD COLUMN '
,col_name,
' '
, datatype,
' COMMENT \''
,remark,
'\''
);
-- set @sql_str='ALTER TABLE brand ADD COLUMN test varchar(10) DEFAULT NULL NULL COMMENT \'test\'';
-- EXECUTE sql_str;
PREPARE
st
from
@sql_str;
EXECUTE
st;
DEALLOCATE
PREPARE
st;
end
if;
END
;
-- 支持默认值 约束设置
call add_column_proc(
'brand'
,
'test1'
,
'varchar(10)'
,
'测试'
);
call add_column_proc(
'brand'
,
'test1'
,
'varchar(10) default null null'
,
'测试'
);
-- 索引创建
drop
PROCEDURE
if EXISTS add_index_proc;
CREATE
PROCEDURE
add_index_proc(t_name
varchar
(100),_index_name
varchar
(100),_index_type
varchar
(100),_index_method
varchar
(100),col_names
varchar
(100))
BEGIN
DECLARE
CurrentDatabase
VARCHAR
(100);
DECLARE
sql_str
VARCHAR
(1000);
DECLARE
default_index_type
VARCHAR
(100)
DEFAULT
'index'
;
DECLARE
flag
int
;
SELECT
DATABASE
()
INTO
CurrentDatabase;
SELECT
1
FROM
information_schema.
statistics
WHERE
table_schema=CurrentDatabase
AND
table_name = t_name
AND
index_name = _index_name ;
select
CurrentDatabase,t_name,_index_name,flag;
if
not
EXISTS(
SELECT
1
FROM
information_schema.
statistics
WHERE
table_schema=CurrentDatabase
AND
table_name = t_name
AND
index_name = _index_name )
then
-- 要执行的字段新增
set
@default_index_type=
'index'
;
set
@sql_str=CONCAT(
'ALTER TABLE '
,t_name);
-- set @sql_str=CONCAT(@sql_str,default_index_type);
if _index_type!=
''
THEN
set
@default_index_type=CONCAT(_index_type,
' '
,@default_index_type);
end
if;
set
@sql_str=concat(@sql_str,
' add '
,@default_index_type,
' '
,_index_name,
'('
,col_names,
')'
);
SELECT
@sql_str,_index_type,default_index_type,col_names;
-- ALTER TABLE `brand` ADD FULLTEXT INDEX `index_s` (`name`) USING BTREE ;
if _index_method!=
''
THEN
set
@sql_str=CONCAT(@sql_str,
' using '
,_index_method);
end
if;
PREPARE
st
from
@sql_str;
EXECUTE
st;
DEALLOCATE
PREPARE
st;
end
if;
END
;
CALL add_index_proc(
'brand'
,
'brand_name'
,
'unique'
,
'btree'
,
'code'
);
|