如何在android的sqlite的外键? [英] how to create foreign key in sqlite android?

查看:362
本文介绍了如何在android的sqlite的外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手机器人。如何创建的表的外键?下面是我的code,创造表这是playerTable和attackTable。请帮助!

I'm newbie android. How to create foreign key in the table? below is my code that create table which are playerTable and attackTable. Please help!

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号开始...这可能吗?

Besides that, can autoincrement start from other number such as 1000? I mean autoincrement start from 1, 2, 3, 4, and so on... i want the number start from 1000, 10001, 1002... is that possible?

推荐答案

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

You can add the foreign key after the column definition, with keyword FOREIGN KEY:

例如创建一个外键表TABLE_ATTACK列playerPassing,指出playerPassing被引用表TABLE_NAME和它的列_id,你把类似下面的code后,TEXT非空:
+外键(+ playerPassing +)参考+ TABLE_NAME +(_id)

For example to create a foreign key to column playerPassing of table TABLE_ATTACK that says that playerPassing is referencing table TABLE_NAME and it's column _id, you put something like the following code after "TEXT not null" : +"FOREIGN KEY("+ playerPassing +") REFERENCES "+ TABLE_NAME + "(_id)"

这篇关于如何在android的sqlite的外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆