在PostgreSQL中创建空表

the*_*ter 4 postgresql create-table

我想在PostgreSQL中的数据库内创建一个简单的表。从我有的文档中CREATE TABLE will create a new, initially empty table in the current database. The table will be owned by the user issuing the command. 使用此命令

CREATE TABLE *table_name*;
Run Code Online (Sandbox Code Playgroud)

我以为我得到了一个新的空表ERROR: syntax error at or near ";"。但是psql抛出了。当我使用一个空的参数列表时,例如:

CREATE TABLE *table_name*();
Run Code Online (Sandbox Code Playgroud)

psql告诉我该表是通过以下方式创建的

postgres=# create table *table_name*();
CREATE TABLE
Run Code Online (Sandbox Code Playgroud)

但是\ l显示未显示新创建的表。而且也无法使用psql -d table_name -U user_name登录。有人可以帮忙吗?

joo*_*oop 6

你可以有一个没有列的表,甚至可以有一些行:

CREATE TABLE nocolumn (dummy INTEGER NOT NULL PRIMARY KEY)
    ;

INSERT INTO nocolumn(dummy) VALUES (1);

ALTER TABLE nocolumn
        DROP COLUMN dummy;

\d nocolumn

SELECT COUNT(*) FROM nocolumn;
Run Code Online (Sandbox Code Playgroud)

输出:

CREATE TABLE
INSERT 0 1
ALTER TABLE
   Table "tmp.nocolumn"
 Column | Type | Modifiers 
--------+------+-----------

 count 
-------
     1
(1 row)
Run Code Online (Sandbox Code Playgroud)


a_h*_*ame 5

您似乎对术语数据库和表感到困惑

但是\l没有显示新创建的表。

当然\l不会显示该表,因为\l将列出数据库而不是关系。要查看所有表,您需要使用\d或\dt。

而且也无法使用 psql -d table_name -U user_name

当然这是不可能的,因为该-d参数用于指定数据库,而不是表