当前位置: 首页 > article >正文

Introduction to SQL

目录

SQL特点

​编辑

Select-From-Where Statements

Meaning of Single-Relation Query

Operational Semantics

* In SELECT clauses

Complex Conditions in WHERE Clause

PATTERNS

NULL Values

Three-Valued Logic

 Multirelation Queries

Aggregations

NULL’s Ignored in Aggregation

Grouping

HAVING Clauses


SQL特点

  • SQL是一种结构化查询语言
  • SQL语言包括DQL(数据查询)、DCL(数据控制)、DDL(数据定义)、DML(数据操纵)
    • 因为关系模式中,实体与实体之间用关系表示联系,所以操作符比较简单,每种操作只需要一种操作符表示
  • SQL语言的操作对象都是集合(查询、插入、修改、删除)
  • SQL语言既是自含式语言,同时也是嵌入式语言
  • 语言简洁
    • 核心功能只有九个

Select-From-Where Statements

SELECT desired attributes

FROM one or more tables

WHERE condition about tuples of

the tables

Meaning of Single-Relation Query

  • Begin with the relation in the FROM clause.

  • Apply the selection indicated by the WHERE clause.

  • Apply the extended projection indicated by the SELECT clause

FROM确定数据源,WHERE进行数据的筛选,SELETE对属性进行投影

Operational Semantics

  • Think of a tuple variable visiting each tuple of the relation mentioned in FROM.

  • Check if the “current” tuple satisfies the WHERE clause.

  • If so, compute the attributes or expressions of the SELECT clause using the components of this tuple.

可以认为元组变量将访问所有FROM中涉及的关系,然后循环的检验是否满足WHERE中的条件,最后将满足条件的数据放到返回结果中,并且进行扩展投影        

* In SELECT clauses

When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.”

SELETE中既可以是属性也可以是常量

Complex Conditions in WHERE Clause

PATTERNS

  • A condition can compare a string to a pattern by: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern>

  • Pattern is a quoted string with % = “any string”; _ = “any character. ”

  • % 0~任意多个字符

  • _ 任意单个字符

  • [] 集合范围内的任意单个字符

  • [^] 不在集合范围内的任意单个字符

  • [-] 前一字符至后一字符中的任一字符

  • ESCAPE 取消后面通配字符的通配作用(也就是说有时候需要使用到字符本身)

NULL Values

  • Tuples in SQL relations can have NULL as a value for one or more components.(SQL关系中允许元组有一种或多种属性是NULL)

  • Meaning depends on context. Two common cases:

Missing value : e.g., we know Joe’s Bar has some address, but we don’t know what it is.(缺省值)

Inapplicable : e.g., the value of attribute spouse for an unmarried person.(不适用值)

Three-Valued Logic

  • To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½.

  • AND = MIN; OR = MAX, NOT(x) = 1-x.

  • Example:

TRUE AND (FALSE OR NOT(UNKNOWN)) =

MIN(1, MAX(0, (1 - ½ ))) =

MIN(1, MAX(0, ½ )) = MIN(1, ½ ) = ½.

 Multirelation Queries

  • Interesting queries often combine data from more than one relation.

  • We can address several relations in one query by listing them all in the FROM clause.

  • Distinguish attributes of the same name by “<relation>.<attribute>” .

Example: Joining Two Relations

Using relations Likes(drinker, beer) and Frequents(drinker, bar), find the beers liked by at least one person who frequents Joe’s Bar.

SELECT beer
ROM Likes, Frequents
WHERE bar = ’Joe’’s Bar’ AND
Frequents.drinker = Likes.drinker;

Aggregations

  • SUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column.

  • Also, COUNT(*) counts the number of tuples.

NULL’s Ignored in Aggregation

  • NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column.

  • But if there are no non-NULL values in a column, then the result of the aggregation is NULL.

NULL值不参与聚合函数的运算

当表为空集时,返回结果为0

Grouping

  • We may follow a SELECT-FROM-WHERE expression by GROUP BY and a list of attributes.(在select-from-where语句后面接GroupBy和一系列属性)
  • The relation that results from the SELECT-FROM-WHERE is grouped according to the values of all those attributes, and any aggregation is
  • applied only within each group.(select返回的结果将会被Group语句进行分组,并且groupby只在分组内应用)

select语句后面是可以接单属性的,当且仅当group后面接的属性

HAVING Clauses

  • HAVING <condition> may follow a GROUP BY clause.
  • If so, the condition applies to each group, and groups not satisfying the condition are eliminated.

注意:

  • WHERE:在分组前过滤行。

  • GROUP BY:对过滤后的行分组。

  • HAVING:对分组后的聚合结果过滤。

例题:

http://www.lryc.cn/news/2387498.html

相关文章:

  • 计算机视觉---YOLOv3
  • #RabbitMQ# 消息队列进阶
  • React从基础入门到高级实战:React 核心技术 - React Router:路由管理
  • 【深度学习】损失“三位一体”——从 Fisher 的最大似然到 Shannon 的交叉熵再到 KL 散度,并走进 PET·P-Tuning微调·知识蒸馏的实战
  • 5 分钟速通密码学!
  • Linux——IP协议
  • Lua 脚本在 Redis 中的运用-24 (使用 Lua 脚本实现原子计数器)
  • Linux信号量(32)
  • 技术视界 | 打造“有脑有身”的机器人:ABC大脑架构深度解析(上)
  • 使用堡塔和XShell
  • 软件项目交付阶段,验收报告记录了什么?有哪些标准要求?
  • LightGBM的python实现及参数优化
  • 封装渐变堆叠柱状图组件附完整代码
  • 分布式项目保证消息幂等性的常见策略
  • 山东大学软件学院创新项目实训开发日志——第十三周
  • 如何在sublime text中批量为每一行开头或者结尾添加删除指定内容
  • Cesium 透明渐变墙 解决方案
  • 网络原理与 TCP/IP 协议详解
  • day022-定时任务-故障案例与发送邮件
  • 新增 git submodule 子模块
  • List优雅分组
  • Linux 使用 Docker 安装 Milvus的两种方式
  • AR眼镜+AI视频盒子+视频监控联网平台:消防救援的智能革命
  • 编程技能:字符串函数10,strchr
  • 使用tunasync部署企业内部开源软件镜像站-Centos Stream 9
  • c/c++的opencv像素级操作二值化
  • C++----Vector的模拟实现
  • Mac redis下载和安装
  • [25-cv-05718]BSF律所代理潮流品牌KAWS公仔(商标+版权)
  • 【PhysUnits】9 取负重载(negation.rs)