我可以在sqlite3中限制列值的数据长度吗?
ex:varchar name(5),Insert语句接受超过5个字符。
答案 0 :(得分:12)
SQLite将varchar(5)
视为text
(即无限字符串),但您可以在列中添加CHECK constraint:
create table pancakes (
name text check(name is null or length(name) <= 5)
)
如果您的name
太长,则会出现“约束失败”错误:
sqlite> create table pancakes (name text not null check(length(name) <= 5));
sqlite> insert into pancakes (name) values ('1234');
sqlite> insert into pancakes (name) values ('12345');
sqlite> insert into pancakes (name) values ('123456');
Error: constraint failed