CREATE TABLE SQL 中的 TEXT (5) 在使用 SQLite 时是什么意思?

huangapple go评论46阅读模式
英文:

What does the TEXT (5) in CREATE TABLE SQL mean when I use SQLitle?

问题

Code A 用于创建一个由 SQLite Studio 3.4.3 创建的 SQLite 表。

1: 在 SQLitle 中,TEXT (5) 是什么意思?这是否意味着 linkTitle 字段的长度不能超过5个字母?如果我为 linkTitle 字段添加一个包含20个字母的记录会发生什么?

2: linkTitle 字段的默认值为 "The title of saved links",它的长度已经超过了5个字母,会发生什么?

英文:

The Code A is to created a SQLitle table made by SQLite Studio 3.4.3.

1: What does the TEXT (5) mean in SQLitle? Does it mean that the length of linkTitle field can't be exceed 5 letters? What will happend if I add a record with 20 letters for linkTitle field?

2: The default value "The title of saved links" of the linkTitle field has exceeded 5 letters, What will happend ?

Code A

CREATE TABLE myTable (
    id          INTEGER   PRIMARY KEY ASC AUTOINCREMENT,    
    linkTitle   TEXT (5)  NOT NULL
                          DEFAULT [The title of saved links],
    linkSaved   TEXT (10) NOT NULL
   
);

答案1

得分: 2

  1. 它没有影响,列类型被视为文本。SQLite支持许多其他数据库使用的SQL语法,因此接受长度限定符,但会忽略除了语法方面之外的其他内容(例如,如果指定非数字内容,将引发语法错误,也可以忽略括号)。

  2. 没有长度限制被规定为说明符。

如果你使用以下语句:

INSERT INTO myTable (linkTitle, linkSaved) VALUES
	('The quick brown fox jumped over the lazy fence','The slow grey elephant could not jump over the fence so crashed though it'),
	(100, zeroblob(10)),
	(10.1234567, '10.1234567')
;
SELECT * FROM myTable;

结果将是:

CREATE TABLE SQL 中的 TEXT (5) 在使用 SQLite 时是什么意思?

这还表明你可以将任何类型的数据保存在任何类型的列中,除了行id的别名(例如id列或行id本身)。这通常是其他数据库不允许的。

此外,列的类型本身非常灵活。你可以指定几乎任何类型,例如the_column ridiculoustype,在这种情况下,SQLite将将其类型设置为NUMERIC(默认/默认类型),请参考链接中第3.1节中有关分配类型亲和性的规则。

你可能需要阅读https://www.sqlite.org/datatype3.html。

英文:
  1. It has no affect, the column type is considered as TEXT. SQLite accommodates much of the SQL that is used by other databases and hence the acceptance of the length specifier but ignoring (other than syntactically (omit either parenthesis and a syntax error will be issued, likewise if a non numeric is specified)).

  2. No length restriction is imposed by the specifier.

If you were to use:-

INSERT INTO myTable (linkTitle,linkSaved) VALUES
	('The quick brown fox jumped over the lazy fence','The slow grey elephant could not jump over the fence so crashed though it'),
	(100,zeroblob(10)),
	(10.1234567,'10.1234567')
;
SELECT * FROM myTable;

The result would be:-

CREATE TABLE SQL 中的 TEXT (5) 在使用 SQLite 时是什么意思?

This also demonstrates that you can save (with the exception of an alias of the rowid, e.g. the id column, or the rowid itself ), any type of data in any type of column. Which other databases typically do not allow.

Furthermore, the column type itself is highly flexible. That is you could specify virtually any type e.g. the_column ridiculoustype and SQLite will make the type NUMERIC (the default/drop through type) in this case (see 3.1 in the link for the rules for assigning a type affinity).

You should perhaps have a read of https://www.sqlite.org/datatype3.html

huangapple
  • 本文由 发表于 2023年2月10日 11:02:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定