仅允许将 3 行添加到特定值的表中

Allow only 3 rows to be added to a table for a specific value(仅允许将 3 行添加到特定值的表中)
本文介绍了仅允许将 3 行添加到特定值的表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手头有一个问题,我需要将分配给经理的项目数量限制为 3 个.表格是:

I have a question in hand where i need to restrict the number of projects assigned to a manager to only 3. The tables are:

Manager:
Manager_employee_id(PK)
Manager_Bonus

Project:
project_number(PK)
Project_cost
Project_manager_employee_id(FK)

谁能建议采取什么方法来实现这一点?

Can anyone suggest what approach to take to implement this?

推荐答案

如何实现对 0,3 的限制?"

"How do I implement the restrict to 0,3?"

这需要一个断言,它在 SQL 标准中定义,但在 Oracle 中没有实现.(虽然有引入它们的举措).

This requires an assertion, which is defined in the SQL standard but not implemented in Oracle. (Although there are moves to have them introduced).

您可以做的是使用物化视图来透明地执行它.

What you can do is use a materialized view to enforce it transparently.

create materialized view project_manager
refresh on commit 
as 
select Project_manager_employee_id
        , count(*) as no_of_projects
from project
group by Project_manager_employee_id
/

魔法是:

alter table project_manager
   add constraint project_manager_limit_ck check 
       ( no_of_projects <= 3 )
/

如果经理的项目计数超过三个,此检查约束将阻止刷新物化视图,该失败将导致触发插入或更新失败.诚然,它并不优雅.

This check constraint will prevent the materialized view being refreshed if the count of projects for a manager exceeds three, which failure will cause the triggering insert or update to fail. Admittedly it's not elegant.

因为 mview 在提交时刷新(即事务性),您需要在 project 表上构建日志:

Because the mview is refreshed on commit (i.e. transactionally) you will need to build a log on project table:

create materialized view log on project

这篇关于仅允许将 3 行添加到特定值的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)