Академический Документы
Профессиональный Документы
Культура Документы
Advanced Querying
In this section
Data types Operators and Functions Joins Subqueries Union and Intersect
Data Types
Data Types
Basic data types are Numerical Character/Text Date/Time Boolean
Data Types
Any data value in a relational database must have a data type The type of a value limits the kinds of operations you can do on a value Columns in a table must have a data type Certain data types are common to most RDBMS Additional RDBMS-specific datatypes
Numerical
integer,int
Whole numbers/integers
float
Floating point number (e.g. 21.6531 or 5555548.78)
numeric(p,s)
'p' number of digits 's' number of digits after decimal point
String/Text
varchar(n)
Variable length character string of max length n
text
Variable length character string of unlimited length PostgreSQL specific
Date/Time
timestamp
Date and time
date
Calender date (day, month and year)
Boolean
boolean
Single true or false value Supported true or false constants:
FALSE false 'f' 'false' 'n' 'no' '0' are 'true' apart from 0
Null values
You can have missing values in column of data these are know as null values Strictly speaking null means 'does not apply' but is often used for unknown values Saves having to insert a value in all rows with awkward consequences. For example, if you inserted zero for all null numbers then average would be inaccurate. Take care with Nulls If you add a number to a null number value the result is null If you concatenate a string to a null string value the result is null
Operators
Numeric Operators (+ - / * ^ % etc.)
Operators
String Operators
Concatenation
Operators
String Comparisons
= != LIKE (with wildcard %)
Operators
Logical Operators
AND OR NOT
Functions
There are a variety useful functions available in SQL eg. log(x) sin(x) sqrt(x) Other functions exist for manipulation of dates, strings etc.
http://www.postgresql.org/docs/8.3/interactive/functions.html
Date/Time Functions
current_timestamp, current_date
Date/Time Functions
Converting dates to strings Actually a data type conversion function to_char(datecolumn, dateformat)
Date/Time Functions
Converting strings to dates to_date(string,dateformat) Useful for non-standard date formats and inserting data from a text file
Aggregate functions
Aggregate functions gather records into a single aggregate row.
Aggregate functions
The 'group by' clause allows you to refine your aggregate query
Aggregate functions
Other aggregate functions include: sum(columnname)
Aggregate functions
avg(columnname)
Distinct
DISTINCT a shortcut to unique results Not a function
Mathematical functions
Only operate on numerical data types Examples include: log(x) sin(x) sqrt(x) trunc(x,s) returns x with any digits past s decimal points truncated
trunc()
trunc(x,s) returns x with any digits past s decimal points truncated
Coalesce
Function available for replacing null values with another value coalesce(column,value)
Typecasting
You can change the data type of a field in a query Known as 'typecasting'
Conditional functions
You may want to print certain values in certain cases Use the 'CASE' function
Joins
Until now we have being querying single tables Need to ask more complex questions across multiple tables e.g. How many comedy videos has Tim Booth rented this month? Power of relational databases is to join information from related tables and answer such questions
Joins
Join tables by matching values from one table's rows to values in another table's rows
Join Example
Keys
The matching values between tables are known as keys Link tables by linking on keys More theory tomorrow but for now... Primary key value in first table e.g. 'id' from customer table Foreign keys matching values in second table e.g. 'accountid' from rental table
Joins
Join creates a new relation from a combination of tables Basis of joins is the Cartesian product all possible combinations of rows between joined tables
Joins
Joins
You need to tell database which values match thus making a more useful join than the cartesian product
Join types
Two types joins are available Inner join Outer join
Outer joins
Outer join
Shows all rows of one of the two tables
eg. Show all customers and rental information even if they haven't rented a video
Subqueries
'IN' and 'EXISTS'
SELECT * FROM table1 WHERE x IN (SELECT y FROM table2)
or:
SELECT * FROM table1 WHERE EXISTS (SELECT y FROM table2 WHERE y = x)
Subqueries
'IN'
Subqueries
'EXISTS'
UNION
Query1
Query2
INTERSECT
Unions
UNION allows appending of one query another Number of columns and data types in each query must match
query1 UNION query2 Duplicates are removed unless you use UNION ALL
Unions
Intersects
INTERSECT shows all rows that are in both queries Number of columns and data types in each query must match
query1 INTERSECT query2 Duplicates are removed unless you use INTERSECT ALL
Intersects