MySQL中
function用户自定义函数
c,fun,fun是面向过程的实现方式
只能传入参数,或不传入参数,不能传出参数,必有返回值
函数中是不能有create table drop table之类的语句
与sp一样,db中object
begin end 相当于{}
(1)输入任意姓名,输出 hello+姓名+!
drop function if exists fun1;
delimiter //
create function fun1(a varchar(32))
returns varchar(32)
begin
    declare x varchar(32);
    set x=concat('hello',a,'!');
    return x;
end //
delimiter ;
select fun1('张三');
                        -- (1)等同于(2)
(2)输入任意姓名,输出 hello+姓名+!
drop function if exists fun1;
create function fun1(a varchar(32))
returns varchar(32)
return concat('hello',a,'!');      -- 因为只有一条语句,所以省略了 begin end
select fun1('张三');

(3)求和
drop function if exists fun1;
delimiter //
create function fun1(a int,b int)
returns int
begin
   declare c int;
 -- set c=a+b;
   select a+b into c;
   return c;
  -- return a+b;
end //
delimiter ;
select fun1(10,30);

(4)select fun1('zhangfei');->stu(stuName) 实现输入姓名,插入到stu表的stuName字段,并返回stu表的记录条数
drop function if exists fun1;
delimiter //
create function fun1(a varchar(32))
returns int
begin
   declare x int;
   insert into stu(stuName) select a;
   select count(1) into x from stu;
   return x;
end //
delimiter ;
select fun1('zhangfei');

(5)取输入字符串的前b个字符,并在其后加...输出,若输入字符串为空,则提示‘输入错误’,若输入字符串长度小于b,则直接输出字符串,若大于b,则截取前b个字符,并在其后加...输出
drop function if exists fun1;
delimiter //
create function fun1(a varchar(32),b int)
returns varchar(32)
begin
   if isnull(a) || char_length(a)=0 then
   return '输入错误';
   elseif  char_length(a)<=b then
   return a;
    else
    return concat(left(a,b),'...');
   end if;
end //
delimiter ;
select fun1('abcdef',3);

(6)输出a个*
drop function if exists fun1;
delimiter //
create function fun1(a int)
returns varchar(32)
begin
   declare b int default 1; -- 1...5
   declare s varchar(32) default '';
   while b<=a
   do
   set s=concat(s,'*');
   set b=b+1;
   end while;
   return s;
end //
delimiter ;
select fun1(5);

-- cursor游标(光标)
很少用,基本不用
对以后用php从一个表内将数据一条条取出来放网页上去显示
-- 什么是cursor
4个步骤
-- cursor,数据缓冲区
1.声明coursor
declare cur cursor for select stuID,stuName from stu oder by stuID;
2.打开coursor
open cur;
cursor有一个指针,默认就指在第一条
3.fetch
declare a int;
declare b varchar(32);
declare c varchar(32);
fetch cur into a,b,c;
select a,b,c;

fectch完数据,指针自动下移一行
while

close cur;

cursor 放在sp中用
drop procedure if exists sp1;
delimiter //
create procedure sp1()
begin
   declare a int;
   declare b varchar(32);
   declare cur cursor for select stuID,stuName from aa order by stuID;
   open cur;
 -- fetch cur into a,b;
 -- fetch cur into a,b;         (有三条数据)
 -- fetch cur into a,b;
  -- select a,b;
  -- close cur;
    set @a=1;
    select count(1) into @b from stu;
    while @a<=@b
    do
    fetch cur into a,b;
    select a,b;
    set @a=@a+1;
     end while;
   close cur;
end //   
delimiter ;
call sp1();

-- event事件
用的也较少 类似linux中的crontab (每天下午执行某事件) 一般用于定期执行某任务
drop event if exists ev1;
create event ev1
on schedule every 1 minute        -- 每隔1分钟执行一次该事件
do
  insert into aa select 5,'孙坚';

alter event ev1 disable; 使其停止执行
on schedule at now()     立即执行,就执行一次,事件不会保存下来

drop event if exists ev1;
delimiter //
create event ev1
on schedule at now() + interval 3 second -- 3秒后执行一次该事件,执行完后该事件消失
do
begin
  insert into aa select 5,'孙坚';
  insert inwo aa select 6,'e';
end//
delimiter ;

alter event ev1 disable;
alter event ev1 enable;

-- view 视图
-- 是一张虚拟表,
select * from v1;
-- data
view->table
-- 方便
-- 安全
stu(stuID,stuName,stuAge,stuAddr)
stuID,stuName->v1 -- 看需求

stu(stuID,stuName)
courses(coursesID,coursesName)
sc(stuID,coursesID,score)

create view v1
as
  select stuName,coursesName,score
  from stu,courses,sc
  where sc.stuID=stu.stuID && sc.coursesID=courses.coursesID

select * from v1;
-- 比较复杂的查询做成view
-- 视图只用于看,显示,select
-- 当要进行数据修改插入什么的时候,直接修改基表

create table stu
(
    stuID int,
    stuName varchar(32),
    stuAge int,
    stuAddr varchar(32),
    primary key(stuID)
)engine=innodb charset=utf8;
-- stuName,stuAge
drop view if exists v1;
create view v1
as
select stuName,stuAge from stu;

-- book(),author(),press
create table author
(
    authorID int,
    authorName varchar(32)
)engine=innodb charset=utf8;
create table press
(
   pressID int,
   pressName varchar(32)
)engine=innodb charset=utf8;
create table book
(
   bookID int,
   bookName varchar(32),
   authorID int,
   pressID int
) engine=innodb charset=utf8;

drop view if exists v1;
create view v1
as
select bookName,authorName,pressName
from book,author,press
where book.authorID=author.authorID && book.pressID=press.pressID;
select * from v1;

-- 根据需求,view只用来查数据
-- 要insert,update,delete就直接操作基表,而不是操作view

MySQL中函数、游标、事件、视图基本应用举例(代码)的更多相关文章

  1. MySQL中函数、游标、事件、视图

    MySQL中函数.游标.事件.视图基本应用举例(代码) MySQL中function用户自定义函数c,fun,fun是面向过程的实现方式只能传入参数,或不传入参数,不能传出参数,必有返回值函数中是不能 ...

  2. [转]MySQL中函数CONCAT及GROUP_CONCAT

    一.CONCAT()函数 CONCAT()函数用于将多个字符串连接成一个字符串. 使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为 +-- ...

  3. Mysql中函数和存储过程的区别

    Mysql中函数和存储过程的区别 存储过程: 1.       可以写sql语句 2.       inout,out构造返回值 3.       调用:call:存储过程名称 4.       可以 ...

  4. MySQL中函数CONCAT及GROUP_CONCAT

    一.CONCAT()函数CONCAT()函数用于将多个字符串连接成一个字符串.使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为+----+ ...

  5. hive 中与mysql 中函数同名不同意的方法记录

    max 函数 在hive中max函数是一个聚合函数,所以,而且返回值是double ,而且后面必须跟group by ,这个和mysql差异很大 Built-in Aggregate Function ...

  6. MySQL中函数CONCAT及GROUP_CONCAT函数的使用

    一.CONCAT()函数 CONCAT()函数用于将多个字符串连接成一个字符串. 以数据表[user]作为实例: SELECT USER_NAME, SEX FROM USER WHERE USER ...

  7. Oracle中和mysql中函数的区别

    oracle                  -->                 mysqlto_char(sysdate,'yyyy-mm-dd')-->date_format(s ...

  8. js便签笔记(6)——jQuery中的ready()事件为何需要那么多代码?

    前言: ready()事件的应用,是大家再熟悉不过的了,学jQuery的第一步,最最常见的代码: jQuery(document).ready(function () { }); jQuery(fun ...

  9. mysql中的游标使用

    1.游标的作用及属性 游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操作:游标有下面这些属性: a.游标是只读的,也就是不能更新它: b.游标是不能滚动的,也就是只能在一个方向上进 ...

随机推荐

  1. C# - object有哪些基本方法类有

    Name Description Equals(Object) Determines whether the specified object is equal to the current obje ...

  2. ios MKNetworkKit 的使用

    常用框架比如:AFNetworking,ASIHttpRequest,SDWebImage,MKNetWorKit等. iOS5已出来这么久了,而ASIHttpRequest的作者已经申明不更新了,在 ...

  3. 登录记住账号和密码小Demo

    读取 // 1.读取沙盒中plist文件 // 1.1.获得沙盒根路径 NSString *home = NSHomeDirectory(); // 1.2.拼接Documents路径 NSStrin ...

  4. CentOS-6.3安装配置SVN

    安装说明 系统环境:CentOS-6.3 安装方式:yum install (源码安装容易产生版本兼容的问题) 安装软件:系统自动下载SVN软件 检查已安装版本 #检查是否安装了低版本的SVN [ro ...

  5. InstallShield集成安装MSDE2000最小版本(一) fishout特许授权发布

    原文:InstallShield集成安装MSDE2000最小版本(一) fishout特许授权发布 原帖地址:http://blog.csdn.net/fishout/archive/2009/10/ ...

  6. SQL点滴21—几个有点偏的语句

    原文:SQL点滴21-几个有点偏的语句 SQL语句是一种集合操作,就是批量操作,它的速度要比其他的语言快,所以在设计的时候很多的逻辑都会放在sql语句或者存储过程中来实现,这个是一种设计思想.但是今天 ...

  7. SQL点滴16—SQL分页语句总结

    原文:SQL点滴16-SQL分页语句总结 今天对分页语句做一个简单的总结,他们大同小异的,只要理解其中一个其他的就很好理解了. 使用top选项 *from Orders orderid from Or ...

  8. 使用Prism提供的类实现WPF MVVM点餐Demo

    使用Prism提供的类实现WPF MVVM点餐Demo 由于公司开发的技术需求,近期在学习MVVM模式开发WPF应用程序.进过一段时间的学习,感受到:学习MVVM模式,最好的方法就是用MVVM做几个D ...

  9. 使用rem设计移动端自适应页面一(转载)

    1.困扰多时的问题 在这之前做Web App开发的的时候,在自适应方面一般都是宽度通过百分比,高度以iPhone6跟iPhone5之间的一个平衡值写死,我们的设计稿都是iPhone5的640 * 11 ...

  10. C# 语言的多线程编程,完全是本科OS里的知识

    基本知识,无参数Thread和带参数的Thread Thread类的参数就是参数指针,可以传入一个无参的函数. 如果要传入带参数的函数,先new一个ParameterizedThreadStart委托 ...