第8章データ管理
Structured Query Language構造化照会言語
The declarative language used in managing and querying structured data in a RDBMS is called Structured Query Language (SQL). SQL implements the four sublanguages mentioned earlier: DDL, DML, DQL, and DCL. SQL is based on relational algebra with many extensions. SQL supports primary keys, foreign keys, inserting data, deleting data, updating data, indexing, recovery, concurrency, and security. SQL focuses on efficiency in addition to specifying the data needed. The main commands in SQL are
CREATE TABLE, which is used to create a new table in a database such as:英語のヒント
RDBMS内の構造化データを管理し、問い合わせるための宣言型言語を、構造化照会言語(SQL)という。SQLは、前に挙げた四つの部分言語、DDL、DML、DQL、DCLを実装する。SQLは、関係代数に多くの拡張を加えたものに基づく。主キー、外部キー、データの挿入・削除・更新、索引、回復、並行性、セキュリティーを支える。SQLは、必要なデータを指定するだけでなく、効率も重視する。主要なコマンドには、データベースに新しい表を作るCREATE TABLEがあり、たとえば次のように使う。
CREATE TABLE Student (
ID int,
LastName varchar(255),
ZipCode int
);which will create a table name Student with three columns: ID, LastName, and ZipCode.
DROP TABLE is used to drop an existing table in a database, and ALTER TABLE, which is used to add, delete, or modify columns in an existing table. SQL schemas contain tables (i.e., relations), and the RDBMS catalog stores these schemas. SQL constraints are used to specify rules for the data in a table to ensure the accuracy and reliability of that data. There are additional features of SQL such as triggers, views, nested/recursive queries, privileges, and metadata.英語のヒント
これは、ID、LastName、ZipCodeという三つの列を持つ、Studentという名前の表を作る。DROP TABLEは、データベース内の既存の表を削除するために使う。ALTER TABLEは、既存の表の列を追加・削除・変更するために使う。SQLのスキーマには、表、すなわち関係が含まれ、RDBMSのカタログがそのスキーマを保存する。SQLの制約は、表のデータの正確性と信頼性を確保するために、そのデータに関する規則を指定する。ほかにも、トリガー、ビュー、入れ子や再帰の問い合わせ、権限、メタデータなどのSQLの機能がある。
Relational Algebra関係代数
The theoretical framework for querying relational databases that uses unary and binary operators on relations to manipulate and retrieve tuples is called relational algebra. The following set of relational algebra operations is a complete set: Select, Project, Union, Rename, Difference, and Cartesian product. It is a complete set because any relational algebra operation can be expressed as a sequence of operations from this set.
英語のヒント
関係に対して単項演算子と二項演算子を使い、タプルを操作・取得する、関係データベースへの問い合わせの理論的な枠組みを、関係代数という。次の関係代数の演算の組は、完全な組である。選択、射影、和、改名、差、直積である。どの関係代数の演算も、この組の演算を順に行うことで表せるため、完全な組という。
Relational algebra operators include set operators and relational operators. The set operators include Union, Intersection, Difference, and Product, which are all binary operators because they use two relations as operands and produce a new relation as a result. In some cases, the operand relations need to be union compatible, which implies that they have the same number of attribute types defined on the same domains. In what follows, we represent a relation as R and its cardinality or number of tuples as |R|.
英語のヒント
関係代数の演算子には、集合演算子と関係演算子がある。集合演算子には、和、共通部分、差、積があり、どれも二つの関係をオペランドとして、新しい関係を返す二項演算子である。場合によっては、オペランドとなる関係に、和両立性が必要になる。これは、同じ定義域上に定められた属性型を、同じ数だけ持つことを意味する。以下では、関係をR、その濃度、すなわちタプル数を|R|と表す。
Union Operator
和演算子
The Union operator applied to two union-compatible relations P and Q results into a new relation so:
英語のヒント
和両立性のある二つの関係PとQに和演算子を適用すると、新しい関係が得られる。すなわち、次のようになる。
with
であり、を満たす。
The resulting relation R consists of the tuples that either belong to P or Q, or both. Duplicate tuples are eliminated.
英語のヒント
得られた関係Rは、PまたはQ、あるいはその両方に属するタプルからなる。重複するタプルは取り除かれる。
Intersection Operator
共通部分演算子
The Intersection operator applied to two union-compatible relations P and Q results in a new relation such that:
和両立性のある二つの関係PとQに共通部分演算子を適用すると、次の条件を満たす新しい関係が得られる。
with
であり、を満たす。
The resulting relation R consists of the tuples that belong to both P and Q.
得られた関係Rは、PとQの両方に属するタプルからなる。
Difference Operator
差演算子
The Difference operator applied to two union-compatible relations P and Q results in a new relation such that:
和両立性のある二つの関係PとQに差演算子を適用すると、次の条件を満たす新しい関係が得られる。
with
The resulting relation R consists of the tuples that belong to P but not to Q. Note that when using the Difference operator, the order of the operands, is important because it concerns a noncommutative operation.
The resulting relation R consists of the tuples that belong to P but not to Q. Note that when using the Difference operator, the order of the operands, is important because it concerns a noncommutative operation.
英語のヒント
であり、を満たす。得られた関係Rは、Pには属するがQには属さないタプルからなる。差演算子は非可換な演算なので、オペランドの順序が重要であることに注意しよう。
Product Operator
積演算子
The Product operator (also known as the Cartesian product, Cross Product, or Cross Join operator) returns the Cartesian Product of two relations. Consider the following two relations: a supplier relation P and a product relation Q. The Cartesian Product of both relations consists of all possible combinations of tuples from P and Q. The resulting relation consists of a set of tuples such that:
英語のヒント
積演算子は、直積演算子、クロス積演算子、クロス結合演算子とも呼ばれ、二つの関係の直積を返す。仕入れ先の関係Pと、商品の関係Qという、二つの関係を考えよう。この二つの直積は、PのタプルとQのタプルの、考えられるすべての組み合わせからなる。得られる関係は、次の条件を満たすタプルの集合からなる。
with
であり、を満たす。
The relational operators are Select, Project, Rename, Join, and Division. The Select, Project, and Rename operators are unary operators because they only use one operand. The Join and Division operators are binary operators because they use two operands.
英語のヒント
関係演算子には、選択、射影、改名、結合、除算がある。選択、射影、改名は、オペランドを一つしか使わないため、単項演算子である。結合と除算は、オペランドを二つ使うため、二項演算子である。
Select Operator
選択演算子
The Select operator is used to return a subset of tuples of a relation based on a selection condition. The latter can consist of predicates combined using Boolean operators (and, or, not). Every predicate consists of a comparison between the values of two domain-compatible attribute types, or between an attribute type and a constant value from the domain of the attribute type.
英語のヒント
選択演算子は、選択条件に基づいて、関係のタプルの部分集合を返すために使う。選択条件は、ブール演算子(and、or、not)で組み合わせた述語から構成できる。各述語は、定義域に互換性のある二つの属性型の値同士、または、ある属性型の値と、その定義域に属する定数値とを比較するものである。
The result of applying a Select operator with a selection condition S on a relation P can be represented as:
関係Pに、選択条件Sを持つ選択演算子を適用した結果は、次のように表せる。
with Select operator
。ここで、選択演算子はである。
Project Operator
射影演算子
The Project operator is used to project a relation on a list of attribute types. The result of applying a Project operator with a list L on a relation P can be represented as:
英語のヒント
射影演算子は、属性型の一覧に従って関係を射影するために使う。関係Pに、一覧Lを持つ射影演算子を適用した結果は、次のように表せる。
with Project operator
。ここで、射影演算子はである。
Note that applying a Project operator to a relation results in a vertical subset of the relation with because duplicate tuples will be eliminated.
英語のヒント
関係に射影演算子を適用すると、その関係の列方向の部分集合が得られ、となることに注意しよう。重複するタプルが取り除かれるためである。
Rename Operator
改名演算子
The Rename operator is used to rename an attribute type of a relation (in case, for example, naming conflicts can occur). The result of applying a Rename operator on a relation P to rename attribute type B to A can be represented as:
英語のヒント
改名演算子は、たとえば名前の衝突が起こりうる場合に、関係の属性型の名前を変更するために使う。関係Pに改名演算子を適用し、属性型BをAへ改名した結果は、次のように表せる。
with Rename operator ρ
。ここで、改名演算子はρである。
Join Operator
結合演算子
The Join operator allows combining tuples of two relations based on specific conditions, also called join conditions. The result of a join is the combined effect of a Cartesian product (×) and selection. The result of applying a Join operator to two relations P and Q with join condition j can be represented as follows:
英語のヒント
結合演算子は、結合条件と呼ばれる特定の条件に基づき、二つの関係のタプルを組み合わせることを可能にする。結合の結果は、直積(×)と選択を組み合わせた結果である。二つの関係PとQに、結合条件jを持つ結合演算子を適用した結果は、次のように表せる。
with Join operator ⋈ and join condition j.
。ここで、結合演算子は⋈、結合条件はjである。
The resulting relation R consists of a set of combinations of pq tuples such that a combined tuple r ∈ R is characterized by:
英語のヒント
得られる関係Rは、タプルpとqを組み合わせたものの集合からなり、その組み合わせたタプルr ∈ Rは、次のように特徴付けられる。
with
であり、を満たす。
Division Operator
除算演算子
The Division operator is a binary operator that divides a relation P by a relation Q with . The result of a Division operator can be represented as:
除算演算子は、を満たす関係Qで、関係Pを割る二項演算子である。除算演算子の結果は、次のように表せる。
with Division operator ÷
。ここで、除算演算子は÷である。
The resulting relation R includes all tuples that belong to P combined with every tuple in Q. Note that the Division operator is not directly implemented in SQL.
得られる関係Rには、Qのすべてのタプルと組み合わされてPに属する、すべてのタプルが含まれる。除算演算子はSQLに直接実装されていないことに注意しよう。
The Select (σ), Project (π), Difference (–), Product (×), and Union (∪) operators are often referred to as the five primitive operators because all other relational algebra operators can be derived from them (derived operators).
英語のヒント
選択(σ)、射影(π)、差(–)、積(×)、和(∪)の各演算子は、五つの基本演算子と呼ばれることが多い。ほかの関係代数の演算子は、すべてこれらから導けるためである。そのような演算子を、導出演算子という。
SQL also allows aggregate functions and grouping operations. For join operations, there are many options, such as the following:
英語のヒント
SQLでは、集約関数とグループ化の演算も使える。結合の演算には、次のように多くの選択肢がある。
• A theta join allows merging two tables based on a theta condition; theta (θ) refers to the comparison operator in the join condition. Depending upon theta, these joins can be distinguished: greater-than-join, less-than-join, greater-than-or-equal-join, less-than-or-equal-join, and an equi-join (if an equals occurs). An equi-join combines tables based on matching values in specified columns; it is equivalent to a theta join, with theta being the equal comparison operator in the join condition.
英語のヒント
• シータ結合は、シータ条件に基づいて二つの表を結合する。シータ(θ)は、結合条件内の比較演算子を指す。シータに応じて、より大きい、より小さい、以上、以下という条件の結合と、等値結合(等号がある場合)を区別できる。等値結合は、指定した列の値が一致することに基づいて表を結合する。これは、結合条件のシータが等値比較演算子である、シータ結合に相当する。
• A natural join creates an implicit join based on the common columns in two tables; it is a variant of the equi-join in which one of the shared join-attribute types is removed from the result.
英語のヒント
• 自然結合は、二つの表に共通する列に基づいて、暗黙の結合を作る。これは等値結合の一種であり、共有される結合属性型のうち、一方を結果から取り除く。
• An inner join represents the intersection of two tables; all joins discussed thus far are inner joins because they do not include tuples lacking corresponding join values in the result.
英語のヒント
• 内部結合は、二つの表の共通部分を表す。これまで扱った結合は、どれも内部結合である。対応する結合値を持たないタプルを、結果に含めないためである。
• An outer join represents the union of two tables; the outer join operator will also include tuples lacking corresponding join values in the result.
英語のヒント
• 外部結合は、二つの表の和集合を表す。外部結合演算子は、対応する結合値を持たないタプルも結果に含める。
A distinction can be made between a left outer join, right outer join, and full outer join. A left outer join includes all tuples from the left relation (P) in the result, either matched with a corresponding tuple from the right relation (Q) based on the join condition or augmented with null values in case no match with a tuple from Q can be made. A left outer join can be represented as . A right outer join includes all tuples from the right relation (Q) in the result, either matched with a corresponding tuple from the left relation (P) based on the join condition or augmented with null values in case no match with a tuple from P can be made. A right outer join can be represented as . A full outer join includes all tuples from P and Q in the result either matched with the counterparts according to the join condition or augmented with null values in case no match can be found. A full outer join can be represented as .
英語のヒント
外部結合は、左外部結合、右外部結合、完全外部結合に分けられる。左外部結合は、左側の関係Pのすべてのタプルを結果に含める。結合条件に基づいて、右側の関係Qの対応するタプルと組み合わせるか、Qのタプルと対応付けられなければ、ヌル値を補う。左外部結合は、と表せる。右外部結合は、右側の関係Qのすべてのタプルを結果に含める。結合条件に基づいて、左側の関係Pの対応するタプルと組み合わせるか、Pのタプルと対応付けられなければ、ヌル値を補う。右外部結合は、と表せる。完全外部結合は、PとQのすべてのタプルを結果に含める。結合条件に従って対応するものと組み合わせるか、対応するものが見つからなければ、ヌル値を補う。完全外部結合は、と表せる。
An example of data structure representation for the relational algebra expression is a query tree.
英語のヒント
関係代数の式をデータ構造で表す例として、問い合わせ木がある。