令人惊讶的是,"关系"数据库does not中的"关系"指的是一个表到另一个表的外键关系."关系是一种数据 struct ,它由一个标题和一组无序的元组组成,这些元组共享相同的类型,"维基百科第'Relation (database)'页这样写道.
在SQL RDBMS(如MS SQL Server和Oracle)中,表是永久存储的关系,其中数据字典中定义的列名形成"标题",行是关系的"元组".
然后,从表中,查询可以返回不同的关系:
create table t (x number primary key, y number not null);
Table created.
SQL> insert into t values (1, 10);
1 row created.
SQL> insert into t values (2, 20);
1 row created.
SQL> select x from t;
X
----------
1
2
与基表相比,select x from t
返回的关系具有更少的列和元素的元组.和select x, y from t where x = 1
将返回具有比基表更少的元组的关系:
SQL> select x, y from t where x = 1;
X Y
---------- ----------
1 10
使用内部联接的示例:
SQL> create table s (x number primary key, words varchar2(100) not null);
Table created.
SQL> insert into s values (1, 'Hello World!');
1 row created.
SQL> insert into s values (3, 'Will not show');
1 row created.
SQL> select t.x, t.y, s.words
2 from t
3 inner join s
4 on t.x = s.x;
X Y WORDS
---------- ---------- ---------------
1 10 Hello World!
从概念上讲,t inner join s on t.x = s.x
要经过以下步骤:
以s
和t
的笛卡尔乘积为例,即取每一行的s
,并将其与每一行的t
相结合,得到一个具有size of s*size of t个元组或行的元组,每个元组包含s
和t
中的所有列,这与以下结果非常相似:
SQL> select * from s, t;
X WORDS X Y
3 Will not show 1 10
3 Will not show 2 20
1 Hello World! 1 10
1 Hello World! 2 20
(或SQL-92语法中的select * from s cross join t
)笛卡尔乘积包含四个元组/四列的行on s.x = t.x
将元组精简为一个,仍然有四列:
SQL> select *
2 from t
3 inner join s
4 on t.x = s.x;
X Y X WORDS
---------- ---------- ---------- ---------------
1 10 1 Hello World!
而select t.x, t.y, s.words
则将关系中的一列刮掉.
Note以上描述了正在发生的事情的概念性或逻辑性模型.数据库附带查询优化器,这些优化器旨在提供结果,就好像所有逻辑步骤都得到了遵循一样,但在工作的物理实现中设法跳过步骤,并使用不属于关系模型一部分的支持物理 struct (如索引).
视图是关系定义,不存储关系,但基于其他关系定义关系,最终在底部有表.(除实例化视图外,根据其他关系预计算并存储关系.)