SQLite表约束 - 对多列唯一SQLite、多列唯一

2023-09-12 01:01:53 作者:-药不能停°

我能找到的语法在SQLite的网站上这个排行榜,但没有例子,我的code崩溃。我有一个单列唯一约束其他表,但我想一个约束添加到表上的两列。这是我有一个消息语法错误。

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 a SQLiteException with the message "syntax error".

CREATE TABLE名(列DEFS) UNIQUE(col_name1,col_name2)ON冲突更换

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

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

I'm doing this based on the following:

表约束

编辑: 需要明确的是,我所提供的链接上的文件说, CONTSTRAINT名应该来我的约束定义了。

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 ...

误差近约束:语法错误

如果我把

...last_column_name last_col_datatype) UNIQUE ...

误差近UNIQUE:语法错误

推荐答案

把UNIQUE声明列定义部分中:

Put the UNIQUE declaration within the column definition section:

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

工作的例子:

CREATE TABLE a (i INT, j INT, UNIQUE(i, j) ON CONFLICT REPLACE);
 
精彩推荐
图片推荐