当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL存储结构用法案例分析

MySQL存储结构用法案例分析

2018年08月08日  | 移动技术网IT编程  | 我要评论

本文实例讲述了mysql存储结构用法。分享给大家供大家参考,具体如下:

前言

今天公司老大让我做一个mysql的调研工作,是关于mysql的存储结构的使用。这里我会通过3个例子来介绍一下mysql中存储结构的使用过程,以及一些需要注意的点。

笔者环境

系统:windows 7

mysql:mysql 5.0.96

准备工作

1.新建两张数据表:student1, student2

新建student1

drop table if exists student1;
create table student1 (
id int not null auto_increment,
name text,
age int,
primary key(id)
);

新建student2

drop table if exists student2;
create table student2 (
id int not null auto_increment,
name text,
age int,
primary key(id)
);

2.向student1中新增数据

insert into student1 (name, age) values ('xiaoming', 18);
insert into student1 (name, age) values ('xiaohong', 17);
insert into student1 (name, age) values ('xiaogang', 19);
insert into student1 (name, age) values ('xiaoyu', 18);
insert into student1 (name, age) values ('xiaohua', 20);

实现功能说明

1.打印student1中的部分信息

2.把student1中的部分数据复制到student2中

3.传入参数作为限制条件,把student1中的部分数据复制到student2中

注意事项

在编写存储结构的时候,我们不能以分号(;)结束。因为我们的sql语句就是以分号(;)结尾的。这里我们要修改一下存储结构的结束符号(&&)。

这里我们使用mysql中的delimiter进行修改,并在存储结构创建完毕时,再改为分号(;)结束即可。

关于这一点在后面的例子中有所体现。在编写mysql的触发器中,也会用到类似的情况。

使用方式

1.打印student1中的部分信息

---------------------------------------------------------------
drop procedure if exists test_pro1;
---------------------------------------------------------------
delimiter &&
create procedure test_pro1()
begin
 set @sentence = 'select * from student1 where age<19;';
 prepare stmt from @sentence;
 execute stmt;
 deallocate prepare stmt;
end &&
delimiter ;

2.复制表存储过程的编写(不带参数)

---------------------------------------------------------------
drop procedure if exists test_pro2;
---------------------------------------------------------------
delimiter &&
create procedure test_pro2()
begin
  declare stop_flag int default 0;
  declare s_name text default '';
  declare s_age int default 0;
  declare cur1 cursor for (select name, age from student1 where age<19);
    declare continue handler for sqlstate '02000' set stop_flag=1;
  open cur1;
    fetch cur1 into s_name, s_age;
  while stop_flag<>1 do
    insert into student2(name, age) values(s_name, s_age);
    fetch cur1 into s_name, s_age;
  end while;
  close cur1;
end &&
delimiter ;

3.复制表存储过程的编写(带参数)

---------------------------------------------------------------
drop procedure if exists test_pro3;
---------------------------------------------------------------
delimiter &&
create procedure test_pro3(in p_age int)
begin
  declare stop_flag int default 0;
  declare s_name text default '';
  declare s_age int default 0;
  declare cur1 cursor for (select name, age from student1 where age<p_age);
    declare continue handler for sqlstate '02000' set stop_flag=1;
  open cur1;
    fetch cur1 into s_name, s_age;
  while stop_flag<>1 do
    insert into student2(name, age) values(s_name, s_age);
    fetch cur1 into s_name, s_age;
  end while;
  close cur1;
end &&
delimiter ;

这里的sqlstate '02000'和not found系统返回值是一样的。

4.使用方式

call test_pro1();

or

call test_pro1(123);

更多关于mysql相关内容感兴趣的读者可查看本站专题:《mysql存储过程技巧大全》、《mysql常用函数大汇总》、《mysql日志操作技巧大全》、《mysql事务操作技巧汇总》及《mysql数据库锁相关技巧汇总

希望本文所述对大家mysql数据库计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网