0% found this document useful (0 votes)
28 views

(PostGreSQL) AdvancedFeatures (Compatibility Mode)

The document discusses advanced database features including views, foreign keys, transactions, and inheritance. Views allow encapsulating query results behind consistent interfaces. Foreign keys maintain referential integrity between tables. Transactions bundle multiple steps into atomic, consistent, isolated, and durable units of work. Inheritance in object-oriented databases allows tables to inherit columns from parent tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

(PostGreSQL) AdvancedFeatures (Compatibility Mode)

The document discusses advanced database features including views, foreign keys, transactions, and inheritance. Views allow encapsulating query results behind consistent interfaces. Foreign keys maintain referential integrity between tables. Transactions bundle multiple steps into atomic, consistent, isolated, and durable units of work. Inheritance in object-oriented databases allows tables to inherit columns from parent tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2/18/2013

Contents

1. Views
Advanced Features 2. Foreign Keys
3. Transactions
4. Inheritance
NGUYEN Hong Phuong
[email protected]

1 2

1. Views 2. Foreign Keys


 Create a view over the query, which gives a  Recall the weather and cities tables from previous
name to the query that you can refer to like an chapter. Consider the following problem: You want to
ordinary table: make sure that no one can insert rows in the weather
CREATE VIEW myview AS table that do not have a matching entry in the cities
table. This is called maintaining the referential integrity
SELECT city, temp_lo, temp_hi, prcp, date, location
of your data.
FROM weather, cities
WHERE city = name; CREATE TABLE cities ( CREATE TABLE weather (
city varchar(80) primary key, city varchar(80) references cities(city),
location point temp_lo int,
SELECT * FROM myview; temp_hi int,
);
 Views allow you to encapsulate the details of prcp real,
date date
the structure of your tables, which might );
change as your application evolves, behind
consistent interfaces.
3 4

3. Transactions An example

 A transaction comprises a unit of work  Consider a bank database that contains balances
for various customer accounts, as well as total
performed within a DBMS (or similar system)
deposit balances for branches.
against a database, and treated in a coherent
and reliable way independent of other  Suppose that we want to record a payment of
transactions. $100.00 from Alices account to Bobs account
UPDATE accounts SET balance = balance - 100.00
 It bundles multiple steps into a single, all-or- WHERE name = Alice;
nothing operation UPDATE branches SET balance = balance - 100.00
WHERE name = (SELECT branch_name FROM accounts WHERE
 The intermediate states between the steps are name = Alice);
not visible to other concurrent transactions UPDATE accounts SET balance = balance + 100.00
WHERE name = Bob;
 If some failure occurs that prevents the
UPDATE branches SET balance = balance + 100.00
transaction from completing, then none of the WHERE name = (SELECT branch_name FROM accounts WHERE
steps affect the database at all. name = Bob);

5 6

1
2/18/2013

An example (cont.) Properties of a transaction - ACID


 Assure that either all these updates  Atomicity
happen, or none of them happen.  A series of DB operations either all occur
 Bob received $100.00 that was not debited or nothing occurs
from Alice?  Prevent updates to the DB occurring only
 Alice was debited without Bob being partially, which can cause greater
credited? problems than rejecting the whole series
 If something goes wrong partway outright.
through the operation, none of the steps  In other words, atomicy means
executed so far will take effect. indivisibility and irreducibility
 Grouping the updates into a transaction  Consistency
gives us this guarantee  Data is consistent after the transaction
7 8

Properties of a transaction - ACID

 Isolation  In PostgreSQL, a transaction is set up


 Define how/when the changes made by one by surrounding the SQL commands of
operation become invisible to other the transaction with BEGIN and
concurrent operations
COMMIT commands
 Durability BEGIN;
 Guarantees that transactions that have UPDATE accounts SET balance = balance - 100.00
committed will survive permanently. WHERE name = Alice;
 For example, if a flight booking reports that ..
a seat has successfully been booked, then COMMIT;
the seat will remain booked even if the
system crashes.

9 10

 We do not want to commit (perhaps we just  A group of statements surrounded by


noticed that Alices balance went negative), we
BEGIN and COMMIT is sometimes
can issue the command ROLLBACK instead of
COMMIT, and all our updates so far will be called a transaction block.
canceled.
 PostgreSQL actually treats every SQL
statement as being executed within a
transaction.
 If you do not issue a BEGIN command, then
each individual statement has an implicit
BEGIN and (if successful) COMMIT wrapped
around it.
11 12

2
2/18/2013

Save points

 Using savepoints to control the statements in a BEGIN;


transaction. UPDATE accounts SET balance = balance - 100.00
 Savepoints allow you to selectively discard WHERE name = Alice;
parts of the transaction, while committing the SAVEPOINT my_savepoint;
rest. UPDATE accounts SET balance = balance + 100.00
 After defining a savepoint with SAVEPOINT, WHERE name = Bob;
you can roll back to the savepoint with -- oops ... forget that and use Wallys account
ROLLBACK TO. ROLLBACK TO my_savepoint;
 All the transactions database changes between UPDATE accounts SET balance = balance + 100.00
defining the savepoint and rolling back to it are WHERE name = Wally;
discarded, but changes earlier than the COMMIT;
savepoint are kept.
13 14

4. Inheritance

 Inheritance is a concept from object-  In this case, a row of capitals inherits


oriented databases. It opens up all columns (name, population, and
interesting new possibilities of altitude) from its parent, cities.
database design. Insert into cities values('Las
CREATE TABLE cities( Vergas',4.2,2174), ('Mariposa',2.1,1953);
name text, Insert into capitals values('Madison',5.6,845,
population real, 'CA');
altitude int);
CREATE TABLE capitals( SELECT *
state char(2) FROM cities
) INHERITS (cities); WHERE altitude > 500;
15 16

SELECT *
FROM cities

SELECT name, altitude


FROM ONLY cities
WHERE altitude > 500;

 In PostgreSQL, a table can inherit


from zero or more other tables.

17

You might also like