Home > Database > Mysql Tutorial > MySQL存储结构的使用_MySQL

MySQL存储结构的使用_MySQL

WBOY
Release: 2016-06-02 08:49:50
Original
954 people have browsed it

前言:

今天公司老大让我做一个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)
);
Copy after login

新建student2

DROP TABLE IF EXISTS student2;
CREATE TABLE student2 (
id INT NOT NULL auto_increment,
name TEXT,
age INT,
PRIMARY KEY(id)
);
Copy after login

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);
Copy after login

实现功能说明:

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 = &#39;select * from student1 where age<19;&#39;;
  prepare stmt from @sentence;
  execute stmt;
  deallocate prepare stmt;
END &&
DELIMITER ;
Copy after login

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

---------------------------------------------------------------
DROP PROCEDURE IF EXISTS test_pro2;
---------------------------------------------------------------
DELIMITER &&
create procedure test_pro2()
begin
    DECLARE stop_flag INT DEFAULT 0;
    DECLARE s_name TEXT default &#39;&#39;;
    DECLARE s_age INT default 0;
    
    DECLARE cur1 CURSOR FOR (select name, age from student1 where age<19);
        DECLARE CONTINUE HANDLER FOR SQLSTATE &#39;02000&#39; 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 ;
Copy after login

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 &#39;&#39;;
    DECLARE s_age INT default 0;
    
    DECLARE cur1 CURSOR FOR (select name, age from student1 where age<p_age); continue="" cur1="" declare="declare" fetch="" for="" handler="" into="" open="" set="" sqlstate="" stop_flag="1;" while="">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 ;</p_age);>
Copy after login

4.使用方式:

call test_pro1();
or
call test_pro1(123);
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template