php - 如何根据父表自动更新子表中的列值?

标签 php sql

My web app architecture for user access separated in two ways:- Manager Table (column: privilege) Employee Table (column: privilege, permission)

Where Manager is a Super-Admin account which will be used to subscribe our service based on package type(eg: basic, silver, gold).

For Employee, this acc will be created by Manager and share the same privilege of Manager by default(the subscribed service package). However, the Manager will need to set a permission level for each function in order for the Employee to be able to access the function in the web app.

The idea is, if session by manager acc, no permission needed. if session by employee, permission needed.

问题: 当 Child 表中的新行创建时,'privilege' 列的默认值应遵循与 Parent 相同的值。此外,如果父级降级权限,子列值也应随之降级。

有没有办法从数据库的角度实现这一目标?我正在使用 postgresql。 我知道这绝对可以从后端实现,但是每个 CRUD 操作都需要做很多逻辑。

最佳答案

您可以尝试使用触发器或存储过程并将逻辑放在数据库中。

关于php - 如何根据父表自动更新子表中的列值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62043396/

相关文章:

php - 如何在 Apache 上安装 PostgreSQL 驱动程序?

php - 在 MySQL 中同步两个表中的行数据的最佳方法是什么?

php - Varchar 类型的 MySql 复合唯一索引

php - 从当前数据中获取记录

php - 魔法函数 __call 函数不起作用

mysql - 在检查行是否存在的两种方法之间做出决定(更多子查询 VS 更多左连接)

mysql - 使用查询中返回的 id 向表中添加新行

mysql - 如何使用 INNER JOIN 和 ORDER BY 优化 MySQL 查询

mysql - INNER JOIN 在这里有用吗?

javascript - 在登录表单发布到 php 并由 php 验证登录详细信息后加载 div