Foreign key referencing a view in Oracle

ⅰ亾dé卋堺 提交于 2019-12-23 06:50:37

问题


I'm attempting to reference a view with a foreign key but I am getting this error:

"Error: ORA-02270: no matching unique or primary key for this column-list"

However, I have created a primary key on this view and verified it in the Constraints tab in TOAD.

This is the table I'm attempting to create:

CREATE TABLE QUESTION
(   
    QUESTION_ID             INTEGER not null,
    CREATED_USER_ID         INTEGER not null,    
    CONSTRAINT PK_QUESTION  PRIMARY KEY (QUESTION_ID),
    CONSTRAINT FK_USER
        FOREIGN KEY (CREATED_USER_ID)
        REFERENCES SOME_VIEW(VIEW_ID)
);

SOME_VIEW is a view based on another view which points to the employee table in another schema.


回答1:


Regardless the possibility of creating foreign keys to views, it is indeed not the best idea to implement.

Database views were designed to let user comfortably query some data he needs, but at the same time to serve as a security barrier, to conceal all database structure, including tables, data constraints in tables, and, yes, also table cross-references.

So, a good practice to me would be to reference an existing table from a your new one, despite its residence in other scheme.




回答2:


The best you can do is to implement SOME_VIEW as a Materialized view, since you are pointing to another schema. Then you can alter the materialized view to add a primary key and even refer to this view from a foreing key.

This is the documentation you should read: https://docs.oracle.com/cd/A97630_01/server.920/a96567/repmview.htm




回答3:


The foreign key against a view is probably what's causing the trouble.



来源:https://stackoverflow.com/questions/3833150/foreign-key-referencing-a-view-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!