在psql中,为什么有些命令没有效果? [英] In psql, why do some commands have no effect?
问题描述
有时我在 psql
中的命令似乎没有效果.知道为什么吗?
Sometimes my commands in psql
seem to be having no effect. Any idea why?
以下是数据库library_development
中所有表的列表:
The below is the list of all tables in the database library_development
:
library_development=> d
List of relations
Schema | Name | Type | Owner
--------+-------------------+-------+----------
public | Pavan | table | postgres
public | schema_migrations | table | sai
(2 rows)
在此之后,我使用删除了表 Pavan
:
library_development-> drop table Pavan
但表格没有被删除,其显示如图所示:
library_development=> d
List of relations
Schema | Name | Type | Owner
--------+-------------------+-------+----------
public | Pavan | table | postgres
public | schema_migrations | table | sai
(2 rows)
还有:
我在 Windows 中使用 PostgreSQL.是否有任何命令可以清除控制台(如 Oracle 中的 cl scr)?
I am using PostgreSQL in Windows. Is there any command to clear the console (Like cl scr present in Oracle)?
在使用 DML 脚本时,我需要在 Postgresql 中执行提交"的概念吗?
Is there any concept of a "commit" I need to perform in Postgresql when working with DML scripts?
推荐答案
语句以分号结尾.
在 psql
中,按下不带分号的 Enter 将语句继续到下一行,将您写入的内容添加到查询缓冲区中,而不是执行它.您会注意到提示从 dbname=>
更改为 dbname->
以指示您在续行上.
In psql
, pressing enter without a semicolon continues the statement onto the next line, adding what you wrote to the query buffer rather than executing it. You will notice that the prompt changes from dbname=>
to dbname->
to indicate that you're on a continuation line.
regress=> DROP TABLE sometable
regress->
Query buffer reset (cleared).
regress=> DROP TABLE sometable;
ERROR: table "sometable" does not exist
regress=>
注意我在没有分号的情况下按 Enter 后,提示更改为 regress-#
并且不执行任何操作.没有表sometable
,所以如果语句运行了会报错.
Notice how after I press enter without a semicolon, the prompt changes to regress-#
and no action is taken. There is no table sometable
, so if the statement had run an error would be reported.
接下来,看下一行
的用法?这会清除查询缓冲区.请注意,当缓冲区被清除时,提示会变回 regress=#
,因为不再缓冲部分语句.
Next, see the use of
on the next line? That clears the query buffer. Notice that the prompt changes back to regress=#
when the buffer is cleared, as there's no partial statement buffered anymore.
这显示了如何跨行拆分语句:
This shows how statements can be split across lines:
regress=> DROP TABLE
regress-> sometable
regress-> ;
ERROR: table "sometable" does not exist
令人困惑的是,像 d
这样的 psql
反斜杠命令是以换行符结束的,而不是以分号结束的,所以它们确实在您运行时运行按回车键.当您想(比如说)在编写语句时查看表定义时,这很方便,但对新手来说有点混乱.
The confusing thing is that psql
backslash commands like d
are newline-terminated, not semicolon terminated, so they do run when you press enter. That's handy when you want to (say) view a table definition while writing a statement, but it's a bit confusing for newcomers.
至于您的其他问题:
如果
psql
中有一个用于 Windows 的清除屏幕"命令,我还没有找到.在 Linux 上,我只使用 control-L,与任何其他使用 readline 的程序相同.在 Windows <代码>!cls 会起作用.
If there's a "clear screen" command in
psql
for Windows I haven't found it yet. On Linux I just use control-L, same as any other readline-using program. In Windows! cls
will work.
PostgreSQL 中的 DDL 是事务性的.你可以BEGIN
一个事务,发出一些DDL,然后COMMIT
事务让它生效.如果您不在显式事务中执行 DDL,则它会立即生效.
DDL in PostgreSQL is transactional. You can BEGIN
a transaction, issue some DDL, and COMMIT
the transaction to have it take effect. If you don't do your DDL in an explicit transaction then it takes effect immediately.
这篇关于在psql中,为什么有些命令没有效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!