oracle WITH CHECK OPTION

  • 2020-06-12 10:51:57
  • OfStack

If the view definition includes conditions such as the WHERE clause and is intended to ensure that any INSERT or UPDATE statements referencing the view apply the WHERE clause, the view must be defined using WITH CHECK OPTION. This option ensures the integrity of the data being modified in the database. If a condition is violated during an INSERT or UPDATE operation, an SQL error is returned.
Here is an example of a view definition using WITH CHECK OPTION. WITH CHECK OPTION is required to ensure that conditions are always checked. In this case, we want to make sure that DEPT is always 10. This limits the input values for the DEPT column. WITH CHECK OPTION is always enforced when a new value is inserted using a view.

CREATE VIEW EMP_VIEW2  
  (EMPNO,EMPNAME,DEPTNO,JOBTITLE,HIREDATE)  
  AS SELECT ID,NAME,DEPT,JOB,HIREDATE FROM EMPLOYEE  
    WHERE DEPT=10  
  WITH CHECK OPTION; 
If the clause does not exist, it is possible for a user using the view to update a record so that it no longer belongs to the view. For example, the following SQL statement may cause 1 problem.
UPDATE EMP_VIEW2 SET DEPT=20 WHERE DEPT=10;
The result of this statement is that the view now contains no records because department 10 no longer has employees.

Related articles: