Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 我应该如何设计一个表,根据行的类型,一行可以有不同的列?_Sql_Database_Postgresql - Fatal编程技术网

Sql 我应该如何设计一个表,根据行的类型,一行可以有不同的列?

Sql 我应该如何设计一个表,根据行的类型,一行可以有不同的列?,sql,database,postgresql,Sql,Database,Postgresql,我计划使用RedditAPI并将保存的帖子存储在数据库中。保存可以分为两种类型-评论或帖子,两种类型都有几个公共列-作者、分数、subreddit等。以及每个类别特有的几个列: 注释-正文、注释id、父项id等。 posts-selftext、链接url、is视频等。 我决定将这两个类别分为各自的表-Comments表和Posts表。但我不知道如何将这些表链接到主表“saves” 我目前的解决方案是为save类型设置一个列kind。comment\u id和post\u id将save链接到自己

我计划使用RedditAPI并将保存的帖子存储在数据库中。保存可以分为两种类型-评论或帖子,两种类型都有几个公共列-
作者、分数、subreddit等。
以及每个类别特有的几个列:

注释-
正文、注释id、父项id等。

posts-
selftext、链接url、is视频等。

我决定将这两个类别分为各自的表-
Comments表
Posts表
。但我不知道如何将这些表链接到主表“saves”

我目前的解决方案是为
save
类型设置一个列
kind
comment\u id
post\u id
save
链接到自己的表中。然而,这感觉像是一个混乱的解决方案,有点麻烦。
保存
既可以有
注释id
也可以有
链接id
(但不能两者都有或两者都没有),我还必须管理此约束

保存表格:

+----+---------+-------+---------------------------------------------+---------+------------+---------+
| ID |  Kind   | title |                  post_url                   | author  | comment_id | post_id |
+----+---------+-------+---------------------------------------------+---------+------------+---------+
|  1 | comment | abc   | https://redd.i/redditpostid/redditcommentid | FusionX | 1          | NULL    |
|  2 | post    | xyz   | https://redd.i/redditpostid                 | XnoisuF | NULL       | 1       |
+----+---------+-------+---------------------------------------------+---------+------------+---------+
邮政表格:

+----+---------+-------------------------------------------+-----------------------+--------------+--------------+
| ID | is_self |                 selftext                  |       post_url        | num_comments |  thumbnail   |
+----+---------+-------------------------------------------+-----------------------+--------------+--------------+
|  1 | no      | NULL                                      | i.imgur.com/xyz.jpg   |         1020 | someimageurl |
|  2 | yes     | "some random selftext of variable length" | redd.it/redditpostid/ |           10 |              |
+----+---------+-------------------------------------------+-----------------------+--------------+--------------+
注释表:

+----+---------------------------------+---------------------+--------------------+
| ID |            body_html            |  reddit_comment_id  |  reddit_parent_id  |
+----+---------------------------------+---------------------+--------------------+
|  1 | comment text of variable length | <reddit comment id> | <reddit parent id> |
+----+---------------------------------+---------------------+--------------------+
+----+---------------------------------+---------------------+--------------------+
|ID | body | html | reddit | comment | ID | reddit | parent | ID|
+----+---------------------------------+---------------------+--------------------+
|1 |可变长度的注释文本| ||
+----+---------------------------------+---------------------+--------------------+
(reddit ID与我的表自己的ID不同,仅在reddit的末尾相关)


有没有更好的方法来设计这个数据库?

我认为您应该将关系的拥有方移到其他两个表中。
因此,与其在
saves
表中设置
comment\u id
post\u id
列,不如在
post
表和
comment
表中设置一个
saves\u id
列。

saves必须是表还是可以是视图?既然你提到了,我不确定,我对数据库还是很陌生。我想按照序列化ID的顺序维护保存(即post1、post2、coment1、post3、comment2)的顺序。视图会这样吗?为什么不使用继承?这对你来说似乎很合适。