mysql存储过程


存储过程:

存储过程简单来说是为以后的使用而保存的一条或多条MySQL语句的集合。


1.查看已有的mysql存储过程

show procedure status;


2.在命令行中创建存储过程


2.1首先,命令行中的分隔符是分号";"也就是说mysql在碰到分号";"时便认为命令输入结束,便回去执行,但是存储过程中是要使用";"的,不能让本来写在存储过程中的分号让命令行理解为输入结束,于是需要修改命令行的分隔符,方法如下:

DELIMITER  //

如此便mysql碰到分号";"便不再认为命令结束,而是看是否碰到了"//"


2.2创建存储过程

delimiter //

CREATE PROCEDURE myAutoInsert()
begin
  declare i int default 20;
  while i<25 do
  insert into test(auto_id,note) values(i,'autoInsert');
  set i=i+1;
  end while;
end//


2.3解释

该存储过程循环插入5条记录到表中


3.查看创建的存储过程

show creaete procedure myAutoInsert;        --存储过程名后面没有括号


4.删除存储过程

drop procedure myAutoInsert;



附录:

随机数产生方法(存储过程难免想到要用随机数)

函数:rand()

用法1:select rand();        --返回浮点类型0-1的浮点随机数

用法2:select rand(N);       --N为种子,当N确定不变时,每次生成的随机数也不变


生成二位的随机数:

select round(round(rand(),2)*100)       --round(X,D)保留X的D位小数


生成1-5的随机数

select ceil(rand()*5)                  --ceil()返回不小于参数的最小整数值,如ceil(3.6)的结果是4


另一个可用于生成随机数的函数uuid():

select uuid()                          --含有abcde的字符,可以select replace(uuid(),'-','')去掉横线

select uuid_short()                    --返回unsigned long long 类型                          

注:每次执行都是不同的,生成规则 第1 2 3 段是与时间有关,time_low、time_mid、time_high_and_version转成16进制后分别对应第1 2 3段。这个时间是从1582-10-15 00:00:00.00到当前时间的100ns值。(实际上系统只能取到精确us,再乘以10)。所以你短时间连续执行的话,比较可能只有第一个值在改,实际上1 2 3都可能会改变。   第4段是你启动这个MySQL后第一次执行select uuid()时的随机数,每次重启会改变。   第5段是mac值转过来的,同一个机器多实例的一般相同。如果mac值获取不到,则是一个随机值。   所以这个值可以认为是每次执行都不相同。并且不同实例之间也只有极微小概率重复。




随附mysql表格一个,方便不到官网也能看得到:


Table 12.12 Mathematical Functions

Name Description
ABS() Return the absolute value
ACOS() Return the arc cosine
ASIN() Return the arc sine
ATAN2()ATAN() Return the arc tangent of the two arguments
ATAN() Return the arc tangent
CEIL() Return the smallest integer value not less than the argument
CEILING() Return the smallest integer value not less than the argument
CONV() Convert numbers between different number bases
COS() Return the cosine
COT() Return the cotangent
CRC32() Compute a cyclic redundancy check value
DEGREES() Convert radians to degrees
EXP() Raise to the power of
FLOOR() Return the largest integer value not greater than the argument
LN() Return the natural logarithm of the argument
LOG10() Return the base-10 logarithm of the argument
LOG2() Return the base-2 logarithm of the argument
LOG() Return the natural logarithm of the first argument
MOD() Return the remainder
PI() Return the value of pi
POW() Return the argument raised to the specified power
POWER() Return the argument raised to the specified power
RADIANS() Return argument converted to radians
RAND() Return a random floating-point value
ROUND() Round the argument
SIGN() Return the sign of the argument
SIN() Return the sine of the argument
SQRT() Return the square root of the argument
TAN() Return the tangent of the argument
TRUNCATE() Truncate to specified number of decimal places

All mathematical functions return NULL in the event of an error



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值