When someone refers to a relation in a database course, what does that mean?
Sha*_*nce 17
令人惊讶的是,"关系型"数据库中的"关系" 并不是指一个表与另一个表的外键关系."关系是一个数据结构,由一个标题和一组无序的元组组成,它们共享相同的类型,"根据维基百科的"关系(数据库)".
在SQL RDBMSes(例如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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
使用内部联接的示例:
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!
Run Code Online (Sandbox Code Playgroud)
从概念上讲,t inner join s on t.x = s.x
执行以下步骤:
采取的笛卡尔积s
和t
,这是采取的每一行s
与每一列结合它t
导致元组S的尺寸*的T大小的元组或行,每行从双方的所有列s
和t
很像的结果:
SQL> select*from s,t;
X WORDS X Y
Run Code Online (Sandbox Code Playgroud)
3 Will not show 1 10
3 Will not show 2 20
1 Hello World! 1 10
1 Hello World! 2 20
Run Code Online (Sandbox Code Playgroud)(或者select * from s cross join t
在SQL-92语法中)从包含四个元组/行和四列的笛卡尔积中,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!
Run Code Online (Sandbox Code Playgroud)
并且select t.x, t.y, s.words
从关系中删除一列.
请注意,上面描述了正在发生的概念或逻辑模型.数据库附带了查询优化器,这些查询优化器旨在将结果视为已遵循所有逻辑步骤,但设法跳过步骤,在工作的物理实现中使用支持物理结构(如索引),而不是关系模型
视图是不存储关系的关系定义,但是基于其他关系定义关系,最终使用底部的表.(物化视图除外,它预先计算并存储基于其他关系的关系.)
我可以看到其他受访者正在给你严格定义什么才能真正被称为"关系",我不反对他们的正确性.然而,在通常的用法中,当有人在数据库课程中引用"关系"时,他们指的是永久存储在数据库(表格)中的表格数据集,或者根据数学描述(视图或查询结果).