关系代数是关系数据库的基础,但是很多书籍对关系代数的讲解都偏向理论,本系列文章试图通过将理论结合sql语句方式,让读者轻松理解关系代数的运算,并进而更好的理解关系数据库。
本篇的内容主要包括以下三个方面:
- 关系代数的概念
- 关系代数的运算简介
- 银行数据库系统的表(定义和数据)
关系代数是一种抽象的查询语言,是关系数据操纵语言的一种传统表达方式。它是用对系的运算来表达查询的。
关系运算符有四类:集合运算符,专门的关系运算符,算术比较符和逻辑运算符,如下表所示:
含义 |
运算符 |
含义 |
|||
运算符 |
- |
并 差 交 |
比较 运算符 |
> ≥ < ≤ = ≠ |
大于 大于等于 小于 小于等于 等(不等)于 |
专门的关系运算符 |
× ÷ |
笛卡尔积 选择 投影 连接 除 |
逻辑 运算符 |
┐ ∧ ∨ |
非 与 或 |
其中选择、投影、笛卡尔积、集合的交、并和差运算可以简单图示如下:
根据运算符的不同,关系代数运算可分为基本运算、附加的关系运算和扩展的关系运算。
为了说明关系代数运算,我们除了给出形式化的定义以外,还会给出一个mysql的具体例子。
首先,我们定义一个银行数据库系统,具体包括如下一些表:
- mysql> show tables;
- +----------------+
- | Tables_in_test |
- +----------------+
- | account |
- | borrower |
- | branch |
- | customer |
- | depositor |
- | loan |
- +----------------+
这些表的定义如下:
- create table customer
- (customer_name char(20),
- customer_street char(30),
- customer_city char(30),
- primary key (customer_name))
- create table branch
- (branch_name char(15),
- branch_city char(30),
- assets numeric(16,2),
- primary key (branch_name))
- create table account
- (account_number char(10),
- branch_name char(15),
- balance numeric(12,2),
- primary key (account_number),
- foreign key (branch_name) references branch (branch_name) )
- create table depositor
- (customer_name char(20),
- account_number char(10),
- primary key (customer_name, account_number),
- foreign key (account_number) references account (account_number),
- foreign key (customer_name) references customer (customer_name))
- create table loan
- (loan_number char(10),
- branch_name char(15),
- amount numeric(12,2),
- primary key (loan_number),
- foreign key (branch_name) references branch (branch_name))
- create table borrower
- (customer_name char(20),
- loan_number char(10),
- primary key (customer_name, loan_number),
- foreign key (customer_name) references customer (customer_name),
- foreign key (loan_number) references loan (loan_number))
下面给出每个表的一些数据例子,从表和列的名称以及数据中我们可以知道其含义,因此这里不再具体说明。
- mysql> select * from account;
- +----------------+-------------+---------+
- | account_number | branch_name | balance |
- +----------------+-------------+---------+
- | A-101 | Downtown | 500 |
- | A-102 | Perryridge | 400 |
- | A-201 | Brighton | 900 |
- | A-215 | Mianus | 700 |
- | A-217 | Brighton | 750 |
- | A-222 | Redwood | 700 |
- | A-305 | Round Hill | 350 |
- +----------------+-------------+---------+
- 7 rows in set (0.00 sec)
- mysql> select * from borrower;
- +---------------+-------------+
- | customer_name | loan_number |
- +---------------+-------------+
- | Admas | L-16 |
- | Curry | L-93 |
- | Hayes | L-15 |
- | Jackson | L-14 |
- | Jones | L-17 |
- | Smith | L-11 |
- | Smith | L-23 |
- | Williams | L-17 |
- +---------------+-------------+
- 8 rows in set (0.00 sec)
- mysql> select * from branch;
- +-------------+-------------+---------+
- | branch_name | branch_city | assets |
- +-------------+-------------+---------+
- | Brighton | Brooklyn | 7100000 |
- | Downtown | Brooklyn | 9000000 |
- | Mianus | Horseneck | 400000 |
- | North Town | Rye | 3700000 |
- | Perryridge | Horseneck | 1700000 |
- | Pownal | Bennington | 300000 |
- | Redwood | Palo Alto | 2100000 |
- | Round Hill | Horseneck | 8000000 |
- +-------------+-------------+---------+
- 8 rows in set (0.00 sec)
- mysql> select * from customer;
- +---------------+-----------------+---------------+
- | customer_name | customer_street | customer_city |
- +---------------+-----------------+---------------+
- | Adams | Spring | Pittsfield |
- | Brooks | Senator | Brooklyn |
- | Curry | North | Rye |
- | Green | Walnut | Stamford |
- | Hayes | Main | Harrison |
- | Johnson | Alma | Palo Alto |
- | Jones | Main | Harrison |
- | Lindsay | Park | Pittsfield |
- | Smith | North | Rye |
- | Turner | Putnam | Stamford |
- | Williams | Nassau | Princeton |
- +---------------+-----------------+---------------+
- 11 rows in set (0.00 sec)
- mysql> select * from depositor;
- +---------------+----------------+
- | customer_name | account_number |
- +---------------+----------------+
- | Hayes | A-102 |
- | Johnson | A-101 |
- | Johnson | A-201 |
- | Jones | A-217 |
- | Lindsay | A-222 |
- | Smith | A-215 |
- | Turner | A-305 |
- +---------------+----------------+
- 7 rows in set (0.00 sec)
- mysql> select * from loan;
- +-------------+-------------+--------+
- | loan_number | branch_name | amount |
- +-------------+-------------+--------+
- | L-11 | Round Hill | 900 |
- | L-14 | Downtown | 1500 |
- | L-15 | Perryridge | 1500 |
- | L-16 | Perryridge | 1300 |
- | L-17 | Downtown | 1000 |
- | L-23 | Redwood | 2000 |
- | L-93 | Mianus | 500 |
- +-------------+-------------+--------+
- 7 rows in set (0.00 sec)
本篇已经较长,下一篇结合银行数据库的例子,给出关系代数的每种运算的形式化定义及其在mysql中的实现。