sqlite3中的数据类型
Most SQL database engines (every SQL database engine other than SQLite,as far as we know) uses static,rigid typing. With static typing,the datatype of a value is determined by its container - the particular column in which the value is stored. SQLite uses a more general dynamic type system. In SQLite,the datatype of a value is associated with the value itself,not with its container. The dynamic type system of SQLite is backwards compatible with the more common static type systems of other database engines in the sense that SQL statement that work on statically typed databases should work the same way in SQLite. However,the dynamic typing in SQLite allows it to do things which are not possible in traditional rigidly typed databases. 1.0 Storage Classes and DatatypesEach value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:
Note that a storage class is slightly more general than a datatype. The INTEGER storage class,for example,includes 6 different integer datatypes of different lengths. This makes a difference on disk. But as soon as INTEGER values are read off of disk and into memory for processing,they are converted to the most general datatype (8-byte signed integer). And so for the most part,"storage class" is indistinguishable from "datatype" and the two terms can be used interchangeably. Any column in an SQLite version 3 database,except anINTEGER PRIMARY KEYcolumn,may be used to store a value of any storage class. All values in SQL statements,whether they are literals embedded in SQL statement text orparametersbound toprecompiled SQL statementshave an implicit storage class. Under circumstances described below,the database engine may convert values between numeric storage classes (INTEGER and REAL) and TEXT during query execution. 1.1 Boolean DatatypeSQLite does not have a separate Boolean storage class. Instead,Boolean values are stored as integers 0 (false) and 1 (true). 1.2 Date and Time DatatypeSQLite does not have a storage class set aside for storing dates and/or times. Instead,the built-inDate And Time Functionsof SQLite are capable of storing dates and times as TEXT,REAL,or INTEGER values:
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions. 2.0 Type AffinityIn order to maximize compatibility between SQLite and other database engines,SQLite supports the concept of "type affinity" on columns. The type affinity of a column is the recommended type for data stored in that column. The important idea here is that the type is recommended,not required. Any column can still store any type of data. It is just that some columns,given the choice,will prefer to use one storage class over another. The preferred storage class for a column is called its "affinity". Each column in an SQLite 3 database is assigned one of the following type affinities:
A column with TEXT affinity stores all data using storage classes NULL,TEXT or BLOB. If numerical data is inserted into a column with TEXT affinity it is converted into text form before being stored. A column with NUMERIC affinity may contain values using all five storage classes. When text data is inserted into a NUMERIC column,the storage class of the text is converted to INTEGER or REAL (in order of preference) if such conversion is lossless and reversible. For conversions between TEXT and REAL storage classes,SQLite considers the conversion to be lossless and reversible if the first 15 significant decimal digits of the number are preserved. If the lossless conversion of TEXT to INTEGER or REAL is not possible then the value is stored using the TEXT storage class. No attempt is made to convert NULL or BLOB values. A string might look like a floating-point literal with a decimal point and/or exponent notation but as long as the value can be expressed as an integer,the NUMERIC affinity will convert it into an integer. Hence,the string '3.0e+5' is stored in a column with NUMERIC affinity as the integer 300000,not as the floating point value 300000.0. A column that uses INTEGER affinity behaves the same as a column with NUMERIC affinity. The difference between INTEGER and NUMERIC affinity is only evident in aCAST expression. A column with REAL affinity behaves like a column with NUMERIC affinity except that it forces integer values into floating point representation. (As an internal optimization,small floating point values with no fractional component and stored in columns with REAL affinity are written to disk as integers in order to take up less space and are automatically converted back into floating point as the value is read out. This optimization is completely invisible at the SQL level and can only be detected by examining the raw bits of the database file.) A column with affinity NONE does not prefer one storage class over another and no attempt is made to coerce data from one storage class into another. 2.1 Determination Of Column AffinityThe affinity of a column is determined by the declared type of the column,according to the following rules in the order shown:
Note that the order of the rules for determining column affinity is important. A column whose declared type is "CHARINT" will match both rules 1 and 2 but the first rule takes precedence and so the column affinity will be INTEGER. 2.2 Affinity Name ExamplesThe following table shows how many common datatype names from more traditional SQL implementations are converted into affinities by the five rules of the previous section. This table shows only a small subset of the datatype names that SQLite will accept. Note that numeric arguments in parentheses that following the type name (ex: "VARCHAR(255)") are ignored by SQLite - SQLite does not impose any length restrictions (other than the large globalSQLITE_MAX_LENGTHlimit) on the length of strings,BLOBs or numeric values.
Note that a declared type of "FLOATING POINT" would give INTEGER affinity,not REAL affinity,due to the "INT" at the end of "POINT". And the declared type of "STRING" has an affinity of NUMERIC,not TEXT. 2.3 Column Affinity Behavior ExampleThe following SQL demonstrates how SQLite uses column affinity to do type conversions when values are inserted into a table.
3.0 Comparison ExpressionsSQLite version 3 has the usual set of SQL comparison operators including "=","==","<","<=",">",">=","!=","<>","IN","NOT IN","BETWEEN","IS",and "IS NOT",. 3.1 Sort OrderThe results of a comparison depend on the storage classes of the operands,according to the following rules:
3.2 Affinity Of Comparison OperandsSQLite may attempt to convert values between the storage classes INTEGER,and/or TEXT before performing a comparison. Whether or not any conversions are attempted before the comparison takes place depends on the affinity of the operands. Operand affinity is determined by the following rules:
3.3 Type Conversions Prior To ComparisonTo "apply affinity" means to convert an operand to a particular storage class if and only if the conversion is lossless and reversible. Affinity is applied to operands of a comparison operator prior to the comparison according to the following rules in the order shown:
The expression "a BETWEEN b AND c" is treated as two separate binary comparisons "a >= b AND a <= c",even if that means different affinities are applied to 'a' in each of the comparisons. Datatype conversions in comparisons of the form "x IN (SELECT y ...)" are handled is if the comparison were really "x=y". The expression "a IN (x,y,z,...)" is equivalent to "a = +x OR a = +y OR a = +z OR ...". In other words,the values to the right of the IN operator (the "x","y",and "z" values in this example) are considered to have no affinity,even if they happen to be column values or CAST expressions. 3.4 Comparison Example
All of the result in the example are the same if the comparisons are commuted - if expressions of the form "a<40" are rewritten as "40>a". 4.0 OperatorsAll mathematical operators (+,-,*,/,%,<<,>>,&,and |) cast both operands to the NUMERIC storage class prior to being carried out. The cast is carried through even if it is lossy and irreversible. A NULL operand on a mathematical operator yields a NULL result. An operand on a mathematical operator that does not look in any way numeric and is not NULL is converted to 0 or 0.0. 5.0 Sorting,Grouping and Compound SELECTsWhen query results are sorted by an ORDER BY clause,values with storage class NULL come first,followed by INTEGER and REAL values interspersed in numeric order,followed by TEXT values in collating sequence order,and finally BLOB values in memcmp() order. No storage class conversions occur before the sort. When grouping values with the GROUP BY clause values with different storage classes are considered distinct,except for INTEGER and REAL values which are considered equal if they are numerically equal. No affinities are applied to any values as the result of a GROUP by clause. The compound SELECT operators UNION,INTERSECT and EXCEPT perform implicit comparisons between values. No affinity is applied to comparison operands for the implicit comparisons associated with UNION,INTERSECT,or EXCEPT - the values are compared as is. 6.0 Collating SequencesWhen SQLite compares two strings,it uses a collating sequence or collating function (two words for the same thing) to determine which string is greater or if the two strings are equal. SQLite has three built-in collating functions: BINARY,NOCASE,and RTRIM.
An application can register additional collating functions using thesqlite3_create_collation()interface. 6.1 Assigning Collating Sequences from SQLEvery column of every table has an associated collating function. If no collating function is explicitly defined,then the collating function defaults to BINARY. The COLLATE clause of thecolumn definitionis used to define alternative collating functions for a column. The rules for determining which collating function to use for a binary comparison operator (=,<,>,<=,>=,!=,IS,and IS NOT) are as follows and in the order shown:
An operand of a comparison is considered to have an explicit collating function assignment (rule 1 above) if any subexpression of the operand uses the postfixCOLLATE operator. Thus,if aCOLLATE operatoris used anywhere in a comparision expression,the collating function defined by that operator is used for string comparison regardless of what table columns might be a part of that expression. If two or moreCOLLATE operatorsubexpressions appear anywhere in a comparison,the left most explicit collating function is used regardless of how deeply the COLLATE operators are nested in the expression and regardless of how the expression is parenthesized. The expression "x BETWEEN y and z" is logically equivalent to two comparisons "x >= y AND x <= z" and works with respect to collating functions as if it were two separate comparisons. The expression "x IN (SELECT y ...)" is handled in the same way as the expression "x = y" for the purposes of determining the collating sequence. The collating sequence used for expressions of the form "x IN (y,...)" is the collating sequence of x. Terms of the ORDER BY clause that is part of aSELECTstatement may be assigned a collating sequence using the 6.2 Collation Sequence ExamplesThe examples below identify the collating sequences that would be used to determine the results of text comparisons that may be performed by various SQL statements. Note that a text comparison may not be required,and no collating sequence used,in the case of numeric,blob or NULL values.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |