sqlite3:唯一约束 [英] sqlite3: Unique constraint
问题描述
我正在尝试使用 Table_A 创建一个新数据库.
表_A
id 名称评论键
1 peter 随机字符串 1234
2 sam 随机字符串 5678
这是架构:
如果不存在则创建表 Table_A (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,名称 VARCHAR(60) 非空,类 TEXT NOT NULL DEFAULT '',关键整数非空,UNIQUE(name, key) ON CONFLICT IGNORE);
现在,当我尝试向该表插入元素时,不会插入重复的条目,这是因为
UNIQUE(name, key) ON CONFLICT IGNORE);
但是 id 增加了,我不知道如何解决这个问题.
实际输出:
Table_Aid 名称 注释 键1 peter 随机字符串 123426 sam 随机字符串 567875 约翰随机字符串 1478
预期输出:
Table_Aid 名称 注释 键1 peter 随机字符串 12342 sam 随机字符串 56783 约翰随机字符串 1478
a>,当您删除 AUTOINCREMENT 关键字时,您将获得自动递增的值,而不会出现已删除条目的间隙.
I am trying to create a new database with Table_A.
Table_A
id name comments key
1 peter random string 1234
2 sam random string 5678
This is the schema:
CREATE TABLE IF NOT EXISTS Table_A (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name VARCHAR(60) NOT NULL,
class TEXT NOT NULL DEFAULT '',
key INTEGER NOT NULL,
UNIQUE(name, key) ON CONFLICT IGNORE);
Now when I try to insert elements to this table no duplicate entries are inserted which is expected because of
UNIQUE(name, key) ON CONFLICT IGNORE);
But the id is incremented and I am not sure how to fix this.
Actual output:
Table_A
id name comments key
1 peter random string 1234
26 sam random string 5678
75 john random string 1478
Expected output:
Table_A
id name comments key
1 peter random string 1234
2 sam random string 5678
3 john random string 1478
As documented in the documentation, you get autoincremented values without gaps for deleted entries when you drop the AUTOINCREMENT keyword.
这篇关于sqlite3:唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!