CREATE TRIGGER trg_returndel AFTER DELETE
ON tc_user for each ROW
BEGIN
INSERT into tc_history(rowkey,time)VALUES(name,NOW());
END
-- 触发器
CREATE TRIGGER trg_returndel_his AFTER DELETE
ON tc_user for each ROW
BEGIN
INSERT into tc_history(rowkey,time)VALUES(OLD.name,NOW());
END
show TRIGGERS
DROP TRIGGER trg_returndel
DELETE from tc_user where id = '5'
-- 存储过程的使用
CREATE PROCEDURE sp_search_customer(IN p_nam VARCHAR(20))
BEGIN
IF P_nam Is NULL OR p_nam = '' THEN
SELECT * FROM tc_user;
else
SELECT * FROM tc_history;
END if;
END
-- 查看所有存储过程
SHOW PROCEDURE STATUS
-- 查看指定存储过程内容
SHOW CREATE PROCEDURE sp_search_customer
CALL sp_search_customer('jijij');
CALL sp_search_customer('');
-- 带反参的存储过程
CREATE PROCEDURE sp_search_customer2(IN p_nam VARCHAR(20),out p_cnt INT)
BEGIN
IF P_nam Is NULL OR p_nam = '' THEN
SELECT * FROM tc_user;
else
SELECT * FROM tc_history;
END if;
SELECT FOUND_ROWS() INTO P_cnt;
END
CALL sp_search_customer2('sss',@num)
SELECT @num
-- 游标使用
CREATE PROCEDURE sp_cursor(
OUT p_result text
)
BEGIN
-- 定义变量标志flag(判断是否所有记录是否都被取出)
DECLARE flag BIT DEFAULT(0);
-- 定义存储当前行的部署名的变量 tmp
DECLARE tmp VARCHAR(20);
-- 声明游标
DECLARE cur CURSOR FOR SELECT DISTINCT name FROM tc_user;
-- 定义取出游标中所有记录的处理
DECLARE continue handler for not found set flag= 1;
-- 打开游标
OPEN cur;
-- 从游标中一行行取出数据
WHILE flag !=1 DO
FETCH cur INTO tmp ;-- 将当前行的内容保存到变量中
-- 标志变量flag 非1时进行如下处理
if flag != 1 THEN
-- 将变量tmp以逗号分割的字符串的形式保存到输出参数 p_result中
SET p_result = CONCAT_WS(',',p_result,tmp);
END IF;
end WHILE;
CLOSE cur;
END
CALL sp_cursor(@P_result)
SELECT @P_result
SELECT CONCAT_WS('|','2','3','来')
CREATE PROCEDURE sp_cursor_youbiao(
OUT p_results text
)
BEGIN
DECLARE flaga BIT DEFAULT(0);
DECLARE tmps VARCHAR(20);
DECLARE curname CURSOR FOR SELECT DISTINCT name FROM tc_user where name like '张%';
DECLARE continue handler for not found set flaga= 1;
OPEN curname;
WHILE flaga !=1 DO
FETCH curname INTO tmps ;
if flaga != 1 THEN
SET p_results = CONCAT_WS(',',p_results,tmps);
END IF;
end WHILE;
CLOSE curname;
END
CALL sp_cursor_youbiao(@ss)
SELECT @ss;
SELECT *from tc_user
-- 导入csv文件
LOAD data INFILE 'D:\用户目录\我的文档\Navicat\查询汇总.txt' INTO TABLE tc_datas FIELDS TERMINATED by ',';
SELECT * INTO OUTFILE 'F:/out.csv' FIELDS TERMINATED by ',' FROM tc_user;
-- 创建存储过程 添加数据
CREATE PROCEDURE sp_mingyangtion_invvi(IN paltes VARCHAR(200),IN vins VARCHAR(255))
BEGIN
INSERT INTO t_vin_car (plate,vin)VALUES(paltes,vins);
END
;
-- 使用存储过程
call sp_mingyangtion_invvi ('车牌号','vin');
-- 删除存储过程
DROP PROCEDURE sp_mingyangtion_invvi;