PostgreSQL研习

王朝mssql·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

用惯了 MySQL 下的 SQL 客户端环境,刚刚使用 PostgreSQL 的时候,真是感觉有点不适应。

好在网络上面文档很多,下面是一些主要的体会。

PostgreSQL 在后台运行的是一个叫做 postmaster 的程序,缺省的 TCP/IP 端口为 5432。

在客户端可以有很多工具连接到服务器上操作数据,对于一个 Unix 下的系统管理员而言,熟练掌握 psql 环境下的数据操作是十分需要的。

在启动 psql 之前,当然要求 psql 在你的 PATH 路径上,所以我们需要指定:

$ export PATH=$PATH:/usr/local/pgsql/bin

然后我们就可以进入了:

[user@host user]$ psql testdb

Welcome to psql, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms

\h for help with SQL commands

\? for help on internal slash commands

\g or terminate with semicolon to execute query

\q to quit

testdb=#

记住在 psql 后面必须跟上数据库名字,不像 mysql ,可以用 use 命令来切换数据库。

所有的 psql 相关的命令都必须以 \ 开头,如果不知道某个命令的用法,可以用 \? 来找到帮助。

\g 为执行查询语句

\q 为退出 psql 环境

\h 为帮助

booktown=# \?

\a toggle between unaligned and aligned mode

\c[onnect] [dbname|- [user]]

connect to new database (currently 'booktown')

\C <title> table title

\copy ... perform SQL COPY with data stream to the client machine

\copyright show PostgreSQL usage and distribution terms

\d <table> describe table (or view, index, sequence)

\d{t|i|s|v} list tables/indices/sequences/views

\d{p|S|l} list permissions/system tables/lobjects

\da list aggregates

\dd [object] list comment for table, type, function, or operator

\df list functions

\do list operators

\dT list data types

\e [file] edit the current query buffer or [file] with external editor

\echo <text> write text to stdout

\encoding <encoding> set client encoding

\f <sep> change field separator

\g [file] send query to backend (and results in [file] or |pipe)

\h [cmd] help on syntax of sql commands, * for all commands

\H toggle HTML mode (currently off)

\i <file> read and execute queries from <file>

\l list all databases

\lo_export, \lo_import, \lo_list, \lo_unlink

large object operations

\o [file] send all query results to [file], or |pipe

\p show the content of the current query buffer

\pset <opt> set table output <opt> = {format|border|expanded|fieldsep|

null|recordsep|tuples_only|title|tableattr|pager}

\q quit psql

\qecho <text> write text to query output stream (see \o)

\r reset (clear) the query buffer

\s [file] print history or save it in [file]

\set <var> <value> set internal variable

\t show only rows (currently off)

\T <tags> HTML table tags

\unset <var> unset (delete) internal variable

\w <file> write current query buffer to a <file>

\x toggle expanded output (currently off)

\z list table access permissions

\! [cmd] shell escape or command

因此,综合上面的说明,我们可以知道:

\dt 命令是显示表

\di 显示索引

\ds 显示序列

\dv 显示视图

\dp 显示权限

\dS 显示系统表

\dl 显示 lobjects

\d tablename 显示表的结构/索引/序列

\d indexname 显示索引的详细信息

执行过的查询语句可以用 \s \i 分别存盘和调入。

psql 中 SQL 语句的输入和 MySQL 类似,一直到分号才结束,但是不同的是,在分号结束前的换行,在 MySQL 里面是 > 符号,而在 psql 里是 -# 符号。

缓冲区的管理

在编辑查询语句缓冲之前,最好设定好你最熟悉的编辑器,例如 vi 的话:

$ set EDITOR='vi'

$ export EDITOR

\e 命令就可以进入编辑,用上你熟悉的 vi 指令来编辑了。

值得一提的是 PostgreSQL 具有子查询和视图等概念,而 MySQL 到现在还没有。

子查询的例子:

booktown=# SELECT title FROM books

booktown-# WHERE author_id = (SELECT id FROM authors

booktown(# WHERE last_name='Geisel'

booktown(# AND first_name='Theodor Seuss');

视图的例子:

CREATE VIEW recent_shipments

booktown-# AS SELECT count(*) AS num_shipped, max(ship_date), title

booktown-# FROM shipments

booktown-# JOIN editions USING (isbn)

booktown-# NATURAL JOIN books AS b (book_id)

booktown-# GROUP BY b.title

booktown-# ORDER BY num_shipped DESC;

从普通的 SQL 语句而言,应该说 PostgreSQL 和 MySQL 是很相似的。因此,PostgreSQL 在一个企业级的应用中,应该比 MySQL 更加具有优势。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航