



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Announcements Chapter 7 Last time I told you that mySQL doesn't support CHECK constraints. However, there is a work-around for one type of CHECK constraint โ Say we have a column like this: Country char(15) not null, constraint countryValues CHECK (Country in ('Canada', 'US', 'England')), In mySQL, we can accomplish the same thing using an ENUM type: Country ENUM('Canada', 'US', 'England'), Views A view is a 'virtual' or derived table.
create view shipclass as select name, shipclass from starship; Now we can use shipclass in a select statement just like any other table. A view that includes only some of the rows in a table: create view shipclass as select name as Name, shipclass as Class from starship where shipclass = 'Admiral'; Note: need to DROP VIEW in between. Add a new row to the starship table, and show how that changes the rows in the view. Using Views to simplify queries We can use views to provide a way for casual users to use the database without having to train them to write complex Joins. For example: create view starshipCrew as select c.starshipID, cm.name, cm.rank from crew c join crewMember cm on c.crewId = cm.crewID; Or: create view starshipCrew as select s.name as Ship, cm.name as Crew, cm.rank as Rank from crew c join crewMember cm on c.crewId = cm.crewID join starship s on s.starshipID = c.starshipID; Now we can easily query the starshipCrew table, using where, order by, or group clauses as necessary. select Ship, count(*) as CrewSize from starshipCrew group by Ship; Using Views to display results of computed columns
Open SQL( Select * from starship ); Move cursor to first row; while ( cursor not past end of result ) { String starshipName = row.name; int crewSize = row.crewSize; // other statements Move cursor to next row; } This allows sequential program code to process the rows one at a time. Triggers A trigger is some program code that is stored in the database. Triggers are executed whenever a certain event occurs. A trigger is attached to a table or view. Triggers may be executed by inserts, updates, or deletes. There are three types of triggers: BEFORE, INSTEAD OF, and AFTER (SQL Server does not support BEFORE). This means that the trigger is executed before, instead of, or after the statement that invokes the trigger. What can triggers be used for?