官术网_书友最值得收藏!

About this book's code examples

The sample output shown here has been created with the psql utility of PostgreSQL, usually running on a Linux system. Most of the code will work the same way if you are using a GUI utility such as pgAdmin3 to access the server instead. Take an example of the following line of code:

postgres=# SELECT 1;

The postgres=# part is the prompt shown by the psql command.

The examples in this book have been tested using PostgreSQL 9.3. They will probably work on PostgreSQL Version 8.3 and later. There haven't been many major changes to how server programming happens in the last few versions of PostgreSQL. The syntax has become stricter over time to reduce the possibility of mistakes in the server programming code. Due to the nature of these changes, most code from newer versions will still run on the older ones, unless it uses very new features. However, the older code can easily fail to run due to one of the newly enforced restrictions.

Switching to the expanded display

When using the psql utility to execute a query, PostgreSQL normally outputs the result using vertically aligned columns:

$ psql -c "SELECT 1 AS test"
 test 
------
 1
(1 row)

$ psql
psql (9.3.2)
Type "help" for help.
postgres=# SELECT 1 AS test;
 test 
------
    1
(1 row)

You can tell when you're seeing a regular output because it will end up showing the number of rows.

This type of output is hard to fit into the text of a book such as this. It's easier to print the output from what the program calls the expanded display, which breaks each column into a separate line. You can switch to the expanded display using either the -x command-line switch or by sending \x to the psql program. Here's an example of using each of these:

$ psql -x -c "SELECT 1 AS test"
-[ RECORD 1 ]
test | 1

$ psql
psql (9.3.2)
Type "help" for help.
postgres=# \x
Expanded display is on.
postgres=# SELECT 1 AS test;
-[ RECORD 1 ]
test | 1

Notice how the expanded output doesn't show the row count and numbers each output row. To save space, not all of the examples in the book will show the expanded output being turned on. You can normally tell which type you can see, by differences such as whether you're seeing rows or RECORD. The expanded mode will normally be preferred when the output of the query is too wide to fit into the available width of the book. It is a good idea to set the expanded mode to auto. This will automatically switch to expanded mode for tables with a lot of columns. You can turn on the expanded mode using \x auto:

postgres=# \x auto
Expanded display is used automatically.
主站蜘蛛池模板: 吉水县| 芦溪县| 宜章县| 四会市| 博湖县| 西乌珠穆沁旗| 凤城市| 石城县| 衡山县| 图木舒克市| 巨野县| 大冶市| 弥渡县| 辽源市| 满洲里市| 河西区| 桂平市| 那坡县| 临安市| 汽车| 旌德县| 横山县| 安陆市| 东台市| 定州市| 饶平县| 屏南县| 印江| 黔江区| 临猗县| 曲靖市| 海南省| 乾安县| 长宁县| 鲜城| 新乡县| 苍溪县| 昌都县| 安达市| 鄱阳县| 百色市|