关系表在sqlite上的android [英] relationship tables in sqlite on android
问题描述
你可以帮助我了解sqlite中两个表之间的关系。
我插入,删除和更新步骤,但是我必须支持两个表之间的关系。我想
所有的代码步骤,之前做的都会改变我是对吗?
你有任何链接或示例来解释关系之后的表关系和任何活动吗?
两个表格,您可以使用外键。外键是关系表中与另一个表的候选键匹配的字段。例如,假设我们有两个表,一个包含所有客户数据的CUSTOMER表,一个包含所有客户订单的ORDER表。这里的意图是所有订单必须与已经在CUSTOMER表中的客户相关联。为此,我们将在ORDER表中放置一个外键,并将其与CUSTOMER表的主键相关。
在 SQLite外键约束可以按以下方式添加:
edit ::
你可以像下面这样设计item_order表:
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)
);
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?
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)
);
这篇关于关系表在sqlite上的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!