Changes between Initial Version and Version 1 of PostgreSQL-PostGis-SQL Language


Ignore:
Timestamp:
Mar 14, 2012 8:47:24 PM (13 years ago)
Author:
celine
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PostgreSQL-PostGis-SQL Language

    v1 v1  
     1back to first page[..][[BR]] 
     2back to ["Work program - Spain"][[BR]] 
     3 
     4= The PostgreSQL System = 
     5 
     6PostgreSQL is a free relational database system supporting the SQL language available on a variety of hardware platforms.[[BR]] 
     7 
     8= PostGIS = 
     9is an extension to the PostgreSQL object-relational database system which allows GIS (Geographic Information Systems) objects to be stored in the database. PostGIS includes support for GiST-based R-Tree spatial indexes, and functions for analysis and processing of GIS objects.[[BR]] 
     10 
     11= SQL = 
     12SQL (Structured Query Language) is the standard relational query language with statements to create, remove, retrieve and maintain data in a relational database. SQL contains three main types of commands, Data Definition language (DDL), Data Manipulation language (DML), and Data Control language (DCL) statements.[[BR]] 
     13The specific version of SQL used by PostgreSQL can be used interactively via the psql front-end, embedded in programming languages like C or called from Java, perl and tcl. PostgreSQL supports a subset of SQL3 with extra types and commands.[[BR]] 
     14 
     15= SQL syntax = 
     16 • SQL keywords and names contain alpha-numeric characters (including ‘_’) and must begin with a letter or ‘_’ and are case insensitive. A name containing other characters or which is an SQL keyword must be written in double quotes ‘"’. Column names, table names and database names are separate, so a column can have the same name as the table it is in. Keywords are usually written in uppercase letters. [[BR]] 
     17 • The comment syntax is the same as C : /* ...*/, (but // comments are not allowed). Also ‘--’ comments out the rest of the line.[[BR]] 
     18 • SQL statements end with ‘;’. 
     19 
     20== Data Definition Statements == 
     21 
     22'''CREATE TABLE'''      creates an empty table defining the structure (see the exercise definition). [[BR]] 
     23'''DROP TABLE'''        destroys a table and all the data it contains (if you have permission).[[BR]] 
     24'''ALTER TABLE'''       change the definition of a table (column names and types etc.) [[BR]] 
     25 
     26== Data Manipulation Statements == 
     27 
     28'''INSERT'''    add rows or part of rows to a table. [[BR]] 
     29'''SELECT'''    perform a query to view some existing data. [[BR]] 
     30'''UPDATE'''    modify existing table entries, selecting which rows are changed. [[BR]] 
     31'''DELETE'''    remove selected rows from a table. [[BR]] 
     32'''ROLLBACK'''  requests that a change be undone. [[BR]] 
     33'''COMMIT'''    requests that the database make a permanent record of a change. [[BR]] 
     34'''COPY'''      reads or writes data between databases and files.[[BR]] 
     35 
     36'''SELECT''' : The most used statement 
     37The SELECT statement is the most frequently used statement in SQL, it is used to retrieve data from one or more tables. The names of the tables used in the statement must be listed in the statement. If a column-name appears in more than one table, references to the column must be written as table.column to avoid ambiguity. The syntax for '''SELECT''' is used in '''UPDATE''' and '''DELETE''', for example:[[BR]] 
     38{{{ 
     39#!sql 
     40SELECT name,pay FROM payroll WHERE name = ’poor man’ ; 
     41UPDATE payroll SET pay = pay * 10 WHERE name = ’poor man’ ; 
     42DELETE FROM payroll WHERE pay < 0 ; 
     43}}} 
     44 
     45== NULL == 
     46 
     47is a special value that represents the fact that a piece of data that is either unknown or undefined. Note that this is not zero, or the empty string. Comparisons with NULL return unknown, use the expression IS NULL or IS NOT NULL for example:[[BR]] 
     48{{{ 
     49#!sql 
     50SELECT ’Unknown’ AS born ,name FROM actors WHERE born IS NULL ; 
     51}}} 
     52 
     53== WHERE == 
     54{{{ 
     55#!sql 
     56SELECT * FROM Localisation WHERE town = 'Bilbao'; 
     57SELECT * FROM Localisation WHERE inhabitant<1000; 
     58SELECT * FROM Localisation WHERE town LIKE'%N%'; 
     59SELECT * FROM Localisation WHERE town IN ('Bilbao','Mundaka'); 
     60SELECT * FROM Localisation WHERE inhabitant BETWEEN 1000 AND 2000; 
     61SELECT * FROM Localisation WHERE inhabitant<1000 AND LIKE'%N%'; 
     62}}} 
     63 
     64== ORDER BY == 
     65The result of a select statement is not sorted unless ORDER BY is used, when the column(s) to be used for sorting are given. Ascending order (ASC) is the default, specify DESC for descending order. For example to list films longest first:[[BR]] 
     66{{{ 
     67#!sql 
     68SELECT title,length FROM films ORDER BY length DESC ; 
     69}}} 
     70 
     71== DISTINCT == 
     72Duplicate rows in the result of SELECT are all shown unless DISTINCT is used. For example to remove duplicate 
     73titles:[[BR]] 
     74{{{ 
     75#!sql 
     76SELECT DISTINCT title FROM Films; 
     77}}} 
     78 
     79== Alias == 
     80Tables or columns may have an alias to shorten queries or let the same table to be used several times in a single query. 
     81Here A is used as a shorthand for the table actor[[BR]] 
     82{{{ 
     83#!sql 
     84SELECT COUNT(A.name) FROM actors A WHERE A.name < ’Zorro’; 
     85}}} 
     86 
     87== GROUP BY == 
     88rearranges the original table into logical partitions, in each partition all rows have the same value in the specified 
     89column(s). For example to group films by director and give the size of each group:[[BR]] 
     90{{{ 
     91#!sql 
     92SELECT director,COUNT(director) FROM films GROUP BY director; 
     93Or you can use: '''min''', '''max''', '''avg''' (average), '''sum''' 
     94}}} 
     95 
     96== HAVING == 
     97selects groups from the partitions generated by GROUP BY. For example: 
     98{{{ 
     99#!sql 
     100SELECT part FROM casting GROUP BY part HAVING part > "A"; 
     101}}} 
     102 
     103== A pattern == 
     104is a string constant that may contain wild-card characters: ‘%’ matches any number of characters, ‘_’ (underscore) 
     105matches a single character for example to select any actors name containing o. 
     106{{{ 
     107#!sql 
     108SELECT name FROM actors WHERE name LIKE ’%o%’; 
     109}}} 
     110 
     111== A set == 
     112is either a bracketed list of expressions, or a nested SELECT query. The ability to nest queries gives SELECT much of its power, for example to look for actor-directors: 
     113{{{ 
     114#!sql 
     115SELECT director FROM films /* Any actor-directors ? */ 
     116WHERE director IN (SELECT name FROM actors ); 
     117}}} 
     118 
     119== EXISTS == 
     120is TRUE if the nested query returns any data, for example: [[BR]] 
     121{{{ 
     122#!sql 
     123SELECT director FROM films 
     124WHERE EXISTS ( SELECT name FROM actors WHERE born < ’1-jan-1900’) ; 
     125}}} 
     126 
     127= Some links = 
     128http://developer.postgresql.org/ [[BR]] 
     129 
     130