20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10<br />

The CHECK OPTION plays an important role in security and data integrity. You certainly don’t want to<br />

allow updates on data that the user is not supposed to see or modify. Furthermore, if the update is valid,<br />

it should usually not occur through that view. Thus, whenever you create a view to use for updates, it is<br />

a good idea to seriously consider whether to use CHECK OPTION.<br />

Dropping Views<br />

298<br />

Obviously, where you have a view, you need a way to drop the view if it is no longer needed. The syntax<br />

is simple enough:<br />

DROP VIEW MYVIEWNAME<br />

Dropping a view can be a bit hazardous, however, because other views might be based on the view<br />

being dropped. Suppose that you have a view of all members in Golden State:<br />

CREATE VIEW MembersFromGoldenState AS<br />

SELECT *<br />

FROM MemberDetails<br />

WHERE (State = ‘Golden State’)<br />

You then build a view based on that view:<br />

CREATE VIEW MembersFromGoldenStateBirthdays AS<br />

SELECT FirstName, LastName, DateOfBirth<br />

FROM MembersFromGoldenState<br />

What happens now if you attempt to drop MembersFromGoldenState?<br />

DROP VIEW MembersFromGoldenState<br />

In order to explicitly specify what to do, there are two additional keywords: CASCADE and RESTRICT.<br />

DROP VIEW MembersFromGoldenState CASCADE<br />

The CASCADE keyword tells the DBMS that if you attempt to drop a view and other views depend on<br />

that view, it should delete the dependent views as well. The DBMS searches through all other views,<br />

looking for references to the view being dropped. If it finds any, then the DBMS drops that view as well.<br />

Because you can build layered views, using the CASCADE keyword can result in entire chains of views<br />

being dropped because views depend on views being dropped.<br />

The RESTRICT keyword, on the other hand, tells the DBMS that the drop should fail if any other views<br />

depend on the view being dropped.<br />

DROP VIEW MembersFromGoldenState RESTRICT<br />

The DROP command (shown above) should fail with an error.<br />

A DROP VIEW statement with no qualifiers may be valid syntax because early versions of <strong>SQL</strong> did not<br />

support RESTRICT and CASCADE, so for backward compatibility some DBMSs may accept the statements.<br />

Specifying exactly what you want to happen when you drop a view, however, is highly advisable.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!