Skip to Main Content
  • Questions
  • Referential Integrity between Views and Tables

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Nimi.

Asked: June 10, 2008 - 2:31 pm UTC

Last updated: June 12, 2008 - 7:15 am UTC

Version: 10 g

Viewed 1000+ times

You Asked

Hi Tom,
I have some constants to be used all through my application and I don't want to give access to anybody. So I am wrapping them up in a Package. I am creating a view from those package constants.

CREATE OR REPLACE VIEW  "Const_VIew" ("Cons_id", "Cons_name") AS 
  Select 1 id, 'Low' from dual
UNION ALL
Select 2 id, 'Medium' from dual
UNION ALL
Select 3 id, 'High' from dual
UNION ALL
Select 4 id, 'Very_High' from dual


In my application I am replacing these constants with package calls.

I want to have tables referencing this view and have referential integrity between this view and the tables.

CREATE TABLE  "TEST_1" 
   ( "TEST_ID" NUMBER NOT NULL ENABLE, 
 "TEST_NAME" VARCHAR2(4000), 
 "CONST_ID_REF" NUMBER NOT NULL ENABLE, 
  CONSTRAINT "TEST_1_PK" PRIMARY KEY ("TEST_ID") ENABLE
   )


I want to have foreign key relation between TEST_1 and Const_View. Is it possible? If not, what are my options?
Any help is greatly appreciated.
Thanks
Nimi

and Tom said...

instead of a create view, use a create table - a lookup table.

The view provides nothing here - just use a real table. The view doesn't "hide" anything from anyone any more or less than a table would/could.

Integrity checks are triggered by insert/update/deletes - not create or replace views. Here, a create or replace view would have to drop and re-enable a constraint - and it doesn't do that.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Nimi, June 11, 2008 - 11:14 pm UTC

Tom,
I am not intending to hide the data. I don't want the user to Change the data.
Ex. If 'Low' corresponds to 1 in my table. I don't want it to be set 2.
My question is will it be enough if I have a look up table and have trigger on it to raise an error when someone tries to change the data?
If so,people with access to DB can always disable the trigger and manipulate the data. Should I try to prevent such situation?
Thanks
Nimi
Tom Kyte
June 12, 2008 - 7:15 am UTC

... I don't want the user to Change the data. ...

and how would a view do that any better?

People can change things because they have been granted the privilege to change things.

If you do not grant UPDATE, INSERT, DELETE - then - well - it will not be modifiable.

If you used a view, I'd just recreate your view to change the data - it would not in any way shape or form prevent me from doing anything.

Use grants just like you would in anything. If you are afraid the person would disable then trigger, then this person has lots of privileges in that schema and would be able to recreate your view!

You cannot enforce referential integrity via a trigger unless you are willing to LOCK THE CHILD TABLE, effectively serializing access.

Please: do this right - just use a table, use grants, secure data like everyone secures data.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library