概述 | 1. 视图中的数据是对现有表数据的引用; 2. 视图是从一个表或者多个表中导出的; 3. 视图中可以使用:select,insert,update,delete,, 4. 视图可以从现有视图中创建,视图一经定义便存储于数据库中; 5. 通过视图看到的数据只是基本表中的数据; 6. 视图中数据修改,基本表中数据也同时修改; 7. 基本表中的数据修改,视图中的数据也同时修改; |
创建 | 视图创建基于select语句和基本表数据; create [or replace] [algorithm={undefined|merge|templateble}] view viewName[{columnList}] as select statement [with [cascaded|local] check option] create 创建新视图,replace替换已有视图; algorithm选择的算法; viewName视图名称 ;columnList属性列;select statement选择语句; [with [cascaded|local] check option]视图更新时保证在视图的权限范围内; l undefined自动选择算法| l merge将使用的视图语句和视图定义结合起来,使得视图定义的某一部分取代语句对应的部分| l templateble将视图结果存入临时表,然后用临时表来执行语句; Ø cascaded更新视图时需要满足所有相关视图和表的条件;(满足所有条件) Ø local表示可更新视图时满足该视图本身定义的条件即可;(满足自身条件) 要求具有create view权限和select权限,如果replace要求drop视图权限; 视图属于数据库,默认在当前数据库创建; 要在指定数据库创建视图,需要使用完全限定名daName.viewName; create view myView as select quantity,price,quantity*price from t; create view myView(quantity,price,total) as select quantity,price,quantity*price from t; create view class(id,name,class) as select student.id,student.name,stuInfo.class from student,stuInfo where student.id=stuInfo.id; |
查看 | describe viewName; show table status like’viewName’;show table status like ‘viewT’\g; show create view viewName;show create view myView; select*from information_schema.views; |
修改 | create or replace view myView as select*from t; 可以用create or replace修改,语法同创建; alter view myView as select quantity from t; |
更新 | update view myView set quantity=5; insert into t values(3,5);更新表,视图会跟随更新; delete from myView where price=5; 视图中的删除是通过删除基本表中的数据实现的; |
删除 | drop view [if exists]viewName[,...] [restrict|cascade] 可以同时删除多个视图,逗号隔开;必须要有drop权限; drop view if exists myView; |
创建 | create procedure create function ■■■存储过程 create procedure pName([pParameter])[characteristics...]routineBody; create procedure 过程名([参数])[特性...]过程体; 参数:[in|out|inout]pParameter输入、输出、可入可出 特性:characteristics [table] |
①语言特性language sql ②结果是否确定[not]deterministic ③使用SQL语句的限制:{constrains sql|no sql|reads sql data|modifies sql data} l constrains sql表明子程序包含SQL语句,但是不包含读写数据的语句;(默认) l no sql表明子程序不包含SQL语句; l reads sql data子程序包含读写数据的语句; l modidies sql data包含读写数据的语句; ④指明谁拥有权限来执行sql security{definer|invoker} l definer表示只有定义者才能执行;(默认) l invoker表示拥有权限的调用者可以执行; ⑤comment ‘string’注释信息,用来描述存储过程或函数; |
create prodedure avgAge() begin select avg(age) as avgAge from student; end; | delimiter // create procedure proc() begin select*from student; end// delimiter; |
create procedure CountProc (out count int) begin select count(*)into count from student; end; | create funtion myFunc() returns char(50) return (select name from student where age=22); |
declare fruitName char(50); declare fruitPrice decimal(8,2); select name,price into fruitName,fruitPrice from fruits where id=’a1’; |
if exprCondition then statement; [elseif exprCondition then statement2]...; [else statementN]; end if; | if val is null then select ‘val is null’; else select ‘val is not null’; end if; |
case expr when value1 then statement1 [when value2 then statement2]... [else statementN] end case; | case val when 1 then select’val is 1’; when 2 then select’val is 2’; else select ‘val is not 1 or 2’; end case; |
case when expr then statement [when expr2 then statement2]... [else statementN] end case; | case when val is null then select’val is null’; when val0 then select’val is greater than 0’; else select’val is 0’; end case; |
[loopLabel:]loop statement end loop [loopLabel] | declare id int default 0; addLoop:loop set id=id+1; if id>=10 then leave addLoop; end if; end loop addLoop; |
[repeatLabel:]repeat statement until expr end repeat[repeatLabel] | declare id int default 0; repeat set id=id+1; until id>=10 end repeat; |
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |