函数的csdn博客教程

函数示例:

DELIMITER $$
create function periodtos(kini int)
returns varchar(30)
no sql
deterministic
begin
	declare mout varchar(30);
	case kini
		when 1 then
			set mout='上午';
		when 2 then
			set mout='下午';
		else
			set mout='未知';
	end case;
	return mout;
end$$
DELIMITER ;

函数说明1:

  • When you create a stored function, you must declare either that it is deterministic or that it does not modify data. Otherwise, it may be unsafe for data recovery or replication.
  • By default, for a CREATE FUNCTION statement to be accepted, at least one of DETERMINISTIC, NO SQL, or READS SQL DATA must be specified explicitly. Otherwise an error occurs

函数说明2:

DETERMINISTIC A routine is considered “deterministic” if it always produces the same result for the same input parameters and NOT DETERMINISTIC otherwise. This is mostly used with string or math processing, but not limited to that.

NOT DETERMINISTIC Opposite of “DETERMINISTIC”. “If neither DETERMINISTIC nor NOT DETERMINISTIC is given in the routine definition, the default is NOT DETERMINISTIC. To declare that a function is deterministic, you must specify DETERMINISTIC explicitly.“. So it seems that if no statement is made, MySQl will treat the function as “NOT DETERMINISTIC”. This statement from manual is in contradiction with other statement from another area of manual which tells that: ” When you create a stored function, you must declare either that it is deterministic or that it does not modify data. Otherwise, it may be unsafe for data recovery or replication. By default, for a CREATE FUNCTION statement to be accepted, at least one of DETERMINISTIC, NO SQL, or READS SQL DATA must be specified explicitly. Otherwise an error occurs

I personally got error in MySQL 5.5 if there is no declaration, so i always put at least one declaration of “DETERMINISTIC”, “NOT DETERMINISTIC”, “NO SQL” or “READS SQL DATA” regardless other declarations i may have.

READS SQL DATA This explicitly tells to MySQL that the function will ONLY read data from databases, thus, it does not contain instructions that modify data, but it contains SQL instructions that read data (e.q. SELECT).

MODIFIES SQL DATA This indicates that the routine contains statements that may write data (for example, it contain UPDATE, INSERT, DELETE or ALTER instructions).

NO SQL This indicates that the routine contains no SQL statements.

CONTAINS SQL This indicates that the routine contains SQL instructions, but does not contain statements that read or write data. This is the default if none of these characteristics is given explicitly. Examples of such statements are SELECT NOW(), SELECT 10+@b, SET @x = 1 or DO RELEASE_LOCK(‘abc’), which execute but neither read nor write data.

Note that there are MySQL functions that are not deterministic safe, such as: NOW(), UUID(), etc, which are likely to produce different results on different machines, so a user function that contains such instructions must be declared as NOT DETERMINISTIC. Also, a function that reads data from an unreplicated schema is clearly NONDETERMINISTIC. *


其他:删除函数: drop function f1;

删除过程: drop procedure p1;

1. 用with而不是用sub query.因为好写好读
with在 数据库系统概念 3.8.6节
2. group by之后的having,操作的是组,而不是组内的元素
3. 值选出一个东西的select语句 可以作 临时变量
4. sqlite3交互式界面开启查询记时:.timer on
5. 尽量用join替代in
6. 尽量写到一句查询里,因为不知道会命中什么优化