我正在尝试找到一种最佳方法来根据其他表在 mysql 表中自动添加(并可能删除)列。目前我通过 cron (bash、php 等)使用脚本来检查表 A,然后更新表 B。我想知道是否有一种方法可以根据表的更新自动使用 mysql 脚本/语句在 mysql 内部执行此操作?
架构作为示例仅用于澄清问题
表 A:这是将手动更新的表
Name : cloud_environments
cloud_env_id : id
cloud_env_name : name (ie dev1)
<DATA>
id:1, name:dev_env_1
id:2, name:staging_env_1
id:3, name:production_env_1
表 B :当在表 A 中放置新值时应自动更新,每个 cloud_env_id 在表 B 中应具有三个字段,
cloud_env_id
status for environment
version for environment
所以初始表格看起来像
name : cloud_env_details
cloud_env_detail_id : id
cloud_env_detail_name_id1 : dev1 //name:dev_env_1
cloud_env_detail_status_id1 : active //status of dev_env_1
cloud_env_detail_version_id1 : 0.3.3 //version dev_env_1
cloud_env_detail_name_id2 : sta1 //name:staging_env_1
cloud_env_detail_status_id2 : active //status of staging_env_1
cloud_env_detail_version_id2 : 0.2.3 //version staging_env_1
cloud_env_detail_name_id3 : prd1 //name:production_env_1
cloud_env_detail_status_id3 : active //status of production_env_1
cloud_env_detail_version_id3 : 0.1.1 //version production_env_1
现在如果我向表A添加一个新环境,例如
id4, name dev_env_2
我想自动将额外的模板列添加到表 B
cloud_env_detail_name_id4 : dev2 //name:dev_env_2
cloud_env_detail_status_id4 : NULL //status of dev_env_2
cloud_env_detail_version_id4 : NULL //version dev_env_2
最佳答案
Triggers
用于您正在谈论的内容。你可以使用它。
关于php - 基于其他表自动向mysql表添加列的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32835466/