Sql ONLY关键字

时间:2018-06-04 13:20:44

标签: sql

在SQL语法中,我发现奇怪的规则表明select * from ONLY (t1)是有效的SQL。

我的问题是:ONLY在这种情况下意味着什么?

它位于" 7.6表格参考"规格部分:

<table primary> ::=
           <table or query name> [ [ AS ] <correlation name> [ <left paren> <derived column list> <right paren> ] ]
     |     <derived table> [ AS ] <correlation name> [ <left paren> <derived column list> <right paren> ]
     |     <lateral derived table> [ AS ] <correlation name> [ <left paren> <derived column list> <right paren> ]
     |     <collection derived table> [ AS ] <correlation name> [ <left paren> <derived column list> <right paren> ]
     |     <table function derived table> [ AS ] <correlation name> [ <left paren> <derived column list> <right paren> ]
     |     <only spec> [ [ AS ] <correlation name> [ <left paren> <derived column list> <right paren> ] ]
     |     <left paren> <joined table> <right paren> 

<only spec> ::= ONLY <left paren> <table or query name> <right paren> 

1 个答案:

答案 0 :(得分:3)

ONLY关键字用于限制查询中使用的表(如果表参与表继承。

在规格中进一步说明如下:

  

如果指定了ONLY,那么TP的结果是一个由T中的每一行组成的表,除了那些在T的正确子表中有子行的行

据我所知,目前只有Postgres

支持

使用以下示例可以看到效果:

create table base (id integer, some_data varchar(100));
create table child () inherits (base);

insert into base values (1, 'base');
insert into child values (2, 'child');

以下内容:

select *
from base;

返回:

id | some_data
---+----------
 1 | base     
 2 | child    

以下内容:

select *
from only (base);

返回:

id | some_data
---+----------
 1 | base     

在线示例:http://rextester.com/JVUM87016