



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!




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. WHERE pub=’March Hare’;
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.
Management^ Database Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
In the foreign key definition, we can specify either I (^) ON DELETE CASCADE I (^) ON UPDATE CASCADE 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,
... );
Management^ Database 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)
Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
I (^) So far we have been operating at the logical level of the database. 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...
Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
Define a view for finding which pubs sell cheap beer (price 2.00 or less).
Sells:
pub beer price Horse and Hound Bad Habit 1. Horse and Hound Rampant Ram 2. Hound and Hare Shining Wit 2. Hound and Hare Rampant Ram 2. March Hare Bad Habit 1. March Hare Rampant Ram 2. Black Horse Bad Habit 2. Black Horse Shining Wit 2. Black Horse Rampant Ram 2. White Horse Rampant Ram 2.
CREATE VIEW cheapBeers AS SELECT pub, beer FROM Sells WHERE price <= 2.00;
Management^ Database Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
SELECT * FROM cheapBeers;
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
Management^ Database Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
Define a view relating drinkers and pubs that are located in the same area:
Pubs:
name loc Horse and Hound B Hound and Hare I March Hare B Black Horse I White Horse B
Drinkers:
name loc Alice I Bob B Carol I Dave 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.
Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
I (^) When using a generic SQL interface (e.g., MySQL Workbench), each statement is its own transaction. I (^) However, in programs, we can group statements into a transaction. I (^) START TRANSACTION is used to mark the beginning of a transaction. I (^) COMMIT causes the transaction to end successfully. All changes to the database are made permanent. I (^) ROLLBACK cause the transaction to abort. All changes to the database are undone.
Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
Transactions are designed to meet the “ACID” test: I (^) “A” stands for “atomicity” — transactions should execute completely or appear not to have run at all I (^) “C” stands for “consistency” — the constraints specified in the database must be preserved I (^) “I” stands for “isolation” — each transaction should appear to execute as if there are no others I (^) “D” stands for “durability” — once a transaction completes, it effect should never be lost
Management^ Database Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
Consider a flight booking transaction on the relation
Flights(FltNo, fltDate, seatNo, seatStatus)
consisting of the query
SELECT seatNo FROM Flights WHERE fltNo=123 AND fltDate=’2010-12-25’ AND seatStatus=’available’;
which returns available seats from which the user selects seat 22A, and the following update
UPDATE Flights SET seatStatus=’occupied’ WHERE fltNo=123 AND fltDate=’2010-12-25’ AND seatNo=’22A’;
Management^ Database Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
Say two users are doing flight bookings simultaneously:
User 1 finds seat 22A empty time User 2 finds ↓ seat 22A empty User 1 sets seat 22A occupied User 2 sets seat 22A occupied
Both customers believe they have booked seat 22A.
Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
The solution to the above problem is as follows: I (^) Database operations can be grouped into a transaction I (^) The programmer can specify that the transaction must be serializable I (^) This means that transactions must behave as if they had been run serially — one after the other, with no overlap I (^) The two invocations of the flight booking operations could not book the same seat if run serially
Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
Consider banking application on relation
Accounts(acctNo, balance)
transferring £100 from account 123 to account 456.
Management^ Database Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
I (^) The transactions we have seen both read from and write to the database. I (^) SQL assumes that this is the default situation: SET TRANSACTION READ WRITE. I (^) But transactions that only read data cannot cause permanent harm to the database. I (^) This can be specified using SET TRANSACTION READ ONLY. I (^) Read-only transactions can be executed concurrently by the system.
Management^ Database Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions
I (^) Dirty data means data written by a transaction that has not yet committed. I (^) A dirty read is a read of dirty data written by another transaction. I (^) The risk is that the transaction that wrote the data may abort. I (^) This results in non-serializable behaviour. I (^) This may or may not be a problem for the reading transaction.