zoukankan      html  css  js  c++  java
  • MySQL视图、存储过程、函数、触发器

    视图

    视图概述

    视图(View)是一种虚拟存在的表。视图并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的表, 并且是在使用视图时动态生成的。

    通俗的讲,视图就是一条SELECT语句执行后返回的结果集。所以我们在创建视 图的时候,主要的工作就落在创建这条SQL查询语句上。

    视图的优势:

    • 简单:使用视图的用户完全不需要关心后面对应的表的结构、关联条件和筛选条件,对用户来说已经是过滤好的复合条件的结果集。

    • 安全:使用视图的用户只能访问他们被允许查询的结果集,对表的权限管理并不能限制到某个行某个列,但是通过视图就可以简单的实现。

    • 数据独立:一旦视图的结构确定了,可以屏蔽表结构变化对用户的影响,源表增加列对视图没有影响;源表修改列名,则可以通过修改视图来解决,不会造成对访问者的影响。

    视图操作

    创建视图

    语法

    CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [(column_list)]
    AS select_statement
    [WITH [CASCADED | LOCAL] CHECK OPTION]
    

    示例:

    mysql> CREATE OR REPLACE view city_country_view
        -> AS
        -> SELECT t.*,c.country_name FROM country c , city t WHERE c.country_id = t.country_id;
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> SELECT * FROM city_country_view;
    +---------+-----------+------------+--------------+
    | city_id | city_name | country_id | country_name |
    +---------+-----------+------------+--------------+
    |       1 | 西安      |          1 | China        |
    |       2 | NewYork   |          2 | America      |
    |       3 | 北京      |          1 | China        |
    |       4 | 上海      |          1 | China        |
    +---------+-----------+------------+--------------+
    4 rows in set (0.00 sec)
    

    修改视图

    语法:

    ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [(column_list)]
    AS select_statement
    [WITH [CASCADED | LOCAL] CHECK OPTION]
    

    选项:

    • WITH [CASCADED | LOCAL] CHECK OPTION 决定了是否允许更新数据使记录不再满足视图的条件
    • LOCAL : 只要满足本视图的条件就可以更新。
    • CASCADED : 必须满足所有针对该视图的所有视图的条件才可以更新。 默认值.

    示例:

    查看视图

    从 MySQL 5.1 版本开始,使用 SHOW TABLES 命令的时候不仅显示表的名字,同时也会显示视图的名字,而不存在单独显示视图的 SHOW VIEWS 命令。

    mysql> show tables;
    +-------------------+
    | Tables_in_demo_01 |
    +-------------------+
    | city              |
    | city_country_view |
    | country           |
    +-------------------+
    4 rows in set (0.00 sec)
    

    查看视图的信息

    mysql> show table status like 'city_country_view'G;
    *************************** 1. row ***************************
               Name: city_country_view
             Engine: NULL
            Version: NULL
         Row_format: NULL
               Rows: NULL
     Avg_row_length: NULL
        Data_length: NULL
    Max_data_length: NULL
       Index_length: NULL
          Data_free: NULL
     Auto_increment: NULL
        Create_time: 2021-05-08 11:26:38
        Update_time: NULL
         Check_time: NULL
          Collation: NULL
           Checksum: NULL
     Create_options: NULL
            Comment: VIEW
    1 row in set (0.00 sec)
    

    查看视图定义

    mysql> show create view city_country_viewG;
    *************************** 1. row ***************************
                    View: city_country_view
             Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `city_country_view` AS select `t`.`city_id` AS `city_id`,`t`.`city_name` AS `city_name`,`t`.`country_id` AS `country_id`,`c`.`country_name` AS `country_name` from (`country` `c` join `city` `t`) where (`c`.`country_id` = `t`.`country_id`)
    character_set_client: utf8mb4
    collation_connection: utf8mb4_0900_ai_ci
    1 row in set (0.00 sec)
    

    删除视图

    语法 :

    ROP VIEW [IF EXISTS] view_name [, view_name] ...[RESTRICT | CASCADE]
    

    示例 , 删除视图city_country_view :

    mysql> DROP VIEW city_country_view ;
    Query OK, 0 rows affected (0.04 sec)
    

    存储过程和函数

    存储过程和函数是事先经过编译并存储在数据库中的一段 SQL 语句的集合,调用存储过程和函数可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的。

    存储过程和函数的区别:

    • 函数 : 是一个有返回值的过程 ;
    • 过程 : 是一个没有返回值的函数 ;

    存储过程

    创建存储过程

    语法:

    CREATE PROCEDURE procedure_name ([proc_parameter[,...]]) begin
    -- SQL语句 end ;
    

    示例:

    delimiter $ -- 修改结束符,避免和mysql默认的结束符";"冲突
    create procedure pro_test1()
    begin
        select 'Hello Mysql' ;
    end$
    delimiter ;
    

    注意:DELIMITER,该关键字用来声明SQL语句的分隔符 , 告诉 MySQL 解释器,该段命令是否已经结束了

    默认情况下,delimiter是分号;。在命令行客户端中,如果有一行命令以分号结束,那么回车后,mysql将会执行该 命令。

    调用存储过程

    call procedure_name();
    

    查看存储过程

    -- 查询db_name数据库中的所有的存储过程
    select name from mysql.proc where db='db_name';
    
    -- 查询存储过程的状态信息 
    show procedure status;
    
    -- 查询某个存储过程的定义
    show create procedure test.pro_test1 G;
    

    删除存储过程

    DROP PROCEDURE [IF EXISTS] sp_name ;
    

    示例:

    mysql> DROP PROCEDURE IF EXISTS pro_test1;
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    

    存储过程语法

    存储过程是可以编程的,意味着可以使用变量,表达式,控制结构 ,来完成比较复杂的功能。

    变量

    • DECLARE:通过 DECLARE 可以定义一个局部变量,该变量的作用范围只能在 BEGIN...END 块中。
    DECLARE var_name[,...] type [DEFAULT value]
    

    示例:

    delimiter $
    create procedure pro_test2()
    begin
       declare num int default 5;
       select num+ 10;
    end$
    delimiter ;
    
    • SET: 直接赋值使用 SET,可以赋常量或者赋表达式,具体语法如下:
    SET var_name = expr [, var_name = expr] ...
    

    示例:

    DELIMITER $
    
    CREATE  PROCEDURE pro_test3()
    BEGIN
      DECLARE NAME VARCHAR(20);
      SET NAME = 'MYSQL';
      SELECT NAME ;
    END$
    
    DELIMITER ;
    
    • select...into:也可以通过select...into方式进行赋值
    DELIMITER $
    
    CREATE  PROCEDURE pro_test5()
    BEGIN
        declare  countnum int;
        select count(*) into countnum from city;
        select countnum;
    END$
    
    DELIMITER ;
    

    if条件判断

    语法:

    if search_condition then statement_list
    	[elseif search_condition then statement_list] ... 
    	[else statement_list]
    end if;
    

    需求:根据身高判断身材类型

    • 身高>180,身材高挑
    • 170<身高<180,标准身材
    • 身高<170,一般身材

    示例:

    delimiter $
    create procedure pro_test6()
    begin
      declare  height  int  default  175;
      declare  description  varchar(50);
      
      if height >= 180 then set description = '身材高挑';
      elseif height >= 170 and height < 180 then set description = '标准身材';
      else set description = '一般身材';
      end if;
      select description ;
    end$
    delimiter ;
    

    传递参数

    语法:

    create procedure procedure_name([in/out/inout] 参数名 参数类型) 
    ...
    
    • IN : 该参数作为输入,也就是需要调用方传入值 , 默认
    • OUT: 该参数作为输出,也就是该参数可以作为返回值
    • INOUT: 既可以作为输入参数,也可以作为输出参数

    需求:根据身高变量输出身材类型

    • IN-输入
    delimiter $
    create procedure pro_test5(in height int)
    begin
      declare description varchar(50) default '';
      if height >= 180 then
        set description='身材高挑';
      elseif height >= 170 and height < 180 then
        set description='标准身材'; 
      else
        set description='一般身材'; 
      end if;
      select concat('身高 ', height , '对应的身材类型为:',description); 
    end$
    delimiter ; 
    
    • OUT-输出
    create procedure pro_test5(in height int , out description varchar(100)) 
    begin
      if height >= 180 then
        set description='身材高挑';
      elseif height >= 170 and height < 180 then 
        set description='标准身材';
      else
        set description='一般身材';
      end if; 
    end$
    

    Tips:

    • @description : 这种变量要在变量名称前面加上“@”符号,叫做用户会话变量,代表整个会话过程他都是有作用的,这个类似于全局变量一样。

    • @@global.sort_buffer_size : 这种在变量前加上 "@@" 符号, 叫做 系统变量

    case结构

    语法结构一:

    CASE case_value
      WHEN when_value THEN statement_list
      [WHEN when_value THEN statement_list] ...
      [ELSE statement_list]
    END CASE;
    

    语法结构二:

    CASE
      WHEN search_condition THEN statement_list
      [WHEN search_condition THEN statement_list] ...
      [ELSE statement_list]
    END CASE;
    

    需求:给出月份,计算所在季度

    示例:

    delimiter $
    create procedure pro_test9(month int)
    begin
      declare result varchar(20);
      case
        when month >= 1 and month <=3 then set result = '第一季度';
        when month >= 4 and month <=6 then set result = '第二季度';
        when month >= 7 and month <=9 then set result = '第三季度';
        when month >= 10 and month <=12 then set result = '第四季度';
      end case;
      select concat('您输入的月份为 :', month , ' , 该月份为 : ' , result) as content ; 
    end$
    delimiter ;
    

    while循环

    语法:

    while search_condition do 
      statement_list
    end while;
    

    需求:从1加到n的值

    delimiter $
    create procedure pro_test8(n int)
    begin
      declare total int default 0;
      declare num int default 1;
      while num<=n do
        set total = total + num;
        set num = num + 1;
      end while;
      select total;
    end$
    delimiter ;
    

    repeat结构

    有条件的循环控制语句, 当满足条件的时候退出循环 。while 是满足条件才执行,repeat 是满足条件就退出循环。

    语法结构 :

    REPEAT
      statement_list
      UNTIL search_condition
    END REPEAT;
    

    需求:从1加到n的值

    delimiter $
    create procedure pro_test10(n int)
    begin
      declare total int default 0;
      repeat
        set total = total + n;
        set n = n - 1;
        until n=0
    end repeat;
      select total ;
    end$
    delimiter ;
    

    loop语句

    LOOP 实现简单的循环,退出循环的条件需要使用其他的语句定义,通常可以使用 LEAVE 语句实现。

    具体语法如下:

    [begin_label:] LOOP 
      statement_list
    END LOOP [end_label]
    

    如果不在 statement_list 中增加退出循环的语句,那么 LOOP 语句可以用来实现简单的死循环。

    1.4.8 leave语句

    用来从标注的流程构造中退出,通常和 BEGIN ... END 或者循环一起使用。

    下面是一个使用 LOOP 和 LEAVE 的简单例子 , 退出循环:

    delimiter $
    CREATE PROCEDURE pro_test11(n int)
    BEGIN
      declare total int default 0;
      ins: LOOP
        IF n <= 0 then
          leave ins;
        END IF;
        set total = total + n;
        set n = n - 1;
      END LOOP ins;
      select total;
    END$
    delimiter ;
    

    游标/光标

    游标是用来存储查询结果集的数据类型 , 在存储过程和函数中可以使用光标对结果集进行循环的处理。

    光标的使用包括光标的声明、OPEN、FETCH 和 CLOSE,其语法分别如下

    声明光标:

    DECLARE cursor_name CURSOR FOR select_statement ;
    

    OPEN光标

    OPEN cursor_name ;
    

    FETCH光标

    FETCH cursor_name INTO var_name [, var_name] ...
    

    CLOSE光标

    CLOSE cursor_name ;
    

    示例:

    初始化脚本

    -- 建表
    create table emp(
      id int(11) not null auto_increment , 
      name varchar(50) not null comment '姓名', 
      age int(11) comment '年龄',
      salary int(11) comment '薪水',
      primary key(`id`)
    )engine=innodb default charset=utf8 ;
    
    -- 插入数据
    insert into 
        emp(id,name,age,salary) 
    values
        (null,'金毛狮王',55,3800),
        (null,'白眉鹰 王',60,4000),
        (null,'青翼蝠王',38,2800),
        (null,'紫衫龙王',42,1800);
    

    查询数据

    -- 查询emp表中数据, 并逐行获取进行展示 
    create procedure pro_test11() begin
      declare e_id int(11);
      declare e_name varchar(50);
      declare e_age int(11);
      declare e_salary int(11);
      declare emp_result cursor for select * from emp;
      open emp_result;
      fetch emp_result into e_id,e_name,e_age,e_salary;
    select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary);
      fetch emp_result into e_id,e_name,e_age,e_salary;
    select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary);
      fetch emp_result into e_id,e_name,e_age,e_salary;
    select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary);
      fetch emp_result into e_id,e_name,e_age,e_salary;
    select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary);
      fetch emp_result into e_id,e_name,e_age,e_salary;
    select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary);
      close emp_result;
    end$
    

    通过循环结构,获取游标中的数据

    DELIMITER $
    create procedure pro_test12()
    begin
      DECLARE id int(11);
      DECLARE name varchar(50);
      DECLARE age int(11);
      DECLARE salary int(11);
      DECLARE has_data int default 1;
      DECLARE emp_result CURSOR FOR select * from emp;
      DECLARE EXIT HANDLER FOR NOT FOUND set has_data = 0;
      open emp_result;
      repeat
        fetch emp_result into id , name , age , salary;
        select concat('id为',id, ', name 为' ,name , ', age为 ' ,age , ', 薪水为: ', salary);
        until has_data = 0
      end repeat;
      close emp_result;
    end$
    DELIMITER ;
    

    存储函数

    语法结构

    CREATE FUNCTION function_name([param type ... ]) 
    RETURNS type
    BEGIN
      ... 
    END;
    

    定义存储函数,查询满足条件的记录数

    delimiter $
    create function count_city(countryId int)
    returns int
    begin
      declare cnum int ;
      select count(*) into cnum from city where country_id = countryId;
      return cnum;
    end$
    delimiter ;
    

    调用

    select count_city(1);
    

    触发器

    触发器介绍

    触发器是与表有关的数据库对象,指在 insert/update/delete 之前或之后,触发并执行触发器中定义的SQL语句集合。

    触发器的这种特性可以协助应用在数据库端确保数据的完整性 , 日志记录 , 数据校验等操作 。

    使用别名 OLD 和 NEW 来引用触发器中发生变化的记录内容,这与其他的数据库是相似的。现在触发器还只支持行级触发,不支持语句级触发。

    触发器类型 NEW 和 OLD的使用
    INSERT 型触发器 NEW 表示将要或者已经新增的数据
    UPDATE 型触发器 OLD 表示修改之前的数据 , NEW 表示将要或已经修改后的数据
    DELETE 型触发器 OLD 表示将要或者已经删除的数据

    创建触发器

    语法结构

    create trigger trigger_name 
    before/after insert/update/delete 
    on tbl_name
    [ for each row ] -- 行级触发器 
    begin
        trigger_stmt ;
    end;
    

    需求:通过触发器记录emp表的数据变更日志,包含新增,修改,删除

    首先创建一张日志表:

    create table emp_logs(
      id int(11) not null auto_increment,
      operation varchar(20) not null comment '操作类型, insert/update/delete', 
      operate_time datetime not null comment '操作时间',
      operate_id int(11) not null comment '操作表的ID',
      operate_params varchar(500) comment '操作参数',
      primary key(`id`)
    )engine=innodb default charset=utf8;
    

    创建 insert 型触发器,完成插入数据时的日志记录 :

    DELIMITER $
    create trigger emp_logs_insert_trigger
    after insert
    on emp
    for each row
    begin
      insert into emp_logs (id,operation,operate_time,operate_id,operate_params)
      values(null,'insert',now(),new.id,concat('插入后(id:',new.id,', name:',new.name,', age:',new.age,', salary:',new.salary,')'));
    end $
    DELIMITER ;
    

    创建 update 型触发器,完成更新数据时的日志记录 :

    DELIMITER $
    create trigger emp_logs_update_trigger
    after update
    on emp
    for each row
    begin
      insert into emp_logs (id,operation,operate_time,operate_id,operate_params)
      values(null,'update',now(),new.id,concat('修改前(id:',old.id,', name:',old.name,', age:',old.age,', salary:',old.salary,') , 修改后(id',new.id, 'name:',new.name,', age:',new.age,', salary:',new.salary,')'));
    end $
    DELIMITER ;
     
    

    创建delete 行的触发器 , 完成删除数据时的日志记录 :

    DELIMITER $
    create trigger emp_logs_delete_trigger
    after delete
    on emp
    for each row
    begin
      insert into emp_logs (id,operation,operate_time,operate_id,operate_params)
      values(null,'delete',now(),old.id,concat('删除前(id:',old.id,', name:',old.name,', age:',old.age,', salary:',old.salary,')'));
    end $
    DELIMITER ;
    

    测试

    insert into emp(id,name,age,salary) values(null, '光明左使',30,3500); 
    insert into emp(id,name,age,salary) values(null, '光明右使',33,3200);
    update emp set age = 39 where id = 3;
    delete from emp where id = 5;
    

    删除触发器

    语法:

    drop trigger [schema_name.]trigger_name
    

    如果没有指定 schema_name,默认为当前数据库 。

    查看触发器

    可以通过执行 SHOW TRIGGERS 命令查看触发器的状态、语法等信息。

    语法结构 :

    show triggers ;
    
    衣带渐宽终不悔,为伊消得人憔悴!
  • 相关阅读:
    解决VM 安装Ubuntu64与 Device/Credential Guard 不兼容,显示不支持64位系统
    WPF处理内容溢出
    .NET Standard 2.0 是什麼?可以吃嗎?
    C#.Net]启动外部程序的几种常用方法汇总
    在C#中接收系统屏幕锁定和解锁的事件
    C#.Net]启动外部程序的几种常用方法汇总
    MongoDB索引的使用
    读取xml并将节点保存到Excal
    开学后的第一篇
    续并查集学习笔记——Gang团伙题解
  • 原文地址:https://www.cnblogs.com/ryxiong-blog/p/14746689.html
Copyright © 2011-2022 走看看