简体   繁体   English

如何确保在SQL的另一个表中未设置外键?

[英]How to make sure that a foreign key is not set in another table in SQL?

I'm working with Oracle Database 12c Enterprise Edition. 我正在使用Oracle Database 12c企业版。

I have three tables: 我有三个表:

table producer (
  pid int primary key,
  name varchar2(50)
);

table movie (
  mid int primary key,
  pid varchar2(50),
  name varchar2(50)
);

table moviecoproducers (
  mid int,
  pid int
);

The pid in the movies table such as the pid and mid in the moviecoproducers table are referenced by an appropriate foreign key. 电影表中的pid(例如moviecoproducers表中的pid和mid)由适当的外键引用。 In addition the combination of the mid and pid values in the moviecoproducers table is unique. 另外,moviecoproducers表中的mid和pid值的组合是唯一的。

I now have to ensure that no producer is referenced as (main) producer in the movie table and as co-producer in the moviecoproducer intermediate table for the same movie. 现在,我必须确保在电影表中没有任何生产者被引用为同一电影的(主要)生产者,在moviecoproducer中间表中也没有被称为联合生产者。 I created a trigger to do that but now I'm asking myself if there would be a more eazy solution (eg a check constraint) if I would redesign my table structure. 我创建了一个触发器来做到这一点,但现在我问自己是否要重新设计表结构是否有更轻松的解决方案(例如检查约束)。

Do I only need a trigger because of a bad design because I have no idea how to do it in a different/better way. 因为设计不好,我是否只需要触发器,因为我不知道如何以不同/更好的方式进行操作。

Create a typical 3-table many-to-many structure. 创建一个典型的3表多对多结构。

table producer (
  pid int primary key,
  name varchar2(50)
);

table movie (
  mid int primary key,
  name varchar2(50)
);

table movieproducer (
  mid int,
  pid int,
  status varchar2(10)
)

status will hold " main " or " co " values. status将保留“ main ”或“ co ”值。

Now you can manage your restrictions with table constraints and no triggers. 现在,您可以使用表约束和无触发器来管理您的限制。

An added bonus: if at some time in the future you want to introduce something like " junior co-producers " you would not need to alter your schema. 另外一个好处是:如果将来您想引入“ 初级联合制作人 ”之类的东西,则无需更改架构。

This design is not fully normalized: lack of status table. 此设计未完全标准化:缺少状态表。 See if you are interested in adding it in. 查看您是否有兴趣添加它。

Okay not sure what the varchar pid represents.. so lets start with a slightly less confusing table structure... 好吧,不知道varchar pid代表什么。.因此,让我们从一个稍微混乱的表结构开始...

table producer ( producer_id int primary key,
                 producer_name varchar2(50) )

table movie ( movie_id int primary key,
              movie_data varchar2(50)
              movie_name varchar2(50) )

table movieproducers ( movie_id int*#
                       producer_type int*
                       producer_id int# )

Here we have 2 shared keys (or unique indexes) within MovieProducers. 在这里,MovieProducers中有2个共享密钥(或唯一索引)。 First the movie_id and producer_type where 1 would be the primary producer and any other number above 1 would be a co-producer preventing duplicating producer types -- and movie_id and producer_id which will eliminate a producer from appearing more than once for a specific movie) 首先是movie_id和producer_type,其中1是主要制片人,大于1的任何其他数字将是共同制片人,以防止重复的制片人类型-movie_id和producer_id将避免制片人针对特定电影多次出现

I believe that would solve the issue you have and not require a trigger 我相信这可以解决您遇到的问题,不需要触发器

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

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