mysql知识备忘(建表存储过程初次安装启动)
Posted zfszhangyuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql知识备忘(建表存储过程初次安装启动)相关的知识,希望对你有一定的参考价值。
建表模板:
--删除重复数据 去重
DROP TABLE IF EXISTS `tb_cust_tel`;
CREATE TABLE `tb_cust_tel` (
`ID` bigint(18) NOT NULL AUTO_INCREMENT,
`cust_id` bigint(18) NOT NULL,
`tel_id` bigint(18) NOT NULL,
`tel_ext_id` int(11) DEFAULT NULL,
`cust_status` int(2) DEFAULT '0',
`input_time` date DEFAULT NULL,
`input_emp_id` bigint(18) DEFAULT NULL,
`modify_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modify_emp_id` bigint(18) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `idx_cust_tele_cust_id` (`cust_id`) USING BTREE,
KEY `idx_cust_tele_tel_id` (`tel_id`) USING BTREE,
KEY `index_STATUS` (`cust_status`) USING BTREE,
KEY `index_input_emp_id` (`input_emp_id`) USING BTREE,
KEY `index_modify_emp_id` (`modify_emp_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=56902126 DEFAULT CHARSET=utf8;
--字符串聚合函数
select cust_id ,GROUP_CONCAT(cat_code) from tmp_custid_zfs1 group by cust_id having count(*)>=2 li
SELECT student_name,
-> GROUP_CONCAT(DISTINCT test_score
-> ORDER BY test_score DESC SEPARATOR ' ')
-> FROM student
-> GROUP BY student_name;
默认为逗号 (‘,’)。通过指定SEPARATOR ''
-- 建索引
drop table tmp_id;
create table tmp_id (select min(id) as id from tmp_tel_0 group by tel );
CREATE INDEX index_id ON tmp_id (id) USING BTREE;
delete from tmp_tel_0 where id not in (select id from tmp_id)
查询数据库的表拼接字符串:
--查询数据库所以 表名 mysqldump
select CONCAT(' mysqldump -u$account -p$pwd db_ndc_2 ',table_name,' > /data/zhangfusheng/$time/',table_name,'.sql')table_name from information_schema.tables
where table_schema='db_ndc_2' and table_type='base table'
and table_name not like '%tmp%' and table_name not like '%temp%'
and table_name not like '%dawei%' and table_name not like '%DAWEI%';
select CONCAT('source /data/newdump/',table_name,'.sql')table_name from information_schema.tables
where table_schema='db_ndc_2' and table_type='base table'
and table_name not like '%tmp%' and table_name not like '%temp%'
and table_name not like '%dawei%' and table_name not like '%DAWEI%';
表join:
--表连接
select E.source,count(distinct A.area_code,A.tel) from tmp_tel_0 as A LEFT JOIN tb_telephone as B on A.tel=B.tel
LEFT JOIN tb_cust_tel as C on B.id=C.tel_id
LEFT JOIN tb_cust_tel_source as D on C.id=D.cust_tel_id
LEFT JOIN tb_source as E on E.id=D.source_type_id
group by E.source
带输入输出参数的存储过程:
--MySQL 带参数的存储过程
DROP PROCEDURE IF EXISTS `P_GET_CLASS_NAME`;
CREATE PROCEDURE P_GET_CLASS_NAME(IN ID int,OUT NAME VARCHAR(50))
BEGIN
IF(ID = 1) THEN
SET NAME = '一班';
END IF;
IF(ID = 2) THEN
SET NAME = '二班';
END IF;
END;
自己写的函数:
--判断一个字符串是数字的
CREATE FUNCTION `IsNum` (str VARCHAR(25)) RETURNS INT
BEGIN
DECLARE iResult INT DEFAULT 0;
IF ISNULL(str) THEN return 0; END IF;-- NULL 字符串
IF str = '' THEN return 0; END IF;-- 空字符串
SELECT str REGEXP '^[0-9]*$' INTO iResult;
IF iResult = 1 THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END
select IsNum('1234444444445a5')
--获取电话区号
drop FUNCTION get_areacode;
CREATE FUNCTION get_areacode (tel VARCHAR(255)) RETURNS VARCHAR(10)
BEGIN
DECLARE iResult VARCHAR(10) DEFAULT '';
IF ISNULL(tel) THEN return ''; END IF;-- NULL 字符串
IF tel = '' THEN return ''; END IF;-- 空字符串
select A.area_code INTO iResult from tb_get_areacode A where area_code=left(tel,A.len);
IF iResult <>'' and iResult is not null THEN
RETURN iResult;
ELSE
RETURN '';
END IF;
END
create table tb_get_areacode(
select distinct area_code,CHAR_LENGTH(area_code) as len from tb_region where area_code is not null
)
存储过程cursor:
create PROCEDURE omc_update_zfs()
BEGIN
DECLARE F_TEL_ADDR_ID INT;
DECLARE F_TEL_ADDR VARCHAR(255);
DECLARE L1 INT DEFAULT 0;
DECLARE F_MIN_MAX_ID VARCHAR(50);
DECLARE F_CUST_MAX INT;
DECLARE F_CUST_NO VARCHAR(14);
DECLARE F_MODIFY_EMP_ID INT;
DECLARE F_ID INT;
DECLARE F_GROUP INT;
DECLARE F_TEL_EXT VARCHAR(11);
DECLARE F_URL VARCHAR(255);
DECLARE F_GROUP_ID VARCHAR(40);
DECLARE F_CODE2 VARCHAR(6);
DECLARE F_CLASS_ID INT;
DECLARE F_ADDR VARCHAR(255);
DECLARE F_NAME1 VARCHAR(255);
DECLARE F_NAME2 VARCHAR(255);
DECLARE F_TEL VARCHAR(64);
DECLARE F_CITY_CODE VARCHAR(6);
DECLARE F_NAME_EXT_ID INT;
DECLARE F_TEL_EXT_ID INT;
DECLARE F_AREA_CODE VARCHAR(6);
DECLARE F_CITY_NAME VARCHAR(64);
DECLARE F_DISTRICT_CODE VARCHAR(6);
DECLARE F_REGION_NAME VARCHAR(64);
DECLARE F_CUST_TEL_ID_DEL INT;
DECLARE F_NAME_ID INT;
DECLARE F_ADDR_ID INT;
DECLARE F_CUST_ID INT;
DECLARE F_CUST_TEL_ID INT;
DECLARE F_TEL_ID INT;
DECLARE I INT DEFAULT 0;
DECLARE DONE INT DEFAULT 0;
DECLARE UP_CURSOR CURSOR FOR SELECT ID,NAME1,NAME2,TEL,AREA_CODE,TEL_EXT,DISTRICT_CODE,ADDR,
TEL_ADDR,URL,GROUP_ID,CODE2,MODIFY_EMP_ID
FROM OMC_UPDATE WHERE DEAL_STATUS=0;
DECLARE CUST_TEL_CURSOR CURSOR FOR SELECT ID FROM tb_cust_tel WHERE TEL_ID=F_TEL_ID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE=1;
SELECT CONCAT(MIN(ID),'-',MAX(ID)) INTO F_MIN_MAX_ID FROM OMC_UPDATE WHERE DEAL_STATUS=0;
INSERT INTO OMC_UPDATE_LOG (MIN_MAX_ID) VALUES(F_MIN_MAX_ID);
OPEN UP_CURSOR;
START TRANSACTION;
READ_LOOP:LOOP
FETCH UP_CURSOR INTO F_ID,F_NAME1,F_NAME2,
F_TEL,F_AREA_CODE,F_TEL_EXT,F_DISTRICT_CODE,F_ADDR,F_TEL_ADDR,F_URL,F_GROUP_ID,F_CODE2,F_MODIFY_EMP_ID;
SET I=I+1;
IF DONE THEN
SELECT I;
LEAVE READ_LOOP;
END IF;
UPDATE OMC_UPDATE SET DEAL_STATUS=1 WHERE ID=F_ID;
SET F_URL=IFNULL(F_URL,'');
SET F_TEL_EXT=IFNULL(F_TEL_EXT,'');
SET F_NAME1=HALF(TRIM(IFNULL(F_NAME1,'')));
SET F_NAME2=HALF(TRIM(IFNULL(F_NAME2,'')));
SET F_TEL=TRIM(IFNULL(F_TEL,''));
SET F_AREA_CODE=TRIM(IFNULL(F_AREA_CODE,''));
SET F_DISTRICT_CODE=TRIM(IFNULL(F_DISTRICT_CODE,''));
SET F_CITY_CODE=CONCAT(LEFT(F_DISTRICT_CODE,4),'00');
SET L1=LENGTH(F_TEL);
IF (SELECT 1 FROM OFF_MANAGE_REGION_L3_DAWEI WHERE CITY_CODE=F_CITY_CODE LIMIT 1)
IS NOT NULL
THEN
SELECT REGION_NAME,CITY_NAME INTO F_REGION_NAME,F_CITY_NAME FROM
OFF_MANAGE_REGION_L3_DAWEI WHERE CITY_CODE=F_CITY_CODE LIMIT 1;
SET F_ADDR=TRIM(HALF(REPLACE(REPLACE(IFNULL(TRIM(F_ADDR),''),F_REGION_NAME,''),F_CITY_NAME,'')));
SET F_TEL_ADDR=TRIM(HALF(REPLACE(REPLACE(IFNULL(TRIM(F_TEL_ADDR),''),F_REGION_NAME,''),F_CITY_NAME,'')));
ELSE
SET F_ADDR=HALF(IFNULL(TRIM(F_ADDR),''));
SET F_TEL_ADDR=HALF(IFNULL(TRIM(F_TEL_ADDR),''));
END IF;
IF(SELECT 1 FROM tb_name WHERE NAME1=F_NAME1 LIMIT 1)IS NULL
THEN
INSERT INTO tb_name (NAME1,NAME2)VALUES(F_NAME1,F_NAME2);
ELSE
UPDATE tb_name SET NAME2=IF(F_NAME2<>'',F_NAME2,NAME2) WHERE NAME1=F_NAME1;
END IF;
SELECT ID INTO F_NAME_ID FROM tb_name WHERE NAME1=F_NAME1 LIMIT 1;
IF F_URL<>'' AND (SELECT 1 FROM tb_name_ext WHERE NAME_ID=F_NAME_ID AND URL=F_URL LIMIT 1)IS NULL
THEN
INSERT INTO tb_name_ext (NAME_ID,URL) VALUES(F_NAME_ID,F_URL);
END IF;
IF F_URL<>''
THEN
SELECT ID INTO F_NAME_EXT_ID FROM tb_name_ext WHERE NAME_ID=F_NAME_ID AND URL=F_URL LIMIT 1;
ELSE
SET F_NAME_EXT_ID=0;
END IF;
IF F_TEL_ADDR<>''
THEN
IF (SELECT 1 FROM tb_address WHERE ADDR=F_TEL_ADDR AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1)IS NULL
THEN
INSERT INTO tb_address (ADDR,DISTRICT_CODE,CITY_CODE)
VALUES(F_TEL_ADDR,F_DISTRICT_CODE,F_CITY_CODE);
END IF;
SELECT ID INTO F_TEL_ADDR_ID FROM tb_address WHERE ADDR=F_TEL_ADDR AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1;
ELSE
SET F_TEL_ADDR_ID=0;
END IF;
IF(SELECT 1 FROM tb_address WHERE ADDR=F_ADDR AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1)IS NULL
THEN
INSERT INTO tb_address (ADDR,DISTRICT_CODE,CITY_CODE)
VALUES(F_ADDR,F_DISTRICT_CODE,F_CITY_CODE);
END IF;
SELECT ID INTO F_ADDR_ID FROM tb_address WHERE ADDR=F_ADDR AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1;
IF(SELECT 1 FROM tb_telephone WHERE TEL=F_TEL AND AREA_CODE=F_AREA_CODE LIMIT 1)IS NULL
THEN
INSERT INTO tb_telephone (TEL,AREA_CODE)
VALUES(F_TEL,F_AREA_CODE);
END IF;
SELECT ID INTO F_TEL_ID FROM tb_telephone WHERE TEL=F_TEL AND AREA_CODE=F_AREA_CODE LIMIT 1;
IF F_TEL_EXT<>'' AND (SELECT 1 FROM tb_tel_ext WHERE TEL_ID=F_TEL_ID AND TEL_EXT=F_TEL_EXT LIMIT 1)IS NULL
THEN
INSERT INTO tb_tel_ext(TEL_ID,TEL_EXT) VALUES(F_TEL_ID,F_TEL_EXT);
END IF;
IF F_TEL_EXT<>''
THEN
SELECT ID INTO F_TEL_EXT_ID FROM tb_tel_ext WHERE TEL_ID=F_TEL_ID AND TEL_EXT=F_TEL_EXT LIMIT 1;
ELSE
SET F_TEL_EXT_ID=0;
END IF;
-- 数据下线
-- --------------------------------------------库内电话是否删除
IF (SELECT 1 FROM tb_customer_depth WHERE CUST_ID IN(
SELECT CUST_ID FROM TB_CUST_TEL_VIEW WHERE TEL=F_TEL AND AREA_CODE=F_AREA_CODE) LIMIT 1)IS NOT NULL
OR
(SELECT 1 FROM tb_cust_busi WHERE CUST_ID IN(
SELECT CUST_ID FROM TB_CUST_TEL_VIEW WHERE TEL=F_TEL AND AREA_CODE=F_AREA_CODE) LIMIT 1)IS NOT NULL
THEN
UPDATE OMC_UPDATE SET DEAL_STATUS=3 WHERE ID=F_ID;
END IF;
IF L1=7 OR L1=8 OR L1=11
THEN
OPEN CUST_TEL_CURSOR;
CUST_TEL_LOOP:LOOP
FETCH CUST_TEL_CURSOR INTO F_CUST_TEL_ID_DEL;
IF DONE THEN
LEAVE CUST_TEL_LOOP;
END IF;
UPDATE tb_cust_tel SET CUST_STATUS=99 WHERE ID=F_CUST_TEL_ID_DEL;
UPDATE tb_cust_tel_source SET CUST_STATUS=99 WHERE CUST_TEL_ID =F_CUST_TEL_ID_DEL;
END LOOP CUST_TEL_LOOP;
CLOSE CUST_TEL_CURSOR;
SET DONE=0;
END IF;
-- -----------------------------------------------
IF (SELECT 1 FROM TB_TEL_SOURCE_VIEW WHERE TEL=F_TEL AND AREA_CODE=F_AREA_CODE AND
SOURCE_TYPE_ID IN(1,5) ORDER BY FIELD(SOURCE_TYPE_ID,
1,5)
LIMIT 1)IS NOT NULL
THEN
SELECT CUST_ID,CUST_TEL_ID INTO F_CUST_ID,F_CUST_TEL_ID
FROM TB_TEL_SOURCE_VIEW WHERE TEL=F_TEL AND AREA_CODE=F_AREA_CODE
AND SOURCE_TYPE_ID IN(1,5) ORDER BY FIELD(SOURCE_TYPE_ID,1,5) LIMIT 1;
IF (SELECT 1 FROM tb_cust_tel WHERE CUST_ID=F_CUST_ID AND CUST_STATUS=0 LIMIT 1)IS NULL
THEN
IF(SELECT 1 FROM TB_CUSTOMER_VIEW WHERE NAME1=F_NAME1 AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1)IS NULL
THEN
UPDATE tb_customer SET NAME_ID=F_NAME_ID,ADDR_ID=F_ADDR_ID,NAME_EXT_ID=IF(F_NAME_EXT_ID=0,NAME_EXT_ID,F_NAME_EXT_ID),
CUST_STATUS=0,MODIFY_EMP_ID=F_MODIFY_EMP_ID WHERE CUST_ID=F_CUST_ID;
ELSE
SELECT CUST_ID INTO F_CUST_ID FROM TB_CUSTOMER_VIEW WHERE NAME1=F_NAME1 AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1;
UPDATE tb_customer SET MODIFY_EMP_ID=F_MODIFY_EMP_ID,NAME_EXT_ID=IF(F_NAME_EXT_ID=0,NAME_EXT_ID,F_NAME_EXT_ID),
CUST_STATUS=0,ADDR_ID=F_ADDR_ID WHERE CUST_ID=F_CUST_ID;
END IF;
ELSE
IF(SELECT 1 FROM TB_CUSTOMER_VIEW WHERE NAME1=F_NAME1 AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1)IS NULL
THEN
SELECT MAX(CUST_ID) INTO F_CUST_MAX FROM tb_customer;
SET F_CUST_NO=CONCAT('SG',LEFT(F_DISTRICT_CODE,4),F_CUST_MAX);
INSERT INTO tb_customer (NAME_ID,ADDR_ID,NAME_EXT_ID,MODIFY_EMP_ID,CUST_NO)
VALUES(F_NAME_ID,F_ADDR_ID,F_NAME_EXT_ID,F_MODIFY_EMP_ID,F_CUST_NO);
ELSE
SELECT CUST_ID INTO F_CUST_ID FROM TB_CUSTOMER_VIEW WHERE NAME1=F_NAME1 AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1;
UPDATE tb_customer SET MODIFY_EMP_ID=F_MODIFY_EMP_ID,NAME_EXT_ID=IF(F_NAME_EXT_ID=0,NAME_EXT_ID,F_NAME_EXT_ID),
CUST_STATUS=0,ADDR_ID=F_ADDR_ID WHERE CUST_ID=F_CUST_ID;
END IF;
END IF;
SELECT CUST_ID INTO F_CUST_ID FROM tb_customer WHERE NAME_ID=F_NAME_ID AND ADDR_ID=F_ADDR_ID LIMIT 1;
UPDATE OMC_UPDATE SET CUST_ID=F_CUST_ID WHERE ID=F_ID;
UPDATE tb_cust_tel SET CUST_ID=F_CUST_ID,ADDR_ID=F_TEL_ADDR_ID,MODIFY_EMP_ID=F_MODIFY_EMP_ID,CUST_STATUS=0,TEL_EXT_ID=IF(F_TEL_EXT_ID=0,TEL_EXT_ID,F_TEL_EXT_ID)
WHERE ID=F_CUST_TEL_ID;
UPDATE tb_cust_tel_source SET LIST_ORDER=1,MODIFY_EMP_ID=F_MODIFY_EMP_ID,CUST_STATUS=0 WHERE CUST_TEL_ID=F_CUST_TEL_ID;
ELSE
-- 新增
IF(SELECT 1 FROM TB_CUSTOMER_VIEW WHERE NAME1=F_NAME1 AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1)IS NULL
THEN
SELECT MAX(CUST_ID) INTO F_CUST_MAX FROM tb_customer;
SET F_CUST_NO=CONCAT('SG',LEFT(F_DISTRICT_CODE,4),F_CUST_MAX);
INSERT INTO tb_customer (NAME_ID,ADDR_ID,NAME_EXT_ID,MODIFY_EMP_ID,CUST_NO)
VALUES(F_NAME_ID,F_ADDR_ID,F_NAME_EXT_ID,F_MODIFY_EMP_ID,F_CUST_NO);
ELSE
SELECT CUST_ID INTO F_CUST_ID FROM TB_CUSTOMER_VIEW WHERE NAME1=F_NAME1 AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1;
UPDATE tb_customer SET MODIFY_EMP_ID=F_MODIFY_EMP_ID,NAME_EXT_ID=IF(F_NAME_EXT_ID=0,NAME_EXT_ID,F_NAME_EXT_ID),
CUST_STATUS=0,ADDR_ID=F_ADDR_ID WHERE CUST_ID=F_CUST_ID;
END IF;
SELECT CUST_ID INTO F_CUST_ID FROM TB_CUSTOMER_VIEW WHERE NAME1=F_NAME1 AND DISTRICT_CODE=F_DISTRICT_CODE LIMIT 1;
UPDATE OMC_UPDATE SET CUST_ID=F_CUST_ID WHERE ID=F_ID;
IF(SELECT 1 FROM tb_cust_tel WHERE CUST_ID=F_CUST_ID AND TEL_ID=F_TEL_ID LIMIT 1)IS NULL
THEN
INSERT INTO tb_cust_tel (CUST_ID,TEL_ID,TEL_EXT_ID,MODIFY_EMP_ID,ADDR_ID)
VALUES(F_CUST_ID,F_TEL_ID,F_TEL_EXT_ID,F_MODIFY_EMP_ID,F_TEL_ADDR_ID);
ELSE
UPDATE tb_cust_tel SET MODIFY_EMP_ID=F_MODIFY_EMP_ID,CUST_STATUS=0,
TEL_EXT_ID=IF(F_TEL_EXT_ID=0,TEL_EXT_ID,F_TEL_EXT_ID),ADDR_ID=F_TEL_ADDR_ID
WHERE CUST_ID=F_CUST_ID AND TEL_ID=F_TEL_ID LIMIT 1;
END IF;
SELECT ID INTO F_CUST_TEL_ID FROM tb_cust_tel WHERE CUST_ID=F_CUST_ID AND TEL_ID=F_TEL_ID LIMIT 1;
IF(SELECT 1 FROM tb_cust_tel_source WHERE CUST_TEL_ID=F_CUST_TEL_ID LIMIT 1)IS NULL
THEN
INSERT INTO tb_cust_tel_source (CUST_TEL_ID,SOURCE_TYPE_ID,LIST_ORDER,MODIFY_EMP_ID)
VALUES(F_CUST_TEL_ID,2,1,F_MODIFY_EMP_ID);
ELSE
UPDATE tb_cust_tel_source SET CUST_STATUS=0,MODIFY_EMP_ID=F_MODIFY_EMP_ID,LIST_ORDER=1,SOURCE_TYPE_ID=2
WHERE CUST_TEL_ID=F_CUST_TEL_ID LIMIT 1;
END IF;
END IF;
-- 提供给华为
/*
IF(SELECT 1 FROM HUAWEI_BAK WHERE CUST_TEL_ID=F_CUST_TEL_ID LIMIT 1)IS NULL
THEN
INSERT INTO HUAWEI_BAK(CUST_TEL_ID,CUST_ID,TEL_ID)
VALUES(F_CUST_TEL_ID,F_CUST_ID,F_TEL_ID);
ELSE
UPDATE HUAWEI_BAK SET DEAL_STATUS=0,CUST_ID=F_CUST_ID,TEL_ID=F_TEL_ID WHERE CUST_TEL_ID=F_CUST_TEL_ID;
END IF;
*/
IF(SELECT 1 FROM on_prod_class WHERE CODE2=F_CODE2 LIMIT 1)IS NOT NULL
THEN
SELECT CLASS_ID INTO F_CLASS_ID FROM on_prod_class WHERE CODE2=F_CODE2;
IF(SELECT 1 FROM tb_cust_class WHERE CUST_ID=F_CUST_ID AND CLASS_ID=F_CLASS_ID LIMIT 1)IS NULL
THEN
INSERT INTO tb_cust_class(CUST_ID,CLASS_ID,MODIFY_EMP_ID) VALUES(F_CUST_ID,F_CLASS_ID,F_MODIFY_EMP_ID);
ELSE
UPDATE tb_cust_class SET CUST_STATUS=0 WHERE CUST_ID=F_CUST_ID AND CLASS_ID=F_CLASS_ID LIMIT 1;
END IF;
END IF;
IF (SELECT 1 FROM tb_group WHERE GROUP_ID=F_GROUP_ID LIMIT 1)IS NOT NULL
THEN
SELECT ID INTO F_GROUP FROM tb_group WHERE GROUP_ID=F_GROUP_ID LIMIT 1;
IF (SELECT 1 FROM tb_cust_group WHERE CUST_ID=F_CUST_ID AND GROUP_ID=F_GROUP LIMIT 1)IS NULL
THEN
INSERT INTO tb_cust_group(CUST_ID,GROUP_ID,MODIFY_EMP_ID)VALUES(F_CUST_ID,F_GROUP,F_MODIFY_EMP_ID);
END IF;
END IF;
IF I%5000=0
THEN
COMMIT;
START TRANSACTION;
END IF;
END LOOP READ_LOOP;
COMMIT;
CLOSE UP_CURSOR;
END
Mysql临时表
当你创建临时表的时候,你可以使用temporary关键字。如:
create temporary table tmp_table(name varchar(10) not null,passwd char(6) not null)‘
mysql 存储过程,函数,触发器备份:
mysqldump -uroot -pHb118114 -P3306 -n -d -t -R db_ndc_2> /usr/local/mysql/zhangfusheng/procedures.sql
mysqldump -uroot -pHb118114 --opt -n -d -t --flush-logs --master-data=2 --single-transaction -R --triggers > /usr/local/mysql/zhangfusheng/procedures.sql
msyql 5.7.13初始化
./bin/mysqld --initialize --user=mac --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql/
mysql 执行批量sql
mysql -u$v_user -p$v_password -e""
用户权限表操作:
删除用户: delete from user where user="repl2";
CREATE USER 'zhangfusheng'@'localhost' IDENTIFIED BY 'zhangfusheng';
grant create,select on *.* to 'zhangfusheng'@'localhost';
<span style="font-family: Arial, Helvetica, sans-serif;">revoke all privileges on bd_yp.* from 'yp114'@'192.168.%';</span>
grant select,update,delete,insert,drop,create on bd_yp.* to 'yp114'@'192.168.%';
revoke select,update,delete,insert on bd_yp.* from 'yp114'@'192.168.%';
CREATE USER 'yp114'@'192.168.%' IDENTIFIED BY '123456';
grant all privileges on *.* to 'yp114'@'192.168.%';
revoke all privileges on mysql.* from 'yp114'@'192.168.%';
revoke select on mysql.* from 'yp114'@'192.168.%';
grant select,update,delete,insert on mysql.* to 'yp114'@'192.168.%';
revoke select,update,delete,insert on mysql.* from 'yp114'@'192.168.%';
flush privileges ;
密码忘记修改:介绍一个非常有用的mysql启动参数—— --skip-grant-tables。
操作方法:
1、service mysql stop
或者:
kill -TERM mysqld
2、以命令行参数启动mysql:
/usr/bin/mysqld_safe --skip-grant-tables &
3、修改管理员密码:
use mysql;
update user set password=password('yournewpasswordhere') where user='root';
flush privileges;
exit;
4、重启mysql
计算距离
select A.id,A.addr,B.addr as addr1,A.poix,A.poiy,B.poix as poix1,B.poiy as poiy1
,getDistance(A.poiy,A.poix,B.poiy,B.poix)
from tb_address A LEFT JOIN tb_address_zfs B on A.id=B.id
where A.poix<>B.poix and A.modify_time<='2016-01-15'
limit 100
getDistance(116.3899,39.91578,116.3904,39.91576);
2016-01-14 17:45:51
select count(*) from tb_address_zfs
CREATE FUNCTION `getDistance`(
lon1 float(10,7)
,lat1 float(10,7)
,lon2 float(10,7)
,lat2 float(10,7)
) RETURNS double
begin
declare d double;
declare radius int;
set radius = 6378140; #假设地球为正球形,直径为6378140米
set d = (2*ATAN2(SQRT(SIN((lat1-lat2)*PI()/180/2)
*SIN((lat1-lat2)*PI()/180/2)+
COS(lat2*PI()/180)*COS(lat1*PI()/180)
*SIN((lon1-lon2)*PI()/180/2)
*SIN((lon1-lon2)*PI()/180/2)),
SQRT(1-SIN((lat1-lat2)*PI()/180/2)
*SIN((lat1-lat2)*PI()/180/2)
+COS(lat2*PI()/180)*COS(lat1*PI()/180)
*SIN((lon1-lon2)*PI()/180/2)
*SIN((lon1-lon2)*PI()/180/2))))*radius;
return d;
end
mysqld 启动失败:
1、可能进程里已经存在mysql进程
解决方法:用命令“ps -ef|grep mysqld”查看是否有mysqld进程,如果有使用“kill -9 进程号”杀死,然后重新启动mysqld!
ps -aux|grep mysql
$kill -s 9 1827
2、可能是第二次在机器上安装mysql,有残余数据影响了服务的启动。
解决方法:去mysql的数据目录/data看看,如果存在mysql-bin.index,就赶快把它删除掉吧。
mysql函数:
--split
CREATE DEFINER=`root`@`%` FUNCTION `func_get_split_string`(
f_string varchar(1000),f_delimiter varchar(5),f_order int) RETURNS varchar(255) CHARSET utf8
BEGIN
declare result varchar(255) default '';
set result = reverse(substring_index(reverse(substring_index(f_string,f_delimiter,f_order)),f_delimiter,1));
return result;
END$$
DELIMITER ;
--WHILE循环
CREATE PROCEDURE myProc()
BEGIN
DECLARE i int;
SET i=1;
loop1: WHILE i<=10 DO
IF MOD(i,2)<>0 THEN /*Even number - try again*/
SELECT CONCAT(i," is an odd number");
END IF;
SET i=i+1;
END WHILE loop1;
END$$
call class_update()
drop procedure if exists class_update;
CREATE PROCEDURE class_update()
BEGIN
-- BDC数据入2.0
DECLARE F_ID INT;
DECLARE F_ID2 INT;
SET F_ID = 1;
SET F_ID2 = 2;
loop1:WHILE F_ID <>F_ID2 DO
SELECT F_ID,F_ID2;
SET F_ID=F_ID2;
END WHILE loop1;
SELECT F_ID,F_ID2;
END
跳出while 循环
LEAVE loop_label;
--事务处理
DROP PROCEDURE IF EXISTS test_sp1 $$
CREATE PROCEDURE test_sp1( )
BEGIN
DECLARE t_error INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
START TRANSACTION;
INSERT INTO test VALUES(NULL, 'test sql 001');
INSERT INTO test VALUES('1', 'test sql 002');
IF t_error = 1 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
END$$
DELIMITER ;
以上是关于mysql知识备忘(建表存储过程初次安装启动)的主要内容,如果未能解决你的问题,请参考以下文章