alter sqlite table 在多列上添加唯一约束
alter sqlite table to add unique constraint on multiple columns
我有一个由 sqlite table 支持的 ContentProvider。所以为了创建我的 table 我使用了
public class H extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase sqliteDatabase) {
…// here I defined my original table without constraints
}
}
最初创建时,table 包含以下列:姓名、年龄、身高。没有约束。没有。
现在我需要向 table 添加约束。所以我增加了 DATABASE_VERSION,然后在 onCreate String 中我添加了 UNIQUE(name,age) ON CONFLICT REPLACE
。
我的问题是,我应该在 onUpgrade
方法中做什么?更简单地说:我如何调用 ALTER TABLE
只是为了添加约束?我的尝试失败了
ALTER TABLE myTable ADD UNIQUE(name,age) ON CONFLICT REPLACE
错误信息如下:
Caused by: android.database.sqlite.SQLiteException: near "CONSTRAINT": syntax error (code 1): , while compiling: ALTER TABLE myTable ADD CONSTRAINT UNIQUE(name,age) ON CONFLICT REPLACE
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "CONSTRAINT": syntax error (code 1): , while compiling: ALTER TABLE myTable ADD CONSTRAINT UNIQUE(name,age) ON CONFLICT REPLACE)
您不能在 SQLite 中添加带有 ALTER TABLE
的约束。
Only the RENAME TABLE and ADD COLUMN variants of the ALTER TABLE
command are supported. Other kinds of ALTER TABLE operations such as
DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted.
Alter table 在 sqlite 中相当有限。然而,为了您的目的,CREATE UNIQUE INDEX 也同样有效。
通常,创建唯一约束与创建唯一索引之间没有区别。参考:
SQLite - Any difference between table-constraint UNIQUE & column-constraint UNIQUE? https://dba.stackexchange.com/questions/144/when-should-i-use-a-unique-constraint-instead-of-a-unique-index
因此,在您的情况下,更改要使用的代码应该更改 onCreate 中的代码以改为使用 CREATE INDEX 语法。
请注意,如果您的 table 已经有重复的条目,这可能仍然会失败。
我有一个由 sqlite table 支持的 ContentProvider。所以为了创建我的 table 我使用了
public class H extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase sqliteDatabase) {
…// here I defined my original table without constraints
}
}
最初创建时,table 包含以下列:姓名、年龄、身高。没有约束。没有。
现在我需要向 table 添加约束。所以我增加了 DATABASE_VERSION,然后在 onCreate String 中我添加了 UNIQUE(name,age) ON CONFLICT REPLACE
。
我的问题是,我应该在 onUpgrade
方法中做什么?更简单地说:我如何调用 ALTER TABLE
只是为了添加约束?我的尝试失败了
ALTER TABLE myTable ADD UNIQUE(name,age) ON CONFLICT REPLACE
错误信息如下:
Caused by: android.database.sqlite.SQLiteException: near "CONSTRAINT": syntax error (code 1): , while compiling: ALTER TABLE myTable ADD CONSTRAINT UNIQUE(name,age) ON CONFLICT REPLACE
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "CONSTRAINT": syntax error (code 1): , while compiling: ALTER TABLE myTable ADD CONSTRAINT UNIQUE(name,age) ON CONFLICT REPLACE)
您不能在 SQLite 中添加带有 ALTER TABLE
的约束。
Only the RENAME TABLE and ADD COLUMN variants of the ALTER TABLE command are supported. Other kinds of ALTER TABLE operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted.
Alter table 在 sqlite 中相当有限。然而,为了您的目的,CREATE UNIQUE INDEX 也同样有效。
通常,创建唯一约束与创建唯一索引之间没有区别。参考:
SQLite - Any difference between table-constraint UNIQUE & column-constraint UNIQUE? https://dba.stackexchange.com/questions/144/when-should-i-use-a-unique-constraint-instead-of-a-unique-index
因此,在您的情况下,更改要使用的代码应该更改 onCreate 中的代码以改为使用 CREATE INDEX 语法。
请注意,如果您的 table 已经有重复的条目,这可能仍然会失败。