为什么没有列的SELECT有效 [英] Why is SELECT without columns valid
问题描述
我不小心写了一个查询,例如my_table中的 select;
,令人惊讶的是它是有效的语句。对我来说更有趣的是,即使 SELECT;
在PostgreSQL中也是有效的查询。您可以尝试使用以下代码编写很多有趣的查询:
I accidently wrote a query like select from my_table;
and surprisingly it is valid statement. Even more interesting to me is that even SELECT;
is a valid query in PostgreSQL. You can try to write a lot funny queries with this:
select union all select;
with t as (select) select;
select from (select) a, (select) b;
select where exists (select);
create table a (b int); with t as (select) insert into a (select from t);
这是某种定义SQL标准的结果,还是有一定的用例?
Is this a consequence of some definition SQL standard, or there is some use case for it, or it is just funny behavior that no one cared to programatically restrict?
推荐答案
SELECT
之后的输出表达式列表可以为空,从而生成零列结果表。根据SQL标准,这是无效的语法。 PostgreSQL允许它与允许零列表保持一致。但是,使用DISTINCT
时不允许有空列表。
The list of output expressions after
SELECT
can be empty, producing a zero-column result table. This is not valid syntax according to the SQL standard. PostgreSQL allows it to be consistent with allowing zero-column tables. However, an empty list is not allowed whenDISTINCT
is used.
如果我没有记错的话,零列表的可能性是表继承的副作用。在Postgres邮件列表中对此进行了讨论(但我现在找不到它们)
The possibility of "zero-column" tables is a side effect of the table inheritance if I'm not mistaken. There were discussions over this on the Postgres mailing lists (but I can't find them right now)
这篇关于为什么没有列的SELECT有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!