1. View
100 SQL:
88: v1
the SELECT .. v1 from
the SELECT asd v1 from
a query set up an alias, easy to use in the future
- create
create view SQL view name AS
PS: Virtual
- Modify
alter view SQL view name AS
- Delete
drop view view name;
2. triggers
when to make a particular table: when CRUD operations, triggers can be used to customize the behavior associated
INSERT INTO TB (....)
- DELIMITER //
- Create the BEFORE the INSERT trigger T1 Student EACH for the ROW ON
- the BEGIN
- the INSERT INTO Teacher (tname) values (NEW.sname);
- the INSERT INTO Teacher (tname) values (NEW.sname);
- the INSERT INTO Teacher (tname) values (NEW. sname);
- the INSERT INTO Teacher (tname) values (NEW.sname);
- the END //
- DELIMITER;
--
- insert into student (gender, class_id, sname) values ( 'F', 1 'Tao'), ( 'F', 1 'Gen');
- NEW, on behalf of that new data
- OLD, on behalf of that old data
3. Functions
DEF F1 (A1, A2):
return A1 + A2
F1 ()
bin ()
built-in functions:
to execute the function CURDATE SELECT ();
Blog
ID title the ctime
. 1 asdf 2019-11
2 asdf 2019-11
. 3 asdf 2019-10
asdf 2019-10. 4
SELECT the ctime, COUNT (. 1) from the ctime Blog Group
SELECT the DATE_FORMAT (the ctime, "% Y-% m"), COUNT (. 1) from the DATE_FORMAT Group Blog (the ctime, "% Y-% m")
2019 2 -11
2019-10 2
custom function (return value):
DELIMITER \\
Create function F1 (
I1 int,
I2 int)
returns int
the BEGIN
DECLARE NUM int default 0;
SET NUM = I1 + I2;
return (NUM);
\\ the END
DELIMITER;
F1 the SELECT (1,100);
4. stored procedure
an alias stored in the MySQL => cook SQL statement
alias ()
for programmers to write SQL alternative
embodiment a:
MySQL: stored procedure
: call the stored procedure
Second way:
MySQL :. .
Program: SQL statements
Three ways:
MySQL :. .
Procedure: classes and objects (SQL statements)
1. Simple
Create Procedure P1 ()
the BEGIN
SELECT * from Student;
the INSERT INTO Teacher (tname) values ( "CT");
the END
Call P1 ()
cursor.callproc ( 'P1')
2 .-parameters (in, OUT, INOUT)
DELIMITER //
Create Procedure P2 (
in int N1,
in N2 int
)
the BEGIN
SELECT * WHERE SID from Student> N1;
the END //
DELIMITER;
call p2(12,2)
cursor.callproc('p2',(12,2))
3. 参数 out
delimiter //
create procedure p3(
in n1 int,
inout n2 int
)
BEGIN
set n2 = 123123;
select * from student where sid > n1;
END //
delimiter ;
set @v1 = 10;
call p2(12,@v1)
select @v1;
set @_p3_0 = 12
ser @_p3_1 = 2
call p3(@_p3_0,@_p3_1)
select @_p3_0,@_p3_1
cursor.callproc('p3',(12,2))
r1 = cursor.fetchall()
print(r1)
cursor.execute('select @_p3_0,@_p3_1')
r2 = cursor.fetchall()
print(r2)
=======> Special
. A transmission parameter may be: in INOUT OUT
B pymysql.
Cursor.callproc ( 'P3', (12,2))
R1 = cursor.fetchall ()
Print (R1)
cursor.execute ( 'the SELECT @ _p3_0, @ _ P3_1')
r2 = cursor.fetchall ()
Print (r2)
Why have the results set out have forged the return value?
// DELIMITER
Create Procedure P3 (
in N1 int,
OUT N2 int execution result stored procedure for identifying 1,2
)
the BEGIN
INSERT INTO VV (..)
INSERT INTO VV (..)
INSERT INTO VV (..)
INSERT INTO VV (..)
INSERT INTO VV (..)
INSERT INTO VV (..)
the END //
DELIMITER;
4. transaction
DELIMITER //
Create Procedure P4 (
OUT int Status
)
the BEGIN
1. If the statement is executed abnormal {
SET Status . 1 =;
ROLLBACK;
}
begin transaction
- Chi subtracted from the account 100
- 90 plus account Fangshao Wei
-- 张根账户加10
commit;
结束
set status = 2;
END //
delimiter ;
===============================
delimiter \\
create PROCEDURE p5(
OUT p_return_code tinyint
)
BEGIN
DECLARE exit handler for sqlexception
BEGIN
-- ERROR
set p_return_code = 1;
rollback;
END;
START TRANSACTION;
DELETE from tb1;
insert into tb2(name)values('seven');
COMMIT;
-- SUCCESS
set p_return_code = 2;
END\\
delimiter ;
5. 游标
delimiter //
create procedure p6()
begin
declare row_id int; -- 自定义变量1
declare row_num int; -- 自定义变量2
declare done INT DEFAULT FALSE;
declare temp int;
declare my_cursor CURSOR FOR select id,num from A;
declare CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open my_cursor;
xxoo: LOOP
fetch my_cursor into row_id,row_num;
if done then
leave xxoo;
END IF;
set temp = row_id + row_num;
insert into B(number) values(temp);
end loop xxoo;
close my_cursor;
end //
delimter ;
6. 动态执行SQL(防SQL注入)
delimiter //
create procedure p7(
TPL VARCHAR in (255),
in Arg int
)
the begin
1. Pre detected something SQL statements legitimacy
2. SQL = + Arg formatted TPL
3. execute SQL statements
SET @xo = Arg;
the PREPARE the FROM XXX 'SELECT * from the WHERE sid Student> ';?
EXECUTE xxx the USING @xo;
DEALLOCATE PREPARE Prod;
End //
delimter;
Call P7 ( "? the SELECT * TB from the WHERE the above mentioned id>", 9)
===>
DELIMITER \\
the CREATE PROCEDURE P8 (
in int NID
)
the BEGIN
SET NID = @nid;
the PREPARE Prod the fROM 'SELECT * WHERE SID from Student>?';
the EXECUTE Prod the USING @nid;
the DEALLOCATE PREPARE Prod;
the END \\
DELIMITER;