Professional Documents
Culture Documents
67
Introduction to SQL
Declarative
Say what you want without specifying how to do it
One of the main reasons for commercial success of DBMSs
Many standards and implementations
ANSI SQL
SQL-92/SQL-2 (Null operations, Outerjoins etc.)
SQL3 (Recursion, Triggers, Objects)
Vendor specific implementations
Bag Semantics instead of Set Semantics
Used in commercial RDBMSs
Example:
CHAR(n)
VARCHAR(n)
BIT(n)
BIT VARYING(n)
INT/INTEGER
FLOAT
REAL, DOUBLE PRECISION
DECIMAL(p,d)
DATE, TIME etc.
68
69
More Examples
Why?
(sid CHAR(9),
courseid CHAR(6));
Examples Contd.
70
71
Can use DEFAULT even with regular definition (as in Slide 69)
INSERT command
INSERT
INTO Students
VALUES (53688,Mark,mark2345,23,3.9)
DELETE command
DELETE
FROM Students S
WHERE S.name = Smith
UPDATE command
UPDATE Students S
SET S.age=S.age+1, S.gpa=S.gpa-1
WHERE S.sid = 53688
72
73
Domains
junkaddress Email,
fromaddress Email,
toaddress Email,
....
Keys
To Specify Keys
Typically only one PRIMARY KEY but any number of UNIQUE keys
Implementor allowed to attach special significance
74
75
Creating Indices/Indexes
Why?
Other Properties
76
"!$#&%('*),+.-&/10(2$35462748%(0(09':!;/&-<=!$#&>?2@2A-&%CBD2@>?2$;!FEG/&2$>:<H0(IJ&KL/1ILKL2@'?ML>N2$O&>N2$'?2$;!;IP!;%C+L&'Q/&'N2$-%(SR$+LJT
U /&&RV!;%(+JW48%X!;#W!$#&2
>?2$0CIY!;%C+L&IJ0L)"+.-&2$0CZ\[]#&2
^&>N'!_%('9>N2$0(IP!;%C+L&IJ0.IL0(KJ2$`&>NIL3GILQIL0CKL2$`&>NIL%CR_IJ&-AO1>?+.R$2$-1/&>?IJ0
46I<Hab+J>]R$>?2@IY!;%C&Kc&2N4d>?2@0(IY!$%(+J&']ab>?+J)eKL%gfD2$+L12$'?Zh[]#&2Q'?2$R@+L&-%(']i*IY!;IJ0(+JKL3548#&%(RY#%(']0(+JKL%CR$IL0jIL&-&2$R@0(IL>NIY!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
Declaring constraints
Domain Constraints
Referential Integrity (Foreign Keys)
More SQL Stuff
Subqueries
Aggregation
SQL Peculiarities
Strange Phenomena
More on Bag Semantics
Ifs and Buts
Embedding SQL in a Programming Environment
Accessing DBs from within a PL
(will be covered in Module 3)
Triggers
Security
82
83
Already Seen
NOT NULL
UNIQUE, PRIMARY KEY etc.
In General
Foreign Keys
An attribute a of R1 is a foreign key if it references
the primary key (say b) of another relation R2
In addition, there is a ref. integrity constraint from R1 to R2.
Example
login is a FOREIGN KEY for Students
84
85
Alternatively
in both cases
SQL Subqueries
Given
Students(sid,name,login,age,gpa)
HasCar(sid,carname)
Find
SELECT carname
FROM Students, HasCar
WHERE Students.login=mark
AND Students.sid=HasCar.sid;
The Subway
SELECT carname
FROM HasCar
WHERE sid=
(SELECT sid FROM Students
WHERE login=mark);
86
87
Aggregation
Given
Students(sid,name,login,age,gpa)
Find
SELECT AVG(age)
FROM Students;
Other Operations
Ordering
Given
Students(sid,name,login,age,gpa)
List
SELECT *
FROM Students
ORDER BY name;
SELECT *
FROM Students
ORDER BY name DESC;
Default is ASC
88
89
Grouping
Given
Students(sid,name,login,age,gpa)
Find
SELECT name
FROM Students
WHERE gpa=4.0
GROUP BY name;
More on Grouping
Given
Students(sid,name,login,age,gpa)
Find
SELECT name
FROM Students
WHERE gpa=4.0
GROUP BY name
HAVING COUNT(*) > 2;
90
91
General Form
SELECT <attribute(s)>
FROM <relation(s)>
WHERE <condition(s)>
GROUP BY <attribute(s)>
HAVING <grouping condition(s)>
Order of Execution
FROM
WHERE
GROUP BY
HAVING
SELECT
Views
92
93
SELECT R.A
FROM R,S,T
WHERE R.A = S.A or R.A = T.A
Confusion Reigns!
94
95
Safety in Queries
Students(id)
Answer: Yes!
Golden Rule
96
97
More Dangers
Students(id,age)
Find all those numbers that are greater than the age of some student
Answer(x) <- Student(id,age), x>age.
98
Given
a relation Composite(x)
which lists all the composite numbers
Write a query to find
the prime numbers
Wrong Way
99
Recursion in Queries
Solution in Datalog
why?
100
101
Recursion in SQL3
Use with caution: Some kinds of recursive queries will not be allowed!
Final Example
102
103
Characteristics of PROLOG
Tuple-at-a-time
Backward Chaining
Top-Down
Goal-Oriented
Fixed-Evaluation Strategy (Depth-First)
Characteristics of RDBMSs
Set-at-a-time (recall relational algebra)
Forward Chaining
Bottom-Up
Query Optimizer figures a good evaluation strategy
Example
ancestor(X,X). parent(amy,bob).
ancestor(X,Y) <- parent(X,Z), ancestor(Z,Y).
Query
104
105
PROLOG Pitfalls
Previous Example
Linear Recursion
Tail Recursion
What if we make it
106
Magic produces
sg(john,Z)?
107
Vendor-Specific Implementations
ORACLE: PL/SQL (procedural extensions to SQL)
Open Database Connectivity Standard
Provides a standard API for transparent database access
used when database independence is important
used when required to connect to diverse data sources
108
109
Tradeoffs
ODBC
PL/SQL etc.
tailored to the details of the underlying DBMS
might not extend to heterogeneous domains
modeled after a specific programming language (e.g. Ada for Pl/SQL)
110
111
SELECT name
FROM Students, Classroll
WHERE Students.name = Classroll.studentname
AND Students.gpa = 4.0
AND Classroll.coursename = CS5614
Two Strategies
Do join and then filter out the ones with gpa <> 4.0 and course <> CS5614
Filter first the ones with gpa <> 4.0 and course <> CS5614 and then Join
Which is Better?
Always good to push selections as far down into the query parse tree
Three Requirements
A Search Space of Plans
A Cost Model (for Plan evaluation)
An Enumeration Algorithm
Ideally
Search Space: contains both good and efficient plans
Cost Models: cheap to compute and accurate
Enumeration Algorithm: efficient (not a monkey-typewriter algorithm)
Example of a Search Space
See Previous Slide
Examples of Cost Models
#(tuples) evaluation
#(main memory locations) etc.
Example of an enumeration algorithm
Sequential enumeration of a lattice of plans
Dynamic Programming vs. Greedy Approaches
112
113
#(Tuples) in a query
Easiest to compute for
Minimum: 0
In-between: #(R) (if Y is a foreign key for R and a key for S)
Maximum: #(R)#(S) (if Ys in R and S are all the same)
Assumptions for Join Size Estimation
Containment of Value Sets
Preservation of Value Sets
Containment of Value Sets
If V(R,Y) <= V(S,Y) then the Ys in R are a subset of the Ys in S
Satisfied when Y is a foreign key in R and a key in S
Preservation of Value Sets
#(R Join S,X) = #(R,X)
#(R Join S,Z) = #(S,Z)
why is this reasonable?
114
115
SELECT movietitle
FROM Actors,ActedIn
WHERE Actors.name = ActedIn.actorname
AND Actors.age = 23
116
117
What is T(n)?
Sample Values
1: 1
2: 1
3: 2
4: 5
5: 14
A formula
T(1) = 1 (Basis)
T(n) = T(1)T(n-1) + T(2)T(n-2) + ..... + T(n-1)T(1)
Classifications
Left-Deep Trees: All right children are leaves
Right-Deep Trees: All left children are leaves
Bushy Trees: Neither Left-Deep nor Right-Deep
Choosing a Join Order: Restricted to Left-Deep Trees
By Dynamic Programming: O(n!)
Greedy Approach: Make local selections
Example
Consider
R(a,b): #(R) = 1000, V(R,a) = 100, V(R,b) = 200
S(b,c): #(S) = 1000, V(S,b) = 100, V(S,c) = 500
T(c,d): #(T) = 1000, V(T,c) = 20, V(T,d) = 50
Possible Join Orders
(R Join S) Join T
(S Join R) Join T (same as above; why?)
(R Join T) Join S
(T Join R) Join S (same as above)
(S Join T) Join R
(T Join S) Join R (same as above)
Cost Estimation = Sizes of Intermediate Relations
(R Join S) Join T: 5000
(R Join T) Join S: 1000000
(S Join T) Join R: 2000
Best Plan = (S Join T) Join R
118
119
To normalize or not to
Sacrificing Redundancy Elimination
Sacrificing Dependency Elimination
Several Choices of Normalized Schemas
Vertical Partitioning Applications
Recomputing Indices
Histograms etc. might be outdated
Restricting Uses of Subqueries
Unnesting query blocks by Joins
Declining the Use of Indices
Table Scans for Small Tables
Rule-based optimization: Rewrite A=6 as A+0=6
Provide Redundant Tables
Decision-Support/ Data Mining Queries
120