The
affinity
of a column is the recommended type
for data stored in that column. When a value is stored in a column
(through an INSERT or UPDATE statement), the runtime attempts to
convert that value from its data type to the specified affinity.
For example, if a Date value (an ActionScript or JavaScript Date
instance) is inserted into a column whose affinity is TEXT, the
Date value is converted to the String representation (equivalent
to calling the object's toString() method) before being stored in
the database. If the value cannot be converted to the specified
affinity an error occurs and the operation is not performed. When
a value is retrieved from the database using a SELECT statement,
it is returned as an instance of the class corresponding to the
affinity, regardless of whether it was converted from a different
data type when it was stored.
If a column accepts NULL values, the ActionScript or JavaScript
value null can be used as a parameter value to store NULL in the
column. When a NULL storage class value is retrieved in a SELECT
statement, it is always returned as the ActionScript or JavaScript
value null, regardless of the column's affinity. If a column accepts
NULL values, always check values retrieved from that column to determine
if they're null before attempting to cast the values to a non-nullable type
(such as Number or Boolean).
Each column in the database is assigned one of the following
type affinities:
-
TEXT (or String)
-
NUMERIC
-
INTEGER (or int)
-
REAL (or Number)
-
Boolean
-
Date
-
XML
-
XMLLIST
-
Object
-
NONE
TEXT (or String)
A column with TEXT or String 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 to text form before
being stored.
NUMERIC
A column with NUMERIC affinity contains values using storage
classes NULL, REAL, or INTEGER. When text data is inserted into
a NUMERIC column, an attempt is made to convert it to an integer
or real number before it is stored. If the conversion is successful,
then the value is stored using the INTEGER or REAL storage class
(for example, a value of '10.05' is converted to REAL storage class before
being stored). If the conversion cannot be performed an error occurs.
No attempt is made to convert a NULL value. A value that's retrieved
from a NUMERIC column is returned as an instance of the most specific
numeric type into which the value fits. In other words, if the value
is a positive integer or 0, it's returned as a uint instance. If
it’s a negative integer, it’s returned as an int instance. Finally,
if it has a floating-point component (it's not an integer) it's returned
as a Number instance.
INTEGER (or int)
A column that uses INTEGER affinity behaves in the same way as
a column with NUMERIC affinity, with one exception. If the value
to be stored is a real value (such as a Number instance) with no
floating point component or if the value is a text value that can
be converted to a real value with no floating point component, it is
converted to an integer and stored using the INTEGER storage class.
If an attempt is made to store a real value with a floating point
component an error occurs.
REAL (or Number)
A column with REAL or NUMBER affinity behaves like a column with
NUMERIC affinity except that it forces integer values into floating
point representation. A value in a REAL column is always returned
from the database as a Number instance.
Boolean
A column with Boolean affinity stores true or false values. A
Boolean column accepts a value that is an ActionScript or JavaScript
Boolean instance. If code attempts to store a String value, a String
with a length greater than zero is considered true, and an empty
String is false. If code attempts to store numeric data, any non-zero
value is stored as true and 0 is stored as false. When a Boolean value
is retrieved using a SELECT statement, it is returned as a Boolean
instance. Non-NULL values are stored using the INTEGER storage class
(0 for false and 1 for true) and are converted to Boolean objects
when data is retrieved.
Date
A column with Date affinity stores date and time values. A Date
column is designed to accept values that are ActionScript or JavaScript
Date instances. If an attempt is made to store a String value in
a Date column, the runtime attempts to convert it to a Julian date.
If the conversion fails an error occurs. If code attempts to store
a Number, int, or uint value, no attempt is made to validate the data
and it is assumed to be a valid Julian date value. A Date value
that's retrieved using a SELECT statement is automatically converted
to a Date instance. Date values are stored as Julian date values
using the REAL storage class, so sorting and comparing operations
work as you would expect them to.
XML or XMLList
A column that uses XML or XMLList affinity stores XML structures.
When code attempts to store data in an XML column using a SQLStatement
parameter the runtime attempts to convert and validate the value
using the ActionScript XML() or XMLList() function. If the value
cannot be converted to valid XML an error occurs. If the attempt
to store the data uses a literal SQL text value (for example INSERT
INTO (col1) VALUES ('Invalid XML (no closing tag)'), the value is
not parsed or validated — it is assumed to be well-formed. If an
invalid value is stored, when it is retrieved it is returned as
an empty XML object. XML and XMLList Data is stored using the TEXT
storage class or the NULL storage class.
Object
A column with Object affinity stores ActionScript or JavaScript
complex objects, including Object class instances as well as instances
of Object subclasses such as Array instances and even custom class
instances. Object column data is serialized in AMF3 format and stored
using the BLOB storage class. When a value is retrieved, it is deserialized
from AMF3 and returned as an instance of the class as it was stored.
Note that some ActionScript classes, notably display objects, cannot
be deserialized as instances of their original data type. Before
storing a custom class instance, you must register an alias for
the class using the flash.net.registerClassAlias() method (or in
Flex by adding [RemoteObject] metadata to the class declaration).
Also, before retrieving that data you must register the same alias
for the class. Any data that can't be deserialized properly, either
because the class inherently can't be deserialized or because of
a missing or mismatched class alias, is returned as an anonymous
object (an Object class instance) with properties and values corresponding
to the original instance as stored.
NONE
A column with affinity NONE does not prefer one storage class
over another. It makes no attempt to convert data before it is inserted.
Determining affinity
The type affinity
of a column is determined by the declared type of the column in
the CREATE TABLE statement. When determining the type the following
rules (not case-sensitive) are applied:
-
If the data
type of the column contains any of the strings "CHAR", "CLOB", "STRI",
or "TEXT" then that column has TEXT/String affinity. Notice that
the type VARCHAR contains the string "CHAR" and is thus assigned
TEXT affinity.
-
If the data type for the column contains the string "BLOB"
or if no data type is specified then the column has affinity NONE.
-
If the data type for column contains the string "XMLL" then
the column has XMLList affinity.
-
If the data type is the string "XML" then the column has
XML affinity.
-
If the data type contains the string "OBJE" then the column
has Object affinity.
-
If the data type contains the string "BOOL" then the column
has Boolean affinity.
-
If the data type contains the string "DATE" then the column
has Date affinity.
-
If the data type contains the string "INT" (including "UINT")
then it is assigned INTEGER/int affinity.
-
If the data type for a column contains any of the strings
"REAL", "NUMB", "FLOA", or "DOUB" then the column has REAL/Number
affinity.
-
Otherwise, the affinity is NUMERIC.
-
If a table is created using a CREATE TABLE t AS SELECT...
statement then all columns have no data type specified and they
are given the affinity NONE.