SQLite3的数据类型转载()
| z SQL 數(shù)據(jù)庫引擎 (據(jù)我們所知,除 SQLite 之外的所有 SQL 數(shù)據(jù)庫引擎)都使用嚴格的靜態(tài)類型。使用靜態(tài)類型,值的類型便由它的容器 -- 存儲值的特定的列 -- 來決定。 SQLite 使用更通用的動態(tài)類型系統(tǒng)。在 SQLit 中,值的數(shù)據(jù)類型與值本身相關(guān),而不是與它的容器。SQLite 的動態(tài)類型系統(tǒng)與其它數(shù)據(jù)庫引擎的常用靜態(tài)類型系統(tǒng)是向后兼容的,在這個意義上,工作在靜態(tài)類型數(shù)據(jù)庫上的 SQL 語句應(yīng)該以同樣的方式工作在 SQLite 中。然而,SQLite 中的動態(tài)類型允許它做傳統(tǒng)的嚴格類型的數(shù)據(jù)庫所不能做的事。 | |||||||||||||||||||||||||||||||
| ? 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 an?INTEGER PRIMARY KEY?column, 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 or?parameters?bound toprecompiled SQL statements?have 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.0 存儲類型與數(shù)據(jù)類型存儲在 SQLite 數(shù)據(jù)庫中的每個值(或是由數(shù)據(jù)庫引擎所操作的值)都有一個以下的存儲類型:
注意,存儲類型比數(shù)據(jù)類型更籠統(tǒng)。以 INTEGER 存儲類型為例,它包括6種不同的長度不等的整數(shù)類型,這在磁盤上是不同的。但是只要 INTEGER 值從磁盤讀取到內(nèi)存進行處理,它們就被轉(zhuǎn)換為更為一般的數(shù)據(jù)類型(8字節(jié)有符號整型)。因此在一般情況下,“存儲類型” 與 “數(shù)據(jù)類型” 沒什么差別,這兩個術(shù)語可以互換使用。 SQLite 版本3數(shù)據(jù)庫中的任何列,除了整型主鍵列,都可用于存儲任何存儲類型的值。 SQL 語句中的任何值,無論它們是嵌入到 SQL 語句中的字面量還是綁定到預(yù)編譯 SQL 語句中的參數(shù),都有一個隱含的存儲類型。在下述情況下,數(shù)據(jù)庫引擎會在執(zhí)行查詢時在數(shù)值存儲類型(INTEGER 和 REAL)和 TEXT 之間進行轉(zhuǎn)換。 | ||||||||||||||||||||||||||||||
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-in?Date And Time Functions?of 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. | 譯者信息 1.1 布爾類型SQLite 并沒有單獨的布爾存儲類型,而是將布爾值存儲為整數(shù) 0 (false) 和 1 (true)。 1.2 日期和時間類型SQLite 沒有另外的存儲類型來存儲日期和時間。SQLite 的內(nèi)置的日期和時間函數(shù)能夠?qū)⑷掌诤蜁r間存為 TEXT、REAL 或 INTEGER 值:
應(yīng)用可以選擇這些格式中的任一種存儲日期和時間,并使用內(nèi)置的日期和時間函數(shù)在這些格式間自由轉(zhuǎn)換。 | ||||||||||||||||||||||||||||||
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. | 譯者信息 2.0 類型親和性為了最大限度地提高 SQLite 和其它數(shù)據(jù)庫引擎之間的兼容性,SQLite 支持列的“類型親和性”的概念。列的類型親和性是指數(shù)據(jù)存儲于該列的推薦類型。這里重要的思想是類型是推薦的,而不是必須的。任何列仍可以存儲任何類型的數(shù)據(jù)。這只是讓一些列有選擇性地優(yōu)先使用某種存儲類型。一個列的首選存儲類型被稱為它的“親和性”。 每個 SQLite 3 數(shù)據(jù)庫中的列都歸于以下的類型親和性中的一種:
一個具有 TEXT 親和性的列使用存儲類型 NULL、 TEXT 或 BLOB 存儲所有數(shù)據(jù)。如果數(shù)值數(shù)據(jù)被插入到一個具有 TEXT 親和性的列,則數(shù)據(jù)在存儲前被轉(zhuǎn)換為文本形式。 | ||||||||||||||||||||||||||||||
| ? 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. | 譯者信息 數(shù)值親和性的列可能包含了使用所有五個存儲類的值。當插入文本數(shù)據(jù)到數(shù)值列時,該文本的存儲類型被轉(zhuǎn)換成整型或?qū)崝?shù)(按優(yōu)先級排序)如果這種轉(zhuǎn)換是無損或可逆的的話。對于文本與實數(shù)類型之間的轉(zhuǎn)換,如果前15個重要十進制數(shù)字被保留的話,SQLite認為這種轉(zhuǎn)換是無損并可逆的。如果文本不能無損地轉(zhuǎn)換成整型或?qū)崝?shù),那這個值將以文本類型存儲。不要試圖轉(zhuǎn)換NULL或BLOB值。 一個字符串可能看上去像帶有小數(shù)點和/或指數(shù)符的浮點文字,但只要這個值可以用一個整型表示,數(shù)值親和性就會把它轉(zhuǎn)換成一個整型。因此,字符串‘3.0e+5’以整型300000,而不是浮點值30000.0的形式存儲在一個數(shù)值親和性的列里。 | ||||||||||||||||||||||||||||||
| ? 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 NONE does not prefer one storage class over another and no attempt is made to coerce data from one storage class into another. | 譯者信息 一個使用整型親和性的列與具有數(shù)值親和性的列表現(xiàn)一致。只是在CAST表達式里,它們之間的區(qū)別體現(xiàn)得明顯。 除了強制將整型值轉(zhuǎn)換成浮點表示外,一個具有實數(shù)親和性的列與具有數(shù)值親和性的列表現(xiàn)一致(作為一個內(nèi)部的優(yōu)化,為了少占用空間,無小數(shù)部分且存儲在實數(shù)親和性列上的小浮點值以整型形式寫到磁盤,讀出時自動轉(zhuǎn)換回浮點值。在SQL級別,這種優(yōu)化是完全不可見的,并且只能通過檢查數(shù)據(jù)庫文件的原始比特檢測到)。 一個具有NONE親和性的列不能從一種存儲類型轉(zhuǎn)換成另一種,也不要試圖強制對它進行轉(zhuǎn)換。 | ||||||||||||||||||||||||||||||
| ? 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: If the declared type contains the string "INT" then it is assigned INTEGER affinity. If the declared type of the column contains any of the strings "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity. Notice that the type VARCHAR contains the string "CHAR" and is thus assigned TEXT affinity. If the declared type for a column contains the string "BLOB" or if no type is specified then the column has affinity NONE. If the declared type for a column contains any of the strings "REAL", "FLOA", or "DOUB" then the column has REAL affinity. Otherwise, the affinity is NUMERIC. 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.1 列親和性測定列的親和性是由它的聲明類型決定的,按照以下順序所示的規(guī)則: 1. 如果聲明類型包含字符串“INT”,那它被指定為整型親和性; 2. 如果列的聲明類型包含任何“CHAR”、“CLOB”或“TEXT”字符串,那么該列具有文本親和性。注意:VARCHAR類型包含“CHAR”并且被指定為文本親和性; 3. 如果列的聲明類型包含“BLOB”或者沒有指定類型,那這列具有NONE親和性; 4. 如果列的聲明類型包含任何“REAL”、“FLOA”或“DOUB”字符串,則該列具有實數(shù)親和性; 5. 否則,它將具有數(shù)值親和性。 注意:判定列親和性規(guī)則的順序是很重要的。一個具有“CHARINT”聲明類型的列將匹配規(guī)則1和2,但是規(guī)則1優(yōu)先所有該列具有整型親和性。 | ||||||||||||||||||||||||||||||
| ? 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 global?SQLITE_MAX_LENGTH?limit) 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.2 親和性名字實例下表顯示了有多少從更傳統(tǒng)的SQL實現(xiàn)的常用數(shù)據(jù)類型名,通過上一節(jié)介紹的五個規(guī)則被轉(zhuǎn)換成各種親和性類型。這張表只顯示了SQLite可接受的一小部分數(shù)據(jù)類型名。注意:跟在類型名后,括號內(nèi)數(shù)值參數(shù)(如:VARCHAR(255))將被SQLite忽略 - SQLite不對字符串、BLOBs或數(shù)值的長度強加任何限制(除了大型全局SQLITE_MAX_LENGTH限制)。
| ||||||||||||||||||||||||||||||
| ? 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. CREATE TABLE t1(t TEXT, -- text affinity by rule 2nu NUMERIC, -- numeric affinity by rule 5i INTEGER, -- integer affinity by rule 1r REAL, -- real affinity by rule 4no BLOB -- no affinity by rule 3 );-- Values stored as TEXT, INTEGER, INTEGER, REAL, TEXT. INSERT INTO t1 VALUES('500.0', '500.0', '500.0', '500.0', '500.0'); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|text-- Values stored as TEXT, INTEGER, INTEGER, REAL, REAL. DELETE FROM t1; INSERT INTO t1 VALUES(500.0, 500.0, 500.0, 500.0, 500.0); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|real-- Values stored as TEXT, INTEGER, INTEGER, REAL, INTEGER. DELETE FROM t1; INSERT INTO t1 VALUES(500, 500, 500, 500, 500); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|integer-- BLOBs are always stored as BLOBs regardless of column affinity. DELETE FROM t1; INSERT INTO t1 VALUES(x'0500', x'0500', x'0500', x'0500', x'0500'); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; blob|blob|blob|blob|blob-- NULLs are also unaffected by affinity DELETE FROM t1; INSERT INTO t1 VALUES(NULL,NULL,NULL,NULL,NULL); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; null|null|null|null|null? ? | 譯者信息 2.3 列親和性行為實例以下SQL演示當有值插入到一張表時,SQLite如何使用列親和性實現(xiàn)類型轉(zhuǎn)換的: CREATE TABLE t1(t TEXT, -- text affinity by rule 2nu NUMERIC, -- numeric affinity by rule 5i INTEGER, -- integer affinity by rule 1r REAL, -- real affinity by rule 4no BLOB -- no affinity by rule 3 );-- Values stored as TEXT, INTEGER, INTEGER, REAL, TEXT.(值分別以文本、整型、整型、實數(shù)、文本形式存儲) INSERT INTO t1 VALUES('500.0', '500.0', '500.0', '500.0', '500.0'); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|text-- Values stored as TEXT, INTEGER, INTEGER, REAL, REAL. DELETE FROM t1; INSERT INTO t1 VALUES(500.0, 500.0, 500.0, 500.0, 500.0); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|real-- Values stored as TEXT, INTEGER, INTEGER, REAL, INTEGER. DELETE FROM t1; INSERT INTO t1 VALUES(500, 500, 500, 500, 500); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|integer-- BLOBs are always stored as BLOBs regardless of column affinity. DELETE FROM t1; INSERT INTO t1 VALUES(x'0500', x'0500', x'0500', x'0500', x'0500'); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; blob|blob|blob|blob|blob-- NULLs are also unaffected by affinity DELETE FROM t1; INSERT INTO t1 VALUES(NULL,NULL,NULL,NULL,NULL); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; null|null|null|null|null | ||||||||||||||||||||||||||||||
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.0 比較表達式同標準SQL一樣,SQLite 3支持如下的比較操作符:"=", "==", "<", "<=", ">", ">=", "!=", "<>", "IN", "NOT IN", "BETWEEN", "IS", 以及 "IS NOT"。 3.1 排序規(guī)則比較的結(jié)果與操作數(shù)的存儲類型有關(guān),同時依據(jù)以下的規(guī)則:
|
轉(zhuǎn)自:http://www.oschina.net/translate/data-types-in-sqlite-version-3?cmp
總結(jié)
以上是生活随笔為你收集整理的SQLite3的数据类型转载()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Office365邮箱Imap迁移步骤
- 下一篇: 关于mysql中int(1)中int后面