我有一个Oracle数据库,我有几个表格,我会尝试尽可能地说明我的创造性思维:
公司
{PK = COMPANY_ID}
供应商
{PK = SUPPLIER_ID}
COMPANY_SUPPLIERS
{PK = SUPPLIER_ID,COMPANY_ID}
{FK = COMPANIES.COMPANY_ID} {FK = SUPPLIERS.SUPPLIER_ID}
COMPANY_USERS
{PK = COMPANY_USERS_ID}
{FK = COMPANIES.COMPANY_ID}
COMPANY_USER_SUPPLIERS
{PK = COMPANY_USER_USER,COMPANY_SUPPLIER_ID}
{FK = COMPANY_USERS.COMPANY_USERS_ID} {FK = SUPPLIERS.SUPPLIER_ID}
其中PK =主键,FK = 5个表之一的外键。
让我们说我们有规则
select count(*)
from COMPANIES C,
SUPPLIERS S,
COMPANY_SUPPLIERS CS,
COMPANY_USERS CU,
COMPANY_USER_SUPPLIERS CUS
where
COMPANY_USER_SUPPLIERS.COMPANY_USERS_ID = COMPANY_USERS.COMPANY_USERS_ID
and
COMPANY_USERS.COMPANY_ID = COMPANIES.COMPANY_ID
and
COMPANIES.COMPANY_ID = COMPANY_SUPPLIERS.COMPANY_ID
and
SUPPLIERS .SUPPLIER_ID = COMPANY_SUPPLIERS.SUPPLIER_ID
and
COMPANY_SUPPLIERS.SUPPLIER_ID = COMPANY_USER_SUPPLIERS.SUPPLIER_ID
应该给出相同的结果
select count(*) from COMPANY_USER_SUPPLIERS;
是否可以通过约束强制执行此操作,如果是,我该怎么办?数据库设计得很差吗?我应该通过触发器强制执行此操作吗?
答案 0 :(得分:0)
无法保证COMPANY_USER_SUPPLIERS中的用户来自与COMPANY_SUPPLIERS中该供应商相关的公司。