如何在sqlite android中创建外键?

时间:2013-12-16 11:45:01

标签: android sqlite

我是新手android。如何在表中创建外键?下面是我创建tableTable和attackTable表的代码。请帮忙!

public void onCreate(SQLiteDatabase db) {
    // Create a database table
            String createPlayerTable = "CREATE TABLE " + TABLE_NAME
                    + " (_id INTEGER PRIMARY KEY      autoincrement," + PlayerName + " TEXT not null, "
                    + PlayerAge + " TEXT not null, "+PlayerBorn+" TEXT not null, "+PlayerWeight+" TEXT not null, "+PlayerHeight+" TEXT not null);";
            db.execSQL(createPlayerTable);

            String createAttackTable = "CREATE TABLE " + TABLE_ATTACK
                    + " (_ID INTEGER PRIMARY KEY autoincrement," + playerAttack + " TEXT not null, "
                    + playerPassing + " TEXT not null);";
            db.execSQL(createAttackTable);              

除此之外,自动增量可以从1000等其他数字开始吗?我的意思是自动增量从1,2,3,4等开始......我希望数字从1000,10001,1002开始......这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以在列定义后添加外键,使用关键字FOREIGN KEY:

例如,要创建一个外键给列播放器table TABLE_ATTACK表示playerPassing正在引用表TABLE_NAME而它是column _id,请输入类似下面的代码之后

"TEXT not null" :
+"FOREIGN KEY("+ playerPassing +") REFERENCES "+ TABLE_NAME + "(_id)"