SQLite 中的多个唯一列
我正在尝试创建一个表,我需要它不允许有 3 个字段相同的行。
当我使用 SQLLite 在 Python 中创建表时,我使用了以下命令,但几乎没有得到任何结果。它通常在写入 2 条记录后停止,因此显然有人相信它是重复的。
CREATE TABLE CorpWalletJournal (
date INT,
refID INT,
refTypeID INT,
ownerName1 TEXT,
ownerID1 INT,
ownerName2 TEXT,
ownerID2 INT,
argName1 TEXT,
argID1 ID,
amount INT,
balance INT,
reason TEXT,
accountKey INT,
UNIQUE (ownerID1, ownerID2, accountKey, argID1)
);
因此,我希望数据库不允许 OwnerID1、ownerID2、accountKey 和 argID1 相同的记录。
有人能帮我解决这个问题吗?
谢谢你!
I am trying to create a table where I need it to NOT allow rows where 3 fields are the same.
When I create the table in Python using SQLLite, I use the follow, but I hardly get any results at all. It usually stops after writing 2 records, so something is obviously believing its duplicated.
CREATE TABLE CorpWalletJournal (
date INT,
refID INT,
refTypeID INT,
ownerName1 TEXT,
ownerID1 INT,
ownerName2 TEXT,
ownerID2 INT,
argName1 TEXT,
argID1 ID,
amount INT,
balance INT,
reason TEXT,
accountKey INT,
UNIQUE (ownerID1, ownerID2, accountKey, argID1)
);
So, I would like the database to NOT allow records where ownerID1, ownerID2, accountKey and argID1 are the same.
Can anyone help me with this at all?
Thank-you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定是什么问题。它在这里工作正常:
我从最后一行得到的错误是:
I'm not sure what is the problem. It works fine here:
The error I get from last line is:
您不是在寻找唯一的,而是在寻找主键。当您设置 PRIMARY KEY (ownerID1、ownerID2、accountKey、argID1) 时,这 4 个值一起就是行索引。
这意味着,如果您写入一个新行,其中这 4 个值等于现有值,它将覆盖该行。因此,这 4 个值的每个组合只能存在一次。
另一方面,UNIQUE 意味着 4 个值中的每一个只能使用一次。
Your are not looking for UNIQUE, but for PRIMARY KEY. When you set PRIMARY KEY (ownerID1, ownerID2, accountKey, argID1), then this 4 values together are the row index.
That means, that if you write a new row with this 4 values equal to an existing one, it will overwrite that one. Therefore every combination of the 4 values can only exist once.
UNIQUE on the other hand, means that each of the 4 values can only be used once.