關係代數——概論

關係代數是關係數據庫的基礎,但是很多書籍對關係代數的講解都偏向理論,本系列文章試圖通過將理論結合sql語句方式,讓讀者輕鬆理解關係代數的運算,並進而更好的理解關係數據庫。

本篇的內容主要包括以下三個方面:

  1. 關係代數的概念
  2. 關係代數的運算簡介
  3. 銀行數據庫系統的表(定義和數據)

關係代數是一種抽象的查詢語言,是關係數據操縱語言的一種傳統表達方式。它是用對系的運算來表達查詢的。

關係運算符有四類:集合運算符,專門的關係運算符,算術比較符和邏輯運算符,如下表所示:

運算符

含義

運算符

含義

集合

運算符

-

 

 

比較

運算符

>

 

= 

大於

大於等於

小於

小於等於

等(不等)於

專門的關係運算符

×

 

 

÷

笛卡爾積

選擇

投影

連接

邏輯

運算符

 

其中選擇、投影、笛卡爾積、集合的交、並和差運算可以簡單圖示如下:
關係代數運算圖示

 

根據運算符的不同,關係代數運算可分爲基本運算、附加的關係運算和擴展的關係運算。

爲了說明關係代數運算,我們除了給出形式化的定義以外,還會給出一個mysql的具體例子。

首先,我們定義一個銀行數據庫系統,具體包括如下一些表:  

  1. mysql> show tables; 
  2. +----------------+ 
  3. | Tables_in_test | 
  4. +----------------+ 
  5. | account        | 
  6. | borrower       | 
  7. | branch         | 
  8. | customer       | 
  9. | depositor      | 
  10. | loan           | 
  11. +----------------+ 

這些表的定義如下:

  1. create table customer  
  2.     (customer_name char(20),  
  3.      customer_street char(30),  
  4.      customer_city char(30),  
  5.      primary key (customer_name))  
  6. create table branch  
  7.     (branch_name char(15),  
  8.      branch_city char(30),  
  9.      assets numeric(16,2),  
  10.      primary key (branch_name))   
  11. create table account  
  12.     (account_number char(10),  
  13.      branch_name char(15),  
  14.      balance numeric(12,2),  
  15.      primary key (account_number),  
  16.      foreign key (branch_name) references branch (branch_name) )      
  17. create table depositor  
  18.     (customer_name char(20),  
  19.      account_number char(10),  
  20.      primary key (customer_name, account_number),  
  21.      foreign key (account_number) references account (account_number),      
  22.      foreign key (customer_name) references customer (customer_name))   
  23. create table loan  
  24.     (loan_number char(10),  
  25.      branch_name char(15),  
  26.      amount numeric(12,2),  
  27.      primary key (loan_number),  
  28.      foreign key (branch_name) references branch (branch_name))  
  29. create table borrower  
  30.     (customer_name char(20),  
  31.      loan_number char(10),  
  32.      primary key (customer_name, loan_number),  
  33.      foreign key (customer_name) references customer (customer_name),  
  34.      foreign key (loan_number) references loan (loan_number))  

下面給出每個表的一些數據例子,從表和列的名稱以及數據中我們可以知道其含義,因此這裏不再具體說明。

 

  1. mysql> select * from account; 
  2. +----------------+-------------+---------+ 
  3. | account_number | branch_name | balance | 
  4. +----------------+-------------+---------+ 
  5. | A-101          | Downtown    |     500 | 
  6. | A-102          | Perryridge  |     400 | 
  7. | A-201          | Brighton    |     900 | 
  8. | A-215          | Mianus      |     700 | 
  9. | A-217          | Brighton    |     750 | 
  10. | A-222          | Redwood     |     700 | 
  11. | A-305          | Round Hill  |     350 | 
  12. +----------------+-------------+---------+ 
  13. 7 rows in set (0.00 sec) 
  14.  
  15. mysql> select * from borrower; 
  16. +---------------+-------------+ 
  17. | customer_name | loan_number | 
  18. +---------------+-------------+ 
  19. | Admas         | L-16        | 
  20. | Curry         | L-93        | 
  21. | Hayes         | L-15        | 
  22. | Jackson       | L-14        | 
  23. | Jones         | L-17        | 
  24. | Smith         | L-11        | 
  25. | Smith         | L-23        | 
  26. | Williams      | L-17        | 
  27. +---------------+-------------+ 
  28. 8 rows in set (0.00 sec) 
  29.  
  30. mysql> select * from branch; 
  31. +-------------+-------------+---------+ 
  32. | branch_name | branch_city | assets  | 
  33. +-------------+-------------+---------+ 
  34. | Brighton    | Brooklyn    | 7100000 | 
  35. | Downtown    | Brooklyn    | 9000000 | 
  36. | Mianus      | Horseneck   |  400000 | 
  37. | North Town  | Rye         | 3700000 | 
  38. | Perryridge  | Horseneck   | 1700000 | 
  39. | Pownal      | Bennington  |  300000 | 
  40. | Redwood     | Palo Alto   | 2100000 | 
  41. | Round Hill  | Horseneck   | 8000000 | 
  42. +-------------+-------------+---------+ 
  43. 8 rows in set (0.00 sec) 
  44.  
  45. mysql> select * from customer; 
  46. +---------------+-----------------+---------------+ 
  47. | customer_name | customer_street | customer_city | 
  48. +---------------+-----------------+---------------+ 
  49. | Adams         | Spring          | Pittsfield    | 
  50. | Brooks        | Senator         | Brooklyn      | 
  51. | Curry         | North           | Rye           | 
  52. | Green         | Walnut          | Stamford      | 
  53. | Hayes         | Main            | Harrison      | 
  54. | Johnson       | Alma            | Palo Alto     | 
  55. | Jones         | Main            | Harrison      | 
  56. | Lindsay       | Park            | Pittsfield    | 
  57. | Smith         | North           | Rye           | 
  58. | Turner        | Putnam          | Stamford      | 
  59. | Williams      | Nassau          | Princeton     | 
  60. +---------------+-----------------+---------------+ 
  61. 11 rows in set (0.00 sec) 
  62.  
  63. mysql> select * from depositor; 
  64. +---------------+----------------+ 
  65. | customer_name | account_number | 
  66. +---------------+----------------+ 
  67. | Hayes         | A-102          | 
  68. | Johnson       | A-101          | 
  69. | Johnson       | A-201          | 
  70. | Jones         | A-217          | 
  71. | Lindsay       | A-222          | 
  72. | Smith         | A-215          | 
  73. | Turner        | A-305          | 
  74. +---------------+----------------+ 
  75. 7 rows in set (0.00 sec) 
  76.  
  77. mysql> select * from loan; 
  78. +-------------+-------------+--------+ 
  79. | loan_number | branch_name | amount | 
  80. +-------------+-------------+--------+ 
  81. | L-11        | Round Hill  |    900 | 
  82. | L-14        | Downtown    |   1500 | 
  83. | L-15        | Perryridge  |   1500 | 
  84. | L-16        | Perryridge  |   1300 | 
  85. | L-17        | Downtown    |   1000 | 
  86. | L-23        | Redwood     |   2000 | 
  87. | L-93        | Mianus      |    500 | 
  88. +-------------+-------------+--------+ 
  89. 7 rows in set (0.00 sec) 

本篇已經較長,下一篇結合銀行數據庫的例子,給出關係代數的每種運算的形式化定義及其在mysql中的實現。

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章