Home » Android » database – relationship tables in sqlite on android

database – relationship tables in sqlite on android

Posted by: admin June 15, 2020 Leave a comment

Questions:

Can you help me about relationship between two tables in sqlite.

I do insert,delete and update steps but I have to support relationship between two tables now. I guess
all of code steps which are done before will be changed Am I right?
Have you got any link or example which explains tables relationships and any activities after relationship?

How to&Answers:

To establish relationship between two tables, you can use Foreign keys. A foreign key is a field in a relational table that matches a Candidate Key of another table.

For example, say we have two tables, a CUSTOMER table that includes all customer data, and an ORDER table that includes all customer orders. The intention here is that all orders must be associated with a customer that is already in the CUSTOMER table. To do this, we will place a foreign key in the ORDER table and have it relate to the primary key of the CUSTOMER table.

In SQLite Foreign Key Constraints can be added in following way ::

edit::
you can design item_order table like ::

CREATE TABLE customer(
         id INTEGER,
         firstName TEXT,
         middleName TEXT,
         lastName   TEXT,
         address TEXT,
         contactNum TEXT
);

 CREATE TABLE item(
        id INTEGER,
        name TEXT,
        description TEXT
 );

 CREATE TABLE order(
        id INTEGER,
        customerID INTEGER,
        date TEXT,
        FOREIGN KEY(customerId) REFERENCES customer(id)
 );

 CREATE TABLE item_order(
        id INTEGER,
        orderID INTEGER,
        itemId  INTEGER,
        quantity INTEGER,
        FOREIGN KEY(orderId) REFERENCES order(Id),
        FOREIGN KEY(itemId) REFERENCES item(Id)
 );

Answer:

Good sample
http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/

// Table Create Statements
// Todo table create statement
private static final String CREATE_TABLE_TODO = "CREATE TABLE "
        + TABLE_TODO + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TODO
        + " TEXT," + KEY_STATUS + " INTEGER," + KEY_CREATED_AT
        + " DATETIME" + ")";

// Tag table create statement
private static final String CREATE_TABLE_TAG = "CREATE TABLE " + TABLE_TAG
        + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TAG_NAME + " TEXT,"
        + KEY_CREATED_AT + " DATETIME" + ")";

// todo_tag table create statement
private static final String CREATE_TABLE_TODO_TAG = "CREATE TABLE "
        + TABLE_TODO_TAG + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
        + KEY_TODO_ID + " INTEGER," + KEY_TAG_ID + " INTEGER,"
        + KEY_CREATED_AT + " DATETIME" + ")";

SELECT * FROM todos td, tags tg, todo_tags tt WHERE tg.tag_name = ‘Watchlist’ AND tg.id = tt.tag_id AND td.id = tt.todo_id;

/*
 * getting all todos under single tag
 * */
public List<Todo> getAllToDosByTag(String tag_name) {
    List<Todo> todos = new ArrayList<Todo>();

    String selectQuery = "SELECT  * FROM " + TABLE_TODO + " td, "
            + TABLE_TAG + " tg, " + TABLE_TODO_TAG + " tt WHERE tg."
            + KEY_TAG_NAME + " = '" + tag_name + "'" + " AND tg." + KEY_ID
            + " = " + "tt." + KEY_TAG_ID + " AND td." + KEY_ID + " = "
            + "tt." + KEY_TODO_ID;

    Log.e(LOG, selectQuery);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {
            Todo td = new Todo();
            td.setId(c.getInt((c.getColumnIndex(KEY_ID))));
            td.setNote((c.getString(c.getColumnIndex(KEY_TODO))));
            td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));

            // adding to todo list
            todos.add(td);
        } while (c.moveToNext());
    }

    return todos;
}

Answer:

I guess you should read a book or web site on basic sql database programming.
You create a relationship between two tables by adding the key (or a field) of one table into the other table. But really, you should get familiar with sql first. After you have done that, you can create relationships, or you’ll find it convenient to use Ormlite.