我有问题,我需要在我的表中添加一个复合主键(_id + col_Region) 但我不知道如何继续,我知道以通常的方式做,但我尝试了这个 语法不起作用:
private static final String DATABASE_CREATE1 = "CREATE TABLE " + MENAGE +
"( " + _id +" INTEGER PRIMARY KEY ,"+ col_Region +" TEXT,"+
LastModifiedTime +" TEXT,"+ Comune_Arrondis +" TEXT,"+ N_district +" INT ");
如何解决这个问题,我在论坛中寻找解决方案,但我没有找到一个,谢谢你的帮助
答案 0 :(得分:1)
语法为:
CREATE TABLE menage (
_id INTEGER NOT NULL,
col_Region TEXT NOT NULL,
LastModifiedTime TEXT,
Comune_Arrondis TEXT,
N_district INT,
PRIMARY KEY (_id, col_Region)
)
或代码:
private static final String DATABASE_CREATE1 = "CREATE TABLE " + MENAGE +
"( " + _id + " INTEGER NOT NULL, " + col_Region + " TEXT NOT NULL, " +
LastModifiedTime +" TEXT," + Comune_Arrondis + " TEXT, " + N_district +" INT, " +
"PRIMARY KEY ( " + _id + ", " + col_Region + "))";
因此,首先声明所有列,然后在末尾声明哪些列构成主键。请注意,构成主键的列应该被赋予NOT NULL约束。