当前位置: 首页 > article >正文

PostgreSQL有意思的现象:支持不带列的表

1、前言

以前从没有试过建一张表,不带任何列。在PG中却支持这种语法。这是个什么鬼?

最近,把PG源码扒了下,简单浏览了下最近的一些merge。其中有一个fix:

eeb0ebad79 ("Fix the initial sync tables with no columns.", 2023-11-22)

    Fix the initial sync tables with no columns.

    The copy command formed for initial sync was using parenthesis for tables
    with no columns leading to syntax error. This patch avoids adding
    parenthesis for such tables.

    Reported-by: Justin G
    Author: Vignesh C
    Reviewed-by: Peter Smith, Amit Kapila
    Backpatch-through: 15
    Discussion: http://postgr.es/m/18203-df37fe354b626670@postgresql.org

简单的说,是它考虑到一张表,在初始SYNC时,有可能没有任何列。按自己的印象,别的DBMS好像没有支持这种语法的。

2、简单验证

如果我们在SQLSERVER哪怕是最新版2022上试一下,

https://dbfiddle.uk/1n2I7Bj9
create table tab_no_col();
-- 立马报错
Msg 102 Level 15 State 1 Line 1
Incorrect syntax near ')'.

图片

切到MySQL, 也不支持这种语法:

https://dbfiddle.uk/xZPbFq4N
create table tab_no_col();
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

图片

再试下Oracle, 发现也不支持。哪怕是23C。

https://dbfiddle.uk/-F2lutlX
create table tab_no_col();
ORA-00931: missing identifier

图片

3、PostgreSQL中的行为

我们就以PostgreSQL 14为例 :

postgres=# create table tab_no_col();
CREATE TABLE
postgres=# insert into tab_no_col default values;
INSERT 0 1
postgres=# select * from tab_no_col;
--
(1 row)

postgres=# insert into tab_no_col default values;
INSERT 0 1
postgres=# insert into tab_no_col default values;
INSERT 0 1
postgres=# insert into tab_no_col default values;
INSERT 0 1
postgres=# select count(*) from tab_no_col;
 count 
-------
     4
(1 row)

postgres=# select * from tab_no_col;
--
(4 rows)

虽然没有真正的值,但是却一样可以插入相关的值,并得到相应的行数。

根据这种特性,我们甚至可以预先建一张没有任何列的表,然后,插入一些列。看看:

postgres=# alter table tab_no_col add col2 varchar(32) null;
ALTER TABLE

postgres=# select * from tab_no_col;
 col2 
------




(4 rows)

更新一些值:

postgres=# update tab_no_col set col2 = 'a_' || ctid || '_' || xmin;
UPDATE 4
postgres=# select * from tab_no_col;
    col2     
-------------
 a_(0,1)_785
 a_(0,2)_786
 a_(0,3)_787
 a_(0,4)_788
(4 rows)

这种功能,也许最大的好处就是先建一张表(空列),然后可以动态的增加或改变一些列。利用FOR循环时,直接"(" 和 ”)" 做匹配,而不用考虑列数是否真正大于0。

反正CREATE TABLE ABC(, 即算完全是空的,语法上它也不会报错。


http://www.kler.cn/news/159972.html

相关文章:

  • Java 数据结构篇-用链表、数组实现队列(数组实现:循环队列)
  • 【动手学深度学习】(六)权重衰退
  • 【Unity入门】声音组件AudioSource简介及实现声音的近大远小
  • 生成对抗网络——研讨会
  • SQL练习
  • QT-在ui界面中给QWidget增加Layout布局的两种方法
  • Tensorflow.js 入门学习指南
  • java内部类详解
  • matlab实践(十):贝塞尔曲线
  • Linux搭建FTP并安装xrdp,实现Windows系统下利用FileZilla传输文件和远程桌面连接
  • Seo优化是什么,怎么进行seo优化
  • 服务器数据恢复—服务器重装系统导致逻辑卷发生改变的数据恢复案例
  • uni-app详解、开发步骤、案例代码
  • 使用Vue写一个日期选择器
  • 使用K-means把人群分类
  • MySql概述及其性能说明
  • 【PUSDN】centos查看日志文件内容,包含某个关键字的前后5行日志内容,centos查看日志的几种方法
  • 9个典型的交通行业AI应用
  • Java面试题(每天10题)-------连载(43)
  • kubeadm快速搭建k8s高可用集群
  • 目标检测常用评价指标
  • MATLAB Simulink +STM32硬件在环 (HIL)实现例程测试
  • 前后端数据传输格式(上)
  • 「音视频处理」音频编码AAC详解,低码率提高音质?
  • 【Python】Python读Excel文件生成xml文件
  • 智能优化算法应用:基于梯度算法无线传感器网络(WSN)覆盖优化 - 附代码
  • Spring boot -- 学习HttpMessageConverter
  • 【LeetCode 0170】【哈希】两数之和(3) 数据结构设计
  • Unity 加载本地或网络图片并转为精灵(Sprite)的方法
  • java WebSocket带参数处理使用