如何在SQLITE3中创建一对多?

发布于 2024-12-06 20:47:53 字数 261 浏览 0 评论 0原文

如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚殇 2024-12-13 20:47:53

按照 iamkrillin 的说法:

CREATE TABLE (points) points_id INT
FOREIGN KEY(man_id) REFERENCES mans(PrimaryKeyField)
ON DELETE CASCADE ON UPDATE CASCADE

这是一个现实世界的例子。假设您有一些人向您推荐业务:您的员工、您的朋友、您做广告的当地企业等。进来的客户称为“推荐”业务。每个人仅算作一次推荐,但一个推荐人可能会推荐很多次(例如,一名员工可能推荐 20 个新客户;该员工是您的推荐人,并且该员工已进行 20 次推荐)。因此,您有 1 个推荐者和 20 个推荐者(一对多):

CREATE TABLE referal(                                         
  referal_id INTEGER UNIQUE NOT NULL PRIMARY KEY,           //A customer can only be 1 referal.
  referal_method TEXT,                                      //How were they refered?  By phone?
  referer_id INTEGER ,                                      //Who refered them?
  FOREIGN KEY(referer_id) REFERENCES referer(referer_id));  //Trace more about referer.

现在,可能会有多个人推荐一个推荐者,但我认为仅补偿单个推荐者是标准的商业惯例。因此,您永远不需要列出两个引用者。这始终是一对一或一对多的关系;因此,您应该将其设为一对多表。我对 CASCADE 的东西不是很精通,但我会尝试弄清楚它是如何适合的。

乍一看,ON UPDATE CASCADE ON DELETE CASCADE 似乎不属于我的答案,因为删除最后一个引用不应删除引用者。

查看 另一个示例

CREATE TABLE all_candy 
   (candy_num SERIAL PRIMARY KEY,
    candy_maker CHAR(25));

CREATE TABLE hard_candy 
   (candy_num INT, 
    candy_flavor CHAR(20),
    FOREIGN KEY (candy_num) REFERENCES all_candy
    ON DELETE CASCADE)

如果您从 hard_candy 表中删除硬糖,那么您也会从 all_candy 表中删除它,因为硬糖是一种糖果,如果糖果的类型发生了变化(例如,变成了停产的糖果),那么无论如何都需要执行新的 INSERT 命令。

我在sqlite3中运行了ON UPDATE CASCADE和ON UPDATE DELETE的测试用例,似乎没有效果。也许它们不适用于 sqlite3 的默认数据库引擎,但 SQLite 官方网站列出了该功能: sqlite.org 提供的一个描述性很强、易于理解的 ON UPDATE CASCADE 示例。读一读,看看你的想法。

这是我用于测试用例的架构:

BEGIN TRANSACTION;
CREATE TABLE candy(id integer primary key not null, name text, description text);
INSERT INTO candy VALUES(1,'Laffy Taffy', 'Delicious, soft candy.');
INSERT INTO candy VALUES(2,'Pop Rocks', 'A candy that explodes in your mouth.');
COMMIT;

BEGIN TRANSACTION;
CREATE TABLE hard_candy(id integer primary key not null, name text, description text, foreign key(id,name,description) references hard_candy ON DELETE CASCADE ON UPDATE CASCADE);
INSERT INTO hard_candy VALUES(2,'Pop Rocks', 'A candy that explodes in your mouth.');
COMMIT;

对任一表的 id-2 描述字段进行各种更新。

Going by what iamkrillin wrote:

CREATE TABLE (points) points_id INT
FOREIGN KEY(man_id) REFERENCES mans(PrimaryKeyField)
ON DELETE CASCADE ON UPDATE CASCADE

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):

CREATE TABLE referal(                                         
  referal_id INTEGER UNIQUE NOT NULL PRIMARY KEY,           //A customer can only be 1 referal.
  referal_method TEXT,                                      //How were they refered?  By phone?
  referer_id INTEGER ,                                      //Who refered them?
  FOREIGN KEY(referer_id) REFERENCES referer(referer_id));  //Trace more about referer.

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:

CREATE TABLE all_candy 
   (candy_num SERIAL PRIMARY KEY,
    candy_maker CHAR(25));

CREATE TABLE hard_candy 
   (candy_num INT, 
    candy_flavor CHAR(20),
    FOREIGN KEY (candy_num) REFERENCES all_candy
    ON DELETE CASCADE)

If you delete a hard candy from the hard_candy table, then you are also deleting it from the all_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:

BEGIN TRANSACTION;
CREATE TABLE candy(id integer primary key not null, name text, description text);
INSERT INTO candy VALUES(1,'Laffy Taffy', 'Delicious, soft candy.');
INSERT INTO candy VALUES(2,'Pop Rocks', 'A candy that explodes in your mouth.');
COMMIT;

BEGIN TRANSACTION;
CREATE TABLE hard_candy(id integer primary key not null, name text, description text, foreign key(id,name,description) references hard_candy ON DELETE CASCADE ON UPDATE CASCADE);
INSERT INTO hard_candy VALUES(2,'Pop Rocks', 'A candy that explodes in your mouth.');
COMMIT;

Ran various updates on either table's id-2 description field.

眼眸印温柔 2024-12-13 20:47:53

其语法如下......

CREATE TABLE (MySecondTable) Foo INT FOREIGN KEY(Foo) REFERENCES MyFirstTable(PrimaryKeyField) ON DELETE CASCASDE ON UPDATE CASCASDE

仅适用于 v3.6.1+

以下是文档 http:// sqlite.org/foreignkeys.html

The syntax for this is as follows....

CREATE TABLE (MySecondTable) Foo INT FOREIGN KEY(Foo) REFERENCES MyFirstTable(PrimaryKeyField) ON DELETE CASCASDE ON UPDATE CASCASDE

only works on v3.6.1+

Here are the docs http://sqlite.org/foreignkeys.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文