복합 고유 인덱스 제약 조건을 사용하여 sqlite 데이터베이스에 테이블을 만들었습니다. 문제는 제약 조건이 적용되지 않는다는 것입니다. 동일한 고유 키 (col2 NOT NULL, col4) ("testvalue", NULL) 중 여러 행을 삽입 할 수 있습니다. documentation에 따르면NULL에 대해 고유 인덱스 제약 조건이 적용되지 않습니다.
, 그럼
For the purposes of UNIQUE constraints, NULL values are considered distinct from all other values, including other NULLs.
public class myTable{
/*table name*/
public static final String TABLE_NAME = "myTable";
/*column names*/
public static final String COLUMN_ID = "_id";
public static final String COLUMN_1 = "col1";
public static final String COLUMN_2 = "col2";
public static final String COLUMN_3 = "col3";
public static final String COLUMN_4 = "col4";
/*create table script*/
public static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME
+ "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT"
+ ", " + COLUMN_1 + " INTEGER"
+ ", " + COLUMN_2 + " INTEGER NOT NULL"
+ ", " + COLUMN_3 + " TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP"
+ ", " + COLUMN_4 + " TEXT"
+ ", FOREIGN KEY(" + COLUMN_2 + ") REFERENCES " + Table_other.TABLE_NAME + "(" + Table_other.COLUMN_ID + ")"
+ ", FOREIGN KEY(" + COLUMN_1 + ") REFERENCES " + Table_yetanother.TABLE_NAME + "(" + Table_yetanother.COLUMN_ID + ")"
+ ")";
/*create index script */
public static final String CREATE_INDEX =
"CREATE UNIQUE INDEX UIDX ON " + TABLE_NAME
+ "(" + COLUMN_2 + "," + COLUMN_4 + ");";
}
참조 [NULL이 다른 데이터베이스 엔진 수스 SQLite는에 처리] (http://www.sqlite.org/nulls.html) 세부. –
@ CL. 감사. 그거 좋은 기사 야. 나는 그것을 통해 갔지만 나는 여전히 해결책을 찾고있다. 나는 유일한 방법은 여기에있는 것 같아요 - http://stackoverflow.com/a/12095133/2105986 – faizal