创建一张表,表的字段和数据来自于一条查询语句
语法: create table 表名 as select *from 另一张表 where 1=1; ----创建一张表会把另一张表中的字段和对应的数据全部复制过去
如果我们只想要字段不要数据
create table 表名 as select *from 另一张表 where 1=2;
如果我们只想要个别字段
create table 表名 as select 表名.字段名 ,表名.字段名2 from 另一张表 where 1=2;
我们创建的这张表也可以来自一个子查询
create table 表名 as 子查询
存储过程procedure
概念: 存储过程是数据库中的一个对象,存储在服务端,用来封装多条SQL 语句且带有逻辑性,可以实现一个功能,由于他在创建时就已经对SQL进行了编译,所以执行效率高,而且可以重复调用,类似我们JAVA中的方法
语法:
DELIMITER $$
create
procedure
‘performance_schema’.‘myTestpro’()
begin
end$ $
DELIMITER;
**注意:**创建存储过程需要管理员分配权限
参数:
in: 输入参数
out:输出参数
inout:输入输出参数
例如:
DELIMITER $ $
create
procedure
‘performance_schema’.‘myTestpro’(
IN num int,out r int
)
begin
delete from emp where empno=num;
select count(*) from emp into r;
end$ $
DELIMITER;
调用存储过程 call
语法:call myTestpro(9527,@rr)
查询结果: select @rr
删除存储过程 drop
语法: drop procedure mytestpro
查看存储过程 show
语法:show procedure status\G;查看所有的存储过程状态
show create procedure 存储过程名字\G;—查看创建存储过程的语句
带有IF 逻辑的存储过程 if then elseif else
delimiter $ $
create procedure pro_testIF(in num int,out str varchar(20))
begin
if num=1 then
set str='星期一‘;
elseif num=2 then
set str=‘星期二’;
ELSEIF num=3 THEN
SET str=‘星期三’;
ELSE
SET str=‘输入错误’;
END IF; -注意要结束if