了解几个容易混淆的概念sql
view
):能够理解成临时表, 若是你每次都须要连表查询, 并且sql老长老长了, 那就试试视图吧, 你会感谢个人.安全
transaction
):一组sql语句, 要么所有执行成功, 要么都不执行session
function
):执行特定功能的代码段, 和存储过程(
procedure
)很像 区别在于, 函数有且只有个返回值
, 存储过程对返回值的个数没有要求, 没有返回值也是能够的
函数
增删改
, 视图
主要用来查
优势:post
缺点:ui
参数名
, 参数类型
, 参数方向
in
, out
, inout
)
in
: 只把值传给存储过程out
: 只从存储过程当中返回值inout
: 参数在存储过程当中进行运算, 而后返回若是没有写方向, 默认是
in
spa
使用存储过程, 为
教师表
添加数据, 只须要姓名
和职称
设计
drop table if exists teacher;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL auto_increment primary key,
`salary` int(11) NOT NULL,
`title` char(20) NOT NULL,
`name` char(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `teacher`
(`id`, `salary`, `title`, `name`)
VALUES
(1, 1500, '初级讲师', '教师1');
INSERT INTO `teacher`
(`id`, `salary`, `title`, `name`)
VALUES
(2, 5500, '高级讲师', '教师2');
INSERT INTO `teacher`
(`id`, `salary`, `title`, `name`)
VALUES
(3, 6500, '金牌讲师', '教师3');
INSERT INTO `teacher`
(`id`, `salary`, `title`, `name`)
VALUES
(4, 7500, '教授', '教师4');
INSERT INTO `teacher`
(`id`, `salary`, `title`, `name`)
VALUES
(5, 8500, '金牌教授', '教师5');
复制代码
drop procedure if exists add_teacher;
create procedure add_teacher(
in teacher_name varchar(20),
in teacher_title varchar(20)
) begin
INSERT INTO `teacher`
(`salary`, `title`, `name`)
VALUES
(3500, teacher_title, teacher_name);
end;
复制代码
call add_teacher('张三','特高级教师');
call add_teacher('李四','特高级教师他爹');
复制代码
以上是建立并用存储过程的实例.code
其中咱们使用了in
类型的参数, 接下来试试out
事务
再看个例子, 添加数据后, 返回该表一共有多少条记录, 也就是说 添加教师后, 能够查看已经有多少老师了
drop procedure if exists add_teacher;
create procedure add_teacher(
in teacher_name varchar(20),
in teacher_title varchar(20),
out teacher_count int)
begin
INSERT INTO `teacher`
(`salary`, `title`, `name`)
VALUES
(3500, teacher_title, teacher_name);
select count(*) into teacher_count from teacher;
end;
复制代码
call add_teacher('张三','特高级教师', @count);
select @count;
call add_teacher('李四','特高级教师他爹', @count);
select @count;
复制代码
inout
表示该参数, 既用于输入, 有用于输出; 好比发年终奖(14薪),最后一个月, 发3个月的工资 咱们的需求, 输入一个数字(每一年的薪水, 14薪, 仍是15薪), 返回最后一个月须要发多少钱
drop procedure if exists count_salary;
create procedure count_salary(
in month int,
inout salary int
) begin
select salary*(month - 11) into salary;
end;
set @salary = 2000;
call count_salary(14,@salary);
select @salary;
复制代码
为了方便理解, 个人变量使用中文...
drop procedure if exists 计算工资;
create procedure 计算工资(
in 月份 int,
inout 月工资 int
) begin
select 月工资*(月份 - 11) into 月工资;
end;
set @月工资 = 2000;
call 计算工资(15,@月工资);
select @月工资;
复制代码
in
,out
和 inout
的区别何在呢?
假设咱们想计算一个数字的平方, 咱们分别用
inout
和in
,out
来试一下
drop procedure if exists count_square;
create procedure count_square(inout a int) BEGIN
select a*a into a;
end;
set @a = 3;
call count_square(@a);
select @a;
复制代码
一样提供中文版
drop procedure if exists 计算平方;
create procedure 计算平方(inout 数字 int) BEGIN
select 数字*数字 into 数字;
end;
set @数字 = 3;
call 计算平方(@数字);
select @数字;
复制代码
drop procedure if exists count_square2;
create procedure count_square2(in a int, out b int) begin
select a*a into b;
end;
call count_square2(3,@b);
select @b;
复制代码
drop procedure if exists 再计算平方;
create procedure 再计算平方(in 第一个数字 int, out 第二个数字 int) begin
select 第一个数字*第一个数字 into 第二个数字;
end;
call 再计算平方(3,@第二个数字);
select @第二个数字;
复制代码
show procedure status like 'salary';
复制代码
show create procedure add_teacher;
复制代码
drop procedure salary;
复制代码
global
) 只要不重启, 都适用session
) 只能是当前会话查看变量(所有)
-- 第一条和第三条效果同样
show variables;
show global variables;
show session variables;
复制代码
查看变量(搜索)
-- 第一条和第三条效果同样
show variables like 'auto%';
show global variables like 'auto%';
show session variables like 'auto%';
复制代码
查看变量(一个) select @@global|[session].系统变量;
-- 第一句和第三句同样
select @@autocommit;
select @@global.autocommit;
select @@session.autocommit;
复制代码
全局变量, 重启失效, 除非改配置文件
修改变量/变量赋值
语法
set global|[session] 系统变量名 = 值;
set @@global|[session].系统变量名 = 值;
set autocommit = 0;
-- 至关于
-- set session autocommit = 0
-- 或者
-- set @@session.autocommit = 0;
-- 全局
-- set @@global.autocommit = 0;
-- set global autocommit = 0;
select @@autocommit;
select @@session.autocommit;
select @@global.autocommit;
复制代码
用户自定义变量
set @variableName = value;
set @variableName := value;
select @variableName := value;
set @hello_world = 'hello world !!!!';
select @hello_world;
set @hello_world := 'hello world !!!!';
select @hello_world;
select @hello_world := 'hello world !!!!';
select @hello_world;
复制代码
查询赋值
set @count = 0;
select count(*) into @count from teacher;
select @count;
复制代码
局部变量(begin end
)
drop PROCEDURE if EXISTS hello_world;
create PROCEDURE hello_world() begin
declare result int;
select count(*) into result from teacher;
select result;
end;
call hello_world;
复制代码
不能在begin end
外使用
drop PROCEDURE if EXISTS hello_world;
create PROCEDURE hello_world() begin
declare result int;
select count(*) into result from teacher;
select result;
end;
call hello_world;
select result;
复制代码
注意, 必须是begin后的第一行
drop PROCEDURE if EXISTS hello_world;
create PROCEDURE hello_world() begin
select 1+1;
declare result int;
select count(*) into result from teacher;
select result;
end;
call hello_world;
复制代码
@变量名能够在begin end
中使用
drop PROCEDURE if EXISTS hello_world;
set @result = 0;
create PROCEDURE hello_world() begin
select count(*) into @result from teacher;
select @result;
end;
call hello_world;
select @result;
复制代码