可变长度的 NVARCHAR?
NVARCHAR with variable length?
我想在 SQL 中创建一个 table,它有一个网站用户的字符串属性。我应该如何声明这个属性,因为名称没有最大长度?我可以将长度留空并这样做吗?
CREATE TABLE User(
id NUMBER PRIMARY KEY NOT NULL
name NVARCHAR
)
此外,我是否需要为 PRIMARY KEYS 说 NOT NULL,或者主键已经永远不会为空?
提前致谢:)
您应该将长度声明为 MAX 并且主键自动为 NOT NULL
CREATE TABLE User(
id NUMBER PRIMARY KEY
name NVARCHAR(MAX)
)
由于您稍后将 dbms 指定为 SQL Lite ,因此 documentation 表示
What is the maximum size of a VARCHAR in SQLite?
SQLite does not enforce the length of a VARCHAR. You can declare a
VARCHAR(10) and SQLite will be happy to let you put 500 characters in
it. And it will keep all 500 characters intact - it never truncates.
How should I declare this attr, since names don't have a max length?
Can I just leave the length empty and do it like this?
是的,在 SQLite 中,您可以使用任何“text affinity”类型名称声明列(CHAR
、TEXT
、CLOB
, 等),它会存储没有长度限制的字符串(或 blob)(全局字符串长度限制除外,默认为 1 GB)。
如果你确实想限制字符串的长度,你可以使用显式 CHECK
约束来实现,比如 CHECK(LENGTH(name) <= 100)
.
Also, do I need to say NOT NULL for PRIMARY KEYS, or are primary keys
already never null?
根据 SQL 标准,PRIMARY KEY
意味着 NOT NULL
。但是,SQLite 有 a long-standing bug 需要显式 NOT NULL
约束。
According to the SQL standard, PRIMARY KEY
should always imply NOT NULL
. Unfortunately, due to a bug in some early versions, this is not
the case in SQLite. Unless the column is an INTEGER PRIMARY KEY
or the
table is a WITHOUT ROWID
table or the column is declared NOT NULL
,
SQLite allows NULL
values in a PRIMARY KEY
column. SQLite could be
fixed to conform to the standard, but doing so might break legacy
applications. Hence, it has been decided to merely document the fact
that SQLite allowing NULL
s in most PRIMARY KEY
columns.
我想在 SQL 中创建一个 table,它有一个网站用户的字符串属性。我应该如何声明这个属性,因为名称没有最大长度?我可以将长度留空并这样做吗?
CREATE TABLE User(
id NUMBER PRIMARY KEY NOT NULL
name NVARCHAR
)
此外,我是否需要为 PRIMARY KEYS 说 NOT NULL,或者主键已经永远不会为空?
提前致谢:)
您应该将长度声明为 MAX 并且主键自动为 NOT NULL
CREATE TABLE User(
id NUMBER PRIMARY KEY
name NVARCHAR(MAX)
)
由于您稍后将 dbms 指定为 SQL Lite ,因此 documentation 表示
What is the maximum size of a VARCHAR in SQLite?
SQLite does not enforce the length of a VARCHAR. You can declare a VARCHAR(10) and SQLite will be happy to let you put 500 characters in it. And it will keep all 500 characters intact - it never truncates.
How should I declare this attr, since names don't have a max length? Can I just leave the length empty and do it like this?
是的,在 SQLite 中,您可以使用任何“text affinity”类型名称声明列(CHAR
、TEXT
、CLOB
, 等),它会存储没有长度限制的字符串(或 blob)(全局字符串长度限制除外,默认为 1 GB)。
如果你确实想限制字符串的长度,你可以使用显式 CHECK
约束来实现,比如 CHECK(LENGTH(name) <= 100)
.
Also, do I need to say NOT NULL for PRIMARY KEYS, or are primary keys already never null?
根据 SQL 标准,PRIMARY KEY
意味着 NOT NULL
。但是,SQLite 有 a long-standing bug 需要显式 NOT NULL
约束。
According to the SQL standard,
PRIMARY KEY
should always implyNOT NULL
. Unfortunately, due to a bug in some early versions, this is not the case in SQLite. Unless the column is anINTEGER PRIMARY KEY
or the table is aWITHOUT ROWID
table or the column is declaredNOT NULL
, SQLite allowsNULL
values in aPRIMARY KEY
column. SQLite could be fixed to conform to the standard, but doing so might break legacy applications. Hence, it has been decided to merely document the fact that SQLite allowingNULL
s in mostPRIMARY KEY
columns.