我发现自己的Person表中的一些属性需要保存多个值/选项,这不是一个好的SQL实践,所以我创建了第二个表,如下所示:
之前:
Person table
-ID (ex. 101)
-Name (ex. John)
-Accessories (ex. Scarf, Mask, Headband, etc..) - One person can have a combination of this
Run Code Online (Sandbox Code Playgroud)
后:
Person Table
-ID
-Name
PersonDetails Table
-PersonID (FK to Person table)
-Attribute type
-Attribute value
Run Code Online (Sandbox Code Playgroud)
一个例子:
Person:
ID:13; Name: John Snow
PersonDetails:
PersonID: 13; Attribute type: Accessories; Attribute value: Scarf
PersonID: 13; Attribute type: Accessories; Attribute value: Mask
Run Code Online (Sandbox Code Playgroud)
你可以看到ID为13的人有围巾和面具.
这是一个好习惯吗?还有哪些方法可以最有效地完成这项工作?
此外,如果有更新,13人没有围巾和面具但只有眼镜?(分别删除2并插入一个新的?这意味着只有一个修改请求的3个查询)