设置表来存储每条记录的可变数量的字段?

发布于 2024-12-12 07:41:41 字数 156 浏览 0 评论 0原文

如果我不知道每条记录要填充的字段数量,我应该如何设置我的数据库/表?

例如,如果我有一个网络表单,允许用户输入他拥有的所有汽车,并且我不想将他限制在某个数量,那么我将如何将其存储在数据库端?

上述问题扩展到类似的情况,例如存储用户的订单(每个订单的商品数量可变)等。

How should I set up my database / table, if I do not know the number of fields I would populate per record?

For example, if I have a web form that allows a user to enter all the cars he owns, and I don't want to limit him to a certain number, how would I store this in the database end?

The above problem extends to similar situations such as storing a user's order (variable number of items per order) etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

人生戏 2024-12-19 07:41:41

相反,在关系数据库管理系统 (RDBMS) 中,您在从属表中创建子记录,将子实体(汽车)与父实体(用户)相关联。有一个称为数据库规范化的概念,其目标是每个表包含单一类型实体的数据。

因此,您有一个包含用户信息的 user 表:

user_id | user_name | email             | ...
  1234  | User1     | [email protected] | ...
  2356  | User2     | [email protected] | ...

然后另一个表用于存储用户每辆车的信息:

user_car_id | user_id | car_label | make      | model | ...
          1 |   1234  | MyCar     | Ford      | 2011  | ...
          2 |   2356  | A Car     | Chevrolet | 2010  | ...
          3 |   1234  | MyOtherCar| BMW       | 2000  | ...

因此,不要将汽车的信息存储在 user 中> 表,您有一个表,用于通过 user_id 列存储与每个用户相关的汽车 (user_car) 信息。这是一对多关系的示例,其中一个用户可以拥有许多相关的汽车。

In Relational Database Management Systems (RDBMS) instead you create child records in a dependent table that relate child entities (cars) with parent entities (users). There is a concept known as database normalization, and the objective is that each table contains data for a single type of entity.

So you have a user table with the user information:

user_id | user_name | email             | ...
  1234  | User1     | [email protected] | ...
  2356  | User2     | [email protected] | ...

Then another table for storing the information of each car of a user:

user_car_id | user_id | car_label | make      | model | ...
          1 |   1234  | MyCar     | Ford      | 2011  | ...
          2 |   2356  | A Car     | Chevrolet | 2010  | ...
          3 |   1234  | MyOtherCar| BMW       | 2000  | ...

So instead of storing the info of the cars in the user table, you have a table for storing car (user_car) information related to each user by way of the user_id column. This is an example of a one-to-many relationship, in which one user can have many related cars.

面如桃花 2024-12-19 07:41:41

这是一个完整的主题:数据库规范化。

简短的回答是你制作了不止一张桌子。

在您的示例中,您将有人员表、汽车表和将人员与汽车联系起来的第三个表

this is an entire topic: database normalization.

the short answer is you make more than one table.

in your example you would have person table, a car table, and a third that linked person to the car

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