数据库基础 课堂笔记 1-3章

Introduction

专业术语

  • DBMS

    • database manegement system
  • Database Users

    • End users 最终用户、交互式用户
      • Casual users
      • Naive users
    • Application programmers 应用程序员
    • DataBase Administrators 数据库管理员 (DBA)
  • 数据库系统的结构

  • 在这里插入图片描述

  • 数据模型

  • 在这里插入图片描述

在这里插入图片描述



Chapter 2 The Relational Model

2.3 Relational Rules

  • RULE 1 : First Normal Form Rule (第一范式规则)

    • In defining tables, the relational model insists that columns that have multi-valued attributes (sometimes called repeating fields) are not permitted
    • column values must be simple types
    • E.G.
      • NOT ALLOWED:在这里插入图片描述
      • the dependents column can’t be defined like this
      • the solution in relational model goes like this: (use two tabels)
        在这里插入图片描述
    • not over yet, the ename col is also not legal, since its type is not simple types, we could not construct a tabel with a column name ename that had three components: ename.fname, ename.lname and ename.mi(类似于面向对象的用法)

  • RULE 2: Access Rows by Content Only Rule

    Rule tow implies that there is no order on the rows, there shoude be no way that a queryin a (pure) relational languagecan ask, for example to retrieve the third row of the table. This simply says that a relation is a set of tuples. which also implies that a pointer to a row to retrieve it at a later time is not permitted

    However, many comercial database systems have always broken RULE 2, prividing a method to retrieve a row of a table by its row identification number(RID or ROWID or TID tuple identification number).

    retrieval by RID is sometimes useful to a database administrator in examing situations where it is suspected that the rows if the table are stored in the proper way.

    though it is sometimes useful, we should not perform normal queries in terms of saved RID values, since RIDS are subject to change when tables are updated in certain circumstances

    ( relational model also states that there should be bo order to the columns of a relation, this rule is broken by the standard SQL language)

  • RULE 3 : The Unique Row Rule
    two tuples in a relation cannot be identical in all column values at once

2.4 Keys, Superkeys, and Null Values

  • KEY在这里插入图片描述

    在这里插入图片描述

  • SuperKey

    we refer to a set of attributes that fulfills property 1 but not necessarily property 2 as a superkey

    so a key is always a superkey, but not vice versa

    A kry or superkey for a table is required to remain a key or superkey as new rows are added

    once keys are defined, it becomes impossible for normal updatesof the table to cause the uniqueness condition for such keys to fail

  • THEOREM 2.4.2 Every table has at least one key

    prof starts from S1 is a superkey

  • DEFINITION 2.4.3 Primary Key of a Table

    (the various keys of a relation are often known as candidate keys, the name implies a selection process whereby one if the candidates will be designated as the primary key)

    A primary key of a table T is the candidate key chosen by the ddatabase designer to uniquely indentify specific rows of T

  • Null Values

  • Rule 4 : Entity Integrity Rule
    No column belonging to a primary key of a table T is allowed to take on null values for any row in T
    (primary key values are the designated identifier for the row,until we have the identifier value settled, we will not allow the eow to be stroed in the table)



2.5 Relational Algebra

there are two types of operations in relational algebra:

  • set-theoretic opreations
    • making use of the fact that tables are essentially sets of rows
      在这里插入图片描述
  • native relational operations
    • focusing on the structure of the rows
      在这里插入图片描述


2.6 Set-Theoretic Operations

Gor sets of rows to be involved in unions, intersections, or differences and to form new tables, the rows in different sets must have the same heading structure

在这里插入图片描述

only tables that are compatible can be involved in unions, intersections, and differences

在这里插入图片描述

  • Assignment and Alias
    在这里插入图片描述
    在这里插入图片描述
  • The Product Operation
    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述在这里插入图片描述
在这里插入图片描述


2.7 Native Relational Operations

  • The Projection Operation
    在这里插入图片描述
    Note that different rows of a table R, When projected onto a subset of columns, may become identical. When this happens, the projection operator will also delete duplicate row, until only onecopy of each duplicate set of rows existed in cloumns that were deleted

  • The Selection Operation
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    examples:

    • 在这里插入图片描述
    • 在这里插入图片描述
    • 在这里插入图片描述

  • The Join Operation

    在这里插入图片描述
    例子:
    在这里插入图片描述
    在这里插入图片描述
    要注意CHEAPS投射到pid上是很有必要的, 不然第一个join会产生一个更大的表, 这个更大的表可能就多出来和 CUSTOMERS 属性一样的列, 从而第二次连接时多了额外的限制条件。而使用了投射到pid上就可以避免额外信息的出现

  • The Division Operation
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    不提前投射的话,约束条件就会太多,导致失去一些情况在这里插入图片描述

    2.7.3 Precedence of Relational Operations

    在这里插入图片描述

2.8 The Interdependence of Operations

在这里插入图片描述
证明用韦恩图

在这里插入图片描述
在这里插入图片描述

2.10 Other Relational Operations

在这里插入图片描述

  • Outer Join
    在这里插入图片描述
    The idea of the left outer and right outer join operators is that we might wish to preserve unmatched rows on one side only.

  • Theta Join
    在这里插入图片描述
    在这里插入图片描述


    Chapter 3 Basic SQL Query Language

    3.3 Simple Select Statements

  • 格式

    select aid, aname from agents where city = 'New York';
    
  • The simple * in the select list is a shorthand symbol meaning retrieve all fields

    select * from customers;
    
  • 注意区分下面两个语句,一个会出现大量重复,还有一个是检索出来的结果只出现一次

    select pid from orders;
    select distinct  pid from orders;
    

  • retrive from several tables
    在这里插入图片描述
    在这里插入图片描述

  • 可以运算
    在这里插入图片描述
    在这里插入图片描述

  • 技巧: 从一个方向搜索,避免重复
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    这里的DISTINCT不能省
    在这里插入图片描述

    • an important example
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述

3.4 Subqueries

  • The IN Predicate
  • The idea: a set of values is returned from the evaluation of the Subquery and then a test for membership is performed by the outer course of events

在这里插入图片描述

Membership can be tested not only against the set provided by a Subquery but also against sets that are explicitly defined, as we defined in the follwing example
在这里插入图片描述
在这里插入图片描述

As we see in the following example, multiple levels of subquery nesting are allowed
在这里插入图片描述
It does not require a qualified reference to a local column name.
在这里插入图片描述

  • we can provide an inner Subquery with data that orifinates in the outer Select, but not vice versa
    在这里插入图片描述
    在这里插入图片描述

  • In FORM
    在这里插入图片描述

  • We can test a pair of values
    在这里插入图片描述
    在这里插入图片描述

  • The Quantified Comparison Predicate

    A quantified predicate compares the simple value of an expression with the result of a Subquery.
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    Note that, while the predicate expr =SOME(Subquery) means the same as expr IN(Subquery), the form expr NOT IN (Subquery) is not the same as expr <>SOME( Subquery). Instead, NOT IN (Subquery)is identical to expr <>ALL(Subquery)

    • 少用any, 多用some
      在这里插入图片描述

  • The EXISTS Predicate

    The EXISTS predicate tests whether the set of rows retrieved in a Subquery is non-empty.

    在这里插入图片描述
    The predicate EXISTS(Subquery) is TRUE if and only if the Subquery returns a non empty set( if there exists an element in the set).

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • NOT EXISTS can be used to implement the MINUS operator from relational algebra
    在这里插入图片描述
    EXCEPT operator directly copies the effect of the MINUS operator
    在这里插入图片描述

3.5 UNION Operators and FOR ALL Conditions

  • THE UNION Operator
    Any number of Subqueries that produce compatible tables can be combined with repeated use of the UNION syntax of Figure 3.9
    在这里插入图片描述
    在这里插入图片描述

  • Division: SQL “FOR ALL…” Conditions
    在这里插入图片描述
    先找反例
    在这里插入图片描述
    更进一步
    在这里插入图片描述
    最终:
    在这里插入图片描述
    一个综合性的例子
    在这里插入图片描述
    思考步骤:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 最好加上括号
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述


3.6 Some Advanced SQL Syntax

  • The INTERSECT and EXCEPT Operators in Advanced SQL
    在这里插入图片描述
    figure 3.9 indicates that two Subquery terms can be connected by a UNION, INTERSECT, or EXCEPT to produce a new Subquery
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • Join Forms in Advanced SQL
    在这里插入图片描述
    在这里插入图片描述
    在FROM里使用Subquery
    在这里插入图片描述

  • (INNER) JOIN
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • OUTER JOIN
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述


3.7 Set Functions in SQL

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
NOTE carefully that null values in a column are not counted
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • Handling Null Values
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述


3.8 Groups of Rows in SQL

在这里插入图片描述
在这里插入图片描述

  • However, the GROUP BY clause of a Select statement can contain more than one column name.
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述


3.9 A complrte Description of SQL Select

在这里插入图片描述
As we see in this general form, the ORDER BY clause is not allowed to appear in Subquery forms, but only in full Select statements

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • Identifiers

  • 用引号括起来的别名
    在这里插入图片描述

  • Expressions, Predicates, and the search_condition

Expressions defined below can also appear in the select list of a Select statement.

  • numeric在这里插入图片描述在这里插入图片描述

  • string
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • Scalar Subqueries as Expressions: Advanced SQL
    A scalar subquery is a Subquery that returns a single value rather than a set of more than one row or more than one cloumn

Scalar Subqueries 可以用在 select list 里, 也可以用在 where clause 里

在这里插入图片描述
在这里插入图片描述

  • A Discussion of the Predicates
  • Comparison Predicate
    在这里插入图片描述
    在这里插入图片描述
    The Subquery on the right is only permitted provided that the result retrieved is known to either contain a single value or be an empty set.
    在这里插入图片描述

because we know that the subquery retrieved only a single value.

如果是与NULL比较, 就返回NULL

在这里插入图片描述

  • Truth Values: TRUE(T), FALSE(F), and UNKNOWN (U)
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • ** The BETWEEN Predicate**
    在这里插入图片描述

  • The Quantified Comparison Predicate
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • The IN Predicate
    在这里插入图片描述

  • The EXISTS Predicate
    在这里插入图片描述
    It ecaluates to TRUE exactly when the Subquery does not result in an empty set. there are no conditions under which this predicate evaluates to UNKNOWN

  • The IS NULL Predicates
    在这里插入图片描述

  • The LIKE Predicate
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

就是转义字符

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.10 Insert, Update, and Delete Statements

** The Insert Statement**
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

The Update Statement
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Delete Statement在这里插入图片描述

在这里插入图片描述



3.11 The Power of The Select Statement

  • The Non-Procedural Select Statement
    在这里插入图片描述
    在这里插入图片描述
  • Turing Power(计算完整性)
  • Limited Power of the Basic SQL Select Statement
    在这里插入图片描述
    方差和中位数在过程性的语言中很容易计算出来,但是在非过程的SQL中,如果数据库系统没有提供相关函数,就没有办法求得

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • Non-Procedural Reports
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • Transitive Closure
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • Limited Power of Boolean Conditions
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Oracle第八课堂笔记中,我们学习了集合运算的概念和用法。集合运算是一种对数据库中的数据进行操作和查询的技术,它可以将多个查询的结果进行合并、交叉和排除等操作。 首先,我们学习了并集运算(UNION)。并集运算可以将两个或多个查询的结果合并为一个结果集,它会去除重复的行,并按照查询语句的顺序进行排序。我们需要注意的是,并集运算中要求两个查询的列数和类型必须匹配。 接着,我们学习了交集运算(INTERSECT)。交集运算可以将两个查询的结果中的共同部分提取出来,生成一个新的结果集。交集运算同样要求两个查询的列数和类型必须匹配,而且结果集中不会出现重复的行。 然后,我们学习了差集运算(MINUS)。差集运算可以从一个查询的结果中排除另一个查询的结果,生成一个新的结果集。差集运算同样要求两个查询的列数和类型必须匹配,并且结果集中不会出现重复的行。 最后,我们练习了使用这些集合运算符来解决实际问题。通过编写SQL查询语句,我们可以根据给定的条件和要求,使用并集、交集和差集等运算符来获取我们需要的结果。 总之,Oracle第八的集合运算课堂笔记和练习让我们了解了集合运算的概念和用法,以及如何使用这些运算符来操作和查询数据库中的数据。这些知识对我们在实际应用中更高效地处理和分析数据具有重要意义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值