adding primary key to sql view

We can add a disabled primary key constraint to a view. That is, the constraint does not fire if an insert or update is run against the view. The database expects integrity to be maintained through constraints on the underlying tables. So the constraint exists solely for the purposes of documentation.

SQL> create view emp_view as select * from emp
  2  /


View created.

SQL> alter view emp_view add constraint vemp_pk primary key (empno) disable
  2  /

View altered.

SQL> 

Caveat: I have never tried this with Hibernate, so I don’t know whether it would work in your scenario. However, I do know sites which use Hibernate exclusively against a layer of views, so I presume it does. Please experiment with the syntax and report back.

Leave a Comment