在psql中,为什么某些命令无效? [英] In psql, why do some commands have no effect?

查看:156
本文介绍了在psql中,为什么某些命令无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,我在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)

也:

  1. 我在Windows中使用PostgreSQL.是否有任何命令可以清除控制台(就像Oracle中的cl scr一样)?

  1. 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-> \r
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.

接下来,在下一行看到\r的用法?这将清除查询缓冲区.请注意,清除缓冲区后,提示将更改回regress=#,因为不再缓冲任何部分语句.

Next, see the use of \r 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反斜杠命令是换行符终止的,而不是分号终止的,因此当您按Enter键时,它们会 do 运行.当您想在编写语句时查看(定义)表定义时很方便,但是对于新手来说有点困惑.

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.

关于您的其他问题:

  1. 如果Windows的psql中存在清除屏幕"命令,则尚未找到它.在Linux上,我只使用control-L,与任何其他使用readline的程序相同.在Windows中,\! cls可以使用.

  1. 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.

DDL是事务性的.您可以BEGIN事务,发出一些DDL,然后COMMIT事务使其生效.如果您未在显式事务中执行DDL,则该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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆