简体   繁体   English

SQLite中的外键

[英]Foreign key in SQLite

I am creating two tables, one for books and one for stores.I want to link every Book in BookStore Table to StoreTable by giving it a Store_ID. 我正在创建两个表,一个用于书籍,一个用于商店。我想通过给它一个Store_ID将BookStore表中的每个Book链接到StoreTable。 That Store_Id will be the primary key in StoreTable.I am having trouble with the syntax of the foreign key.I also checked http://www.sqlite.org/foreignkeys.html for reference but that didn't provide me with enough clarification.I would be much obliged if anyone helps me. 该Store_Id将是StoreTable中的主键。我在使用外键的语法时遇到了麻烦。我还检查了http://www.sqlite.org/foreignkeys.html以获得参考,但这没有为我提供足够的说明如果有人帮助我,我将非常有义务。

public class DatabaseHelper  extends SQLiteOpenHelper{


//Name of databases
public static final String DATABASE_NAME = "Library.db";
//Version of database
public static final int  DATABASE_VERSION = 1;

//Table of Stores
public static final String STORE_TABLE = "Store_Table";
public static final String STORE_ID = "Store_ID";
public static final String STORE_NAME = "Store_name";
public static final String STORE_Address = "Store_Address";
public static final String STORE_LAT = "lat";
public static final String STORE_LNG = "lng";

//Table of Books
public static final String BOOKS_TABLE = "Books_Table";
public static final String BOOK_ID = "Book_ID";
public static final String BOOK_NAME = "Book_name";
public static final String BOOK_AUTHOR = "Book_Author";
public static final String BOOKStore = "BookStore_ID";

//Creating Stores Table
private static final String SQL_CREATE_TABLE_STORE = "CREATE TABLE " + STORE_TABLE + "("
        + STORE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + STORE_NAME + " TEXT NOT NULL, "
        + STORE_Address + " TEXT NOT NULL, "
        + STORE_LAT + " TEXT NOT NULL, "
        + STORE_LNG + " TEXT NOT NULL"
        +");";
//Creating Books Table
private static final String SQL_CREATE_TABLE_BOOKS = "CREATE TABLE " + BOOKS_TABLE + "("
        + BOOK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + BOOK_NAME + " TEXT NOT NULL, "
        + BOOK_AUTHOR + " TEXT NOT NULL, "
 //How to relate BookStore with Store_ID here?
        +FOREIGN KEY(BOOKStore) REFERENCES STORE_TABLE(STORE_ID));
        +");";



public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

    sqLiteDatabase.execSQL(SQL_CREATE_TABLE_STORE);
    sqLiteDatabase.execSQL(SQL_CREATE_TABLE_BOOKS);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    //Clear all data
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + STORE_TABLE);
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + BOOKS_TABLE);

    //RECREAT THE TABLES
    onCreate(sqLiteDatabase);

}
}

I see multiple problems with the CREATE TABLE statement for the Books_Table . 我看到Books_TableCREATE TABLE语句存在多个问题。 First, if you want to designate a column in a table as a foreign key, that column first has to exist. 首先,如果要将表中的列指定为外键,则该列必须首先存在。 You were referring to the BookStore_ID column when defining your foreign key, but you never actually defined this column. 在定义外键时,您指的是BookStore_ID列,但从未实际定义此列。 Second, if you want to mark a column as a foreign key, there is a specific syntax. 其次,如果要将列标记为外键,则有特定的语法。 See below for details. 有关详情,请参见下文。

CREATE TABLE Books_Table (
    Book_ID INTEGER PRIMARY KEY AUTOINCREMENT,
    Book_name TEXT NOT NULL,
    Book_Author TEXT NOT NULL,
    BookStore_ID INTEGER,
    CONSTRAINT fk_bookstore FOREIGN KEY (BookStore_ID)
        REFERENCES Store_Table(Store_ID)
);

This is the correct answer: 这是正确的答案:

private static final String SQL_CREATE_TABLE_BOOKS = "CREATE TABLE " + BOOKS_TABLE + "("
        + BOOK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + BOOK_NAME + " TEXT NOT NULL, "
        + BOOK_AUTHOR + " TEXT NOT NULL, "
 //How to relate BookStore with Store_ID here?
        + " FOREIGN KEY ("+ BOOKStore +") REFERENCES "+STORE_TABLE+"("+STORE_ID+"));";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM