简体   繁体   English

如何组织和管理数据库中的“嵌套”表?

[英]How to organize and manage 'nested' tables within a database?

I've just started exploring SQL databases, but I've run into an issue with how I store 'compound' structures in an existing table (if that's even the right way to go about it). 我刚刚开始探索SQL数据库,但是遇到了如何在现有表中存储“复合”结构的问题(如果这是正确的解决方法)。 For example, let's say that I have a database table with rows of users , where each user has a Unique ID , a hashed password , an email address , a phone number , etc. 例如,假设我有一个数据库表,其中包含用户行,其中每个用户都有唯一ID ,哈希密码电子邮件地址电话号码等。

Simple enough. 很简单。 But, then I want to allow each user to create and store an array of posts . 但是,然后,我想允许每个用户创建并存储一系列帖子 Each post would have a post id , content , date , and various other metadata. 每个帖子都有一个帖子idcontentdate和其他各种元数据。 If this was C++, I would probably have an array/vector of Posts as a member of the User class, and than I'd store an array/vector of User objects somewhere. 如果这是C ++,则可能将Posts的数组/向量作为User类的成员,然后将User对象的数组/向量存储在某个地方。 Is it possible to store a table within a table in SQL, so that each user has access to their own individual table of posts? 是否可以在SQL表格中存储表格,以便每个用户都可以访问自己的个人帖子表格?

Or, would it be better to create two separate tables (a users table, and a posts table), using some common element (like user ID or user name) to retrieve user-specific data from the posts table, and vice-versa? 还是最好使用一些公共元素(例如用户ID或用户名)来创建两个单独的表(一个用户表和一个帖子表),以从帖子表中检索用户特定的数据,反之亦然?

I'm trying to understand how to implement a complex database that might be able to manage a large number of users, with user-specific sets of data like posts, messages, etc. So what might be a good approach to take going forward? 我试图了解如何实现一个复杂的数据库,该数据库可以管理大量用户,并具有特定于用户的数据集(如帖子,消息等)。那么,什么是继续前进的好方法?

As you already mentioned, in relational data model, you can define two tables like below: 如前所述,在关系数据模型中,您可以定义两个表,如下所示:

table 1 : Users 表1: 用户

user_id      user_name
-----------  ------------------
1            'Tom'
2            'John'

table 2 : Posts 表2: 职位

post_id    user_id       content                post_date          
--------   ----------    -------------------    ---------------------
1          1             'Hello, I am Tom.'     2014-04-02 14:14
2          1             'good bye'             2014-04-02 20:10
3          2             'I am John'            2014-04-02 22:22

You can read an introductory article here: 您可以在此处阅读介绍性文章:

Relational_model: http://en.wikipedia.org/wiki/Relational_model Relational_model: http : //en.wikipedia.org/wiki/Relational_model

Hope this helps. 希望这可以帮助。

You don't store table within table. 您不在表内存储表。 You can store data in multiple tables and assign primary key for one table and foreign key for another table. 您可以将数据存储在多个表中,并为一个表分配主键,为另一个表分配外键。
Read about Primary key, Foreign key and Relational Model. 阅读有关主键,外键关系模型的信息。
Once your these concepts are cleared read about Database Normalization 清除这些概念后,请阅读有关数据库规范化的信息

You don't store tables within tables. 您不在表中存储表。 As your third paragraph suggests, the strategy is to use some common key to "relate" table rows to each other. 正如您的第三段所建议的那样,该策略是使用一些公用键将表行彼此“关联”。

The "unique ID" you describe is usually called a "primary key". 您描述的“唯一ID”通常称为“主键”。 You might have a table of users with a primary key that auto-increments each time you add a record. 您可能有一个带有主键的用户表,该表在每次添加记录时都会自动增加。 A function would be available to you so that after inserting, you could determine what the primary key is of the record you just added, so that you can add records to other tables that refer to the primary key of the users table. 您将可以使用一个函数,以便在插入后可以确定刚添加的记录的主键是什么,以便可以将记录添加到引用用户表的主键的其他表中。

You should probably read about Database normalization ant the relational model , specifically about the differences between Normal Forms . 您可能应该阅读有关数据库规范化关系模型的信息 ,特别是有关法线形式之间的差异的信息。

With regard to selection of a field to relate posts to users, I suggest you don't use the username, and instead use some internal reference that isn't visible to the users. 关于选择将帖子与用户相关的字段,我建议您不要使用用户名,而应使用一些用户不可见的内部引用。 While your application might not allow it now, if you wanted to offer users the opportunity to change their username, tying internal database structure to something based on user input would only cause problems in the future. 尽管您的应用程序现在可能不允许使用,但如果您想为用户提供更改其用户名的机会,则将内部数据库结构与基于用户输入的内容联系在一起只会在将来引起问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM