瞭解幾個容易混淆的概念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;
複製代碼