你需要连接表才能在android sqlite中拥有外键/主键匹配吗?

时间:2013-12-13 22:42:41

标签: android sql sqlite foreign-keys

嗨我对Android开发很新。 我目前有两个表,我是否需要加入这些表才能获得PK / FK匹配?

我正在尝试遵循这个设计:绿色= PK,橙色= FK enter image description here

我目前有这段代码,但我认为没错。

public void onCreate(SQLiteDatabase db) {
    // SQL statement to create book table
    String CREATE_PLAYER_TABLE = "CREATE TABLE " + TABLE_PLAYER_INFO + "( "
            + KEY_PLAYER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_FNAME + " TEXT,"
            + KEY_LNAME + " TEXT,"
            + KEY_POSITION + " TEXT,"
            + KEY_HEIGHT + " TEXT,"
            + KEY_AGE + " TEXT," 
            + KEY_WEIGHT + " TEXT," 
            + KEY_TEAM + " TEXT,"
            + FORIGN_KEY + " TEXT,"
            + " FOREIGN KEY ("+FORIGN_KEY+") REFERENCES " + TABLE_TEAM_INFO + " ("+KEY_TEAM_ID+"))";

    String CREATE_TEAM_TABLE = "CREATE TABLE " + TABLE_TEAM_INFO + "( "
            + KEY_TEAM_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_TEAMNAME + " TEXT" + ")";

    // create books table
    db.execSQL(CREATE_PLAYER_TABLE);
    db.execSQL(CREATE_TEAM_TABLE);
}

任何帮助都会很棒 感谢

1 个答案:

答案 0 :(得分:0)

要创建数据库,您不需要JOIN。这没有意义,因为你不能加入一个尚不存在的表。 JOIN仅用于数据读取。

This将帮助您创建关系。