-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture34.sql
More file actions
81 lines (56 loc) · 1.74 KB
/
Lecture34.sql
File metadata and controls
81 lines (56 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Local Tables
"""
A create table statement names a table globally
A with clause of a select statement names a table that is local to the statement
"""
create table parents as
select "abraham" as parent, "barack" as child union
select "abraham" , "clinton" union
select "delano" , "herbert" union
select "fillmore" , "abraham" union
select "fillmore" , "delano" union
select "fillmore" , "grover" union
select "eisenhower" , "fillmore";
create table parents as
select "abraham" as parent, "barack" as child union
...
with
best(dog) as (
select "eisenhower" union
select "barack"
)
select parent from parents, best where child=dog;
"""
parents is a global table
best is a local table, it
"""
# Local Tables can be Declared Recursively
"""
An ancestor is your parent or an ancestor of your parent
"""
with
ancestors(ancestor, descendent) as (
select ancestor, child
from ancestor, parents
where parent = descendent
)
select ancestor from ancestors where descendent = "herbert" ;
To create a table with a global name, you need to select the contents of the local table
create table odds as
with
odds(n) as (
select 1 union
select n+2 from odds where n <15
)
select n from odds
"""Which names above can change without affecting the result?"""
# Limits on Recursive Select Statements
"""
Recurisve table definitions are only possible within a with clause
No mutual recursion: two or more tables cannot be defined in terms of each other
No tree recursion: the table being defined can only appear once in a from clause
"""
# Language is Recursive
"""
Noun phrases can contain relative pronouns that introduce relative clauses
"""