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 dialog box simple mode SQL in Code View Modifying an existing query dialog box advanced mode Static pages are processed like this: http://www.macromedia.com/software/index.htm http Web Web Browser Server <html> ColdFusion Dreamweaver Flash </html> Dynamic Web Pages Benefits of Building Dynamic Pages Dynamic pages are processed like this: http://www.macromedia.com/software/index.cfm Automatically reflect changes in data Content management by non technical business users Web Browser http Web Server Application Server Database Maintaining site is easier <html> ColdFusion Dreamweaver Flash </html> 1
Databases Explained What is SQL? Collection of data stored in some organized fashion Tables Column and Datatypes Rows Primary Keys For this session we will be using two tables Structured Query Language 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 SQL Code Example Anatomy of a SQL Statement SELECT {Field(s)} SELECT ProductName, Price, Description ORDER BY Price DESC FROM {Table(s)} WHERE {Join Condition and Filter(s)} ORDER BY {Field(s)} Basic SQL Code Examples SELECT * SELECT ProductID, ProductName, Price ORDER BY ProductName SELECT ProductName, Description WHERE ProductID = 2 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? 2
Understanding Relational Tables You wouldn t want to store that data along with the products 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 Why Use Joins More efficient data storage, easier manipulation and greater scalability 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. On the fly Joining 2 or More Tables Join Types Example Data If you are using more than one table you MUST join the tables If you forget you will return a cross join or Cartesian product Two options are available to join tables: Option 1 in the WHERE clause Option 2 in the FROM clause You have two tables Don t Forget to Join the Tables, Tables Joined in the WHERE Clause, WHERE.CategoryID =.CategoryID 95 records 16 records 3
Tables Joined in the FROM Clause 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. 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 Returns all the rows from the second table, even if there are no matches in the first table. INNER JOIN INNER JOIN ON.CategoryID =.CategoryID 16 records LEFT OUTER JOIN LEFT OUTER JOIN ON.CategoryID =.CategoryID RIGHT OUTER JOIN RIGHT OUTER JOIN ON.CategoryID =.CategoryID 19 records 17 records Filtering Data Filtering Data The previous examples would retrieve all rows from the tables Retrieving just the data you want involves specifying a search criteria, also known as a filter Data is filtered by specifying the search criteria in the WHERE clause In addition, you can use AND, OR and NOT SELECT ProductName, Price WHERE Price < 500 SELECT ProductName, Price WHERE ProductName = ColdFusion MX Standard OR ProductName = Dreamweaver 8 4
Wildcard Filtering Review So Far Filtering against unknown values Use the LIKE operator, with wildcards % (percent sign) 0 or more characters _ (underscore) character Dynamic pages are good SQL will help create dynamic pages SQL is easy to learn and not so scary anymore SELECT ProductName, Price WHERE ProductName LIKE C% Resources SQL for Designers Teach Yourself SQL in 10 minutes by Ben Forta http://www.forta.com/books/0672325675/ SQL: Visual QuickStart Guide, 2 nd edition by Chris Fehily http://www.peachpit.com/title/0321334175 Best part Dreamweaver will generate the SQL code for you Could be easily accomplished for ASP, ColdFusion, JSP and PHP applications In this session, we will use ColdFusion Interactive Online SQL Training for Beginners http://sqlcourse.com/ Connecting to a Database Connecting to a Database To create a dynamic page we first need to connect to a database We connect to the database by creating a datasource Dreamweaver enables you to create connections On the Database tab in the Application panel, click the + (plus) button and select the type of database Datasource will then be available to us via the Databases panel in Dreamweaver 5
Connecting to a Database Connecting to a Database Microsoft Access Connection Dialog Once you have created the datasource you can view its contents in the Databases tab Creating a Introducing the Dialog Box To begin, click the plus sign on the Bindings tab and select (Query) Using the form fields construct your SQL statement Code View Modifying an Existing Query Dreamweaver automatically generates the SQL code To modify an existing query simply double click the in the Bindings tab <cfquery name= qget datasource= sa106w > SELECT ProductName, Price, Description ORDER BY ProductName ASC </cfquery> 6
Introducing the Dialog Box Advanced Summary This dialog allows for multi table queries Dynamic web pages are good SQL is the translation language between our applications and the database data SQL is easy to learn Dreamweaver generates SQL for ASP, ColdFusion, JSP and PHP applications Resources Macromedia Dreamweaver MX 2004 with ASP, ColdFusion, and PHP: Training from the Source by Jeff Bardzell http://www.peachpit.com/title/0321241576/ Macromedia Hands On Training Macromedia Dreamweaver MX 2004: Dynamic Application Development Fast Track to ColdFusion MX 7 http://www.macromedia.com/training/ Thank you. Ideas in motion. 7