您的位置:首页 > 数据库

【Sqlit数据库官方API学习—part01】9个基本函数

2014-08-12 10:20 323 查看
官方API参考1:http://www.sqlite.org/lang_aggfunc.html

汉化API参考2:http://www.sqlite.com.cn/POPlist.asp?classid=5

这里仅仅写出了我认为重要的部分,需要的可以详细查看官方API。

1、Aggregate Functions(9种函数类型)

avg(X)

count(X)

count(*)

group_concat(X)

group_concat(X,Y)

max(X)

min(X)

sum(X)

无的时候返回null

total(X)

无的时候返回0.0,浮点型

1)sum和total的区别

The sum() and total() aggregate functions return sum of all non-NULL values in the group. If there are no non-NULL input rows then sum() returns NULL but total() returns 0.0. NULL is not
normally a helpful result for the sum of no rows but the SQL standard requires it and most other SQL database engines implement sum() that way so SQLite does it in the same way in order to be compatible. The non-standard total() function is provided as a convenient
way to work around this design problem in the SQL language.
The result of total() is always a floating point value. The result of sum() is an integer value if all non-NULL inputs are integers. If any input to sum() is neither an integer or a NULL then sum() returns
a floating point value which might be an approximation to the true sum.
Sum() will throw an "integer overflow" exception if all inputs are integers or NULL and an integer overflow occurs at any point during the computation. Total() never throws an integer overflow.
2)group_concat()

The group_concat() function returns a string which is the concatenation of all non-NULL values of X. If parameter Y is present then it is used as the separatorbetween
instances of X. A comma (",") is used as the separator if Y is omitted. The order of the concatenated elements is arbitrary.
将X中所有的non-NULL values都返回成一个字符串,默认的分割符(","),顺序是随机的。

3)一个例子

参考:/article/10108079.html

DepID StaffName

----------------------

101 AAAaa

101 BBBb

101 CCCccc

202 DDDddd

202 EEE

202 FFF

303 GGGGG

上表说明,DepID为部门ID,StaffName为员工姓名,一个部门当然会有0..N多个员工

用SQL语句将上面的表变成下面组合起来的方式。

DepID StaffNames

-------------------

101 AAAaa;BBBb;CCCccc

202 DDDddd;EEE;FFF

303 GGGGG

首先想到的是合并记录,百度后,发现这个需求叫聚合,后查询sqlite聚合函数,才找到group_concat()。group_concat()函数要和group by语句同时使用才能产生效果。

select DepID,group_concat(StaffNames) from tablename group by DepID

******************************************************************************************************

2、ALTER TABLE(2种修改表的功能)



1)RENAME

If the table being renamed has triggers or indices, then these remain attached to the table after it has been renamed. However, if there are any view definitions, or statements executed by triggers that refer to the table being renamed, these are not
automatically modified to use the new table name. If this is required, the triggers or view definitions must be dropped and recreated to use the new table name by hand.

The 'ALTER TABLE ... RENAME TO ...' command does not update action statements within triggers or SELECT statements within views. If the table being renamed is referenced from within
triggers or views, then those triggers and views must be dropped and recreated separately by the application.

如果被重命名的表有trigger或索引,这些trigger或索引仍然有效。然而,如果有任何的view的定义,或者被trigger(指向重命名的表)执行的的声明,这些不会自动修正到使用新的表明。这些trigger和view要dropped并分别重建。

If foreign key constraints are enabled when a table is renamed, then
any REFERENCES clauses in any table (either the table being renamed or some other table) that refer to the table being renamed are modified to refer
to the renamed table by its new name.

2)ADD COLUMN
The new column may take any of the forms permissible in a CREATE TABLE statement, with the following restrictions:

The column may not have a PRIMARY KEY or UNIQUE constraint./不能有PRIMARY KEY or UNIQUE声明
The column may not have a default value of CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP, or an expression in parentheses./不能有CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP的默认值或者带括号的表达式
If a NOT NULL constraint is specified, then the column must have a default value other than NULL./假如有NOT NULL constraint,默认值不能是NULL
If foreign key constraints are enabled and
a column with a REFERENCES clause is added, the column must have a default value of NULL./???

After ADD COLUMN has been run on a database, that database will not be readable by SQLite version 3.1.3 and earlier.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: