简体   繁体   English

引用2个不同表的列

[英]referencing columns of 2 different tables

Following schema: 以下架构:

archivebox (id, mediatype_id)
mediatype (id, name)
archivebox_records (id, archiveboxId, recordId)
picture (id,name)
print (id,name)

recordId should be referenced whether to pictureId or printId. 应该引用recordId是pictureId还是printId。 How is it possible to solve this problem? 怎么可能解决这个问题? I work with mysql. 我使用mysql。

Thanks! 谢谢!

It's always best to be explicit in your designs. 在您的设计中明确表达始终是最好的。 This would involve creating two tables archive_pictures and archive_prints with foreign key relationships to the respective tables. 这将涉及创建两个表archive_picturesarchive_prints以及与各个表的外键关系。

If you really don't want to go down that route, try adding some sort of record indicator to the archive_records table, eg 如果你真的不想沿着那条路走下去,可以尝试在archive_records表中添加某种记录指示器,例如

ALTER TABLE `archive_records` ADD `record_type` ENUM('picture', 'print') NOT NULL;

You can then create queries based on this indicator 然后,您可以根据此指标创建查询

SELECT p.name FROM picture p
INNER JOIN archive_records ar
    ON ar.record_type = 'picture' AND p.id = ar.recordId

Another clean solution is to merge picture and print into 另一个干净的解决方案是合并pictureprint

medium( id, name, type)

where type can be "print" or "picture" or when the datamodel grows the foreign key referencing a table containing the allowed values. 其中type可以是"print""picture"或者当datamodel增长引用包含允许值的表的外键时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM