SQL
[single page] [slide show]
SQL: "Intergalactic Dataspeak"
History
- invented by IBM
- used to mean "Structured Query Language"
- defined by ANSI/ISO standards
- current: SQL-92
- emerging: SQL-99
- no product is 100% standard
SQL Components
Data definition language (DDL)
Data manipulation language (DML)
SQL in Access
Evil twin of a query
- Design view
- table: columns and constraints
- relationships: joins
- Confusion: Access calls anything SQL statement a query
Access Query Types
|
Select
Action
- Make table
- Delete
- Append
- Update
|
|
SQL-only
Crosstab
Parameter |
Access DDL
Usually generated automatically by Table Design View
Statements
CREATE TABLE
ALTER TABLE
- add or delete columns or indexes
DROP TABLE
CREATE INDEX
- create an index for a column
[example] [notes]
Access SQL Data Types
| SQL |
Table Design |
| Data Type |
Field Size |
Boolean
Logical
Yes/No |
Yes/No |
|
Byte
Integer1 |
Number |
Byte |
Counter
Autoincrement |
AutoNumber |
Long Integer |
Currency
Money |
Currency |
|
Datetime
Date
Time |
Date/Time |
|
Short
Integer2
Smallint |
Number |
Integer |
Long
Int
Integer
Integer4 |
Number |
Long Integer |
|
| SQL |
Table Design |
| Data Type |
Field Size |
Single
Float4
Real |
Number |
Single |
Double
Float
Float8
Number
Numeric |
Number |
Double |
Text
Alphanumeric
Char
Character
String |
Text |
|
Longtext
Longchar
Memo
Note |
Memo |
|
Longbinary
General
OLEobject |
(OLE) Object |
|
| GUID |
AutoNumber |
ReplicationID |
|
Access DML
Usually generated automatically by Query Design View
Statements
|
SELECT
- select/project rows/columns from source table(s) into new
table
UNION
- union of two or more tables
UPDATE
- change column values in existing rows
DELETE
|
|
INSERT INTO
- append new rows to a table
SELECT INTO
- save the result of a
SELECT in a new table
TRANSFORM
PARAMETERS
- obtain user input when query is run
|
SELECT
|
Simplified form:
SELECT predicate table.column, ...
FROM table, ...
WHERE condition
NB: ignoring (for now):
|
|
Predicates (optional)
- no duplicate rows
DISTINCT
- based on selected columns only
DISTINCTROW
- partial results
|
Stupid (well, not really) SELECT Tricks
|
Concatenate columns
SELECT columna & " " & columnb AS new_column
FROM ...
|
|
Nested joins
SELECT column, ...
FROM tablea INNER JOIN (
tableb INNER JOIN tablec ON ...
) ON ...
|
|
Parameters
SELECT column, ...
FROM ...
WHERE column op [prompt]
|
|
SELECT INTO
SELECT column, ...
INTO new_table
FROM ...
|
examples
Updatable Queries
|
A SELECT result is a table...
- has rows and columns
- presented as datasheet
|
|
...but not necessarily a relation
- not necessarily a reversible relationship between
- a row in the result table
- rows in the base tables
- e.g.
- 2 base tables with 1:n relationship
- 3 or more base tables
- UNION
- join without explicit foreign key
|
The general solution (is it updatable?) is quite complex, and implementation-dependent.
Readings