Local & Cascaded Check
Explore how local and cascaded check options control rule verification scopes in SQL views. Understand their impact on data inserts and updates through multi-level views to ensure consistency and integrity within relational databases.
We'll cover the following...
LOCAL AND CASCADED CHECK
Local and cascaded check clauses are used to determine the scope of rule testing when a view is created based on another view. To summarize, Local check option restricts the rule checking to only the view being defined whereas the Cascaded check option checks the rules of all underlying views. In the absence of these keywords, cascaded check is used as default.
Syntax
CREATE [OR REPLACE] VIEW view_name AS
select_statement
WITH [LOCAL | CASCADED] CHECK OPTION;
Connect to the terminal below by clicking in the widget. Once connected, the command line prompt will show up. Enter or copy and paste the command ./DataJek/Lessons/44lesson.sh and wait for the MySQL prompt to start-up.
Cascaded Check
-
We will start by creating a view ActorsView1 which shows all actors who are older than 40.
CREATE VIEW ActorsView1 AS SELECT * FROM Actors WHERE TIMESTAMPDIFF(YEAR, DoB, CURDATE()) > 40;Seven rows from the Actors table satisfy the WHERE clause.
In the absence of the WITH CHECK OPTION clause there is no restriction on updates through ActorsView1. We can insert a 20-year-old actor to the Actors table using this view as follows:
INSERT INTO ActorsView1
VALUES (DEFAULT, 'Young', 'Actress', '2000-01-01', 'Female', 'Single', 000.00);
The record is inserted in the table even though it does ...