Using Simple SELECT Statements

Một phần của tài liệu Beginning Databases with Postgre SQL phần 2 pptx (Trang 36 - 39)

As with all relational databases, we retrieve data from PostgreSQL using the SELECT statement.

It’s probably the most complex statement in SQL, but it really is at the heart of using relational databases effectively.

Let’s start our investigation of SELECT by simply asking for all the data in a particular table.

We do this by using a very basic form of the SELECT statement, specifying a list of columns and a FROM clause with a table name:

Table 4-1. Basic psql Commands

Command Description

\? Get a help message

\do List operators

\dt List tables

\dT List types

\h <cmd> Get help on a SQL command; replace <cmd> with the actual command

\i <filename> Execute commands read from the filename <filename>

\r Reset the buffer (discard any typing)

\q Quit psql

SELECT <comma-separated list of columns> FROM <table name>

If we can’t remember what the exact column names are called, or want to see all the columns, we can just use an asterisk (*) in place of the column list.

Note In this book, we show SQL keywords that structure the commands in uppercase in the text to make them stand out clearly. SQL is not case-sensitive, although a few implementations do make table names case- sensitive. Data stored in SQL databases is case-sensitive, so the character string "Newtown" is different from the character string "newtown".

Try It Out: Select All Columns from a Table

We will start by fetching all the data from the item table:

SELECT * FROM item;

Remember that the semicolon (;) is for the benefit of psql, to tell it you have finished typing. Strictly speaking, it is not part of SQL. If you prefer, you can terminate SQL statements typed into psql with \g, which has the same effect as the semicolon. If you are using a different tool to send SQL to PostgreSQL, you may not need either of these terminators.

Enter the command, and you’ll see PostgreSQL’s response:

bpsimple=> SELECT * FROM item;

item_id | description | cost_price | sell_price ---+---+---+--- 1 | Wood Puzzle | 15.23 | 21.95 2 | Rubic Cube | 7.45 | 11.49 3 | Linux CD | 1.99 | 2.49 4 | Tissues | 2.11 | 3.99 5 | Picture Frame | 7.54 | 9.95 6 | Fan Small | 9.23 | 15.75 7 | Fan Large | 13.36 | 19.95 8 | Toothbrush | 0.75 | 1.45 9 | Roman Coin | 2.34 | 2.45 10 | Carrier Bag | 0.01 | 0.00 11 | Speakers | 19.73 | 25.32 (11 rows)

bpsimple=>

How It Works

We simply asked PostgreSQL for all the data from all the columns in the item table, using an * for the column names. PostgreSQL gave us just that, but neatly arranged with column headings and a pipe (|) symbol to separate each column. It even told us how many rows we retrieved.

But suppose we didn’t want all the columns? In general, you should ask PostgreSQL, or indeed any relational database, to retrieve only the data you actually want. Each column of

each row that is retrieved adds a little extra work. There is no point in making the server do work unnecessarily; it’s always nice to keep things clean and efficient.

You will also find that, once you start having SQL embedded in other languages (see Chapter 14), specifying exact columns will protect you against changes to the database schema.

For example, if you use * to retrieve all columns and an additional column had been inserted in a table since the code was tested, you may find that you are processing data from a different column than the one you intended. If a column that you are using is deleted, then the SQL in your program will fail, since the column can no longer be retrieved; however, that is a much easier bug to find and correct than some application code accessing the wrong column while processing data. If you specify the columns by name, you have the option of searching all your code to see if the column names appear, before making changes to the database, and

preventing bugs from ever occurring.

Let’s try restricting the columns we retrieve. As we saw in the syntax earlier, we do this by specifying each column we want, separated by a comma. If we don’t want the columns in the order we specified when we created the database table, that’s fine—we can specify the columns in any order we like, and they will be returned in that order.

Try It Out: Select Named Columns in a Specific Order

To retrieve the name of the town and last name of all our customers, we must specify the name of the columns for town and last name, and, of course, the table from which to retrieve them.

Here is the statement we need and PostgreSQL’s response:

bpsimple=> SELECT town, lname FROM customer;

town | lname ---+--- Hightown | Stones Lowtown | Stones Nicetown | Matthew Yuleville | Matthew Oakenham | Cozens Nicetown | Matthew Bingham | Stones Bingham | Stones Histon | Hickman Tibsville | Howard Bingham | Jones Winersby | Neill Oxbridge | Hardy Welltown | O'Neill Milltown | Hudson (15 rows)

bpsimple=>

How It Works

PostgreSQL returns all the data rows from the table we specified, but only from the columns we requested. It also returns the column data in the order in which we specified the columns in the SELECT statement.

Một phần của tài liệu Beginning Databases with Postgre SQL phần 2 pptx (Trang 36 - 39)

Tải bản đầy đủ (PDF)

(66 trang)