简体   繁体   English

为什么要在PostgreSQL中创建空(无行,无列)表

[英]Why to create empty (no rows, no columns) table in PostgreSQL

In answer to this question I've learned that you can create empty table in PostgreSQL. 在回答这个问题时,我了解到可以在PostgreSQL中创建空表。

create table t();

Is there any real use case for this? 是否有任何实际用例? Why would you create empty table? 为什么要创建空表? Because you don't know what columns it will have? 因为您不知道它将包含哪些列?

These are the things from my point of view that a column less table is good for. 从我的角度来看,这些都是无列表最适合的事情。 They probably fall more into the warm and fuzzy category. 它们可能更多地属于温暖和模糊的类别。

1. One practical use of creating a table before you add any user defined columns to it is that it allows you to iterate fast when creating a new system or just doing rapid dev iterations in general. 1.在向用户表添加任何用户定义的列之前创建表的一种实际用途是,它允许您在创建新系统或通常进行快速开发迭代时快速进行迭代。

2. Kind of more of 1, but lets you stub out tables that your app logic or procedure can make reference too, even if the columns have yet to be put in place. 2.种类繁多的1,但即使您尚未放置这些列,也可以使您的表存根,您的应用程序逻辑或过程也可以引用这些表。

3. I could see it coming in handing in a case where your at a big company with lots of developers. 3.如果您在一家拥有大量开发人员的大公司中工作,我可以看到它的成功。 Maybe you want to reserve a name months in advance before your work is complete. 也许您想在工作完成前几个月保留一个名称。 Just add the new column-less table to the build. 只需将新的无列表添加到构建中即可。 Of course they could still high jack it, but you may be able to win the argument that you had it in use well before they came along with their other plans. 当然,他们仍然可以高价购买它,但是您可能会赢得一个论点,即在他们与其他计划一起实施之前就已经充分使用了它。 Kind of fringe, but a valid benefit. 种附带的东西,但是有好处。

All of these are handy and I miss them when I'm not working in PostgreSQL. 所有这些都很方便,当我不在PostgreSQL中工作时,我会想念它们的。

It is not empty table - only empty result. 它不是空表-只有空结果。 PostgreSQL rows contains some invisible (in default) columns. PostgreSQL行包含一些不可见(默认)列。 I am not sure, but it can be artifact from dark age, when Postgres was Objected Relational database - and PG supported language POSTQUEL. 我不确定,但是当Postgres是Objected Relational数据库和PG支持的语言POSTQUEL时,它可能是黑暗时代的产物。 This empty table can work as abstract ancestor in class hierarchy. 该空表可以用作类层次结构中的抽象祖先。

List of system columns 系统列清单

I don't know the precise reason for its inclusion in PostgreSQL, but a zero-column table - or rather a zero-attribute relation - plays a role in the theory of relational algebra, on which SQL is (broadly) based. 我不知道将它包含在PostgreSQL中的确切原因,但是零列表-或更确切地说是零属性关系-在关系代数理论中起作用,关系代数是SQL的基础(广泛地)。

Specifically, a zero-attribute relation with no tuples (in SQL terms, a table with no columns and no rows) is the relational equivalent of zero or false, while a relation with no attributes but one tuple (SQL: no columns, but one row, which isn't possible in PostgreSQL as far as I know) is true or one. 具体来说,没有元组的零属性关系(以SQL术语来说,没有列且没有行的表)是零或false的关系等价关系,而没有属性但只有一个元组的关系(SQL:没有列,但是一个行,就我所知,这在PostgreSQL中是不可能的)是真的还是一个。 Hugh Darwen, an outspoken advocate of relational theory and critic of SQL, dubbed these "Table Dum" and "Table Dee", respectively. 直言不讳的关系理论的倡导者和SQL的批评家休·达尔文(Hugh Darwen)分别将它们称为“表Dum”和“表Dee”。

In normal algebra x + 0 == x and x * 0 == 0 , whereas x * 1 == x ; 在正常代数中x + 0 == xx * 0 == 0 ,而x * 1 == x ; the idea is that in relational algebra, Table Dum and Table Dee can be used as similar primitives for joins, unions, etc. 这个想法是在关系代数中,表Dum和表Dee可以用作连接,联合等的相似基元。

PostgreSQL internally refers to tables (as well as views and sequences) as "relations", so although it is geared around implementing SQL, which isn't defined by this kind of pure relation algebra, there may be elements of that in its design or history. PostgreSQL在内部将表(以及视图和序列)称为“关系”,因此尽管它是围绕实现SQL进行的,SQL不是由这种纯关系代数定义的,但在其设计或历史。

I don't think mine is the intended usage however recently I've used an empty table as a lock for a view which I create and change dynamically with EXECUTE. 我不认为这是我的预期用途,但是最近我使用了一个空表作为视图的锁,该视图由EXECUTE动态创建和更改。 The function which creates/replace the view has ACCESS EXCLUSIVE on the empty table and the other functions which uses the view has ACCESS. 创建/替换视图的函数在空表上具有ACCESS EXCLUSIVE,而使用该视图的其他函数具有ACCESS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM