Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
26 views5 pages

34-SQL 4pp

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

34-SQL 4pp

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Announcements

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope
(or static scope) [You can see what names are in scope by inspecting the definition]

Lexical scope: The parent of a frame is the environment in which a procedure was defined
Dynamic scope: The parent of a frame is the environment in which a procedure was called

Special form to create dynamically


Dynamic Scope scoped procedures (mu special form
Global frame μ
only exists in Project 4 Scheme) f (λ (x) ...)
mu
g (λ (x y) ...)
(define f (lambda (x) (+ x y)))

(define g (lambda (x y) (f (+ x x)))) f1: g [parent=global]

x 3
(g 3 7)
y 7
Lexical scope: The parent for f's frame is the global frame
Error: unknown identifier: y f2: f [parent=global]
f1
Dynamic scope: The parent for f's frame is g's frame x 6

13
4
Database Management Systems

Database management systems (DBMS) are important, heavily used, and interesting!

A table is a collection of records, which are rows that have a value for each column

A table has Latitude Longitude Name A column has a


Databases columns and rows
38 122 Berkeley
name and a type

A row has a value 42 71 Cambridge


for each column
45 93 Minneapolis

The Structured Query Language (SQL) is perhaps the most widely used programming language

SQL is a declarative programming language

Declarative Programming

In declarative languages such as SQL & Prolog: Cities:

• A "program" is a description of the desired result latitude longitude name


• The interpreter figures out how to generate the result
38 122 Berkeley

In imperative languages such as Python & Scheme: 42 71 Cambridge


• A "program" is a description of computational processes
45 93 Minneapolis Structured Query Language (SQL)
• The interpreter carries out execution/evaluation rules
region name
create table cities as
select 38 as latitude, 122 as longitude, "Berkeley" as name union west coast Berkeley
select 42, 71, "Cambridge" union other Minneapolis
select 45, 93, "Minneapolis";
other Cambridge

select "west coast" as region, name from cities where longitude >= 115 union
select "other", name from cities where longitude < 115;

7
SQL Overview Getting Started with SQL

The SQL language is an ANSI and ISO standard, but DBMS's implement custom variants Install sqlite (version 3.8.3 or later): http://sqlite.org/download.html
• A select statement creates a new table, either from scratch or by projecting a table
Use sqlite online: code.cs61a.org/sql
• A create table statement gives a global name to a table
• Lots of other statements exist: analyze, delete, explain, insert, replace, update, etc.
• Most of the important action is in the select statement

Today's theme:

9 10
http://awhimsicalbohemian.typepad.com/.a/6a00e5538b84f3883301538dfa8f19970b-800wi

Selecting Value Literals Naming Tables

A select statement always includes a comma-separated list of column descriptions SQL is often used as an interactive language

A column description is an expression, optionally followed by as and a column name The result of a select statement is displayed to the user, but not stored

select [expression] as [name], [expression] as [name] ,


; ... A create table statement gives the result a name

Selecting literals creates a one-row table Parents:


create table [name] as [select statement];
The union of two select statements is a table Eisenhower parent Eisenhower child
containing the rows of both of their results
create table parents as abraham barack
select "delano" as parent, "herbert" as child;union Fillmore select "delano" as parent, "herbert" as child union abraham Fillmore clinton
select "abraham" , "barack" union select "abraham" , "barack" union
delano herbert
select "abraham" , "clinton" union select "abraham" , "clinton" union
fillmore abraham
select "fillmore" , "abraham" union select "fillmore" , "abraham" union
Abraham Delano Grover Afillmore
braham Delano
delano Grover
select "fillmore" , "delano" union select "fillmore" , "delano" union
select "fillmore" , "grover" union select "fillmore" , "grover" union fillmore grover

select "eisenhower" , "fillmore"; Barack Clinton Herbert select "eisenhower" , "fillmore"; Barack Clinton
eisenhower Herbert
fillmore

11 12
Select Statements Project Existing Tables

A select statement can specify an input table using a from clause

A subset of the rows of the input table can be selected using a where clause

An ordering over the remaining rows can be declared using an order by clause

Column descriptions determine how each input row is projected to a result row

Projecting Tables select [expression] as [name], [expression] as [name], ... ; Eisenhower


select [columns] from [table] where [condition] order by [order];
Fillmore
select child from parents where parent = "abraham";

select parent from parents where parent > child;


Abraham Delano Grover
child parent
barack fillmore
clinton fillmore (Demo) Barack Clinton Herbert

14

Arithmetic in Select Expressions

In a select expression, column names evaluate to row values

Arithmetic expressions can combine row values and constants

create table lift as


select 101 as chair, 2 as single, 2 as couple union
Arithmetic select 102 , 0 , 3 union
select 103 , 4 , 1;

select chair, single + 2 * couple as total from lift;


101

chair total
101 6 102

102 6
103 6 103

16
Discussion Question
Given the table ints that describes how to sum powers of 2 to form various integers
create table ints as
select "zero" as word, 0 as one, 0 as two, 0 as four, 0 as eight union
select "one" , 1 , 0 , 0 , 0 union
select "two" , 0 , 2 , 0 , 0 union
select "three" , 1 , 2 , 0 , 0 union
select "four" , 0 , 0 , 4 , 0 union
select "five" , 1 , 0 , 4 , 0 union
select "six" , 0 , 2 , 4 , 0 union
select "seven" , 1 , 2 , 4 , 0 union
select "eight" , 0 , 0 , 0 , 8 union
select "nine" , 1 , 0 , 0 , 8;
(A) Write a select statement for a two-column (B) Write a select statement for the
table of the word and value for each integer word names of the powers of two

word value word


zero 0 one
one 1 two
two 2 four
three 3 eight
... ... (Demo)
17

You might also like