Table of Contents
前言:
笔者环境:
准备工作:
1.新建两张数据表:student1, student2
2.向student1中新增数据
实现功能说明:
注意事项:
4.使用方式:
1.打印student1中的部分信息
2.复制表存储过程的编写(不带参数)
3.复制表存储过程的编写(带参数)
Home Database Mysql Tutorial MySQL存储结构的使用_MySQL

MySQL存储结构的使用_MySQL

Jun 02, 2016 am 08:49 AM
structure

前言:

今天公司老大让我做一个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
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the syntax and structure characteristics of lambda expressions? What are the syntax and structure characteristics of lambda expressions? Apr 25, 2024 pm 01:12 PM

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

What is the origin of the basic structure and technology of the internet? What is the origin of the basic structure and technology of the internet? Dec 15, 2020 pm 04:48 PM

The basic structure and technology of the internet originated from ARPANET. ARPANET is a milestone in the development of computer network technology. Its research results have played an important role in promoting the development of network technology and laid the foundation for the formation of the Internet. Arpanet (Arpanet) was the world's first operational packet switching network developed by the U.S. Defense Advanced Research Projects Agency. It is the ancestor of the global Internet.

How to implement a layout with a fixed navigation menu using HTML and CSS How to implement a layout with a fixed navigation menu using HTML and CSS Oct 26, 2023 am 11:02 AM

How to use HTML and CSS to implement a layout with a fixed navigation menu. In modern web design, fixed navigation menus are one of the common layouts. It can keep the navigation menu always at the top or side of the page, allowing users to browse web content conveniently. This article will introduce how to use HTML and CSS to implement a layout with a fixed navigation menu, and provide specific code examples. First, you need to create an HTML structure to present the content of the web page and the navigation menu. Here is a simple example

In-depth analysis of the structure and purpose of the MySQL.proc table In-depth analysis of the structure and purpose of the MySQL.proc table Mar 15, 2024 pm 02:36 PM

The MySQL.proc table is a system table that stores stored procedure and function information in the MySQL database. By in-depth understanding of its structure and purpose, you can better understand the operating mechanism of stored procedures and functions in MySQL, and perform related management and optimization. The structure and purpose of the MySQL.proc table will be analyzed in detail below, and specific code examples will be provided. 1. The structure of the MySQL.proc table. The MySQL.proc table is a system table that stores the definitions and related information of all stored procedures and functions.

How to design the mall's evaluation table structure in MySQL? How to design the mall's evaluation table structure in MySQL? Oct 31, 2023 am 08:27 AM

How to design the mall's evaluation table structure in MySQL? In a shopping mall system, evaluation is one of the most important functions. Evaluations can not only provide reference for other users, but also help merchants understand users’ feedback and opinions on products. Designing a reasonable evaluation form structure is crucial to the operation of the mall system and user experience. This article will introduce how to design the mall's evaluation table structure in MySQL and provide specific code examples. First, we need to create two basic tables: product table and user table. product list (product

What are the common flow control structures in Python? What are the common flow control structures in Python? Jan 20, 2024 am 10:38 AM

There are four common flow control structures in Python, namely sequential structure, conditional structure, loop structure and jump structure. The following will introduce them one by one and provide corresponding code examples. Sequential structure: A sequential structure is a structure in which the program is executed in a predetermined order from top to bottom, without specific keywords or syntax. Sample code: print("This is the sequence structure example 1")print("This is the sequence structure example 2")print("This is the sequence structure example 2")

Exploring the internal structure of the Linux file system Exploring the internal structure of the Linux file system Mar 21, 2024 am 10:03 AM

Title: Exploring the Internal Structure of the Linux File System The Linux operating system is famous for its stability and flexibility, and the file system, as one of its cores, plays a key role. An in-depth understanding of the internal structure of the Linux file system not only helps us understand the working principle of the operating system, but also helps us better manage and optimize the system. This article will explore the internal structure of the Linux file system with detailed code examples and explanations. 1. Introduction to file systems File systems are used by computers to organize and store files and to

Oracle home directory structure and management techniques Oracle home directory structure and management techniques Mar 07, 2024 pm 04:03 PM

The structure and management skills of Oracle's home directory. As the industry's leading enterprise-level database management system, Oracle database's home directory structure and management skills are very important for database administrators. This article will introduce the structure, important directories and management techniques of the Oracle home directory in detail, and provide specific code examples to help readers better understand and manage the Oracle database. Oracle home directory structure In the Oracle database, the home directory includes two important directories: ORACLE_BA

See all articles