waheed abbax
waheed abbax

Reputation: 13

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. 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.

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);

}
}

Upvotes: 0

Views: 1024

Answers (2)

César Cuesta
César Cuesta

Reputation: 16

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+"));";

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522151

I see multiple problems with the CREATE TABLE statement for the Books_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. 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)
);

Upvotes: 1

Related Questions