Вы находитесь на странице: 1из 60

NEBCDatabaseCourse2008

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:

TRUE true 't' 'true' 'y' 'yes' '1' all integers

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

Booleans and Nulls


Like other data types, booleans can be set to NULL Be aware that NULL does not mean 'false'. Negating NULL does not evaluate to 'true' AND and OR operations with NULL return NULL, apart from (NULL or true) = true

Booleans and Nulls


Take particular care in the WHERE clause of queries SELECT ... WHERE some_col = true; SELECT ... WHERE some_col = false; SELECT ... WHERE some_col != true; SELECT ... WHERE NOT some_col; SELECT ... WHERE some_col = NULL; SELECT ... WHERE some_col != NULL; SELECT ... WHERE some_col IS NULL; SELECT ... WHERE NOT coalesce(some_col, false);

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

Character String Functions


substr(s,n,l) returns sub-string of string s, starting at character n of length l upper(column) converts string to uppercase lower(column) converts string to lowercase

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

Implementing Joins in SQL


Inner join

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

Outer Joins in SQL


'LEFT OUTER JOIN' specifies which of the tables you want to see all rows from

Outer Joins in SQL

SQL- multiple tables


Show customer names and the titles of the videos they have rented

SQL- multiple tables


Join tables in succession 'customer' to 'rental' with id and accountid

SQL- multiple tables

SQL- multiple tables


Show customer names and the titles of the videos they have rented

SQL- multiple tables

SQL- multiple tables


Show customer names and the titles of the videos they have rented

SQL- multiple tables

SQL- multiple tables

Implementing Joins in SQL


Note that table name is required for ambiguous columns (e.g. video.id) Can use table aliases instead
SELECT c.firstname, r.videoid FROM customer AS c INNER JOIN rental AS r ON (c.id=r.accountid)

Useful for queries across multiple tables

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'

Unions and Intersects


Query1 Query2

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

Вам также может понравиться