z

SQL 数据库引擎 (据我们所知,除 SQLite 之外的所有 SQL 数据库引擎)都使用严格的静态类型。使用静态类型,值的类型便由它的容器 -- 存储值的特定的列 -- 来决定。

SQLite 使用更通用的动态类型系统。在 SQLit 中,值的数据类型与值本身相关,而不是与它的容器。SQLite 的动态类型系统与其它数据库引擎的常用静态类型系统是向后兼容的,在这个意义上,工作在静态类型数据库上的 SQL 语句应该以同样的方式工作在 SQLite 中。然而,SQLite 中的动态类型允许它做传统的严格类型的数据库所不能做的事。

1.0 Storage Classes and Datatypes

Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:

  • NULL. The value is a NULL value.

  • INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

  • 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).

  • BLOB. The value is a blob of data, stored exactly as it was input.

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 存储类型与数据类型

存储在 SQLite 数据库中的每个值(或是由数据库引擎所操作的值)都有一个以下的存储类型:

  • NULL. 值是空值。
  • INTEGER. 值是有符号整数,根据值的大小以1,2,3,4,6 或8字节存储。
  • REAL. 值是浮点数,以8字节 IEEE 浮点数存储。
  • TEXT. 值是文本字符串,使用数据库编码(UTF-8, UTF-16BE 或 UTF-16LE)进行存储。
  • BLOB. 值是一个数据块,按它的输入原样存储。

注意,存储类型比数据类型更笼统。以 INTEGER 存储类型为例,它包括6种不同的长度不等的整数类型,这在磁盘上是不同的。但是只要 INTEGER 值从磁盘读取到内存进行处理,它们就被转换为更为一般的数据类型(8字节有符号整型)。因此在一般情况下,“存储类型” 与 “数据类型” 没什么差别,这两个术语可以互换使用。

SQLite 版本3数据库中的任何列,除了整型主键列,都可用于存储任何存储类型的值。

SQL 语句中的任何值,无论它们是嵌入到 SQL 语句中的字面量还是绑定到预编译 SQL 语句中的参数,都有一个隐含的存储类型。在下述情况下,数据库引擎会在执行查询时在数值存储类型(INTEGER 和 REAL)和 TEXT 之间进行转换。

1.1 Boolean Datatype

SQLite 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 Datatype

SQLite 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:

  • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

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 并没有单独的布尔存储类型,而是将布尔值存储为整数 0 (false) 和 1 (true)。

1.2 日期和时间类型

SQLite 没有另外的存储类型来存储日期和时间。SQLite 的内置的日期和时间函数能够将日期和时间存为 TEXT、REAL 或 INTEGER 值:

  • TEXT  ISO8601 字符串 ("YYYY-MM-DD HH:MM:SS.SSS")。
  • REAL 儒略日数 (Julian Day Numbers),按照前公历,自格林威治时间公元前4714年11月24日中午以来的天数。
  • INTEGER Unix 时间,自 1970-01-01 00:00:00 UTC 以来的秒数。

应用可以选择这些格式中的任一种存储日期和时间,并使用内置的日期和时间函数在这些格式间自由转换。

2.0 Type Affinity

In 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:

  • TEXT
  • NUMERIC
  • INTEGER
  • REAL
  • NONE

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 和其它数据库引擎之间的兼容性,SQLite 支持列的“类型亲和性”的概念。列的类型亲和性是指数据存储于该列的推荐类型。这里重要的思想是类型是推荐的,而不是必须的。任何列仍可以存储任何类型的数据。这只是让一些列有选择性地优先使用某种存储类型。一个列的首选存储类型被称为它的“亲和性”。

每个 SQLite 3 数据库中的列都归于以下的类型亲和性中的一种:

  • TEXT
  • NUMERIC
  • INTEGER
  • REAL
  • NONE

一个具有 TEXT 亲和性的列使用存储类型 NULL、 TEXT 或 BLOB 存储所有数据。如果数值数据被插入到一个具有 TEXT 亲和性的列,则数据在存储前被转换为文本形式。

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.

译者信息

数值亲和性的列可能包含了使用所有五个存储类的值。当插入文本数据到数值列时,该文本的存储类型被转换成整型或实数(按优先级排序)如果这种转换是无损或可逆的的话。对于文本与实数类型之间的转换,如果前15个重要十进制数字被保留的话,SQLite认为这种转换是无损并可逆的。如果文本不能无损地转换成整型或实数,那这个值将以文本类型存储。不要试图转换NULL或BLOB值。

一个字符串可能看上去像带有小数点和/或指数符的浮点文字,但只要这个值可以用一个整型表示,数值亲和性就会把它转换成一个整型。因此,字符串‘3.0e+5’以整型300000,而不是浮点值30000.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 NONE does not prefer one storage class over another and no attempt is made to coerce data from one storage class into another.

译者信息

一个使用整型亲和性的列与具有数值亲和性的列表现一致。只是在CAST表达式里,它们之间的区别体现得明显。

除了强制将整型值转换成浮点表示外,一个具有实数亲和性的列与具有数值亲和性的列表现一致(作为一个内部的优化,为了少占用空间,无小数部分且存储在实数亲和性列上的小浮点值以整型形式写到磁盘,读出时自动转换回浮点值。在SQL级别,这种优化是完全不可见的,并且只能通过检查数据库文件的原始比特检测到)。

一个具有NONE亲和性的列不能从一种存储类型转换成另一种,也不要试图强制对它进行转换。

2.1 Determination Of Column Affinity

The affinity of a column is determined by the declared type of the column, according to the following rules in the order shown:

  1. If the declared type contains the string "INT" then it is assigned INTEGER affinity.

  2. 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.

  3. If the declared type for a column contains the string "BLOB" or if no type is specified then the column has affinity NONE.

  4. If the declared type for a column contains any of the strings "REAL", "FLOA", or "DOUB" then the column has REAL affinity.

  5. 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 列亲和性测定

列的亲和性是由它的声明类型决定的,按照以下顺序所示的规则:

1. 如果声明类型包含字符串“INT”,那它被指定为整型亲和性;

2. 如果列的声明类型包含任何“CHAR”、“CLOB”或“TEXT”字符串,那么该列具有文本亲和性。注意:VARCHAR类型包含“CHAR”并且被指定为文本亲和性;

3. 如果列的声明类型包含“BLOB”或者没有指定类型,那这列具有NONE亲和性;

4. 如果列的声明类型包含任何“REAL”、“FLOA”或“DOUB”字符串,则该列具有实数亲和性;

5. 否则,它将具有数值亲和性。

注意:判定列亲和性规则的顺序是很重要的。一个具有“CHARINT”声明类型的列将匹配规则1和2,但是规则1优先所有该列具有整型亲和性。

2.2 Affinity Name Examples

The 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.

Example Typenames From The
CREATE TABLE Statement
or CAST Expression
Resulting Affinity Rule Used To Determine Affinity
INT
INTEGER
TINYINT
SMALLINT
MEDIUMINT
BIGINT
UNSIGNED BIG INT
INT2
INT8
INTEGER 1
CHARACTER(20)
VARCHAR(255)
VARYING CHARACTER(255)
NCHAR(55)
NATIVE CHARACTER(70)
NVARCHAR(100)
TEXT
CLOB
TEXT 2
BLOB
no datatype specified
NONE 3
REAL
DOUBLE
DOUBLE PRECISION
FLOAT
REAL 4
NUMERIC
DECIMAL(10,5)
BOOLEAN
DATE
DATETIME
NUMERIC 5

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 亲和性名字实例

下表显示了有多少从更传统的SQL实现的常用数据类型名,通过上一节介绍的五个规则被转换成各种亲和性类型。这张表只显示了SQLite可接受的一小部分数据类型名。注意:跟在类型名后,括号内数值参数(如:VARCHAR(255))将被SQLite忽略 - SQLite不对字符串、BLOBs或数值的长度强加任何限制(除了大型全局SQLITE_MAX_LENGTH限制)。

Example Typenames From The
CREATE TABLE Statement
or CAST Expression
Resulting Affinity Rule Used To Determine Affinity
INT
INTEGER
TINYINT
SMALLINT
MEDIUMINT
BIGINT
UNSIGNED BIG INT
INT2
INT8
INTEGER 1
CHARACTER(20)
VARCHAR(255)
VARYING CHARACTER(255)
NCHAR(55)
NATIVE CHARACTER(70)
NVARCHAR(100)
TEXT
CLOB
TEXT 2
BLOB
no datatype specified
NONE 3
REAL
DOUBLE
DOUBLE PRECISION
FLOAT
REAL 4
NUMERIC
DECIMAL(10,5)
BOOLEAN
DATE
DATETIME
NUMERIC 5

注意: 因为在“POINT”末尾的“INT”,一个“ FLOATING POINT”声明类型 会被赋予整型亲和性,而不是实数亲和性。而且“STRING”声明类型具有数值亲和性,而不是文本亲和性。

2.3 Column Affinity Behavior Example

The 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如何使用列亲和性实现类型转换的:

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

3.0 Comparison Expressions

SQLite version 3 has the usual set of SQL comparison operators including "=", "==", "<", "<=", ">", ">=", "!=", "<>", "IN", "NOT IN", "BETWEEN", "IS", and "IS NOT", .

3.1 Sort Order

The results of a comparison depend on the storage classes of the operands, according to the following rules:

  • 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().

译者信息

3.0 比较表达式

同标准SQL一样,SQLite 3支持如下的比较操作符:"=", "==", "<", "<=", ">", ">=", "!=", "<>", "IN", "NOT IN", "BETWEEN", "IS", 以及 "IS NOT"。

3.1 排序规则

比较的结果与操作数的存储类型有关,同时依据以下的规则:

  • NULL值小于其他任何值(包括另外一个NULL)
  • INTEGER或REAL小于TEXT,BLOB值;若两个INTEGER(或者REAL)比较,则按照实际的数值进行。
  • TEXT小于BLOB,若两个TEXT比较,结果则由适当的整理顺序决定
  • 若两个BLOD比较,与memcmp()的结果一致

转自:http://www.oschina.net/translate/data-types-in-sqlite-version-3?cmp

SQLite3的数据类型转载()相关推荐

  1. sqlite3日期数据类型

    一.sqlite3日期数据类型,默认用datetime解析(根据stackflow) 使用时注意三点: 1. 创建表时,字段 DT 的类型为 date 2. 插入数据时,DT字段直接为 str 类型 ...

  2. c语言sqlite3写数据类型,在sqlite数据库中,int类型不等于integer数据类型

    在sqlite数据库中,int类型不等于integer数据类型 这些天一直在忙销售管理软件易卖通客户端的程序编写,由于需要采用本地数据缓存机制来提高程序的数据访问效率,所以需要在客户端使用一个小巧的本 ...

  3. r语言如何读取matlab数据类型,[转载]R语言数据类型解析[转]

    寻求帮助,使用 help(solve),?solve 和 help("solve")是一样的,如果需要搜索可以用 help.search(solve) 或者 ??solve.另外使 ...

  4. mysql 批量更新数据类型

    select column_name,data_type,CONCAT('alter table `',table_name,'` modify column `',column_name,'` da ...

  5. sql server 与 mysql在自定以数据类型的区别

    sql server   中可以使用 create  TYPE postal_code FORM varchar(6) not null; 用于限定邮编的数据位数,他基于varchar数据类型 注意: ...

  6. JavaScript中的基本数据类型与引用数据类型

    JavaScript中的基本数据类型与引用数据类型 转载自:https://www.cnblogs.com/c2016c/articles/9328725.html 1.栈(stack)和堆(heap ...

  7. 第五章· MySQL数据类型

    一.数据类型介绍 二.列属性介绍 一.数据类型介绍 1.四种主要类别  1)数值类型 2)字符类型 3)时间类型 4)二进制类型 2.数据类型的 ABC 要素 1)Appropriate(适当) 2 ...

  8. Mac安装sqlite3

    1,下载 最新版本的 Mac OS X 会预安装 SQLite,但是如果没有可用的安装,只需按照如下步骤进行: 去安装最新的3.8.6版本,和linux的sqlite3安装版本是同一个. 软件地址为: ...

  9. sqlite3 可视化工具 linux,【SQLite3下载】SQLite3可视化工具 v3 3.27.2 官方版-开心电玩...

    软件介绍 SQLite3是一款嵌入式的轻型数据库软件,该软件所占内存较低,需要搭配嵌入式产品使用,能够适用于Windows/Linux/Unix等主流的操作系统,需要搭配嵌入式产品使用,相比较于同种类 ...

最新文章

  1. 深度剖析RabbitMQ可靠性消息投递以及实践方案
  2. 华为如何打造智能终端的有趣灵魂?(下)
  3. 成功解决TypeError: Encoders require their input to be uniformly strings or numbers. Got [‘float‘, ‘int‘,
  4. 端口安全原理介绍及配置命令
  5. docker安装flowable-ui
  6. mac json工具_工具类封装的思路 | 钉钉群机器人为例
  7. 2014年国际IT外包10大事件
  8. 在内容也中寻找母板页中的控件
  9. “内心强大的人”依然会被移出首页
  10. unittest模块:单元测试
  11. 阿里巴巴的矢量图标之字体图标
  12. H5(html5)入门教程
  13. 从抖音到“奶头乐“,它是如何一步步毁掉我们的?
  14. tas5424_TAS5414A,TAS5424A,-电子电路图,电子技术资料网站
  15. 微处理器常见问题及解答
  16. ADB有线连接车机连不上的解决方案2
  17. 【ANDROID学习】
  18. linux永久自动挂载
  19. python二手房使用教程_利用Python对链家网北京二手房进行简单数据分析
  20. [OAuth2.0三方登录系列文章-1]OAuth2.0与三方登录的端到端方案

热门文章

  1. mysql bind param_为什么bindParam必须要指定类型?
  2. 单元测试 applicationinfomanager bean无法注入_你真的会用Spring吗?如何在单例Bean中注入原型Bean...
  3. kaldi语音识别实战pdf_语音识别相关工具和资料分享
  4. oracle 导出数据 utl,【原创】利用utl_file包进行表数据导出
  5. python预测随机数据_随机森林预测
  6. mongodb 3.4 安装_Python数据分析及可视化实例之CentOS7.2+MongoDB V3.4 安装
  7. 【杂谈】如何学会看arxiv.org才能不错过自己研究领域的最新论文?
  8. 全球及中国5-氯-2-羟基苯甲酸产业专项调研与投资潜力预测报告2022-2028年
  9. 全球及中国云厨房(仅限外卖的餐厅)行业前景态势与投资决策建议报告2022版
  10. 调试Release发布版程序的Crash错误