markdown
データベースの基本md 32d7237
lecture/information/database/database-basics.lecture.n.md
Download PDF

データベースの基本きほん

date2026-07-14document_iddoc_8713b89d39f89f55dc2f5612128682d5descriptionデータベースとDBMSの役割、関係モデル、キー、整合性制約、正規化、トランザクションの基礎を説明する。prerequisitesHTTPの基本type講義statusactiverelateddata/lecture/information/database/database-portal.lecture.n.md / data/lecture/information/database/sql-basics.lecture.n.md / data/lecture/information/networks/http-basics.lecture.n.md / data/lecture/information/information-engineering-portal.lecture.n.md
informationdatabaseundergraduatelecture
data/lecture/information/database/database-portal.lecture.n.md

1導入どうにゅう

この講義こうぎでは、データベース管理かんりシステムによってデータを永続的えいぞくてきかつ一貫いっかんして管理かんりするための基本概念きほんがいねんを説明せつめいする。特とくに、関係かんけいモデルの構成要素こうせいようそ、キーと整合性制約せいごうせいせいやく、正規化せいきかの目的もくてき、トランザクションの ACID 特性とくせいを導入どうにゅうする。

HTTP サーバは要求ようきゅうを処理しょりし、必要ひつように応おうじて表現ひょうげんを生成せいせいするが、利用者りようしゃ、商品しょうひん、注文ちゅうもんなどのデータは個々ここの通信つうしんが終了しゅうりょうした後あとにも保持ほじされる。このようなデータを複数ふくすうのプログラムや利用者りようしゃから安全あんぜんに共有きょうゆうするには、保存形式ほぞんけいしきだけでなく、制約せいやく、同時実行どうじじっこう、障害しょうがいからの回復かいふくを管理かんりする仕組しくみが必要ひつようである。

2データベースと DBMS

データベースDatabaseとは、特定とくていの目的もくてきに従したがって組織化そしきかされ、永続的えいぞくてきに管理かんりされるデータの集合しゅうごうである。

データベース管理かんりシステムDatabase management system; DBMSとは、データベースの定義ていぎ、保存ほぞん、検索けんさく、更新こうしん、アクセス制御せいぎょ、同時実行制御どうじじっこうせいぎょ、障害回復しょうがいかいふくなどを提供ていきょうするソフトウェアである。データベースが管理対象かんりたいしょうとなるデータであるのに対たいし、DBMS はそのデータを管理かんりする機構きこうである。

3関係かんけいモデルの構成要素こうせいようそ

関係かんけいモデルRelational modelでは、データを関係かんけいとして表現ひょうげんする。実装上じっそうじょうは表ひょうとして表示ひょうじされることが多おおいが、関係かんけいは単たんなる行列形式ぎょうれつけいしきのファイルではなく、属性ぞくせいとタプルからなる数学的すうがくてきな構造こうぞうである。

  • 属性ぞくせいAttributeは、データを特徴とくちょうづける名前なまえの付ついた項目こうもくである。
  • ドメインDomainは、ある属性ぞくせいが取とり得うる値あたいの集合しゅうごうである。
  • タプルTupleは、各属性かくぞくせいに対応たいおうする値あたいをまとめた一件いっけんのデータである。
  • 関係かんけいスキーマRelation schemaは、関係名かんけいめいと属性ぞくせいの構成こうせいを定さだめる。
  • 関係かんけいインスタンスRelation instanceは、ある時点じてんでスキーマに適合てきごうするタプルの集合しゅうごうである。

たとえば、学生がくせいを表現ひょうげんする関係かんけいスキーマを次つぎのように記述きじゅつできる。

Student(student_id,name,department_id)

この場合ばあい、student_id、name、department_id が属性ぞくせいであり、個々ここの学生がくせいに対応たいおうする値あたいの組くみがタプルである。

4キー

スーパーキーSuperkeyとは、関係かんけいスキーマで許容きょようされるすべてのインスタンスにおいて、各かくタプルを一意いちいに識別しきべつすることが制約せいやくされた属性集合ぞくせいしゅうごうである。候補こうほキーCandidate keyとは、いずれの真部分集合しんぶぶんしゅうごうもスーパーキーではない極小きょくしょうなスーパーキーである。

主しゅキーPrimary keyとは、候補こうほキーのうち、タプルの主要しゅような識別子しきべつしとして選択せんたくしたものである。候補こうほキーは複数ふくすう存在そんざいし得うるが、主しゅキーとして選択せんたくするものは一ひとつである。

外部がいぶキーForeign keyとは、ある関係かんけいの属性集合ぞくせいしゅうごうであり、その値あたいが同一どういつまたは別べつの関係かんけいの候補こうほキーを参照さんしょうする。たとえば、Student.department_id が学部がくぶの関係かんけいにある Department.department_id を参照さんしょうすれば、学生がくせいと所属学部しょぞくがくぶを関連かんれんづけられる。

5整合性制約せいごうせいせいやく

整合性制約せいごうせいせいやくIntegrity constraintとは、データベースの状態じょうたいが満みたすべき条件じょうけんである。代表的だいひょうてきな制約せいやくには、次つぎのものがある。

  • ドメイン制約せいやくは、属性値ぞくせいちを所定しょていのドメインへ制限せいげんする。
  • キー制約せいやくは、候補こうほキーの値あたいがタプルを一意いちいに識別しきべつすることを要求ようきゅうする。
  • 実体整合性じったいせいごうせいは、主しゅキーの構成属性こうせいぞくせいが欠損けっそんせず、各かくタプルを識別しきべつできることを要求ようきゅうする。
  • 参照整合性さんしょうせいごうせいReferential integrityは、外部がいぶキーの非空ひくうの値あたいが参照先さんしょうさきに存在そんざいすることを要求ようきゅうする。

DBMS はこれらの制約せいやくに違反いはんする更新こうしんを拒否きょひし、または宣言せんげんされた参照動作さんしょうどうさに従したがって関連かんれんするデータを更新こうしんする。ただし、データが現実世界げんじつせかいを正確せいかくに反映はんえいしているかどうかは、スキーマに表現ひょうげんされた制約せいやくだけでは完全かんぜんに保証ほしょうできない。

6正規化せいきかの目的もくてき

同一どういつの事実じじつを複数箇所ふくすうかしょへ記録きろくすると、一部いちぶだけを更新こうしんすることによる不整合ふせいごう、データを追加ついかできない状況じょうきょう、削除さくじょに伴ともなう別べつの事実じじつの消失しょうしつが生しょうじ得うる。これらをそれぞれ更新こうしん、挿入そうにゅう、削除さくじょの異常いじょうAnomalyという。

正規化せいきかNormalizationとは、属性間ぞくせいかんの依存関係いぞんかんけいに基もとづいて関係かんけいスキーマを分解ぶんかいし、望のぞましくない冗長性じょうちょうせいと更新異常こうしんいじょうを抑制よくせいするための設計過程せっけいかていである。正規化せいきかはあらゆる重複ちょうふくを消去しょうきょすることではなく、依存関係いぞんかんけいを適切てきせつに表現ひょうげんすることを目的もくてきとする。性能せいのうや利用形態りようけいたいに応おうじて、意図的いとてきに冗長性じょうちょうせいを導入どうにゅうする非正規化ひせいきかもある。

7トランザクションと ACID

トランザクションTransactionとは、DBMS が一ひとつの論理的ろんりてきな作業単位さぎょうたんいとして扱あつかう操作列そうされつである。たとえば送金そうきんでは、一方いっぽうの口座こうざからの減額げんがくと他方たほうの口座こうざへの増額ぞうがくを一体いったいとして処理しょりする必要ひつようがある。

トランザクションの代表的だいひょうてきな特性とくせいを ACID と総称そうしょうする。

  • 原子性げんしせいAtomicity: トランザクションの全操作ぜんそうさを反映はんえいするか、いずれも反映はんえいしない。
  • 一貫性いっかんせいConsistency: トランザクションが正ただしく実行じっこうされると、制約せいやくを満みたす状態じょうたいから別べつのそのような状態じょうたいへ移行いこうする。
  • 独立性どくりつせいIsolation: 並行へいこうするトランザクションの干渉かんしょうを制御せいぎょし、所定しょていの分離ぶんりレベルに対応たいおうする観測結果かんそくけっかを提供ていきょうする。
  • 永続性えいぞくせいDurability: 確定かくていしたトランザクションの結果けっかは、以後いごの障害しょうがいに対たいして保持ほじされる。

ACID は実装方式じっそうほうしきそのものではなく、トランザクション処理しょりが目標もくひょうとする性質せいしつである。また、一貫性いっかんせいは DBMS だけで自動的じどうてきに得えられるものではなく、適切てきせつな制約せいやくと正ただしいトランザクション処理しょりを必要ひつようとする。

8要点ようてん

  • データベースは組織化そしきかされたデータであり、DBMS はその定義ていぎ、操作そうさ、保護ほごを担になう。
  • 関係かんけいスキーマは属性ぞくせいの構成こうせいを定さだめ、関係かんけいインスタンスはある時点じてんのタプルの集合しゅうごうである。
  • 候補こうほキー、主しゅキー、外部がいぶキー、および整合性制約せいごうせいせいやくは、データの識別しきべつと関連かんれんを表現ひょうげんする。
  • 正規化せいきかは望のぞましくない冗長性じょうちょうせいと更新異常こうしんいじょうを抑制よくせいし、トランザクションは複数ふくすうの操作そうさを論理的ろんりてきな単位たんいとして管理かんりする。

9次つぎに進すすむ講義こうぎ

data/lecture/information/database/sql-basics.lecture.n.md

Database Fundamentals

data/lecture/information/database/database-portal.lecture.n.md

1Introduction

This lecture explains the fundamental concepts through which a database management system manages data persistently and consistently. In particular, it introduces the components of the relational model, keys and integrity constraints, the purpose of normalization, and the ACID properties of transactions.

An HTTP server processes requests and generates representations when needed, but data concerning users, products, and orders must remain available after individual communications end. Sharing such data safely among multiple programs and users requires mechanisms that manage not only storage formats but also constraints, concurrency, and recovery from failures.

2Databases and DBMSs

A databaseDatabase is a collection of data that is organized for a particular purpose and managed persistently.

A database management systemDatabase management system; DBMS is software that provides database definition, storage, querying, updating, access control, concurrency control, failure recovery, and related functions. A database is the data being managed, whereas a DBMS is the mechanism that manages it.

3Components of the Relational Model

The relational modelRelational model represents data as relations. Relations are often displayed as tables in implementations, but a relation is not merely a file arranged in rows and columns; it is a mathematical structure consisting of attributes and tuples.

  • An attributeAttribute is a named component that characterizes the data.
  • A domainDomain is the set of values that an attribute may take.
  • A tupleTuple is one data item containing a value corresponding to each attribute.
  • A relation schemaRelation schema specifies a relation name and its attribute structure.
  • A relation instanceRelation instance is the set of tuples that conforms to a schema at a particular time.

For example, a relation schema representing students can be written as follows.

Student(student_id,name,department_id)

Here, student_id, name, and department_id are attributes, and each collection of values corresponding to one student is a tuple.

4Keys

A superkeySuperkey is a set of attributes constrained to identify every tuple uniquely in every instance permitted by a relation schema. A candidate keyCandidate key is a minimal superkey: no proper subset of it is itself a superkey.

A primary keyPrimary key is the candidate key selected as the principal identifier of tuples. A relation may have multiple candidate keys, but only one is selected as its primary key.

A foreign keyForeign key is a set of attributes in one relation whose values reference a candidate key in the same or another relation. For example, if Student.department_id references Department.department_id in a department relation, it associates each student with a department.

5Integrity Constraints

An integrity constraintIntegrity constraint is a condition that database states must satisfy. Representative constraints include the following.

  • A domain constraint restricts an attribute value to its specified domain.
  • A key constraint requires candidate-key values to identify tuples uniquely.
  • Entity integrity requires the primary-key attributes to be present so that each tuple can be identified.
  • Referential integrityReferential integrity requires every non-null foreign-key value to exist in the referenced relation.

A DBMS rejects updates that violate these constraints or updates related data according to declared referential actions. Constraints expressed in a schema cannot, however, fully guarantee that data accurately reflects the real world.

6Purpose of Normalization

Recording the same fact in several locations can cause inconsistency when only one occurrence is updated, inability to insert data, or loss of a separate fact during deletion. These problems are called update, insertion, and deletion anomaliesAnomaly, respectively.

NormalizationNormalization is the design process of decomposing relation schemas according to dependencies among attributes in order to reduce undesirable redundancy and update anomalies. Normalization does not mean eliminating every duplication; its purpose is to represent dependencies appropriately. DenormalizationDenormalization may deliberately introduce redundancy in response to performance requirements or access patterns.

7Transactions and ACID

A transactionTransaction is a sequence of operations that a DBMS treats as one logical unit of work. A funds transfer, for example, must treat the debit from one account and the credit to another as one unit.

Four representative transaction properties are collectively called ACID.

  • AtomicityAtomicity: either all operations of a transaction take effect, or none do.
  • ConsistencyConsistency: when a transaction executes correctly, it moves the database from one constraint-satisfying state to another.
  • IsolationIsolation: interference among concurrent transactions is controlled to provide observations corresponding to a specified isolation level.
  • DurabilityDurability: the effects of a committed transaction remain preserved despite subsequent failures.

ACID describes desired properties of transaction processing rather than a particular implementation. Consistency is not obtained automatically from the DBMS alone; it also requires appropriate constraints and correct transaction logic.

8Key Points

  • A database is organized data, whereas a DBMS defines, operates on, and protects that data.
  • A relation schema specifies an attribute structure, while a relation instance is the set of tuples present at a particular time.
  • Candidate keys, primary keys, foreign keys, and integrity constraints express identification and relationships among data.
  • Normalization reduces undesirable redundancy and update anomalies, while transactions manage several operations as a logical unit.

9Next Lecture

data/lecture/information/database/sql-basics.lecture.n.md
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる