设置表来存储每条记录的可变数量的字段?
如果我不知道每条记录要填充的字段数量,我应该如何设置我的数据库/表?
例如,如果我有一个网络表单,允许用户输入他拥有的所有汽车,并且我不想将他限制在某个数量,那么我将如何将其存储在数据库端?
上述问题扩展到类似的情况,例如存储用户的订单(每个订单的商品数量可变)等。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相反,在关系数据库管理系统 (RDBMS) 中,您在从属表中创建子记录,将子实体(汽车)与父实体(用户)相关联。有一个称为数据库规范化的概念,其目标是每个表包含单一类型实体的数据。
因此,您有一个包含用户信息的
user
表:然后另一个表用于存储用户每辆车的信息:
因此,不要将汽车的信息存储在
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:Then another table for storing the information of each car of a user:
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 theuser_id
column. This is an example of a one-to-many relationship, in which one user can have many related cars.这是一个完整的主题:数据库规范化。
简短的回答是你制作了不止一张桌子。
在您的示例中,您将有人员表、汽车表和将人员与汽车联系起来的第三个表
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