mysql-视图,创建表,存储过程,循环,判断实操命令

news2025/9/19 20:05:52

数据库操作命令在IDEA工具database的console命令

数据库表结构与视图

在这里插入图片描述

事务隔离级别RR(REPEATABLE-READ)解决不可重复读演示

mysql-RR隔离级别

-- 查询隔离级别
select @@transaction_isolation;

-- 设置隔离级别
set session transaction isolation level read committed ;
set session transaction isolation level REPEATABLE READ ;

start transaction;

select * from sys_user;

commit ;

rollback ;
-- SQL性能分析
show global status like 'Com_______';
-- 查看慢查询是否开启
show variables like 'slow_query_log';

-- linux 重启msql
-- systemctl restart mysqld;

-- 创建视图
create or replace view v_users as select id,username from sys_user where id <10;

explain select id,username from sys_user where username <10;

explain select * from v_users where id = 2;

-- 查询创建视图的语句
show create view  v_users;

drop view if exists v_users;
-- 视图检查选项,插入的数据跟据视图的where条件进行检查是否符合,如果不符合会不让插入数据
-- with cascaded check option
create or replace view v_users as select id,username from sys_user where id <10 with cascaded check option ;
-- 视图可以插入数据并且 会插入到真实表中
insert into v_users values (3,'wangliu');
insert into v_users values (30,'wangliu');

-- 视图简化多表联查 只需要查询视图不需要每次都增加其他的条件
-- 视图屏蔽/隐藏敏感数据字段 授权给无权查看的人

--  存储过程
CREATE PROCEDURE PRO_USERS()
BEGIN
    SELECT COUNT(*) FROM sys_user;
end;
-- 调用存储过程
CALL PRO_USERS();
-- 查看存储过程
select * from information_schema.ROUTINES where ROUTINE_SCHEMA = 'test';

-- 查看存储过程建表语句
show create procedure PRO_USERS;

-- 删除存储过程
drop procedure if exists PRO_USERS;

-- 系统变量 session|global 默认都是session
show session variables ;
show session variables  like 'auto%';

select @@autocommit;

select @@session.autocommit;
select @@global.autocommit;
set @@autocommit = 1;

-- 用户自定义变量
set @myName :='zxd';
set @myAge := 30;

set @myName :='lisi',@myAge := 40;

select @myName,@myAge;

select count(*) into @myCount from sys_user;

select @myCount;

-- 局部变量声明 begin ...end 之前
drop procedure if exists pro_test2;
create procedure pro_test2()
begin
    declare var_age int default 0;
    set var_age := 20;
    select var_age;
end;

show create procedure pro_test2;

call pro_test2();

-- IF ELSEIF ELSE 存储过程逻辑分支判断
create procedure pro_p3()
begin
    declare score int default 58;
    declare res varchar(10);
    if score >80 then
        set res:= '优秀';
        elseif score >60 then
        set res :='及格';
        else
        set res:= '不及格';
    end if;
    select res;
end;

call pro_p3();

-- 存储过程带输入,输出参数的
create procedure pro_p4(in score int,out res varchar(10))
begin
    if score >80 then
        set res:= '优秀';
    elseif score >60 then
        set res :='及格';
    else
        set res:= '不及格';
    end if;
    select res;
end;

call pro_p4(18,@res);
select @res;

-- INOUT 出参与入参是同一个参数的写法
drop procedure if exists p5;

create procedure p5(inout score double)
begin
    set score := score*0.5;
end;

set @score :=178.8;
call p5(@score);
select @score;

-- 根据传入的月份,返回季度信息
drop procedure p6;
create procedure p6(in month int,out res varchar(20))
begin
    case
        when month >=1 and month <=3
            then set res:= '第一季度';
        when month >=4 and month<=6
            then set res:= '第二季度';
        when month >=7 and month<=9
            then set res:= '第三季度';
        when month >=10 and month<=12
            then set res:= '第四季度';
        else
            set res:='非法参数';
        end case ;
    select concat('您输入的月份 ',month,',所属季度为',res) into res;
end;

call p6(7,@res);
select @res;

-- while 循环逻辑
drop procedure p7;
create procedure p7(inout num int)
begin
    declare total int default 0;
    while num >0 do
        set total:= total +num;
        set num := num -1;
        end while;
    set num := total;
end;

set @num := 10;
call p7(@num);
select @num;

-- repeat 循环逻辑 SQL逻辑会先执行一次,不管是否满足until条件
drop procedure p8;
create procedure p8(inout num int)
begin
    declare total int default 0;

    repeat
        set total := total + num;
        set num:= num -1;
    until  num <=0
        end repeat;
    set num:= total;
end;

set @num :=-10;
call p8(@num);
select @num;

-- loop循环
drop procedure p9;
create procedure p9(in num int,out res int)
begin
    #一定要初始化赋值
    set res:=0;
    sum:loop
        if num <=0 then
            leave sum;
        end if;
        set res := res +num;
        set num:=num -1;
    end loop sum;
end;

call p9(-10,@res);
select @res;

-- loop 退出当前循环 测试一个数字相加 只加偶数不加奇数
drop procedure p10;
create procedure p10(in num int,out res int)
begin
    #一定要初始化赋值
    set res:=0;
    sum:loop
        if num <=0 then
            leave sum;
        end if;
        if num%2 = 1 then
            set num:= num -1;
            iterate sum;
        end if;
        set res := res +num;
        set num:=num -1;
    end loop sum;
end;

call p10(10,@res);
select @res;

-- cursor
-- 准备一张表
create table tb_user(
    id int auto_increment primary key ,
    name varchar(20),
    age int ,
    phone int,
    email varchar(100),
    zhuanye varchar(50)
);
-- 查看默认建表语句
show create table tb_user;
desc tb_user;
drop table tb_user;
select * from tb_user;
insert into tb_user values (null,'唐僧',80,1990618888,'tangsanzang@qq.com','玄奘法师'),
                           (null,'猪八戒',400,1990613333,'zhubajie@qq.com','净坛使者');
-- 创建存储过程
create procedure p11(in v_num int)
begin
    # 声明的临时变量必须在游标之上
    declare v_name varchar(20);
    declare v_age int default 0;
    declare pro_cur cursor for select name ,age from tb_user where age > v_num;
    #增加异常处理
    # [02000][1329] No data - zero rows fetched, selected, or processed
    #declare exit handler for sqlstate '02000' close pro_cur;
    declare exit handler for not found close pro_cur;
    # 准备要生成的表
    drop table if exists pro_tb_user;
    create table if not exists pro_tb_user(
        id int auto_increment primary key ,
        name varchar(20),
        age int
    );
    #打开游标
    open pro_cur;
    #遍历游标 死循环 知道循环到最后一条查不到数据退出
    while true do
        fetch pro_cur into v_name,v_age;
        insert into pro_tb_user values (null,v_name,v_age);
    end while;
    #关闭游标
    close pro_cur;
end;
drop procedure p11;

call p11(100);

select * from pro_tb_user;

create procedure p12(in v_num int)
begin
    # 声明的临时变量必须在游标之上
    declare v_name varchar(20);
    declare v_age int default 0;
    declare done int default 0;
    declare pro_cur cursor for select name ,age from tb_user where age > v_num;
    #增加异常处理
    # [02000][1329] No data - zero rows fetched, selected, or processed
    #declare exit handler for sqlstate '02000' close pro_cur;
    declare continue handler for not found set done:=1;
    # 准备要生成的表
#     drop table if exists pro_tb_user;
    create table if not exists pro_tb_user(
      id int auto_increment primary key ,
      name varchar(20),
      age int
    );
    #打开游标
    open pro_cur;
    #循环数据
    ext_loop :loop
        fetch pro_cur into v_name,v_age;
        if done =1 then
            leave ext_loop;
        end if;
        insert into pro_tb_user values (null,v_name,v_age);
    end loop;
    #关闭游标
    close pro_cur;
end;

call p12(50);

-- 表锁测试
-- 读锁 -都可以读 但是不能写
-- 写锁 -只有自己能读能写,别人都不能读不能写
lock tables test.tb_user read ;

select * from tb_user;
-- 创建表与删除表字段
alter table pro_tb_user add column java int;
desc pro_tb_user;
alter table pro_tb_user drop column java;

unlock tables;

-- 测试元元数据锁
start transaction ;
select * from tb_user;
select object_type,object_schema,object_name,lock_type,lock_duration from performance_schema.metadata_locks t;

commit ;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1503986.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

常见BUG如何在测试过程中分析定位

前言 在测试的日常工作中&#xff0c;相信经常有测试的小伙伴遇到类似的情况&#xff1a;在项目上线时&#xff0c;只要出现问题&#xff08;bug&#xff09;&#xff0c;就很容易成为“背锅侠”。 软件测试人员在工作中是无法避免的要和开发人员和产品经理打交道的&#xff…

鸿蒙原生应用元服务开发-WebGL网页图形库开发概述

WebGL的全称为Web Graphic Library(网页图形库)&#xff0c;主要用于交互式渲染2D图形和3D图形。目前HarmonyOS中使用的WebGL是基于OpenGL裁剪的OpenGL ES&#xff0c;可以在HTML5的canvas元素对象中使用&#xff0c;无需使用插件&#xff0c;支持跨平台。WebGL程序是由JavaScr…

代码随想录训练营第41天 | 动态规划:01背包理论基础、动态规划:01背包理论基础(滚动数组)、LeetCode 416.分割等和子集

动态规划&#xff1a;01背包理论基础 文章讲解&#xff1a;代码随想录(programmercarl.com) 视频讲解&#xff1a;带你学透0-1背包问题&#xff01;_哔哩哔哩_bilibili 动态规划&#xff1a;01背包理论基础&#xff08;滚动数组&#xff09; 文章讲解&#xff1a;代码随想录(…

openJDK17官方镜像报Error loading shared library libfreetype.so

新项目使用openJDK17做的&#xff0c;做完后打包成docker镜像到服务器上运行 docker镜像基础镜像用的是openjdk:17-jdk-alpine 运行后加载验证码的时候报&#xff1a;Error loading shared library libfreetype.so 搜了一圈没找到哪里有共用的带字体库的jdk17镜像&#xff0…

【web | CTF】BUUCTF [强网杯 2019]随便注

天命&#xff1a;这题考点有两个&#xff0c;第一个是闭合&#xff0c;第二个是叠堆注入 先探测一下是不是单引号闭合&#xff0c;其实我一开始以为是没有引号闭合的&#xff0c;毕竟是数字 经过测试&#xff0c;的确是单引号闭合 然后探测未知的东西&#xff0c;我习惯性直接…

Mybaties-Plus saveBatch()、自定义批量插入、多线程批量插入性能测试和对比

一.背景 最近在做一个项目的时候&#xff0c;由于涉及到需要将一个系统的基础数据全量同步到另外一个系统中去&#xff0c;结果一看&#xff0c;基础数据有十几万条&#xff0c;作为小白的我&#xff0c;使用单元测试&#xff0c;写了一段代码&#xff0c;直接采用了MP(Mybati…

Java新特性

本文重点分析Java12到Java17在性能方面和云计算方面取得的进展 Java 7&#xff0c;8&#xff0c;11. 17以及还未发布的Java 21均是LTS&#xff08;Long Term Support&#xff09;版本&#xff0c;Oracle提供5年的维护周期&#xff0c;以及3年的付费额外支持&#xff0c;一共8年…

ULTRAL SCALE FPGA TRANSCEIVER速率

CPLL支持2-6.25速率 QPLL支持速率 实际使用CPLL最高可以超过这个&#xff0c;QPLL最低也可以低于这个&#xff0c;xilinx留的阈量还是比较大。

5G智能制造纺织工厂数字孪生可视化平台,推进纺织行业数字化转型

5G智能制造纺织工厂数字孪生可视化平台&#xff0c;推进纺织行业数字化转型。纺织工业作为传统制造业的重要组成部分&#xff0c;面临着转型升级的紧迫需求。随着5G技术的快速发展&#xff0c;智能制造成为纺织工业转型升级的重要方向。数字孪生可视化平台作为智能制造的核心技…

Python快速入门系列-2(Python基础语法)

第三章&#xff1a;Python基础语法 3.1 变量与数据类型3.1.1 变量的定义与赋值3.1.2 数据类型3.1.3 类型转换 3.2 注释与缩进3.2.1 注释3.2.2 缩进 3.3 条件语句与循环结构3.3.1 条件语句3.3.2 循环结构 3.4 函数与模块3.4.1 函数3.4.2 参数和返回值3.4.3 模块3.4.4 标准库中的…

基于springboot+vue实现物资仓储物流管理系统项目【项目源码+论文说明】计算机毕业设计

基于springbootvue实现物资仓储物流管理系统演示 摘要 随着我国经济及产业化结构的持续升级&#xff0c;越来越多的企业借助信息化及互联网平台实现了技术的创新以及竞争力的提升&#xff0c;在电子经济的影响下仓储物流业务也获得了更多的关注度&#xff0c;利用系统平台实现…

KubeSphere4.0企业版

一、介绍 简要介绍 在 KubeSphere 企业版 v4.0 中&#xff0c;推出了全新的 KubeSphere 架构&#xff1a;KubeSphere LuBan&#xff0c;它构建在 ​​Kubernetes​​ 之上&#xff0c;支持高度可配置和可扩展。KubeSphere LuBan&#xff0c;是一个分布式的云原生可扩展开放架…

分享关于如何解决系统设计问题的逐步框架

公司广泛采用系统设计面试&#xff0c;因为在这些面试中测试的沟通和解决问题的技能与软件工程师日常工作所需的技能相似。面试官的评估基于她如何分析一个模糊的问题以及如何逐步解决问题。测试的能力还包括她如何解释这个想法&#xff0c;与他人讨论&#xff0c;以及评估和优…

突破编程_前端_JS编程实例(自适应表格列宽)

1 开发目标 针对如下的表格组件&#xff1a; 根据表格的各个列字符串宽度动态调整表格列宽&#xff1a; 2 详细需求 本组件目标是提供一个自动调整 HTML 表格列宽的解决方案&#xff0c;通过 JS 实现动态计算并调整表格每列的宽度&#xff0c;以使得表格能够自适应容器宽度&a…

00X集——cad vba 中arc(弧)对象详解

弧是CAD中一个常见的图元&#xff0c;在vba中的类名为AcadArc,创建方法为set myarc thisdrawing.modelspace.addarc(Center, Radius, StartAngle, EndAngle) Center 圆心&#xff08;Variant[变体] (三元素双精度数组); 仅用于输入 指定圆弧圆心的三维WCS坐标。&#xff09; …

SpringBoot学习之自定义注解和AOP 切面统一保存操作日志(二十九)

一、定义一个注解 这个注解是用来控制是否需要保存操作日志的自定义注解(这个类似标记或者开关) package com.xu.demo.common.anotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; i…

视频可回溯系统技术方案vue3+ts+tegg+mysql+redis+oss

一、 项目背景 保险、基金、银行等众多行业在做技术平台时都会需要一种能够准确了解用户操作行为的方式方法。诸如通过埋点、平台监控、视频可回溯等&#xff0c;通过技术手段&#xff0c;保存用户操作轨迹&#xff0c;以此规范安全销售、平台健康检查、出现纠纷时可追溯、问题…

round四舍五入在python2与python3版本间区别

round()方法返回数值的小数点四舍五入到n个数字。 语法 以下是round()方法的语法&#xff1a; round( x ,n) 参数 x --这是一个数值&#xff0c;表示需要格式化的数值 n --这也是一个数值,表示小数点后保留多少位 返回值 该方法返回 数值x 的小数点四舍五入到n个数字 …

现在海外问卷调查项目还能做么?

可以做。 目前比较好做的问卷渠道有ROM、YUNO等&#xff0c;都是非常优质的渠道&#xff0c;也是现在很多公司正在做的渠道。 这些渠道每天的题目数量是很多的&#xff0c;根本做不完。每天只要花时间做就能获得不错的收入。 我自己做这个项目很长时间了&#xff0c;这个项目…

Pytorch学习 day09(简单神经网络模型的搭建)

简单神经网络模型的搭建 针对CIFAR 10数据集的神经网络模型结构如下图&#xff1a; 由于上图的结构没有给出具体的padding、stride的值&#xff0c;所以我们需要根据以下公式&#xff0c;手动推算&#xff1a; 注意&#xff1a;当stride太大时&#xff0c;padding也会变得很大…