最好将json数据存储在affiliate_phone_id
字段中,还是使用单独的表来规范化它?
例如:
mysql> select * from phone;
+----+----------+-------------+-------------------------------------------------------------------------------------------+
| id | name | description | affiliate_phone_id |
+----+----------+-------------+-------------------------------------------------------------------------------------------+
| 32 | iPhone 5 | faster CPU | [{"affiliate_id":2,"affiliate_phone_id":123},{"affiliate_id":3,"affiliate_phone_id":222}] |
+----+----------+-------------+-------------------------------------------------------------------------------------------+
上述数据显示每个零售商都有自己的电话ID,例如:
公司2(affiliate_id = 2),他们的电话ID是123
或者做这样的单独表会更好:
mysql> select * from phone_affiliate;
+----+----------+--------------+--------------------+
| id | phone_id | affiliate_id | affiliate_phone_id |
+----+----------+--------------+--------------------+
| 1 | 32 | 2 | 123 |
| 2 | 32 | 3 | 222 |
+----+----------+--------------+--------------------+
还是其他更好的建议?
答案 0 :(得分:3)
规范化表格。
也许,你在phone_affiliate表中不需要“id”字段。您可以设置主键(phone_id,affiliate_id)或(phone_id,affiliate_id,affiliate_phone_id),这取决于数据之间的关系。
mysql> select * from phone_affiliate;
+----------+--------------+--------------------+
| phone_id | affiliate_id | affiliate_phone_id |
+----+----------+--------------+---------------+
| 32 | 2 | 123 |
| 32 | 3 | 222 |
+----+----------+--------------+---------------+
答案 1 :(得分:1)
只需使用规范化表,如果您必须以不同于JSON的格式访问数据,这将有所帮助。