You are on page 1of 9

∃ !

" #" $ %&


#& $'
( )
%* '

We will occasionally use this


arrow notation unless there
∀ !

' *
+ , $
%
&'%
!
&()"

*
"
'(
" "

$$ &
#$

+, % '
is danger of no confusion. - ) (
Ronald Graham . '

Elements of Ramsey Theory

. "
- - )!
/ 0 ! 1" 2 #"$3 ∈
#"$ $" -* *
"
- " $ <,>, =,≤,≥, ≠
#" $ ' - (!
0% %! " $"
-* " " $" ¬ p, p ∧ q, p ∨ q -* / $"
$" " " $
∃R( p(R)) -*
$" * ∀R( p(R)) -*

. ' 6 7
% ) ( 5

" * ∃X %∀ X {S |S ∈Sailors ∧ S.rating > 7}


% ( %X ' 8 $
%* / % - . -*
0 * $ * 9: * ; <' ,
4 % * 0! % % ) ) ( 5'
1 2 3 {S | ∃S1 ∈Sailors(S1.rating > 7
∧ S.sname = S1.sname
" ∧ S.age = S1.age)}
4 * * * $ $52
," = $>$ 1 3
* %$ * $" 7 $
%>$ "
4 * - * " % */ %
/ $

1
?
?
% %6 5 7 %(
{S | S∈Sailors ∧ S.rating > 7 ∧
8 9 {S |∃R(R∈Reserves ∧ R.sid
S∈Sailors ∧ S.rating > 7=∧S.sid
{S | S∈Sailors ∧ S.rating > 7 ∧ ∧ ∃B(B∈Boats ∧ B.bid = R.bid
∃R(R∈Reserves ∧ R.sid = S.sid
∃R(R∈Reserves ∧ R.sid = S.sid ∧ B.color
∧ R.bid = 103)} = ‘red’))}
∧ R.bid = 103)}
. @ A-* , B9CD
= * $∃ $ :(
* 57 - *, * * 7 ( % )'
" 0 . ( ( 7 %
/4;

! " " * E
EE ! )" F
. -* , Find sailors who’ve reserved all Red boats
* ∀) {S | S∈Sailors ∧
{S | S∈Sailors ∧ ∀B ∈ Boats ( B.color = > %7
∀B∈Boats (∃R∈Reserves ∃R(R∈Reserves ∧ S.sid = R.sid
(S.sid = R.sid ∧ B.bid = R.bid))}
∧ B.bid = R.bid))} Alternatively…
{S | S∈Sailors ∧
%
)
< <
∀B ∈ Boats ( B.color ≠ > %7 ∨
%<' ∃R(R∈Reserves ∧ S.sid = R.sid
∧ B.bid = R.bid))}

a b is the same as ¬a ∨ b H $ () 6-
∃ 0 0 *
( ;@ * '
b ? (
S|¬ S ∈ Sailors
T F ( ;
G
$ E
EEE! , * I
T T F $ * "
$
+, A #" % %%$!
a ?
%/ % *
)
)
$ / % ! J K*
F T T % 7 ( (
! / 0 ) ) # ')'
* )
-% /4$ , 0* 0 , (
)( B ' # 0 /4
C$

2
"" %
0 " H $∀
" % ) 0% %* 0
) ) D % '
• ∀x (P(x)) - is only true if P(x) is true for
)(
every x in the universe
$ $ / %
E • Usually:
$ / " $-* * %- ∀x ((x ∈ Boats) (x.color = “Red”)
"$* - " !
0 , ) ) * 0 • logical implication,
/ % "L * * *" $$ a b means that if a is true, b must be
- )( % , true
* $ "
a b is the same as ¬a ∨ b

. -* ,
{S | S∈Sailors ∧ {S | S∈Sailors ∧
∀B( (B∈Boats) ∀B( (B∈Boats ∧ B.color = “red”)
∃R(R∈Reserves ∧ S.sid = R.sid ∃R(R∈Reserves ∧ S.sid = R.sid
∧ B.bid = R.bid))} ∧ B.bid = R.bid))}
% < % <
< <
) % ' ) % '
{S | S∈Sailors ∧ {S | S∈Sailors ∧
∀B(¬(B∈Boats) ∨ ∀B(¬(B∈Boats) ∨ (B.color ≠ “red”) ∨
∃R(R∈Reserves ∧ S.sid = R.sid ∃R(R∈Reserves ∧ S.sid = R.sid
∧ B.bid = R.bid))} ∧ B.bid = R.bid))}

%
/4! " / 0
- F ) % !
4 ) ) * 0 ) % '
A " ( ) ) !
&&4G & & 4 ) )
$ " $% *" D
&H4G & H 4 ) )
- %
" &<H ( '
* % " $ /
Life is just a bowl of queries. 0 - * "L & J*
* * - *
-Anon G " $ ) *
(not Forrest Gump) $ * *%

3
* % !!
• CREATE TABLE table_name
*" - % / % ( { column_name data_type [ DEFAULT default_expr ] [
&9;;; column_constraint [, ... ] ] | table_constraint } [, ... ] )
= $% %
G MN 7 & O " • Data Types (PostgreSQL) include:
8 % $-* *- 6 * ' %
I character(n) – fixed-length character string
&>CC) $ character varying(n) – variable-length character string
&;> smallint, integer, bigint, numeric, real, double precision
8 % " " " date, time, timestamp, …
6 * " M / O serial - unique ID for indexing and cross reference
" % " …
P8 J * ) * $ • PostgreSQL also allows OIDs, arrays, inheritance, rules…
" * conformance to the SQL-1999 standard is variable so we won’t use
these in the project.

-J " -J
• CREATE TABLE table_name • CREATE TABLE table_name
( { column_name data_type [ DEFAULT default_expr ] [ ( { column_name data_type [ DEFAULT default_expr ] [
column_constraint [, ... ] ] | table_constraint } [, ... ] ) column_constraint [, ... ] ] | table_constraint } [, ... ] )

Column Constraints: Table Constraints:


• [ CONSTRAINT constraint_name ] • [ CONSTRAINT constraint_name ]
{ NOT NULL | NULL | UNIQUE | PRIMARY KEY | CHECK { UNIQUE ( column_name [, ... ] ) |
(expression) | PRIMARY KEY ( column_name [, ... ] ) |
REFERENCES reftable [ ( refcolumn ) ] [ ON DELETE action ] [ CHECK ( expression ) |
ON UPDATE action ] }
FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ (
action is one of: refcolumn [, ... ] ) ] [ ON DELETE action ] [ ON UPDATE
NO ACTION, CASCADE, SET NULL, SET DEFAULT action ] }
expression for column constraint must produce a boolean result
and reference the related column’s value only. Here, expressions, keys, etc can include multiple columns

()" * !8
(0 ( 0' ($"
Q0 R 6 G80 ST(S & / *$ -
0 Q0 UC
!( G80 D $ 9:% - -
V !0 (
0 Q0 9C
N= 0G = H=G H( V
.N (G
W=T(S (.( (= ( sid name login age gpa
N=!( ( (=N0 G N= 53666 Jones jones@cs 18 3.4
K 53688 Smith smith@ee 18 3.2
(0 ( 0' (
!( G80 D 6 G 80 ST(S
$ 7 " *$
" 0 Q0 UC
N= 0G = 9 Q( T @ 9CC0=! " X@ < ,
K

4
SELECT [DISTINCT] target-list
% 8 FROM relation-list
$
% 7 - $ - ' % WHERE qualification

& 0 $ "
%- * & $ * "
!"# $%&
& 0 $ $ &
sid cid grade sid name login age gpa / $ " " 0=! N
53831 Carnatic101 C 53666 Jones jones@cs 18 3.4 =N
53831 Reggae203 B 53688 Smith smith@ee 18 3.2 " 0 0 9 0 > -*
53650 Topology112 A $<, >, = , ≤, ≥, ≠
53666 History105 B Note: obviously
no referential !G G
= %- * *
integrity - *
S.name E.cid constraints have G (( * $ *
#
Jones History105 " I M" O
been used here.

% " 9 6
" $ / % $ " $
*$ - %
9 . N8 " & $ S.sid S.name S.login S.age S.gpa E.sid E.cid E.grade
( 53666 Jones jones@cs 18 3.4 53831 Carnatic101 C
53666 Jones jones@cs 18 3.4 53832 Reggae203 B
> +Q( ( * 53666 Jones jones@cs 18 3.4 53650 Topology112 A
* $ M O 53666 Jones jones@cs 18 3.4 53666 History105 B
53688 Smith smith@ee 18 3.2 53831 Carnatic101 C
D (( ! - $ 53688 Smith smith@ee 18 3.2 53831 Reggae203 B
M 7 O 53688 Smith smith@ee 18 3.2 53650 Topology112 A
53688 Smith smith@ee 18 3.2 53666 History105 B
UG $!G G= $ " -
6 %* $
$ -% "
/ %I
0 "L - $ " $
$ !"# $%&
* " -

>! * $ D! H- "

S.sid S.name S.login S.age S.gpa E.sid E.cid E.grade S.sid S.name S.login S.age S.gpa E.sid E.cid E.grade
53666 Jones jones@cs 18 3.4 53831 Carnatic101 C 53666 Jones jones@cs 18 3.4 53831 Carnatic101 C
53666 Jones jones@cs 18 3.4 53832 Reggae203 B 53666 Jones jones@cs 18 3.4 53832 Reggae203 B
53666 Jones jones@cs 18 3.4 53650 Topology112 A 53666 Jones jones@cs 18 3.4 53650 Topology112 A
53666 Jones jones@cs 18 3.4 53666 History105 B 53666 Jones jones@cs 18 3.4 53666 History105 B
53688 Smith smith@ee 18 3.2 53831 Carnatic101 C 53688 Smith smith@ee 18 3.2 53831 Carnatic101 C
53688 Smith smith@ee 18 3.2 53831 Reggae203 B 53688 Smith smith@ee 18 3.2 53831 Reggae203 B
53688 Smith smith@ee 18 3.2 53650 Topology112 A 53688 Smith smith@ee 18 3.2 53650 Topology112 A
53688 Smith smith@ee 18 3.2 53666 History105 B 53688 Smith smith@ee 18 3.2 53666 History105 B

!"# $%& !"# $%&

5
Reserves sid bid day
()" *"
=- * ! 22 101 10/10/96
! !% ' (" ) * ( ! + , +
95 103 11/12/96 ! '-./ (" ) ! /
+ - * Sailors sid
$ sname rating age
! !% % '0 (" ) * ( ! + , +
22 Dustin 7 45.0 0 ! '-./ ! ' .//
)" 31 Lubber 8 55.5
95 Bob 3 63.5 ! !% 1 '
G
$* % (" ) "
$ * Boats 0 (" ) 2 #!
bid bname color * ( ! + , + ' 0 2/
101 Interlake blue ()" , + '0 / " % /
%*
*- 102 Interlake red
- * 103 Clipper green
" $
$E 104 Marine red

0 * ? % " =
SELECT sname M O - **
FROM Sailors, Reserves * . N8
WHERE Sailors.sid=Reserves.sid
AND bid=103
- " /
= -* " %
(sid) sname rating age (sid) bid day $ )" $ " " " "
22 dustin 7 45.0 22 101 10/10/96 . N8 M $&7 O
22 dustin 7 45.0 95 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96 1
31 lubber 8 55.5 95 103 11/12/96 1 !"# 0 .3
95 Bob 3 63.5 22 101 10/10/96
95 Bob 3 63.5 95 103 11/12/96 - 1
!"# 0 .3

8 = . -* ,
Q , )" -*
/ $
&7 )"
4 4 2 2 1
4 2
4 5 2
+ !G G
= * / %"
= * %MYO $ $$ E
% , - 7 +* * $
$ $ % "
* (( E
+ !G G
= * $* / %
4 " $
$ E
4 5 -.

6
()
*" ) (( "
* -,
M (, ; $ " *
H 0 " "
67 ! - ! -
67 ! - ! -
$# 8
(, $%9:08
* ) +Q( (
! - ! - 5V, $ % * 5Z, $
- C " %*
- - 6

. , $ -* , . , $ -* , %
H=GN= " * $ %
- & " $ -* * G
$- " % N %0=! *
*" * $ / / %- * - - +*%E
G $&7
% % 1
0 % 0 !"#
'% $ 8 % $ 8/ % % 1
% %% %-1
Vs. 0- % 0 !"#
1 -

% % 1 '%!"# 0$ %8 !"#
0 % $ 8/
0 % 0 !"# % $ 8 !"# - 0 %- 0
<"( " !"# '% $ 8 !"# %- $ 8/

% % 1
0 % 0 !"# % $ 8

0=! F =
Key field!
I"+ + "!%
? % A /4! LM+ +
( .' ( % /4* 0;
% %
0 E 1 0 % . N8 Q0 G
=W
(
' Names of sailors who’ve reserved boat # 103:
!"# 0 % 0
!"# % $ 8
- ,! +J +A"
# %H?
I@ $ ("
(" '
? %% /4BK 1
% %( 0 % %
0 .3/
0 % 7 1
' " % 7 %8 9 I'
I:"?
' 6 I !"# 0 % 0 " % % %* !
!"# % $ 8
* $ . *
* */ $ % " * / %

7
= -*
Find names of sailors who’ve reserved boat # 103: 8 & " N

L 7 %0 ?
I +J? " %@I?/@+'
=( ' I:"?
I I:"+J? " %I:"@I?/@+'
1 - ( ! -IO -44
0 .3 !"# / % ) )
+J? " . ?I' %M !
0I:"+J? "
? @I?/@+ % %N %(0 '(% %
( 8 9' 5 !"+ ' -
H=G H( * $ / %K -
- $ 8/
(* 0 ( % '
* $ / % $ * / %
I

- G
=( ( H G
=
Find sid’s of sailors who’ve reserved both a red and a green boat: !
Find sailors who’ve reserved all boats.
% % 1
0 % 0
!"# % $ 8
!"# (" ' - SELECT S.sname
FROM Sailors S Sailors S such that ...
% %- 1 -
- 0 %- 0 WHERE NOT EXISTS (SELECT B.bid there is no boat B without
!"# %- $ 8/ FROM Boats B ...
WHERE NOT EXISTS (SELECT R.bid
0 +J +A"* E )I:"?I' a Reserves tuple showing S reserved B FROM Reserves R
M %0 ) % # %7 $ WHERE R.bid=B.bid
7 %( % %) ( P AND R.sid=S.sid))

COUNT (*)
' & "" % COUNT ( [DISTINCT] A)
SUM ( [DISTINCT] A)
0 $* " - & 0 N AVG ( [DISTINCT] A)
$ / % " ) , MAX (A)
$ % * $* )( ' MIN (A)

" single column


SELECT COUNT (*)
" $
$ * FROM Sailors S

SELECT AVG (S.age)


% % " %- % - / %
FROM Sailors S
* % " $ $ $ WHERE S.rating=10
-% %) / % $
*- - SELECT COUNT (DISTINCT S.rating)
FROM Sailors S
" $ % % * WHERE S.sname=‘Bob’
$ + /

8
COUNT (*)
COUNT ( [DISTINCT] A)
SUM ( [DISTINCT] A) . " $*
0 N AVG ( [DISTINCT] A)
MAX (A) SELECT S.sname, MAX (S.age)
" * 0 FROM Sailors S
MIN (A)
;
single column SELECT S.sname, S.age
FROM Sailors S
SELECT S.sname " %* 0 *
WHERE S.age =
FROM Sailors S %* 0
(SELECT MAX (S2.age)
WHERE S.rating= (SELECT MAX(S2.rating) - J
;> FROM Sailors S2)
FROM Sailors S2)
" SELECT S.sname, S.age
% " FROM Sailors S
SELECT AVG ( DISTINCT S.age) 6 " WHERE (SELECT MAX (S2.age)
FROM Sailors S FROM Sailors S2)
WHERE S.rating=10 = S.age

W NH6'S Q0 G
=W
7 % )) )
#
* 0 )$ '
" " - - % *" * $
$
% ! % ) 0 )
) '
G - , -* -" %
) -* * $ * I
- -* $ " 9 9CK
- - 9C/ * * I
SELECT MIN (S.age)
For i = 1, 2, ... , 10: FROM Sailors S
WHERE S.rating = i

You might also like