[英]What are some solutions for altering a table's column for storing multiple values?
We have a table: 我们有一张桌子:
message (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`subject` VARCHAR(255) NOT NULL DEFAULT '',
`message` TEXT NOT NULL,
`attachment` VARCHAR(255) NULL DEFAULT NULL,
`new` TINYINT(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
Now we need to have multiple attachments . 现在我们需要有多个附件 。
Currently only one attachment link is being saved. 当前仅保存一个附件链接。 A solution might be to add another foreign key table or use comma separated values in column.
一个解决方案可能是添加另一个外键表或在列中使用逗号分隔的值 。 But we need a better solution for this which can use old system alongwith new , without much alteration.
但是我们需要一个更好的解决方案,该解决方案可以将旧系统与新系统一起使用,而无需进行太多更改。 Any suggestions?
有什么建议么?
As @BurhanKhalid says, any solution to this will require changes in your application. 正如@BurhanKhalid所说,任何对此的解决方案都将需要您的应用程序中的更改。 You should just to do it right and normalize your data with a separate
attachments
table with a foreign key. 您只需要正确执行操作,并使用带有外键的单独
attachments
表对数据进行规范化即可。 To support legacy app versions , leave the attachment
column in the original table alone, then create a trigger to fill it with the first attachment link that gets added to the new, normalized table. 要支持旧版应用程序 ,请保留原始表中的
attachment
列,然后创建触发器以使用添加到新的标准化表中的第一个附件链接进行填充。 Long-term, you want to fix your legacy app to use the new table and then deprecate the old column. 从长远来看,您想修复旧应用程序以使用新表,然后弃用旧列。
首先,更改表,使attachment
的类型为TEXT
,然后序列化附件(例如,作为json或xml)并将其放入attachment
列中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.