If you're adding rownum
as a dummy primary key column, then you should add a primary key constraint, not just a null check constraint - which isn't allowed on a view anyway.
As noted in the documentation, only certain constraint types are allowed, and must be defined with disable
and novalidate
.
Notes on View Constraints
View constraints are a subset of table constraints and are subject to the following restrictions:
- You can specify only unique, primary key, and foreign key constraints on views. However, you can define the view using the WITH CHECK OPTION clause, which is equivalent to specifying a check constraint for the view.
- View constraints are supported only in DISABLE NOVALIDATE mode. You cannot specify any other mode. You must specify the keyword DISABLE when you declare the view constraint. You need not specify NOVALIDATE explicitly, as it is the default.
...
So you can do:
-- example view with rownum dummy PK column
create view v42 as
select rownum as pk, table_name
from user_tables d;
View V42 created.
alter view v42 add constraint pk_generate2 primary key (pk) disable novalidate;
View V42 altered.
The documentation also points out that
Oracle does not enforce view constraints. However, operations on views are subject to the integrity constraints defined on the underlying base tables. This means that you can enforce constraints on views through constraints on base tables.
but with a value based on rownum
(or row_number()
) that isn't going to be a problem.