Uploaded by Lewis Hamilton

English

advertisement
I have done this many times. I will tell you all about how to become better at English.
I THINK COLUMN AFFINITY
= IS DIFF FROM TYPE
AFFINITY!!!!!!!
SQLite datatypes
 Most SQL DB engines (Every SQL DB engine other than SQLite) = Use static, rigid typing
o W/static typing = Datatype of a value = determined by its container (i.e. Datatype set for
the column u are storing the value in)
 SQLite = Dynamic type sys
o Makes it backwards compatible w/most static type sys (Other DB engine)
 In the sense that SQL statements that works on statically typed DB = Work the
same way in SQLite
Storage classes/datatypes
 Each value stored in SQLite DB = Has one of these storage class:
o
Storage class = More general > datatype
 E.g: INTEGER storage class = Inc 7 diff integer datatypes (of diff lengths)
 This makes a diff on disk (Additional reading if u want)
 But as soon as INTEGER values are read off of disk & into [mem] for processing --> Converted to most general datatype (8-byte signed integer)
 i.e. 'Storage class' = indistinguishable from 'datatype'
& they can be used interchangeably
o
Any SQLite3 column (except INTEGER PRIMARY KEY) = Can be used to store value of any
storage class
Boolean datatype
 SQLite = Doesn't have separate Boolean storage class
o Boolean stored as integers 0 (false) & 1 (true)
Date & time datatype


o
SQLite = Also doesn't have date/time storage class
Instead = Built-in Date And Time Functions = Can store date/time as TEXT, REAL, or INTEGER
value
U can choose to store date/time in any formats + freely convert b/w formats using built-in Date
And Time Functions
Flexible typing

(From Flexible Typing) i.e. If u put SMALLINT as ur datatype, altho it isn't a built-in datatype
w/in SQLite, SQLite will be able to deduce that u want the built-in INTEGER datatype from
SQLite & automatically store all the values w/in that column as INTEGER

Note: How "Example" is highlighted
o I guess SQLite can deduce more non-built-in typenames apart from the e.g given
Type affinity
 SQL DB engines that use rigid typing = Usually try to automatically convert values --to-->
appropriate datatype
o E.g:

Rigid-typed DB will convert string '123' ---into---> integer 123 & integer 456 ---into---> string
'456' (B4 doing the INSERT)
o
To ensure above e.g works on SQLite as it does on other SQL DB engine (i.e. Maximise
compatibility):
 SQLite supports the concept of "type affinity" on columns

Q: What is type affinity?
o Recommended type for data stored in that column
 NOT REQUIRED, BUT RECOMMENDED
 i.e. Any column can still store any data type
 Column def = can consist of just column name (& nothing else)
 It's just that some columns (given the choice) = Prefer to use 1 storage class >
another
 Preferred storage class for a column = That column's affinity
o
(From Flexible Typing) When datatype name provided = they can be just abt any text
 SQLite attempts to deduce preferred datatype for the column (Based on datatype
name in column def)

But the preferred datatype = advisory, not mandatory
 That preferred datatype = "Column Affinity"
(Extract from Flexible Typing)

Above can lead to annoying situations like below for rigid typing lover:
(Extract from Flexible Typing)
o
i.e. Diff datatypes can live inside a column (Which obv will have a column datatype set up)
o
Note: INTEGER or REAL value = NEVER stored in TEXT column
 Since they can always be converted into its equivalent TEXT representation
o
Note 2: INTEGER = NEVER stored in REAL column
 Since it can always be converted into REAL
o
Note 3: TEXT doesn't always look like INTEGER or REAL
 Hence, TEXT can't always be converted into INTEGER or REAL
 Only converted & stored when TEXT = well-formed integer or real literal
 But if not = Stored as TEXT
o
Note 4: Blob can't be converted into anything + nothing else can be converted into a BLOB
Each column (SQLite3) can be assigned these type affinities:
 TEXT
 NUMERIC
 INTEGER
 REAL
 BLOB

Note: A column w/BLOB affinity = doesn't prefer a storage class
o i.e. No attempt is made to convert b/w any of the storage classes

Note 2: When numerical data inserted into column w/TEXT affinity = It's converted into text
form b4 being stored

Note 3: A str may look like a floating pt w/a decimal pt and/or exponent notation
o But as long as value = can be expressed as integer
o A column w/numeric affinity will convert it into INTEGER
o
E.g: Str '3.0e+5' = is stored in a column w/NUMERIC affinity as INTEGER 300000, not as
floating pt 300000.0

Note 4: A column using INTEGER affinity = behaves the same as a column w/NUMERIC affinity
o Difference is: only evident in a CAST expression
 CAST(4.0 AS INT) = Returns integer 4
 CAST(4.0 AS NUMERIC) = Returns floating pt 4.0

Note 5: A column w/REAL affinity = behave like a column w/NUMERIC affinity
o Except: it forces integer value = into floating pt
Determination of column affinity (Column affinity = DIFF from Type affinity!)
 (For table not declared as STRICT)
The column affinity = determined by declared type of column by rules below:
Rule
1. If declared type contains the str 'INT' = Column assigned INTEGER affinity
2. "
"
"
"
" " 'CHAR', 'CLOB', 'TEXT' = Column assigned TEXT affinity (E.g:
VARCHAR = contains the str 'CHAR' = i.e. Assigned TEXT affinity)
3. "
"
"
"
"
" 'BLOB' or no type specified = Column assigned BLOB affinity
4. "
"
"
"
"
" 'REAL', FLOA', 'DOUB' = Column assigned REAL affinity
5. Otherwise = Column assigned NUMERIC affinity
o i.e. Catch-all if no other rules apply
o Will 1st try to convert to INTEGER, then REAL, then just store the data as TEXT
Note: If a datatype fits into 1+ affinity = Rule earlier in the list applied
 E.g:
CHARINT = Match both rule 1 & 2, but first rule = take precedence, so column affinity =
INTEGER
INT column = will have INTEGER affinity, not NUMERIC or REAL
(From Type Affinity article on Medium)
(From Type Affinity article on Medium)
Download