有没有办法在数据库中有“指向行的指针”?
例如,我有 X 个产品行,所有这些行都代表不同的产品,但许多具有相同的字段值,只是它们的“id”和“color_id”不同。
我想只复制行,但这可能容易出错,而且必须对多行进行小的更改,这又是错误的。
问题:有没有办法把一些行填满,然后用一个特殊的值“指向”某些字段值?
例如:
id | field1 | field2 | field3 | color_id ----------------------------------------------- 1 | value1 | value2 | value3 | blue 2 | point[1] | point[1] | point[1] | red (same as row 1, except id and color) 3 | point[1] | point[1] | point[1] | green (same as row 1, except id and color) 4 | valueA | valueB | valueC | orange 5 | point[4] | point[4] | point[4] | brown (same as row 4, except id and color) 6 | valueX | valueY | valueZ | pink 7 | point[6] | point[6] | point[6] | yellow (same as row 6, except id and color)
我正在使用 MySQL,但这是一个更普遍的问题。此外,如果这完全违背了数据库理论,我们将不胜感激解释为什么这是不好的。
最佳答案
这确实违背了数据库设计。查找规范化和关系代数的描述。这很糟糕,主要是因为您的评论是“复制行,但这可能容易出错,而且必须在多行上进行小的更改,这又是错误的。”
关系数据库的思想是作用于数据集,通过匹配主键和外键来查找东西,绝对不使用或根本不考虑指针。
如果您有每个产品的公共(public)数据,则创建一个产品表
create table product (
product_id int,
field1 ...,
field2 ...,
field3
)
with primary key on product_id
主表将包含字段 id、color_id 和 product_id
如果产品表看起来像
product_id | field1 | field2 | field3
-----------------------------------------------
1 | value1 | value2 | value3
2 | valueA | valueB | valueC
3 | valueX | valueY | valueZ
主表看起来像
id | product_id | color_id
--------------------------------
1 | 1 | blue
2 | 1 | red
3 | 1 | green
4 | 2 | orange
5 | 2 | brown
6 | 3 | pink
7 | 3 | yellow
关于mysql - 数据库 "pointers"到行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1546147/