SQLite 表约束 - 在多列上唯一 [英] SQLite table constraint - unique on multiple columns

查看:57
本文介绍了SQLite 表约束 - 在多列上唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 SQLite 网站上找到语法图表",但没有示例,我的代码崩溃了.我有其他表在单个列上具有唯一约束,但我想在两列上向表添加约束.这就是我所拥有的导致 SQLiteException 消息语法错误"的原因.

I can find syntax "charts" on this on the SQLite website, but no examples and my code is crashing. I have other tables with unique constraints on a single column, but I want to add a constraint to the table on two columns. This is what I have that is causing an SQLiteException with the message "syntax error".

CREATE TABLE name (column defs) 
UNIQUE (col_name1, col_name2) ON CONFLICT REPLACE

我这样做是基于以下几点:

I'm doing this based on the following:

表格约束

要清楚,我提供的链接上的文档说CONTSTRAINT name 应该在我的约束定义之前.

To be clear, the documentation on the link I provided says that CONTSTRAINT name should come before my constraint definition.

不过,可能会导致解决方案的一些事情是,在我括号中的列定义之后的任何内容都是调试器所抱怨的.

Something that may lead to the solution though is that whatever follows my parenthesized column definitions is what the debugger complains about.

如果我把

...last_column_name last_col_datatype) CONSTRAINT ...

错误在靠近CONSTRAINT":语法错误

如果我把

...last_column_name last_col_datatype) UNIQUE ...

错误在UNIQUE"附近:语法错误

推荐答案

将 UNIQUE 声明放在列定义部分;工作示例:

Put the UNIQUE declaration within the column definition section; working example:

CREATE TABLE a (
    i INT,
    j INT,
    UNIQUE(i, j) ON CONFLICT REPLACE
);

这篇关于SQLite 表约束 - 在多列上唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆