关系表在sqlite上的android [英] relationship tables in sqlite on android

查看:83
本文介绍了关系表在sqlite上的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以帮助我了解sqlite中两个表之间的关系。



我插入,删除和更新步骤,但是我必须支持两个表之间的关系。我想

所有的代码步骤,之前做的都会改变我是对吗?
你有任何链接或示例来解释关系之后的表关系和任何活动吗?

解决方案

两个表格,您可以使用外键。外键是关系表中与另一个表的候选键匹配的字段。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆