[英]Parent and child tables MySQL
I was trying to figure out the difference (if any) between two mutually exclusive ways to design a DB. 我试图找出设计数据库的两种互斥方式之间的区别(如果有)。
Suppose we have a table of users consisting of userID
's, what would be considered 'best practice' in terms of storing user data? 假设我们有一个由
userID
组成的userID
表,就存储用户数据而言,什么被视为“最佳实践”?
Our Table: 我们的桌子:
userID PK.
I've seen both of the following being used and it feels that the last one is less useful: 我已经看到了以下两种用法,并且觉得最后一个用处不大:
meta data stored: 元数据存储:
Method 1: 方法1:
meta_id(pk), userID(fk), attribute1, attribute 2 ... attribute n
Method 2: 方法2:
meta_id(pk), userID(fk),attribute_name,attribute_value
So instead of having one single meta row for each user, we end up with multiple ones. 因此,我们没有为每个用户创建一个单独的元行,而最终得到多个。 This is the design WordPress uses.
这是WordPress使用的设计。
My Questions: 我的问题:
Well both of the question does not have a sharp answer. 那么,这两个问题都没有一个敏锐的答案。 Both are different techniques, both are different designs, both needs different query styles.
两者都是不同的技术,都是不同的设计,都需要不同的查询样式。
For use case, i think pretty simple for this question. 对于用例,我认为这个问题非常简单。
If your attribute types and number of attrs. 如果您的属性类型和属性数量。 are constant (or close to constant) method1 should be used.
如果是常数(或接近常数),则应使用method1。 More specifically, If you can set a logical limit to the number of attrs.
更具体地说,如果可以将逻辑限制设置为attrs的数量。 then you have an "n" in method1.
那么您在method1中有一个“ n”。 (since field names are not flex. attr names should be same between objects that you store just like html)
(因为字段名称不是flex。在存储的对象之间,attr名称应该相同,就像html一样)
If you have a random number of attr. 如果您有随机数的attr。 & random number of attr.
&attr的随机数。 names.
名。 Then method 2 should be used.
然后应使用方法2。 Cause it can adjust to any permutation of (#ATRname , $ATRvalue and #ATRS).
因为它可以调整为(#ATRname,$ ATRvalue和#ATRS)的任何排列。
If the correct design used for a DB. 如果用于DB的设计正确。 performance will not be a problem.
性能不会有问题。
XML objects stored in db. XML对象存储在db中。
HTML1.0 objects stored in db. HTML1.0对象存储在db中。
Yes both are former designs for industrial DBs. 是的,两者都是工业DB的先前设计。 for different purposes.
用于不同的目的。
I assumed that xml objects has random number of subobject with random attr. 我假设xml对象具有随机属性的子对象随机数。 names .
名字。 Unlike html.
与html不同。 It just have limited number of attrs.
它的attrs数量有限。 in real-life example (because of html is an code representation of visual boxes) it is not be used for complex structures.
在实际示例中(由于html是可视框的代码表示),它不用于复杂的结构。
In addition 此外
The most professional design use both of them combined. 最专业的设计将两者结合使用。 For example.
例如。 2.table looks like a MANY TO MANY REL.
2.table看起来像一对多关系。 between TABLE1.1xTABLE1.2...xTABLE1.N.
在TABLE1.1xTABLE1.2 ... xTABLE1.N之间。
another thing is. 另一件事是。 i do not feel like meta-id and former-id used together.
我不觉得meta-id和以前的id一起使用。 one is fk and one is pk.
一个是fk,一个是pk。 which is weird because pk means it can be used for relations.
这很奇怪,因为pk意味着它可以用于关系。 fk means, it should be unique (pro stuff not necessarily) === another mutant pk.
fk表示,它应该是唯一的(不一定是专业的东西)===另一个突变体pk。 It probably helps to backend operations or updates , merging etc.
它可能有助于后端操作或更新,合并等。
I hope it helped. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.