To establish a relationship between two tables, you can use Foreign keys . A foreign key is a field in a relational table that corresponds to the candidate key of another table.
For example, let's say that we have two tables, the CUSTOMER table, which includes all customer data, and the ORDER table, which includes all customer orders. It is assumed that all orders must be associated with a customer who is already in the CUSTOMER table. To do this, we put the foreign key in the ORDER table and bind it to the primary key of the CUSTOMER table.
In SQLite, External Key Constraints can be added as follows:
edit :: you can create the item_order table as ::
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) );
Eight
source share