1、DDL、DML、DCL經常使用語句 html
一、DDL(Data Definition Language)數據庫定義語言mysql
(1)數據庫模式定義sql
#建立數據庫 create database if exsites db_name; #選定數據庫 use db_name; #刪除數據庫 drop database if exists db_name; #修改數據庫 alter database db_name set ...; #展現所建立的數據庫 show databases;
(2)表定義 數據庫
#建立表 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) ); #刪除表 drop table if exists test_table; #展現表結構 desc test_table;
二、DML(data manipulation language)數據庫操做語言安全
insert into test_table(s_age) values(18); insert into test_table set s_age=19; #插入部分列值數據 inert ...select...; #case...when 匹配條件 select s_name as name,s_sex case when 'f' then ‘女’ else '男' end as sex from test_table; #使用內置函數 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; #交叉鏈接(笛卡爾積) select * from tb1 cross join tb2; #內鏈接 #---左外鏈接 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; #---右外鏈接 select stu_info.sno,stu_info.sname,stu_score.sscore from stu_score right join stu_info on stu_score.sno=stu_info.sno; #比較運算符 select * from customers where cust_id!=2; select * from customers where cust_id<>2; #邏輯運算符 #---and 與 select * from customers where cust_id>2 and cust_sex=1; #---or 或 select * from customers where cust_id>2 or cust_sex=1; #二者之間 範圍 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; #子查詢 select * from stu_info where sno in(select sno from stu_score); #分組查詢 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 篩選---過濾分組後的數據 select saddress,ssex ,count(*) from stu_info group by saddress,ssex having count(*)>1;
三、DCL(Data Control Language)數據庫控制語言服務器
安全與訪問控制 -- 查看 mysql 數據庫的使用者帳號 select user from mysql.user; -- 密碼加密 select password(456); -- 建立用戶 create user 'zhangsan'@'localhost' identified by '123', 'lisi'@'localhost' identified by password '*531E182E2F72080AB0740FE2F2D68 9DBE0146E04'; -- 刪除用戶帳號 drop user lisi@localhost; -- 重命名 rename user 'zhangsan'@'localhost' to 'wangwu'@'localhost'; -- 修改密碼 set password for 'wangwu'@'localhost'='*6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119'; -- 設置權限 grant select n test1.customers o 'wangwu'@'localhost'; -- 建立兩個用戶 grant select,update on test1.customers to 'liming'@'localhost' identified by '123', 'huang'@'localhost' identified by '789'; --執行全部數據庫操做的權限 grant all on test1.* to 'wangwu'@'localhost'; -- 添加用戶的權限 grant create user on *.*to 'wangwu'@'localhost'; -- 權限轉移 grant select,update on test1.customers to 'zhou'@'localhost' identified by '123' with grant option; -- 權限撤回 revoke select on test1.customers from 'zhou'@'localhost';
IN 輸入參數:表示調用者向過程傳入值(傳入值能夠是字面量或變量);
OUT 輸出參數:表示過程向調用者傳出值(能夠返回多個值)(傳出值只能是變量);
INOUT 輸入輸出參數:既表示調用者向過程傳入值,又表示過程向調用者傳出值(值只能是變量);網絡
mysql> delimiter $$ mysql> CREATE PROCEDURE proc_add_stu(
-> IN sNo INTEGER, -> OUT sid int -> ) mysql> BEGIN #存儲過程開始 -> insert into student(s_no) values(sNo); -> SELECT LAST_INSERT_ID() into sid; #將選定列的值直接存儲到局部變量中 -> END $$ #存儲過程結束 mysql> delimiter; #將語句的結束符號恢復爲分號 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; #局部變量賦值(begin...和end之間) -> select P_in; -> end$$ mysql> delimiter ; mysql> set @p_in=1; #全局變量@p_in賦值 mysql> call in_param(@p_in); #將全局變量@p_in的值做爲參數傳遞給局部變量p_in +------+ | p_in | +------+ | 1 | +------+ +------+ | P_in | +------+ | 0 | +------+ mysql> select @p_in; #輸出全局變量@p_in的結果 +-------+ | @p_in | +-------+ | 1 | +-------+
以上能夠看出,p_in 在存儲過程當中被修改,但並不影響 @p_id 的值,由於前者爲局部變量、後者爲全局變量。ide
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 | +-------+ #由於out是向調用者輸出參數,不接收輸入的參數,因此存儲過程裏的p_out爲null
+-------+ | p_out | +-------+ | 2 | +-------+ mysql> select @p_out; #輸出全局變量(用戶變量)結果 +--------+ | @p_out | +--------+ | 2 | +--------+ #調用了out_proce存儲過程,輸出參數,改變了p_out變量的值
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 | +----------+ #調用了inout_param存儲過程,接受了輸入的參數,也輸出參數,改變了變量
變量做用域函數
內部的變量在其做用域範圍內享有更高的優先權,當執行到 end。變量時,內部變量消失,此時已經在其做用域外,變量再也不可見了,應爲在存儲過程外不再能找到這個申明的變量,可是你能夠經過 out 參數或者將其值指派給會話變量來保存其值。性能
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 ;
條件語句
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 ;
循環語句
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 mc cursor for select * from class; -- 打開遊標 open mc; -- 獲取結果 fetch mc into id,name; -- 這裏是爲了顯示獲取結果 select id,name; -- 關閉遊標 close mc; end;
#刪除已經存在的存儲函數 DROP FUNCTION IF EXISTS func_stu; #建立存儲函數(聲明返回類型爲varChar(50)) CREATE FUNCTION func_stu(in_id INT) RETURNS VARCHAR(50) BEGIN DECLARE o_name VARCHAR(50); #聲明局部變量 SELECT name INTO o_name FROM tb_stu WHERE id = in_id; #tb_stu指事先建立好的數據庫 RETURN o_name; END;
SELECT func_stu(1);
DROP FUNCTION IF EXISTS func_stu;
五、修改存儲函數
ALTER FUNCTION func_name [characteristic ...] characteristic: COMMENT 'string' | LANGUAGE SQL | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA } | SQL SECURITY { DEFINER | INVOKER }
感謝閱讀,如需轉載,請註明出處,謝謝!http://www.javashuo.com/article/p-ftodbktn-ck.html