sudo su - postgresWe will acquire the database file by typing:
wget http://pgfoundry.org/frs/download.php/527/world-1.0.tar.gzExtract the gzipped archive and change to the content directory:
tar xzvf world-1.0.tar.gz cd dbsamples-0.1/worldCreate a database to import the file structure into:
createdb -T template0 worlddbFinally, we will use the .sql file as input into the newly created database:
psql worlddb < world.sqlWe are now ready to log into our newly create environment:
psql worlddb
\d+
List of relations Schema | Name | Type | Owner | Size | Description --------+-----------------+-------+----------+--------+------------- public | city | table | postgres | 264 kB | public | country | table | postgres | 48 kB | public | countrylanguage | table | postgres | 56 kB | (3 rows)We have three tables here. If we want to see the columns that make up the "city" table, we can issue this command:
\d city
Table "public.city" Column | Type | Modifiers -------------+--------------+----------- id | integer | not null name | text | not null countrycode | character(3) | not null district | text | not null population | integer | not null Indexes: "city_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "country" CONSTRAINT "country_capital_fkey" FOREIGN KEY (capital) REFERENCES city(id)We can see information about each of the columns, as well as this table's relationship with other sets of data.
SELECT columns_to_return FROM table_name;For example, if we issue "\d country", we can see that the "country" table has many columns. We can create a query that lists the name of the country and the continent it is on with the following:
SELECT name,continent FROM country;
name | continent ----------------------------------------------+--------------- Afghanistan | Asia Netherlands | Europe Netherlands Antilles | North America Albania | Europe Algeria | Africa American Samoa | Oceania Andorra | Europe . . .To view all of the columns in a particular table, we can use the asterisk (*) wildcard character. This means "match every possibility" and, as a result, will return every column.
SELECT * FROM city;
id | name | countrycode | district | population ------+-----------------------------------+-------------+-------------------------------+------------ 1 | Kabul | AFG | Kabol | 1780000 2 | Qandahar | AFG | Qandahar | 237500 3 | Herat | AFG | Herat | 186800 4 | Mazar-e-Sharif | AFG | Balkh | 127800 5 | Amsterdam | NLD | Noord-Holland | 731200 6 | Rotterdam | NLD | Zuid-Holland | 593321 7 | Haag | NLD | Zuid-Holland | 440900 . . .Here, we see the "city" table in its entirety.
SELECT columns FROM table ORDER BY column_names [ ASC | DESC ];If we wanted to select the country and continent from the country table, and then order by continent, we can give the following:
SELECT name,continent FROM country ORDER BY continent;
name | continent ----------------------------------------------+--------------- Algeria | Africa Western Sahara | Africa Madagascar | Africa Uganda | Africa Malawi | Africa Mali | Africa Morocco | Africa Côte d\u0092Ivoire | Africa . . .As you can see, by default the order statement organizes data in ascending order. This means that it starts at the beginning of lettered organizations and the lowest number for numerical searches.
SELECT name,continent FROM country ORDER BY continent DESC;
name | continent ----------------------------------------------+--------------- Paraguay | South America Bolivia | South America Brazil | South America Falkland Islands | South America Argentina | South America Venezuela | South America Guyana | South America Chile | South America . . .We can also choose to sort by more than one column. We can have a primary sort field, and then additional sort fields that are used if multiple records have the same value in the primary sort field.
SELECT name,continent FROM country ORDER BY continent,name;
name | continent ----------------------------------------------+--------------- Angola | Africa Burundi | Africa Benin | Africa Burkina Faso | Africa Botswana | Africa Central African Republic | Africa Côte d\u0092Ivoire | Africa Cameroon | Africa Congo, The Democratic Republic of the | Africa . . .We now have alphabetical sorting in two columns.
SELECT name FROM city WHERE countrycode = 'USA';
name ------------------------- New York Los Angeles Chicago Houston Philadelphia Phoenix San Diego Dallas San Antonio . . .String values, like USA above, must be placed in single-quotations to be interpreted correctly by Postgres.
SELECT name FROM city WHERE countrycode = 'USA' AND name LIKE 'N%';
name -------------------- New York Nashville-Davidson New Orleans Newark Norfolk Newport News Naperville New Haven North Las Vegas Norwalk New Bedford Norman (12 rows)We can, of course, sort these results just like with regular, unfiltered select data.
SELECT name FROM city WHERE countrycode = 'USA' AND name LIKE 'N%' ORDER BY name;
name -------------------- Naperville Nashville-Davidson Newark New Bedford New Haven New Orleans Newport News New York Norfolk Norman North Las Vegas Norwalk (12 rows)
SELECT country.name AS country,city.name AS capital,continent FROM country JOIN city ON country.capital = city.id ORDER BY continent,country;
country | capital | continent ---------------------------------------+-----------------------------------+--------------- Algeria | Alger | Africa Angola | Luanda | Africa Benin | Porto-Novo | Africa Botswana | Gaborone | Africa Burkina Faso | Ouagadougou | Africa Burundi | Bujumbura | Africa Cameroon | Yaoundé | Africa Cape Verde | Praia | Africa Central African Republic | Bangui | Africa Chad | N´Djaména | Africa . . .This query has a few different parts. Let's start at the end and work backwards.
\d country
. . . . . . Foreign-key constraints: "country_capital_fkey" FOREIGN KEY (capital) REFERENCES city(id) . . . . . .This statement tells us that the "capital" column within the "country" table is a reference to the "id" column within the "city" table. This means that we can almost treat these two tables as one giant table by matching up the values in those columns.
table_name.column_nameIn our case, we are selecting records that have matching values in both tables, where the capital column of the country table should be compared to the id column of the city table.
Article ID: 230
Created On: Mon, Dec 30, 2013 at 1:54 AM
Last Updated On: Sun, Jan 5, 2014 at 8:55 PM
Authored by: ASPHostServer Administrator [asphostserver@gmail.com]
Online URL: http://faq.asphosthelpdesk.com/article.php?id=230