I. common statements of DDL, DML and DCL
1. DDL (Data Definition Language) database definition language
(1) database schema definition
#Create database create database if exsites db_name; #Selected database use db_name; #Delete database drop database if exists db_name; #modify the database alter database db_name set ...; #Show database created show databases;
(2) table definition
#Create table create table test_table ( s_id int not null auto_increment, s_name char(50) not null default "hanmei", s_age int not null, primary key(s_id), index index_name(s_name) ); #Delete table drop table if exists test_table; #Display table structure desc test_table;
2. DML (data manipulation language) database operation language
insert into test_table(s_age) values(18); insert into test_table set s_age=19; #Insert partial column value data inert ...select...; #case...when Matching condition select s_name as name,s_sex case when 'f' then 'female' else 'male' end as sex from test_table; #Use built-in functions select count(*) from customers; select max(cust_id) from customers; select min(cust_id) from customers; select sum(cust_id) from customers; select avg(cust_id) from customers; #Cross connect (Cartesian product) select * from tb1 cross join tb2; #Internal connection #---Left outer join select * from stu_info inner join stu_score on stu_info.sno=stu_score.sno; select stu_info.sno,stu_info.sname,stu_score.sscore from stu_info left join stu_score on stu_info.sno=stu_score.sno; #---Right outer join select stu_info.sno,stu_info.sname,stu_score.sscore from stu_score right join stu_info on stu_score.sno=stu_info.sno; #Comparison operator select * from customers where cust_id!=2; select * from customers where cust_id<>2; #Logical operators #---and And select * from customers where cust_id>2 and cust_sex=1; #---or or select * from customers where cust_id>2 or cust_sex=1; #Between the two select * from customers where cust_id between 2 and 4; select * from customers where cust_id>=2 and cust_id<=4; #in select * from customers where cust_id in(2,4); select * from customers where cust_id=2 or cust_id=4; #Subquery select * from stu_info where sno in(select sno from stu_score); #Group query select ssex,count(*)from stu_info group by ssex; select saddress,ssex,count(*) from stu_info group by saddress,ssex; select saddress,ssex,count(*) from stu_info group by saddress,ssex with rollup; #having screen---Filter grouped data select saddress,ssex ,count(*) from stu_info group by saddress,ssex having count(*)>1;
3. DCL (Data Control Language) database control language
Security and access control -- See mysql User account of database select user from mysql.user; -- Cipher encryption select password(456); -- Create user create user 'zhangsan'@'localhost' identified by '123', 'lisi'@'localhost' identified by password '*531E182E2F72080AB0740FE2F2D68 9DBE0146E04'; -- delete user account drop user lisi@localhost; -- rename rename user 'zhangsan'@'localhost' to 'wangwu'@'localhost'; -- Change Password set password for 'wangwu'@'localhost'='*6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119'; -- Setting permissions grant select n test1.customers o 'wangwu'@'localhost'; -- Create two users grant select,update on test1.customers to 'liming'@'localhost' identified by '123', 'huang'@'localhost' identified by '789'; --Permission to perform all database operations grant all on test1.* to 'wangwu'@'localhost'; -- Add user's rights grant create user on *.*to 'wangwu'@'localhost'; -- Permission transfer grant select,update on test1.customers to 'zhou'@'localhost' identified by '123' with grant option; -- Permission withdrawal revoke select on test1.customers from 'zhou'@'localhost';
IN input parameter: indicates the value passed IN by the caller to the procedure (the value can be literal or variable);
OUT output parameter: indicates the outgoing value from the procedure to the caller (multiple values can be returned) (the outgoing value can only be a variable);
INOUT I / O parameters: both the value passed in from the caller to the process and the value passed out from the process to the caller (the value can only be a variable);
mysql> delimiter $$ mysql> CREATE PROCEDURE proc_add_stu(
-> IN sNo INTEGER, -> OUT sid int -> ) mysql> BEGIN #Stored procedure start -> insert into student(s_no) values(sNo); -> SELECT LAST_INSERT_ID() into sid; #Stores the value of the selected column directly in a local variable -> END $$ #End of stored procedure mysql> delimiter; #Returns the end of a statement to a semicolon mysql> call pro_add_stu('0001');
mysql> delimiter $$ mysql> create procedure in_proce(in p_in int) -> begin -> select p_in; -> set p_in=0; #Local variable assignment (between begin... And end) -> select P_in; -> end$$ mysql> delimiter ; mysql> set @p_in=1; #Global variable @ P inassignment mysql> call in_param(@p_in); #Pass the value of the global variable @ P InAs a parameter to the local variable p in +------+ | p_in | +------+ | 1 | +------+ +------+ | P_in | +------+ | 0 | +------+ mysql> select @p_in; #Output the result of global variable @ P in +-------+ | @p_in | +-------+ | 1 | +-------+
As can be seen above, P ﹣ in is modified in the stored procedure, but does not affect the value of @ P ﹣ ID, because the former is a local variable and the latter is a global variable.
mysql> delimiter // mysql> create procedure out_proce(out p_out int) -> begin -> select p_out; -> set p_out=2; -> select p_out; -> end -> // mysql> delimiter ; mysql> set @p_out=1; mysql> call out_proce(@p_out); +-------+ | p_out | +-------+ | NULL | +-------+ #because out It outputs parameters to the caller and does not receive the input parameters, so in the stored procedure p_out by null
+-------+ | p_out | +-------+ | 2 | +-------+ mysql> select @p_out; #Output global variable (user variable) results +--------+ | @p_out | +--------+ | 2 | +--------+ #The out Pro stored procedure is called to output parameters and change the value of the P out variable
mysql> delimiter $$ mysql> create procedure inout_proce(inout p_inout int) -> begin -> select p_inout; -> set p_inout=2; -> select p_inout; -> end -> $$ mysql> delimiter ; mysql> set @p_inout=1; mysql> call inout_proce(@p_inout); +---------+ | p_inout | +---------+ | 1 | +---------+ +---------+ | p_inout | +---------+ | 2 | +---------+ mysql> select @p_inout; +----------+ | @p_inout | +----------+ | 2 | +----------+ #The inout param stored procedure is called, the input parameters are accepted, and the output parameters are also changed
Variable scope
Internal variables have a higher priority in their scope when executed to end. When a variable is in use, the internal variable disappears. At this time, the variable is no longer visible. The declared variable can no longer be found outside the stored procedure. However, you can save its value by out parameter or assigning its value to the session variable.
mysql > DELIMITER // mysql > CREATE PROCEDURE proc3() -> begin -> declare x1 varchar(5) default 'outer'; -> begin -> declare x1 varchar(5) default 'inner'; -> select x1; -> end; -> select x1; -> end; -> // mysql > DELIMITER ;
Conditional statement
mysql > DELIMITER // mysql > CREATE PROCEDURE proc2(IN parameter int) -> begin -> declare var int; -> set var=parameter+1; -> if var=0 then -> insert into t values(17); -> end if; -> if parameter=0 then -> update t set s1=s1+1; -> else -> update t set s1=s1+2; -> end if; -> end; -> // mysql > DELIMITER ;
Loop statement
mysql > DELIMITER // mysql > CREATE PROCEDURE proc4() -> begin -> declare var int; -> set var=0; -> while var<6 do -> insert into t values(var); -> set var=var+1; -> end while; -> end; -> // mysql > DELIMITER ;
create procedure p1() begin declare id int; declare name varchar(15); -- declare cursor declare mc cursor for select * from class; -- Open cursor open mc; -- Get results fetch mc into id,name; -- This is to show the results select id,name; -- Close cursor close mc; end;
#Delete existing storage function DROP FUNCTION IF EXISTS func_stu; #Create storage function (declaration return type is varChar(50)) CREATE FUNCTION func_stu(in_id INT) RETURNS VARCHAR(50) BEGIN DECLARE o_name VARCHAR(50); #Declare local variables SELECT name INTO o_name FROM tb_stu WHERE id = in_id; #TB? Stu refers to the database created in advance RETURN o_name; END;
SELECT func_stu(1);
DROP FUNCTION IF EXISTS func_stu;
5. Modify the storage function
ALTER FUNCTION func_name [characteristic ...] characteristic: COMMENT 'string' | LANGUAGE SQL | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA } | SQL SECURITY { DEFINER | INVOKER }
Thank you for reading, if you need to reprint, please indicate the source, thank you! https://www.cnblogs.com/huyangshu-fs/p/11669708.html