Tipo de Datos Sqlite
Tipo de Datos Sqlite
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 statements 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.
REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or
UTF-16LE).
TEXT
NUMERIC
INTEGER
REAL
BLOB
(Historical note: The "BLOB" type affinity used to be called "NONE". But that term was easy to
confuse with "no affinity" and so it was renamed.)
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 a CAST 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 BLOB does not prefer one storage class over another and no attempt is made
to coerce data from one storage class into another.
A value with storage class NULL is considered less than any other value (including another
value with storage class NULL).
An INTEGER or REAL value is less than any TEXT or BLOB value. When an INTEGER
or REAL is compared to another INTEGER or REAL, a numerical comparison is performed.
A TEXT value is less than a BLOB value. When two TEXT values are compared an
appropriate collating sequence is used to determine the result.
When two BLOB values are compared, the result is determined using memcmp().
The right-hand operand of an IN or NOT IN operator has no affinity if the operand is a list
and has the same affinity as the affinity of the result set expression if the operand is a
SELECT.
An expression that is a simple reference to a column value has the same affinity as the
column. Note that if X and Y.Z are column names, then +X and +Y.Z are considered
expressions for the purpose of determining affinity.
An expression of the form "CAST(expr AS type)" has an affinity that is the same as a
column with a declared type of "type".
If one operand has INTEGER, REAL or NUMERIC affinity and the other operand has
TEXT or BLOB or no affinity then NUMERIC affinity is applied to other operand.
If one operand has TEXT affinity and the other has no affinity, then TEXT affinity is applied
to the other operand.
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 Operators
All 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 SELECTs
When 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.