在mysql的存储过程创建临时表的例子,是学习mysql 临时表操作的不错的例子。
操作步骤:
mysql> mysql> mysql> CREATE TABLE Employee( //创建普通表 -> id int, -> first_name VARCHAR(15), -> last_name VARCHAR(15), -> start_date DATE, -> end_date DATE, -> salary FLOAT(8,2), -> city VARCHAR(10), -> description VARCHAR(15) -> ); Query OK, 0 rows affected (0.03 sec) mysql> mysql> mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description) -> values (1,‘Jason‘, ‘Martin‘, ‘19960725‘, ‘20060725‘, 1234.56, ‘Toronto‘, ‘Programmer‘); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description) -> values(2,‘Alison‘, ‘Mathews‘, ‘19760321‘, ‘19860221‘, 6661.78, ‘Vancouver‘,‘Tester‘); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description) -> values(3,‘James‘, ‘Smith‘, ‘19781212‘, ‘19900315‘, 6544.78, ‘Vancouver‘,‘Tester‘); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description) -> values(4,‘Celia‘, ‘Rice‘, ‘19821024‘, ‘19990421‘, 2344.78, ‘Vancouver‘,‘Manager‘); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description) -> values(5,‘Robert‘, ‘Black‘, ‘19840115‘, ‘19980808‘, 2334.78, ‘Vancouver‘,‘Tester‘); Query OK, 1 row affected (0.01 sec) mysql> mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description) -> values(6,‘Linda‘, ‘Green‘, ‘19870730‘, ‘19960104‘, 4322.78,‘New York‘, ‘Tester‘); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description) -> values(7,‘David‘, ‘Larry‘, ‘19901231‘, ‘19980212‘, 7897.78,‘New York‘, ‘Manager‘); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description) -> values(8,‘James‘, ‘Cat‘, ‘19960917‘, ‘20020415‘, 1232.78,‘Vancouver‘, ‘Tester‘); Query OK, 1 row affected (0.00 sec) mysql> mysql> select * from Employee; //查询数据 +------+------------+-----------+------------+------------+---------+-----------+-------------+ | id | first_name | last_name | start_date | end_date | salary | city | description | +------+------------+-----------+------------+------------+---------+-----------+-------------+ | 1 | Jason | Martin | 1996-07-25 | 2006-07-25 | 1234.56 | Toronto | Programmer | | 2 | Alison | Mathews | 1976-03-21 | 1986-02-21 | 6661.78 | Vancouver | Tester | | 3 | James | Smith | 1978-12-12 | 1990-03-15 | 6544.78 | Vancouver | Tester | | 4 | Celia | Rice | 1982-10-24 | 1999-04-21 | 2344.78 | Vancouver | Manager | | 5 | Robert | Black | 1984-01-15 | 1998-08-08 | 2334.78 | Vancouver | Tester | | 6 | Linda | Green | 1987-07-30 | 1996-01-04 | 4322.78 | New York | Tester | | 7 | David | Larry | 1990-12-31 | 1998-02-12 | 7897.78 | New York | Manager | | 8 | James | Cat | 1996-09-17 | 2002-04-15 | 1232.78 | Vancouver | Tester | +------+------------+-----------+------------+------------+---------+-----------+-------------+ 8 rows in set (0.00 sec) mysql> mysql> mysql> mysql> delimiter $$ mysql> mysql> CREATE PROCEDURE myProc() //创建存储过程、并创建临时表 -> -> BEGIN -> DROP TEMPORARY TABLE IF EXISTS employeeTemp; -> CREATE TEMPORARY TABLE employeeTemp AS -> SELECT id,start_date -> FROM employee; -> END$$ Query OK, 0 rows affected (0.00 sec) mysql> mysql> delimiter ; mysql> call myProc(); //调用存储过程 Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> select * from employeeTemp; +------+------------+ | id | start_date | +------+------------+ | 1 | 1996-07-25 | | 2 | 1976-03-21 | | 3 | 1978-12-12 | | 4 | 1982-10-24 | | 5 | 1984-01-15 | | 6 | 1987-07-30 | | 7 | 1990-12-31 | | 8 | 1996-09-17 | +------+------------+ 8 rows in set (0.00 sec) mysql> drop TEMPORARY TABLE employeeTemp; //删除临时表 Query OK, 0 rows affected (0.00 sec) mysql> drop procedure myProc; //删除存储过程 Query OK, 0 rows affected (0.00 sec) mysql> mysql> mysql> mysql> mysql> drop table Employee; //删除普通表 Query OK, 0 rows affected (0.00 sec) mysql> mysql> /*******************************/
一,创建数据库
mysql>create database db_proc;
1 |
mysql>create database db_proc; |
二,创建表
mysql>CREATE TABLE `proc_test` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT, #ID,自动增长
`username` varchar(20) NOT NULL, #用户名
`password` varchar(20) NOT NULL, #密码
PRIMARY KEY (`id`) #主键
) ENGINE=MyISAM AUTO_INCREMENT=50 DEFAULT CHARSET=utf8; #设置表引擎和字符集
1 2 3 4 5 6 |
mysql>CREATE TABLE `proc_test` ( `id` tinyint(4) NOT NULL AUTO_INCREMENT, #ID,自动增长 `username` varchar(20) NOT NULL, #用户名 `password` varchar(20) NOT NULL, #密码 PRIMARY KEY (`id`) #主键 ) ENGINE=MyISAM AUTO_INCREMENT=50 DEFAULT CHARSET=utf8; #设置表引擎和字符集 |
三、创建存储过程
create procedure mytest(in name varchar(20),in pwd varchar(20))#定义传入的参数
begin
insert into proc_test(username,password) values(name,pwd);
#把传进来的参数name和pwd插入表中,别忘记分号
end; #注意这个分号别忘记了
1 2 3 4 5 |
create procedure mytest(in name varchar(20),in pwd varchar(20))#定义传入的参数 begin insert into proc_test(username,password) values(name,pwd); #把传进来的参数name和pwd插入表中,别忘记分号 end; #注意这个分号别忘记了 |
四、测试调用存储过程
用法:call 存储过程名称(传入的参数)
call proc_test("绝心是凉白开","www.ttlsa.com")
1 |
call proc_test("绝心是凉白开","www.ttlsa.com") |
username为”绝心是凉白开“传入数据库中,密码”www.ttlsa.com“
五、查看数据库中有无加入的数据
select * from proc_test where username=‘绝心是凉白开’;#如果有内容说明成功了
1 |
select * from proc_test where username=‘绝心是凉白开’;#如果有内容说明成功了 |
六、删除存储过程
drop procdure 存储过程名;