Docsity

sql-updates-for-printing.pdf, Lecture notes of Printing

Toggle sidebar
bg1
Database
Management
Peter Wood
SQL Updates
SQL Update Commands
SQL Views
SQL Transactions
SQL Update Commands
IInsertion of one row: INSERT INTO RVALUES . . .
IInsertion using a query: INSERT INTO RSELECT
...
IDeletion: DELETE FROM RWHERE . . .
Ie.g., DELETE FROM Drinkers WHERE name=’Alice’;
will delete all rows in which Alice appears from the
Drinkers table
IUpdating: UPDATE RSET . . . WHERE . . .
Ie.g., to increase all the March Hare’s prices by 5%
UPDATE Sells SET price = price * 1.05
WHERE pub=’March Hare’;
Database
Management
Peter Wood
SQL Updates
SQL Update Commands
SQL Views
SQL Transactions
Updates and Constraints
Updates can cause constraints to be violated
Iinsert a row with a duplicate primary key value
Iinsert/update a row with a foreign key value where
corresponding primary key value does not exist
Idelete/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.
Database
Management
Peter Wood
SQL Updates
SQL Update Commands
SQL Views
SQL Transactions
Foreign Key Constraints
In the foreign key definition, we can specify either
ION DELETE CASCADE
ION UPDATE CASCADE
for actions on the referenced relation to cascade to
referencing relation, or
ION DELETE SET NULL
ION 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
Management
Peter Wood
SQL Updates
SQL Update Commands
SQL Views
SQL Transactions
Other Constraints
IWe have already covered primary key and foreign
key constraints
IOther integrity constraints on a single relation
include:
Inot null
Iunique
Icheck(. . . )
Iappear in create table statement
Ie.g., check (price > 0)
pf3
pf4
pf5

Partial preview of the text

Download sql-updates-for-printing.pdf and more Lecture notes Printing in PDF only on Docsity!

Management Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions

SQL Update Commands

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 and Constraints

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

Foreign Key Constraints

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

Other Constraints

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

Views

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

Defining a View

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

Querying a View

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

Defining Another View

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

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

Transaction properties

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

Transaction example

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

Interleaved execution

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

Serializability

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

Atomicity

Consider banking application on relation

Accounts(acctNo, balance)

transferring £100 from account 123 to account 456.

  1. Ensure there is at least £100 in account 123.
  2. Add £100 to account 456: UPDATE Accounts SET balance = balance + 100 WHERE acctNo = 456;
  3. Subtract £100 from account 123: UPDATE Accounts SET balance = balance - 100 WHERE acctNo = 123; What if there is a failure after Step (2)? We need the transaction to execute atomically — either all steps are executed or none are.

Management^ Database Peter Wood SQL Updates SQL Update Commands SQL Views SQL Transactions

Types of 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

Dirty Reads

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.