实施列类型

SQLite 使用动态类型并忽略声明的列类型:

> CREATE TABLE Test (
      Col1 INTEGER,
      Col2 VARCHAR(2),       -- length is ignored, too
      Col3 BLOB,
      Col4,                  -- no type required
      Col5 FLUFFY BUNNIES    -- use whatever you want
  );
> INSERT INTO Test VALUES (1, 1, 1, 1, 1);
> INSERT INTO Test VALUES ('xxx', 'xxx', 'xxx', 'xxx', 'xxx');
> SELECT * FROM Test;
1   1   1   1   1         
xxx xxx xxx xxx xxx       

(但是,声明的列类型用于类型关联 。)

要强制执行类型,你必须使用 typeof() 函数添加约束 :

CREATE TABLE Tab (
    Col1 TEXT   CHECK (typeof(Col1) = 'text' AND length(Col1) <= 10),
    [...]
);

(如果这样的列应该是 NULLable,则必须明确允许'null'。)