Declarative_Programming
声明式语言
"程序"是对预期结果的描述
interpreter想出如何产生结果
命令式语言
"程序"是对计算过程的描述
interpreter执行execution/evaluation规则用 Python 描述 SQL
output_table = [] for row in FROM(*input_tables): if WHERE(row): output_table += [SELECT(row)] if ORDER_BY: output_table = ORDER_BY(output_table) if LIMIT: output_table = output_table[:LIMIT]注意,
ORDER BY和LIMIT子句仅在确定了输出表中的所有行之后才应用。
SQL
SQL(Structured Query Language 结构化查询语言)
select语句可以从头开始创建一个新表,也可以通过投射表来创建一个新表create table语句给表起了一个全局名称
还有很多其他语句:
analyze, delete, explain, insert, replace, update等。大多数重要的操作是在
select语句中的
select
select语句总是包含一个以逗号分隔的列描述列表。列的描述是一个表达式,后面可选择as和列名
select [expression] as [name]两个选择语句的
union是一个包含它们两个结果的行的表。select "delano" as parent, "herbert" as child union select "abraham" , "barack" union select "abraham" , "clinton" union select "fillmore" , "abraham" union select "fillmore" , "delano" union select "fillmore" , "grover" union select "eisenhower" , "fillmore";select 的结果只呈现给用户,不储存
create table语句给结果一个名字create table [name] as [select statement];create table parents as select "delano" as parent, "herbert" as child union select "abraham" , "barack" union select "abraham" , "clinton" union select "fillmore" , "abraham" union select "fillmore" , "delano" union select "fillmore" , "grover" union select "eisenhower" , "fillmore";
Projecting Tables(投影表)
select呈现现有表格一个
select语句可以使用from子句指定一个输入表select [columns] from [table] where [condition] order by [order][column]指的是select [expression] as [name], [expression] as [name], ... ;中的[name]select child from parents where parent = "abraham";select parent from parents where parent > child;
可以使用
where子句选择输入表中的行的子集。可以使用一个
order by子句来声明剩余行的排序。列的描述决定了每条输入行如何投影到结果行。
Arithmetic(算术)
在
select表达式中,column名是row值。算术表达式可以将
row值和常量相结合
create table lift as
select 101 as chair, 2 as single, 2 as couple union
select 102 , 0 , 3 union
select 103 , 4 , 1;
select chair, single + 2 * couple as total from lift;char
total
101
6
102
6
103
6
Last updated
Was this helpful?