
Database SQL Update Commands Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions I Insertion of one row: INSERT INTO R VALUES . I Insertion using a query: INSERT INTO R SELECT ... I Deletion: DELETE FROM R WHERE . I e.g., DELETE FROM Drinkers WHERE name=’Alice’; will delete all rows in which Alice appears from the Drinkers table I Updating: UPDATE R SET . WHERE . I e.g., to increase all the March Hare’s prices by 5% UPDATE Sells SET price = price * 1.05 WHERE pub=’March Hare’; Database Updates and Constraints Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions Updates can cause constraints to be violated I insert a row with a duplicate primary key value I insert/update a row with a foreign key value where corresponding primary key value does not exist I delete/update a row with a primary key value referenced by a foreign key value The default behaviour is for the database system to disallow such updates. Assume name is the primary key for the Pubs relation. create table Sells (... foreign key (pub) references Pubs on delete cascade on update set null, . ); Database Foreign Key Constraints Management In the foreign key definition, we can specify either Peter Wood SQL Updates I ON DELETE CASCADE SQL Update Commands SQL Views I ON UPDATE CASCADE SQL Transactions for actions on the referenced relation to cascade to referencing relation, or I ON DELETE SET NULL I ON UPDATE SET NULL for the referencing relation’s values to be set to null Database Foreign Key Constraints Management In the foreign key definition, we can specify either Peter Wood SQL Updates I ON DELETE CASCADE SQL Update Commands SQL Views I ON UPDATE CASCADE SQL Transactions for actions on the referenced relation to cascade to referencing relation, or I ON DELETE SET NULL I ON UPDATE SET NULL for the referencing relation’s values to be set to null Assume name is the primary key for the Pubs relation. create table Sells (... foreign key (pub) references Pubs on delete cascade on update set null, . ); Database Other Constraints Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions I We have already covered primary key and foreign key constraints I Other integrity constraints on a single relation include: I not null I unique I check(. ) I appear in create table statement I e.g., check (price > 0) Database Views Management Peter Wood SQL Updates SQL Update Commands I So far we have been operating at the logical level of SQL Views the database. SQL Transactions I For various reasons (e.g. security) not all users should have access to the entire logical model. I The view (or external) level provides appropriate access for users. I Views are defined using queries. I (Virtual) views are not precomputed and their results stored. I Instead they can be queried and the system “combines” the query and view definition. I SQL syntax is CREATE VIEW V AS SELECT . pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 Sells: March Hare Bad Habit 1.75 March Hare Rampant Ram 2.50 Black Horse Bad Habit 2.50 Black Horse Shining Wit 2.25 Black Horse Rampant Ram 2.50 White Horse Rampant Ram 2.75 CREATE VIEW cheapBeers AS SELECT pub, beer FROM Sells WHERE price <= 2.00; Database Defining a View Management Define a view for finding which pubs sell cheap beer Peter Wood (price 2.00 or less). SQL Updates SQL Update Commands SQL Views SQL Transactions CREATE VIEW cheapBeers AS SELECT pub, beer FROM Sells WHERE price <= 2.00; Database Defining a View Management Define a view for finding which pubs sell cheap beer Peter Wood (price 2.00 or less). SQL Updates SQL Update Commands SQL Views pub beer price SQL Transactions Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 Sells: March Hare Bad Habit 1.75 March Hare Rampant Ram 2.50 Black Horse Bad Habit 2.50 Black Horse Shining Wit 2.25 Black Horse Rampant Ram 2.50 White Horse Rampant Ram 2.75 Database Defining a View Management Define a view for finding which pubs sell cheap beer Peter Wood (price 2.00 or less). SQL Updates SQL Update Commands SQL Views pub beer price SQL Transactions Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 Sells: March Hare Bad Habit 1.75 March Hare Rampant Ram 2.50 Black Horse Bad Habit 2.50 Black Horse Shining Wit 2.25 Black Horse Rampant Ram 2.50 White Horse Rampant Ram 2.75 CREATE VIEW cheapBeers AS SELECT pub, beer FROM Sells WHERE price <= 2.00; pub beer Horse and Hound Bad Habit Horse and Hound Rampant Ram March Hare Bad Habit Find pubs selling Bad Habit for 2.00 or less in Bloomsbury: SELECT pub FROM cheapBeers, Pubs WHERE beer=’Bad Habit’ AND cheapBeers.pub=Pubs.name AND Pubs.location=’Bloomsbury’; pub Horse and Hound March Hare Database Querying a View Management Peter Wood SELECT * FROM cheapBeers; SQL Updates SQL Update Commands SQL Views SQL Transactions Find pubs selling Bad Habit for 2.00 or less in Bloomsbury: SELECT pub FROM cheapBeers, Pubs WHERE beer=’Bad Habit’ AND cheapBeers.pub=Pubs.name AND Pubs.location=’Bloomsbury’; pub Horse and Hound March Hare Database Querying a View Management Peter Wood SELECT * FROM cheapBeers; SQL Updates SQL Update Commands pub beer SQL Views SQL Transactions Horse and Hound Bad Habit Horse and Hound Rampant Ram March Hare Bad Habit SELECT pub FROM cheapBeers, Pubs WHERE beer=’Bad Habit’ AND cheapBeers.pub=Pubs.name AND Pubs.location=’Bloomsbury’; pub Horse and Hound March Hare Database Querying a View Management Peter Wood SELECT * FROM cheapBeers; SQL Updates SQL Update Commands pub beer SQL Views SQL Transactions Horse and Hound Bad Habit Horse and Hound Rampant Ram March Hare Bad Habit Find pubs selling Bad Habit for 2.00 or less in Bloomsbury: pub Horse and Hound March Hare Database Querying a View Management Peter Wood SELECT * FROM cheapBeers; SQL Updates SQL Update Commands pub beer SQL Views SQL Transactions Horse and Hound Bad Habit Horse and Hound Rampant Ram March Hare Bad Habit Find pubs selling Bad Habit for 2.00 or less in Bloomsbury: SELECT pub FROM cheapBeers, Pubs WHERE beer=’Bad Habit’ AND cheapBeers.pub=Pubs.name AND Pubs.location=’Bloomsbury’; Database Querying a View Management Peter Wood SELECT * FROM cheapBeers; SQL Updates SQL Update Commands pub beer SQL Views SQL Transactions Horse and Hound Bad Habit Horse and Hound Rampant Ram March Hare Bad Habit Find pubs selling Bad Habit for 2.00 or less in Bloomsbury: SELECT pub FROM cheapBeers, Pubs WHERE beer=’Bad Habit’ AND cheapBeers.pub=Pubs.name AND Pubs.location=’Bloomsbury’; pub Horse and Hound March Hare name loc name loc Horse and Hound B Alice I Hound and Hare I Bob B Pubs: Drinkers: March Hare B Carol I Black Horse I Dave B White Horse B Eve S CREATE VIEW coLocated(drinker,pub) AS SELECT Drinkers.name, Pubs.name FROM Pubs, Drinkers WHERE Pubs.loc=Drinkers.loc; Note that the view defines new attribute names. Database Defining Another View Management Peter Wood Define a view relating drinkers and pubs that are located SQL Updates SQL Update Commands in the same area: SQL Views SQL Transactions CREATE VIEW coLocated(drinker,pub) AS SELECT Drinkers.name, Pubs.name FROM Pubs, Drinkers WHERE Pubs.loc=Drinkers.loc; Note that the view defines new attribute names. Database Defining Another View Management Peter Wood Define a view relating drinkers and pubs that are located SQL Updates SQL Update Commands in the same area: SQL Views SQL Transactions name loc name loc Horse and Hound B Alice I Hound and Hare I Bob B Pubs: Drinkers: March Hare B Carol I Black Horse I Dave B White Horse B Eve S Note that the view defines new attribute names. Database Defining Another View Management Peter Wood Define a view relating drinkers and pubs that are located SQL Updates SQL Update Commands in the same area: SQL Views SQL Transactions name loc name loc Horse and Hound B Alice I Hound and Hare I Bob B Pubs: Drinkers: March Hare B Carol I Black Horse I Dave B White Horse B Eve S CREATE VIEW coLocated(drinker,pub) AS SELECT Drinkers.name, Pubs.name FROM Pubs, Drinkers WHERE Pubs.loc=Drinkers.loc; Database Defining Another View Management Peter Wood Define a view relating drinkers and pubs that are located SQL Updates SQL Update Commands in the same area: SQL Views SQL Transactions name loc name loc Horse and Hound B Alice I Hound and Hare I Bob B Pubs: Drinkers: March Hare B Carol I Black Horse I Dave B White Horse B Eve S CREATE VIEW coLocated(drinker,pub) AS SELECT Drinkers.name, Pubs.name FROM Pubs, Drinkers WHERE Pubs.loc=Drinkers.loc; Note that the view defines new attribute names. I all we can do is insert (’Mad Hatter’,NULL) into Pubs and (’Fred’,NULL) into Drinkers I but now (’Fred’,’Mad Hatter’) isn’t even in the view when we query it I if pub name is a key, we know the Black Horse is in Islington I therefore Fred must live in Islington I so insert (’Fred’,’Islington’) into Drinkers I What if instead INSERT INTO coLocated VALUES(’Fred’,’Mad Hatter’)? Database Inserting into views Management Peter Wood SQL Updates SQL Update Commands I Since a view is virtual, any insertion must be to the SQL Views table(s) on which the view is defined.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages46 Page
-
File Size-