Macromedia Certified Developer ColdFusion / Dreamweaver ... hundreds of
classes for Allaire, Macromedia and their .... ColdFusion, JSP and PHP
applications.
Biography Sean Hedenskog
Talking to Databases: SQL for Designers Sean Hedenskog
Agent Instructor Macromedia Certified Master Instructor Macromedia Certified Developer ColdFusion / Dreamweaver
Reside in Naperville, IL Since 1998 has successfully taught hundreds of classes for Allaire, Macromedia and their partners
Ideas in motion.
Overview
Static Web Pages
Static pages v. Dynamic pages What is SQL? Anatomy of a SQL statement Connecting to a database Recordset dialog box – simple mode SQL in Code View Modifying an existing query Recordset dialog box – advanced mode
§ Static pages are processed like this:
Dynamic Web Pages
Benefits of Building Dynamic Pages
§ Dynamic pages are processed like this:
§ Automatically reflect changes in data
http://www.macromedia.com/software/index.htm
http
Web Browser
Web Server
ColdFusion Dreamweaver Flash
§ Content management by nontechnical business
http://www.macromedia.com/software/index.cfm
users http
Web Browser
Web Server
Application Server
Database
§ Maintaining site is easier
ColdFusion Dreamweaver Flash
1
Databases Explained
What is SQL?
§ Collection of data stored in some organized
§ Structured Query Language
fashion – Tables – Column and Datatypes – Rows – Primary Keys § For this session we will be using two tables – Products – Categories
SQL Code Example
§ Standard language for getting information from
and updating database data § Some relational databases that use SQL are
Oracle, Microsoft SQL Server, Access, Sybase, Informix and DB2 § All contain the commands to Select, Insert,
Update, Delete, Create and Drop
Anatomy of a SQL Statement § SELECT {Field(s)}
SELECT ProductName, Price, Description
§ FROM {Table(s)}
FROM Products ORDER BY Price DESC
§ WHERE {Join Condition and Filter(s)} § ORDER BY {Field(s)}
Basic SQL Code Examples SELECT * FROM Products
SELECT ProductID, ProductName, Price FROM Products ORDER BY ProductName
Understanding Relational Tables § Imagine you have a table containing
product information – Product name, product description, price and vendor information § Multiple catalog items created by the
same vendor – Where would you store the information? SELECT ProductName, Description FROM Products WHERE ProductID = 2
2
Understanding Relational Tables
Why Use Joins
§ You wouldn’t want to store that data along
with the products
§ More efficient data storage, easier
manipulation and greater scalability
– Because vendor information is same for each product, you repeat the information and waste time and storage space – If vendor changes you will have to update all the vendor information – When data is repeated you increase the chances that the data will be entered differently
§ If data is stored in multiple tables how can
you retrieve the data? § The answer is to use a join. It associates
tables within a SELECT statement. – Onthefly
Joining 2 or More Tables
Join Types Example Data
§ If you are using more than one table you MUST
§ You have two tables
join the tables § If you forget you will return a cross join or
Cartesian product § Two options are available to join tables:
Products
Categories
19 records
5 records
– Option 1 – in the WHERE clause – Option 2 – in the FROM clause
Don’t Forget to Join the Tables
Tables Joined in the WHERE Clause
SELECT CategoryName, ProductName, Price
SELECT CategoryName, ProductName, Price FROM Products, Categories
FROM Products, Categories
WHERE Products.CategoryID = Categories.CategoryID
ORDER BY CategoryName
ORDER BY CategoryName
Products 19 records
Categories 5 records
Recordset 95 records
Products 19 records
Categories 5 records
Recordset 16 records
3
Tables Joined in the FROM Clause
INNER JOIN SELECT CategoryName, ProductName, Price
§ You have three options: – Option 1 – INNER JOIN • Returns all rows from both tables where there is a match. If there are rows in one table that do not have matches in the other, those rows will not be listed.
FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID ORDER BY CategoryName
– Option 2 – LEFT OUTER JOIN • Returns all the rows from the first table, even if there are no matches in the second table.
– Option 3 – RIGHT OUTER JOIN
Products
• Returns all the rows from the second table, even if there are no matches in the first table.
19 records
Categories 5 records
Recordset 16 records
LEFT OUTER JOIN
RIGHT OUTER JOIN
SELECT CategoryName, ProductName, Price
SELECT CategoryName, ProductName, Price
FROM Products LEFT OUTER JOIN Categories ON Products.CategoryID = Categories.CategoryID
FROM Products RIGHT OUTER JOIN Categories ON Products.CategoryID = Categories.CategoryID
ORDER BY CategoryName
ORDER BY CategoryName
Products 19 records
Categories 5 records
Recordset 19 records
Products 19 records
Categories 5 records
Filtering Data
Filtering Data
§ The previous examples would retrieve all rows
SELECT ProductName, Price
from the tables § Retrieving just the data you want involves
Recordset 17 records
FROM Products WHERE Price