Anda di halaman 1dari 75

Relational algebra

Whats relational algebra?


Defines basic operations on relation instances
composition of operations to form queries
basis for SQL
Useful to represent execution plans
what are the operations needed to execute a query
what is the order of execution of these operations
Basic operations
Selection (choose subset of rows)
Projection (choose subset of columns)
Cross product
Union [
Difference
Rename
Join o
n
Union, Intersection and Difference
Same operations as those on any sets
Apply operations on tuples with same schema
Rename
R(A1,A2,. . . ) (S)
Actors (Name, Age, Addr)

Stars(N ame,Age,City) (Actors)


Actors Stars
Name Age Addr Name Age City
Priyanka 34 Mumbai Priyanka 34 Mumbai
Chopra Chopra
Tom Cruise 54 LA Tom Cruise 54 LA
Bill Nighy 67 LA Bill Nighy 67 LA
Abhishek 40 Mumbai Abhishek 40 Mumbai
Bachchan Bachchan
Selection (1/2)
R1 = C (R2)
C is a condition on attributes of R2
Actors
Return all actors living in Mumbai
Name Age Addr
Priyanka 34 Mumbai Addr=0 M umbai0 (Actors)
Chopra
Name Age Addr
Tom Cruise 54 LA
Priyanka 34 Mumbai
Bill Nighy 67 LA Chopra
Abhishek 40 Mumbai Abhishek 40 Mumbai
Bachchan Bachchan
Selection (2/2)
R1 = C (R2) Return all actors whose age is more
than 35.
Age>35 (Actors)
Actors Name Age Addr
Name Age Addr Tom Cruise 54 LA
Priyanka 34 Mumbai Bill Nighy 67 LA
Chopra
Abhishek 40 Mumbai
Tom Cruise 54 LA Bachchan
Bill Nighy 67 LA
Abhishek 40 Mumbai Return all actors whose age is more
Bachchan than 35 and who live in Mumbai
Age>35 and Addr=0 M umbai (Actors)

Name Age Addr


Abhishek 40 Mumbai
Bachchan
Projection (1/2)
R1 = L (R2)
Actors Return the name and age of all actors
Name Age Addr N ame,Age (Actors)
Priyanka 34 Mumbai
Chopra Name Age
Tom Cruise 54 LA Priyanka 34
Bill Nighy 67 LA Chopra
Abhishek 40 Mumbai Tom Cruise 54
Bachchan Bill Nighy 67
Abhishek 40
Bachchan
Projection (2/2)
R1 = L (R2)
Actors Return the addresses of the actors
Name Age Addr Addr (Actors)
Priyanka 34 Mumbai
Chopra
Addr
Tom Cruise 54 LA
Mumbai
Bill Nighy 67 LA
LA
Abhishek 40 Mumbai
Bachchan

Duplicate elimination under set semantics


Cross product
R3 = R1 R2
Actors Movies
Name Age Addr Name Year Title
Priyanka 34 Mumbai Priyanka 2011 Don-II
Chopra Chopra
Tom Cruise 54 LA Tom Cruise 2011 MI-IV
Bill Nighy 67 LA Bill Nighy 2009 Valkyrie
Abhishek 40 Mumbai Abhishek 2010 Raavan
Bachchan Bachchan
Actor.name Age Addr Movies.Name Year Title
Priyanka Chopra 34 Mumbai Priyanka Chopra 2011 Don-II

15 more rows
Joins (1/2)
Equi-join
R3 = R1 o
nC R2
C is a condition on attributes of R1 and/or R2
Actors Movies Return all information about actors and
Name Age Addr Name Year Title their movies

PC 34 Mumbai PC 2011 Don-II


Actors o
nA.N ame=M.N ame M ovies
TC 54 LA TC 2011 MI-IV
Name Age Addr Year Title
BN 67 LA BN 2009 Valkyrie
PC 34 Mumbai 2011 Don-II
AB 40 Mumbai AB 2010 Raavan
TC 54 LA 2011 MI-IV

BN 67 LA 2009 Valkyrie

AB 40 Mumbai 2010 Raavan


Joins (2/2)
Natural joins
implicitly compares attributes of the same name for equality
Theta join
conditions not restricted to equality
Left-outer/right-outer/full-outer joins
non-matching tuples are still returned
Self-join
table joining with itself
Left outer joins
R=Xo
nY

Return all information about actors and


Actors Movies their movies
Name Age Addr Name Year Title Actors o
nA.N ame=M.N ame M ovies
PC 34 Mumbai PC 2011 Don-II

TC 54 LA TC 2011 MI-IV Name Age Addr Year Title

BN 67 LA AB 2010 Raavan PC 34 Mumbai 2011 Don-II

AB 40 Mumbai TC 54 LA 2011 MI-IV

BN 67 LA null null

AB 40 Mumbai 2010 Raavan


Self Join
Return all grandparents and their
grand children
Actors Actors_1
Name Age Addr Parent Name Age Addr Parent
PC 34 Mumbai Madhu PC 34 Mumbai Madhu
TC 54 LA Mary TC 54 LA Mary
BN 67 LA Catherine BN 67 LA Catherine
AB 40 Mumbai Jaya AB 40 Mumbai Jaya
Jaya 68 Mumbai Indira Jaya 68 Mumbai Indira
Composition of operators (1/2)
Actors
Return the names and addresses of actors
Name Age Addr over 35
Priyanka Chopra 34 Mumbai N ame,Addr ( Age>35 (Actor))
Tom Cruise 54 LA

Bill Nighy 67 LA Return the names of actors over 35 who live


Abhishek Bachchan 40 Mumbai
in Mumbai
N ame ( Age>35 and Addr=0 M umbai0 (Actor))
Composition of operators (2/2)
Actors Movies

Name Age Addr Name Year Title

Priyanka Chopra 34 Mumbai Priyanka Chopra 2011 Don-II

Tom Cruise 54 LA Tom Cruise 2011 MI-IV

Bill Nighy 67 LA Bill Nighy 2009 Valkyrie

Abhishek Bachchan 40 Mumbai Abhishek Bachchan 2010 Raavan

Return the names of actors below the age of 50 who


have acted in a movie in 2011
N ame ( Age<50 AND Y ear=2011 (Actors o
nA.N ame=M.N ame M ovies))

Allmovies = Actors o
nA.N ame=M.N ame M ovies
M ovies1 = Age<50 AND Y ear=2011 (AllM ovies)
Result = N ame (M ovies1)
Notation (1/2)
Return the names and addresses of actors
over 35
N ame,Addr ( Age>35 (Actor)) N ame,Addr R2

R1 = Age>35 (Actor)
Age>35
R2 = N ame,Addr (R1)

Actor
Notation (2/2)
Return the names of actors below the age of 50 who
have acted in a movie in 2011
N ame ( Age<50 AND Y ear=2011 (Actors o
nA.N ame=M.N ame M ovies))

Allmovies = Actors o
nA.N ame=M.N ame M ovies
M ovies1 =
Age<50 AND Y ear=2011 (AllM ovies)
Result = N ame (M ovies1)



Actors Movies
Relational algebra for bags
Efficiency issues if we consider relations as sets
Extra effort to eliminate duplicates
Select, project, join (SPJ) work exactly the same
Applied to one tuple at a time
Set operations become bag operations
Need to be careful about semantics
Union, Intersection, Difference
More operators
Duplicate elimination (R)
Extended projection
Aggregation
count, min, max, sum, avg
Grouping
Sorting
Extended Projection
Same as projection, but some operations can
be directly expressed
Prices Return the total price of petrol
City Petrol Tax City,P etrol+T ax (P rices)
Bangalore 39.2 33.2
Delhi 38.2 31.5 City Petrol+Tax
Mumbai 41.47 30 Bangalore 72.4
Delhi 69.7
Mumbai 71.47
Aggregation and grouping (1/2)
Grouping L (R)
L is a list of grouping attributes and/or aggregate
operators
Return total boxoffice returns per movie
Movie City Boxoffice
aggregate
MI-IV LA 2,000,000 grouping attribute
Don-II LA 500,000
M ovie,Sum(Boxof f ice) (M ovies)
MI-IV NY 3,000,000

Movie City Boxoffice Movie Boxoffice


MI-IV LA 2,000,000 MI-IV 5,000,000
MI-IV NY 3,000,000 Don-II 500,000
Don-II LA 500,000
Aggregation and grouping (2/2)
Grouping L (R)
L is a list of grouping attributes and/or aggregate
operators
Return total boxoffice returns per city
Movie City Boxoffice
aggregate
MI-IV LA 2,000,000 grouping attribute
Don-II LA 500,000
City,Sum(Boxoffice) (Movies)
MI-IV NY 3,000,000

Movie City Boxoffice City Boxoffice


MI-IV LA 2,000,000 LA 2,500,000
Don-II LA 500,000 NY 3,000,000
MI-IV NY 3,000,000
SQL 1
You should have tried out by now
Creating a database
Insertion of tuples
Creating, dropping tables
SQL basic structure
SELECT L Attributes of the output relation

FROM R List of all relations involved

WHERE C Conditions to be satisfied


Selection (1/2)
Actors
Return all actors living in Mumbai
Name Age Addr
Addr=0 M umbai0 (Actors)
Priyanka 34 Mumbai
Chopra SELECT Name, Age, Addr
Tom Cruise 54 LA FROM Actors
Bill Nighy 67 LA WHERE Addr = Mumbai

Abhishek 40 Mumbai
Bachchan SELECT *
FROM Actors
WHERE Addr = Mumbai
Selection (2/2)

Return all actors whose age is more Return all actors whose age is more
than 35. than 35 and who live in Mumbai
Age>35 (Actors) Age>35 and Addr=0 M umbai (Actors)

SELECT * SELECT *
FROM Actors FROM Actors
WHERE Age > 35 WHERE Age > 35 AND
Addr = Mumbai
Projection
Actors
Name Age Addr Return the name and age of all actors
Priyanka 34 Mumbai
Chopra N ame,Age (Actors)
Tom Cruise 54 LA
SELECT Name, Age
Bill Nighy 67 LA
FROM Actors
Abhishek 40 Mumbai
Bachchan
Return the addresses of the actors SELECT Addr
FROM Actors
Addr (Actors)
SELECT DISTINCT (Addr)
FROM Actors
Equi-Joins
Actors Movies

Name Age Addr Name Year Title Return all information about actors and
PC 34 Mumbai PC 2011 Don-II their movies
TC 54 LA TC 2011 MI-IV Actors o
nA.N ame=M.N ame M ovies
BN 67 LA BN 2009 Valkyrie
SELECT *
AB 40 Mumbai AB 2010 Raavan
FROM Actors, Movies
WHERE Actors.Name = Movies.Name
Left outer joins
Return all information about actors and
Actors Movies their movies
Name Age Addr Name Year Title Actors o
nA.N ame=M.N ame M ovies
PC 34 Mumbai PC 2011 Don-II

TC 54 LA TC 2011 MI-IV
SELECT *
BN 67 LA AB 2010 Raavan
FROM Actors LEFT OUTER JOIN Movies
AB 40 Mumbai ON (Actors.Name = Movies.Name)

What happens when a you compare something with a null value? Or when you
compare a null with a null?
Self Join
Return all grandparents and their
grand children
Actors Actors_1
Name Age Addr Parent Name Age Addr Parent
PC 34 Mumbai Madhu PC 34 Mumbai Madhu
TC 54 LA Mary TC 54 LA Mary
BN 67 LA Catherine BN 67 LA Catherine
AB 40 Mumbai Jaya AB 40 Mumbai Jaya
Jaya 68 Mumbai Indira Jaya 68 Mumbai Indira

SELECT *
FROM Actors AS Actors1, Actors AS Actors2
WHERE Actors1.Parent = Actors2.Name
Composition of operators (1/2)
Actors
Return the names and addresses of actors
Name Age Addr over 35
PC 34 Mumbai N ame,Addr ( Age>35 (Actor))
TC 54 LA

BN 67 LA SELECT Name, Addr


FROM Actors
AB 40 Mumbai
WHERE Age > 35

Return the names of actors over 35 who live SELECT Name


in Mumbai FROM Actors
WHERE Age > 35
N ame ( Age>35 and Addr=0 M umbai0 (Actor)) AND Addr = Mumbai
Composition of operators (2/2)
Return the names of actors below the age of 50 who
have acted in a movie in 2011
N ame ( Age<50 AND Y ear=2011 (Actors o
nA.N ame=M.N ame M ovies))

Allmovies = Actors o
nA.N ame=M.N ame M ovies
M ovies1 = Age<50 AND Y ear=2011 (AllM ovies)
Result = N ame (M ovies1)

SELECT Actors.Name
FROM Actors, Movies
WHERE Age < 50
AND Year = 2011
AND Actors.Name = Movies.Name
SQL 2
More operators
Duplicate elimination
Extended projection
Aggregation
count, min, max, sum, avg
Grouping
Sorting
Aggregation and grouping (1/2)
Grouping L (R)
L is a list of grouping attributes and/or aggregate
operators
Return total boxoffice returns per movie
Movie City Boxoffice
aggregate
MI-IV LA 2,000,000 grouping attribute
Don-II LA 500,000
M ovie,Sum(Boxof f ice) (M ovies)
MI-IV NY 3,000,000

Movie City Boxoffice Movie Boxoffice


MI-IV LA 2,000,000 MI-IV 5,000,000
MI-IV NY 3,000,000 Don-II 500,000
Don-II LA 500,000
Aggregation and grouping (2/2)
Return total boxoffice returns per movie
Movie City Boxoffice
M ovie,Sum(Boxof f ice) (M ovies)
MI-IV LA 2,000,000
Don-II LA 500,000
SELECT Movie, SUM (Boxoffice)
MI-IV NY 3,000,000 FROM Movies
GROUP BY Movie
Return movies for which total boxoffice
returns were greater than 1,000,000
SELECT Movie, SUM (Boxoffice)
FROM Movies
GROUP BY Movie
HAVING SUM (Boxoffice) > 1000000
Sorting
Sorting tuples by column
SELECT Movie, SUM (Boxoffice)
SELECT *
FROM Movies
FROM Movies
GROUP BY Movie
ORDER BY City
HAVING SUM (Boxoffice) > 1000000
ORDER BY Movie

SELECT Movie, Boxoffice SELECT Movie, SUM (Boxoffice) AS BO


FROM Movies FROM Movies
ORDER BY Boxoffice DESC GROUP BY Movie
HAVING SUM (Boxoffice) > 1000000
ORDER BY BO DESC
Subqueries (1/2)
Temporary relations, constants
SELECT L
Replace any of these relations
FROM R1, R2, , Rn
with another query
WHERE C

SELECT L
FROM R
WHERE C1 op v1 Replace the value with a query
which returns a constant

Replace the value with a query


which returns a relation
Subqueries (2/2)
SELECT *
FROM (SELECT *
FROM MOVIES) AS NEWMOVIES

Actors SELECT *
Name Age Addr
FROM Actors
WHERE Age <
PC 34 Mumbai (SELECT AVG(Age)
TC 54 LA FROM Actors)
BN 67 LA

AB 40 Mumbai
Conditions involving relations (1/2)
SELECT L
FROM R
WHERE C1 op v1 Replace the value with a query
which returns a constant

Replace the value with a query


which returns a relation
SELECT *
FROM Actors C1 IN R, C1 NOT IN R
WHERE Name IN C1 > ALL R, C1 op ANY R
(SELECT Name C1 > ANY R, C1 op ANY R
FROM Movies)
Conditions involving relations (2/2)
Actors

Name Age Addr SELECT *


PC 34 Mumbai FROM Actors
WHERE Age < ALL
TC 54 LA
(SELECT Age
BN 67 LA FROM Actors)
AB 40 Mumbai
SELECT *
FROM Actors
WHERE Age <
(SELECT AVG(Age)
FROM Actors)

SELECT *
FROM Actors
WHERE Age < ANY
(SELECT Age
FROM Actors)
Correlated subqueries
Actors SELECT *
FROM Actors AS A
Name Age Addr
WHERE Age < ALL
PC 34 Mumbai (SELECT Age
TC 54 LA FROM Actors
WHERE Name = A.Name)
BN 67 LA

AB 40 Mumbai
Indexes and Views
Indexes (1/4)
Data structure to access specific tuples fast
Very important for query processing and query optimization
Useful when no. of results very small compared to the total no. of
tuples
Indexes (2/4)
SELECT * FROM Actor
WHERE Name = Priyanka

Only one result


But table may contain thousands/millions of tuples
An index will point directly to the tuple
Indexes (3/4)
SELECT * FROM
Actor
WHERE Name =
Priyanka

CREATE INDEX
NameIndex
ON Actor (Name)

Priyanka
Abhishek Tom
Aamir
Indexes (4/4)
Many variations
Multi-column
Clustered index
Multi-dimensional
Many data structures
B-tree and variants
Hash indexes

Views (1/5)
Views are tables created from existing tables
Materialized (physically exist)
CREATE MATERIALIZED VIEW
Virtual (dont physically exist)
CREATE VIEW
Offer a simplified view of the data
Secure data from non-authorized users
Materialized views used in speeding up query processing
Views (2/5)
Actors

Name Age Addr CREATE VIEW ActorView AS


PC 34 Mumbai SELECT Name, Age
FROM Actors
TC 54 LA Doesnt actually exist!
BN 67 LA
ActorsView But, we can pretend
AB 40 Mumbai
Name Age
that it does

PC 34
Can have any query here.
TC 54

BN 67

AB 40
Views (3/5)
Querying views same as querying any other
table ActorsView

Name Age
SELECT * SELECT * PC 34
FROM ActorView FROM ActorView
TC 54
WHERE Name = PC
BN 67

AB 40
SELECT Name, Addr
FROM ActorView Error! Addr does not exist in
WHERE Name = PC the view
Views (4/5)
Actors Movies

Name Age Addr Name Year Title

Priyanka Chopra 34 Mumbai Priyanka Chopra 2011 Don-II

Tom Cruise 54 LA Tom Cruise 2011 MI-IV

Bill Nighy 67 LA Bill Nighy 2009 Valkyrie

Abhishek Bachchan 40 Mumbai Abhishek Bachchan 2010 Raavan

CREATE VIEW Actors55 AS Actors55


SELECT Actors.Name, Movies.Year Name Year
FROM Actors, Movies
Priyanka Chopra 2011
WHERE Age < 55
AND Actors.Name = Movies.Name Tom Cruise 2011

Abhishek Bachchan 2010


Views (5/5)
Actors
Actors55
Name Age Addr Name Year SELECT Actors.Name
PC 34 Mumbai Priyanka Chopra 2011 FROM Actors, Movies
TC 54 LA WHERE Age < 55
Tom Cruise 2011
AND Year = 2011
BN 67 LA Abhishek Bachchan 2010 AND Actors.Name = Movies.Name
AB 40 Mumbai

SELECT Name
FROM Actors55
WHERE Year = 2011
Queries with views
Actors Movies
CREATE VIEW ActorMovie2009 AS Name Age Addr Name Year Title
SELECT Actors.Name, Movies.Title,
Priyanka Chopra 34 Mumbai Priyanka Chopra 2011 Don-II
Movies.Year
FROM Actors, Movies Tom Cruise 54 LA Tom Cruise 2011 MI-IV
WHERE Actors.Name = Movies.Name Bill Nighy 67 LA Bill Nighy 2009 Valkyrie
AND Movies.Year >= 2009
Abhishek Bachchan 40 Mumbai Abhishek Bachchan 2010 Raavan
SELECT Actors.Name
FROM Actors, Movies SELECT Name
WHERE Actors.Name = Movies.Name FROM ActorMovie2009
AND Movies.Year >= 2009 WHERE Name = PC
AND Actors.Name = PC

SELECT Actors.Name
FROM Actors, Movies
WHERE Actors.Name = Movies.Name
AND Movies.Year >= 2008
AND Actors.Name = PC
Updating Views (1/3)
Can we update views?
Insertion, deletion, update CREATE VIEW ActorView AS
SELECT Name, Age
What happens to the base table? FROM Actors

INSERT INTO ActorView INSERT INTO Actors


VALUES (AJ, 36) VALUES (AJ, 36, NULL)

Insert pad with NULLs


Updating Views (2/3)
Actors55
CREATE VIEW Actors55 AS Name Year
SELECT Actors.Name, Movies.Year
Priyanka Chopra 2011
FROM Actors, Movies
WHERE Age < 55 Tom Cruise 2011
AND Actors.Name = Movies.Name Abhishek Bachchan 2010

Name Age Addr Name Year Title


INSERT INTO Actors55
VALUES (Bruce Willis, 1988) Priyanka Chopra 34 Mumbai Priyanka Chopra 2011 Don-II

Tom Cruise 54 LA Tom Cruise 2011 MI-IV

Bill Nighy 67 LA Bill Nighy 2009 Valkyrie

INSERT INTO Actors Abhishek Bachchan 40 Mumbai Abhishek Bachchan 2010 Raavan
VALUES (Bruce Willis, NULL, NULL)

INSERT INTO Movies


VALUES (Bruce Willis, 1988, NULL)
Updating Views (3/3)
Deletion of tuples
delete all tuples from the base table which gave rise to this tuple
What happens when tuples in the base tables are deleted?
Practical H.W.
How do you define virtual and materialized views in Postgresql?
How are queries on views handled in Postgresql?
What kind of constraints can we specify?
Constraints and Triggers
Common constraints (1/2)
Keys
How is an entity uniquely identified?
(Name, Year of birth) identifies an actor uniquely
Single-value constraints
Unique values in a given context (keys are single-value constraints)
Null values
Common constraints (2/2)
Referential integrity
Remove the null, insist on the value
If an actor acts in a movie, then that movie has to exist in the
database
Domain constraints
Restricting the value set of attributes
Age in range from 0 to 100 (or is it 0 to 25?)
Keys

CREATE TABLE movies


(movieid int, name varchar(30), year int,
CONSTRAINT movies_key PRIMARY KEY (movieid));

Type of constraint
Keyword, indicating a
constraint to follow

Name of constraint

CREATE TABLE actors


(name varchar(30), age int, addr varchar(30),
CONSTRAINT actors_key PRIMARY KEY (name, addr));
Uniqueness constraints

CREATE TABLE movies Cannot be null


(movieid int, name varchar(30), year int,
CONSTRAINT movies_key PRIMARY KEY (movieid),
CONSTRAINT name_unique UNIQUE (name)) Can be null

INSERT INTO movies VALUES (1, Don, 2010);


INSERT INTO movies VALUES (1, Don2, 2011);
INSERT INTO movies VALUES (2, Don2, 2011);
INSERT INTO movies VALUES (3, Don3, 2010);
Referential Integrity (1/2)
ActedIn Movies
Name Movie Movie Year
PC Don2 MI-IV 2011
TC MI-IV Eclipse 2008
KS Eclipse Don2 2011
DKR 2012

Foreign key
Key or Unique constraint

CREATE TABLE ActedIn (


Name varchar(30), Movie varchar(30),
CONSTRAINT actedIn_fkey
FOREIGN KEY (Movie) REFERENCES Movies (Movie))
Referential Integrity (2/2)
Reject modifications which
Reject, Cascade, Set-null violate constraints
Transfer modifications
Set attribute(s) to null if
needed
ActedIn Movie insert into actedIn: (CB, DKR)
Name Movie Movie Year insert into actedIn: (TC, MI-III)
PC Don2 MI-IV 2011
delete from actedIn: (KS, Eclipse)
TC MI-IV Eclipse 2008
delete from Movie: (DKR, 2012)
KS Eclipse Don2 2011
delete from Movie: (Eclipse, 2008)
CB DKR DKR 2012
update Movie: (DKR, 2012) to (DK, 2011)
CREATE TABLE ActedIn (
Name varchar(30), Movie varchar(30),
CONSTRAINT actedIn_fkey
FOREIGN KEY (Movie) REFERENCES Movie (movieid)
ON DELETE CASCADE)
Circular Constraints (1/3)
Actors Movies

Name Age Addr Famous_ Name Year Title


Movie
Priyanka Chopra 2011 Don-II
Priyanka Chopra 34 Mumbai Don-II
Tom Cruise 2011 MI-IV
Tom Cruise 54 LA MI-IV
Bill Nighy 2009 Valkyrie
Bill Nighy 67 LA Valkyrie
Abhishek Bachchan 2010 Raavan
Abhishek Bachchan 40 Mumbai Raavan
Tom Cruise 2003 TLS

insert into Actors: (Kristen Stewart, 27, LA, Breaking Dawn);


insert into Movies: (Kristen Stewart, 2011, Breaking Dawn);
Circular Constraints (2/3)
Notion of Transaction
An atomic unit of execution
Currently, the two inserts together form a single transaction
insert into Actors: (Kristen Stewart, 23, LA, Breaking Dawn);
insert into Movies: (Kristen Stewart, 2011, Breaking Dawn);

Defer constraint checking until after transaction


Circular Constraints (3/3)
Actors Movies

Name Age Addr Famous_ Name Year Title


Movie
Priyanka Chopra 2011 Don-II
Priyanka Chopra 34 Mumbai Don-II
Tom Cruise 2011 MI-IV
Tom Cruise 54 LA MI-IV
Bill Nighy 2009 Valkyrie
Bill Nighy 67 LA Valkyrie
Abhishek Bachchan 2010 Raavan
Abhishek Bachchan 40 Mumbai Raavan
Tom Cruise 2003 TLS

CREATE TABLE Actors (


Name varchar(30), Age int, Addr varchar (30), Famous_Movie varchar(30),
CONSTRAINT actors_fkey
FOREIGN KEY (Famous_Movie) REFERENCES Movies (Title)
DEFERRABLE INITIALLY DEFERRED)

Practical HW: How to specify transactions in PostgreSQL?


Constraints on Attributes and Tuples
NOT NULL constraints
Ensure that the value of the attribute is not null
CHECK constraints
Any constraint (could be a SQL query)

CREATE TABLE Actors (


Name varchar(30), Age int, Addr varchar (30), Famous_Movie varchar(30),
CHECK (Age <= 100 OR Age >= 1), CHECK Name NOT NULL )

CREATE TABLE Actors (


Name varchar(30), Age int, Cartoon_Char boolean,
CHECK (Age <= 100 OR Age >= 1 OR Cartoon_Char = TRUE))
Constraints on the Schema
ASSERTIONs which should hold true at all times
That is, all updates
Similar to CHECK constraints
General SQL queries (but should return true or false)

HW: Does PostgreSQL allow assertions? How can they be defined?


Altering Constraints
Constraints can be added and/or deleted
Newly added constraints should already hold on the current
instance

HW: Use of ALTER TABLE command to add/delete constraints


Triggers (1/2)
Event-Condition-Action
Insert, delete, update (wake up before or after)
Check condition
If condition holds, perform action
Triggers (2/2)
People Super-rich
Name #Houses Name #Houses
Priyanka 3 Amitabh 10
Amitabh 10 Sachin 15
Sachin 15 Shah Rukh 13
Shah Rukh 13 We need at least 5 houses to be super-rich.

An insertion here, potentially triggers ...an insertion here.

CREATE RULE rich_people AS CREATE TRIGGER rich_people AS


ON <event> BEFORE/AFTER <event> ON <table>
DO <task> WHEN <condition>
EXECUTE PROCEDURE <procedure>

INSERT, DELETE or UPDATE


in addition to or instead of
Rewriting Queries for Views
INSERT INTO ActorView INSERT INTO Actors
VALUES (AJ, 36) VALUES (AJ, 36, NULL)

SELECT Actors.Name
SELECT Name FROM Actors, Movies
FROM ActorMovie2009 WHERE Actors.Name = Movies.Name
WHERE Name = PC AND Movies.Year >= 2009
AND Actors.Name = PC

CREATE RULE X AS
ON <SELECT/INSERT> TO <table>
DO INSTEAD <task>

HW: How does PostgreSQL implement the above rewrites?

Anda mungkin juga menyukai