如何在SQLITE3中创建一对多?
如何在SQLITE3中创建一对多? 我有 2 个表:
Mans:
_id name
1 antony
2 fred
我
point
_id date point
1 23 77
24 99
2 25 78
5 0
不知道 SQL 语法,请帮助我。
How to create one to many in SQLITE3?
I have 2 tables:
Mans:
_id name
1 antony
2 fred
and
point
_id date point
1 23 77
24 99
2 25 78
5 0
I don't know SQL syntax, help me , please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照 iamkrillin 的说法:
这是一个现实世界的例子。假设您有一些人向您推荐业务:您的员工、您的朋友、您做广告的当地企业等。进来的客户称为“推荐”业务。每个人仅算作一次推荐,但一个推荐人可能会推荐很多次(例如,一名员工可能推荐 20 个新客户;该员工是您的推荐人,并且该员工已进行 20 次推荐)。因此,您有 1 个推荐者和 20 个推荐者(一对多):
现在,可能会有多个人推荐一个推荐者,但我认为仅补偿单个推荐者是标准的商业惯例。因此,您永远不需要列出两个引用者。这始终是一对一或一对多的关系;因此,您应该将其设为一对多表。我对 CASCADE 的东西不是很精通,但我会尝试弄清楚它是如何适合的。
乍一看,
ON UPDATE CASCADE ON DELETE CASCADE
似乎不属于我的答案,因为删除最后一个引用不应删除引用者。查看 另一个示例:
如果您从
hard_candy
表中删除硬糖,那么您也会从all_candy
表中删除它,因为硬糖是一种糖果,如果糖果的类型发生了变化(例如,变成了停产的糖果),那么无论如何都需要执行新的 INSERT 命令。我在sqlite3中运行了ON UPDATE CASCADE和ON UPDATE DELETE的测试用例,似乎没有效果。也许它们不适用于 sqlite3 的默认数据库引擎,但 SQLite 官方网站列出了该功能: sqlite.org 提供的一个描述性很强、易于理解的 ON UPDATE CASCADE 示例。读一读,看看你的想法。
这是我用于测试用例的架构:
对任一表的 id-2 描述字段进行各种更新。
Going by what iamkrillin wrote:
Here is a real-world example. Let's say you have some people who refer business to you: your staff, your friends, local businesses where you do advertising, etc. Customers who come in are called 'referal' business. Each person only counts as one referal, but a referer may refer many referals (for example, an employee may refer 20 new customers; the employee is your referer, and the employee has made 20 referals). So, you have 1 referer and 20 referals (one-to-many):
Now, it is possible that more than one person refers a referal, but I think that it is standard business practice to only compensate a single referer. So, you never need to list two referers. This will always be a 1-to-1 or a 1-to-many relationship; therefore, you should make it a 1-to-many table. I'm not very proficient in the CASCADE stuff, but I'll try to figure out how that fits.
At first glance, it appears
ON UPDATE CASCADE ON DELETE CASCADE
does not belong in my answer because removing the last referal should not remove the referer.Looking at a different example:
If you delete a hard candy from the
hard_candy
table, then you are also deleting it from theall_candy
table because a hard candy is a type of candy, and if the type of candy has changed (for example, to discontinued candies), then a new INSERT command needs to be executed, anyway.I ran a test case for ON UPDATE CASCADE and ON UPDATE DELETE in sqlite3, and it seems it has no effect. Perhaps they do not work with the default db engine for sqlite3, but the functionality IS listed at the official SQLite website: a very descriptive, easy-to-follow example of ON UPDATE CASCADE by sqlite.org. Have a read and see what you think.
This is the schema I used for my test case:
Ran various updates on either table's id-2 description field.
其语法如下......
仅适用于 v3.6.1+
以下是文档 http:// sqlite.org/foreignkeys.html
The syntax for this is as follows....
only works on v3.6.1+
Here are the docs http://sqlite.org/foreignkeys.html