Presentation is loading. Please wait.

Presentation is loading. Please wait.

Databases & SQL Basics Kurtis D. Leatham

Similar presentations


Presentation on theme: "Databases & SQL Basics Kurtis D. Leatham"— Presentation transcript:

1 Databases & SQL Basics Kurtis D. Leatham KDL@KomputerMan.com http://www.KomputerMan.com

2 June 28 th – July 1 st 2006 2 About Me I own KomputerMan.com, a 1 employee Cold Fusion application development consulting company. Developing CF applications since 1998 and have been consulting since 1999. I am not one of those people who has the time to delve into how CF does its magic. I am much more concerned with how I can use CF to make my own magic! Oh yeah… and I like to talk. (the wife inserted that one!)

3 June 28 th – July 1 st 2006 3 Introduction  Database access is easy with CF Just need to learn a few basics about databases, SQL, and some CF  Some CF experience is presumed, not critical Most of the CF used is easy to pick up  Many topics are not CF specific May apply to any ODBC call All examples work with MS Access and CF

4 June 28 th – July 1 st 2006 4 Today’s Agenda  Databases, SQL Basics, and ColdFusion Working with Databases in CF  Databases and Datasources  Database basics CF and SQL Basics  Creating queries  Build dynamic SQL statements  Displaying query results  Interact with a database CF Forms and SQL  Insert and select data with a CF Form Q&A

5 June 28 th – July 1 st 2006 5  A DBMS is software that organizes databases Access is both a DBMS and a database Doesn't have to run on the CF server  Databases are the heart of business apps Either you have one, or will create one  Creating databases is beyond scope of class Jill Jack Pets CFML Server Database Management Systems

6 June 28 th – July 1 st 2006 6 Database Management Systems  ODBC Drivers communicate with DB via SQL commands  CF uses “Datasources” to refer to the ODBC Drivers  SQL stands for Structured Query Language Jill Jack CFML Server SQL Data Pets

7 June 28 th – July 1 st 2006 7  A Datasource is the CF name for the DB Describes the name, physical location, and ODBC driver for connecting to DB  Can choose any name, unique to CF Server  May need to create DBMS-specific or driver- specific SQL DBMS: MS Access DB Name: Pets Filename: Pets.mdb Driver: ODBC Pets Datasources

8 June 28 th – July 1 st 2006 8 Datasources  Defined and Created by the CF Admin Can use OBDC driver or CF JDBC Drivers  Various parameters can be set, to affect performance and features Can store the username and password SQL operations can be restricted (read only) BLOBs can be enabled  See CF Administrator manual for details

9 June 28 th – July 1 st 2006 9 Datasources  Use CF Administrator to create the 'Pets_DB' DSN In CF Admin click on Data & Services Click on Data Sources Fill in the blanks then hit the Add button

10 June 28 th – July 1 st 2006 10 Datasources  Locate the database Hit the Submit button

11 June 28 th – July 1 st 2006 11 Database Basics  A Database is a collection of data, hopefully stored in an organized fashion Composed of tables Tables are organized in rows and columns Each column is assigned a Datatype to define the type of data that the column can hold  Datatype examples: text, date, currency, etc… Pets PetTypes Pets People 2 1 PeoCnt (Identity) People JackFrost 12/31/1848SantaClause BirthDate (Date) FName (Text 20) LName (Text 30) 11/24/1863

12 June 28 th – July 1 st 2006 12  Tables are Similar to a spreadsheet but a Table IS NOT a spreadsheet Tables should describe one thing and one thing ONLY  I.E. A persons name, address, birth date, etc…  Not a persons name and the name of their dog(s) Database Basics 2 1 PeoCnt (Identity) People JackFrost 12/31/1848SantaClause BirthDate (Date) FName (Text 20) LName (Text 30) 11/24/1863 Pets PetTypes Pets People

13 June 28 th – July 1 st 2006 13  Each row in the table should be uniquely identified with a Primary Key (PK) A PK is not required, but I won't build a table without one Recommendation: Use an Auto Incrementing field as your PK Concatenated PK's work but are a pain in the posterior relationship!!! Primary Keys 2 1 PeoCnt (Identity) People JackFrost 12/31/1848SantaClause BirthDate (Date) FName (Text 20) LName (Text 30) 11/24/1863

14 June 28 th – July 1 st 2006 14 Primary Keys  No two rows can have the same primary key value  Each row must have a primary key value (no nulls) Null: Column having no value at all  Not the same as space or empty string

15 June 28 th – July 1 st 2006 15 Our Example Database  Simple application that tracks people and their pets One person can have multiple pets One pet can only be associated to one person Each pet must have a type such as Dog or Cat

16 June 28 th – July 1 st 2006 16 Our Example Database  PeoCnt is the PK for the People table PeoCnt is a Foreign Key in the Pets Table  PetCnt is the PK for the Pets table  PetTypeCnt is the PK for the PetTypes table PetTypeCnt is a Foreign Key in the Pets table

17 June 28 th – July 1 st 2006 17 Selecting Data with SQL  The SELECT query is most frequently used query Retrieves data from one or more tables At a minimum a SELECT query takes two clauses  The data to be retrieved (SELECT clause)  The table to retrieve the data from (FROM clause) May also specify  Filtering conditions (WHERE clause)  Sort order (ORDER BY clause)  Can use Joins to link tables together (JOIN or WHERE clause)

18 June 28 th – July 1 st 2006 18 Simple Select Statement  Specify the columns to be retrieved Must specify at least one column Can retrieve all columns in a table with SELECT *  Recommendation:Specify the needed columns  Some databases require table names to be fully qualified with a prefix indicating the table name  People.FName SELECT FName, LName, City FROM People Example 1

19 June 28 th – July 1 st 2006 19 Datasources Part II  used to create CF queries DATASOURCE indicates the DSN to use NAME provides a name for the record set Can pass any SQL that’s acceptable to the DBMS DSN contains Username and Password to connect to the database SELECT FName, LName, City FROM People

20 June 28 th – July 1 st 2006 20 Datasources Part II  Set up a Request variable for your DSN The Request variable gets set in Application.cfm If the Datasource ever changes you just make one change in Application.cfm and you are finished SELECT FName, LName, City FROM People As set inside Application.cfm

21 June 28 th – July 1 st 2006 21 Displaying Query Results  used to display the record set To show the first record #GetPeople.FName[1]# To show all records of the record set #FName#, #LName#, #City#

22 June 28 th – July 1 st 2006 22 Selecting Data with AS  Can rename (alias) a column using AS Use it to give a shorter name to a long column name The Aliases name is temporary, lasting only for the life of the record set Useful when a column in the table has a name that would be illegal in CF  I've had to alias fields like '# of Items' SELECT FName AS MyName, City FROM People Example 2

23 June 28 th – July 1 st 2006 23  You can concatenate two or more columns together Syntax is specific to the DBMS so refer to the DBMS documentation  Can perform mathematical calculations on numeric columns SELECT Name, (Salary * 1.10) AS AdjSalry SELECT (LName + ‘, ’ + FName) AS MyName, City FROM People Selecting Data with AS Example 3

24 June 28 th – July 1 st 2006 24 Selecting Data with WHERE  Can select specific records with a WHERE clause Find the Person WHERE PeoCnt=1 You can filter on columns you don’t SELECT  If the datatype of the column in the WHERE clause is numeric, the value is specified without quotes SELECT FName, LName, PeoCnt FROM People WHERE PeoCnt = 1 Example 4

25 June 28 th – July 1 st 2006 25 Selecting Data with WHERE  If the datatype of the column in the WHERE clause is alphanumeric, the value is specified with quotes or tick marks SELECT FName, LName, City FROM People WHERE FName = 'Jack' Example 5

26 June 28 th – July 1 st 2006 26 Selecting Data with WHERE  The WHERE clause can contain dates Formatted dates varies by DBMS but I've found this format to be pretty universal Returns the FName, LName, and City of all people born before Jan 1, 1901 SELECT FName, LName, City FROM People WHERE BirthDate <= #CreateODBCDate('01/01/1901')# Example 6

27 June 28 th – July 1 st 2006 27 Selecting Data with AND or OR SELECT FName, LName, City FROM People WHERE BirthDate <= #CreateODBCDate('01/01/1901')# AND FName = 'Jack'  Can use more than one parameter in your WHERE clauses Use the AND clause to match both parameters Use the OR clause to match one of the parameters SELECT FName, LName, City FROM People WHERE BirthDate <= #CreateODBCDate('01/01/1901')# OR FName = 'Jack' Example 7

28 June 28 th – July 1 st 2006 28  Can search for a match on multiple values using the IN clause Values are separated with commas and are enclosed with parentheses This performs the equivalent of an “OR” search  WHERE PeoCnt = 1 OR PeoCnt = 3 OR PeoCnt = 4 SELECT FName, LName, PeoCnt FROM People WHERE PeoCnt IN (1,3,4) Selecting Data with IN Example 8

29 June 28 th – July 1 st 2006 29 Selecting Data with NOT  To negate a condition, use the NOT operator SELECT FName, LName, PeoCnt FROM People WHERE NOT PeoCnt IN (3,5,7) SELECT FName, LName, City FROM People WHERE BirthDate IS NOT NULL Example 9

30 June 28 th – July 1 st 2006 30  Can search for a match of wildcards using the LIKE clause Finds all FName records beginning with a J % can be used anywhere in the string To find records with name containing “ar”, like Charles, Arnold, Barbara, Karen, use  WHERE FName LIKE ‘%ar%’ Beware: wildcard matches are generally the slowest form of filtering SELECT FName, LName, City FROM People WHERE FName LIKE ‘J%’ Selecting Data with LIKE Example 10

31 June 28 th – July 1 st 2006 31 Selecting Data With AND  This query will return all people that have pets and their pets names This can be done using a JOIN I prefer the WHERE clause unless using an OUTTER JOIN (Not covered in this class) SELECT FName, LName, Pet_Name, TypeOfPet FROM People, Pets, PetTypes WHERE People.PeoCnt = Pets.PeoCnt AND Pets.PetTypeCnt = PetTypes.PetTypeCnt Example 11

32 June 28 th – July 1 st 2006 32 Selecting Data With AND  Use ORDER BY to sort the record set Can specify multiple, comma-separated columns  Data is sorted by the first column, then by the second column, etc… Data is sorted in ascending order by default  Force descending order with DESC clause SELECT FName, LName, City FROM People ORDER BY LName, FName DESC SELECT FName, LName, Pet_Name, TypeOfPet FROM People, Pets, PetTypes WHERE People.PeoCnt = Pets.PeoCnt AND Pets.PetTypeCnt = PetTypes.PetTypeCnt ORDER BY LName DESC, Pet_Name Example 12

33 June 28 th – July 1 st 2006 33  Can search for a match based on Form variables Alphanumeric variables need tick marks, numeric variables do not SELECT FName, LName, City FROM People WHERE PeoCnt = #Form.PeoCnt# Creating Dynamic Queries SELECT FName, LName, City FROM People WHERE FName = '#Form.FName#' Example 13

34 June 28 th – July 1 st 2006 34 Creating Dynamic Queries  CF builds the SQL at run time using conditional statements and variables Powerful yet easy to use feature of CF CF processes the CF tags and variables before passing the resulting SQL to the database SELECT FName, LName, City FROM People WHERE City = '#CityArray[cc]#' OR City = '#CityArray[cc]#' Example 14

35 June 28 th – July 1 st 2006 35 INSERT Operations  The INSERT statement inserts one or more rows into a table Include all columns that do not permit nulls Data can be inserted into one table at a time only  INSERT can be tied to Form Controls (SELECT, INPUT, Radio, etc…) INSERT INTO People (FName, LName, Address, City, State, ZipCode) VALUES ('#Form.FName#', '#Form.LName#', '#Form.Address#', '#FORM.City#', '#Form.State#', '#Form.ZipCode#') Example 15

36 June 28 th – July 1 st 2006 36 UPDATE Operations  The UPDATE statement updates data in one or more rows: Specify the table to be updated, rows to be affected, and the new values Can update several columns at once  If no WHERE clause is used, change is made to ALL rows in the table. Could be disastrous! Could be intentional Use caution!!! UPDATE People SET FName = 'Santa Rockin', LName = 'My Clause' WHERE PeoCnt = 1 Example 16

37 June 28 th – July 1 st 2006 37 Conclusion  Database and SQL processing is easy CF makes it even easier  Still plenty more for you to learn, I.E.  BLOCKFACTOR  CACHEDWITHIN  Practice the examples offered here  Download the sample application http://www.komputerman.com/CFUG/Index.cfm

38 June 28 th – July 1 st 2006 38 Q&A ?


Download ppt "Databases & SQL Basics Kurtis D. Leatham"

Similar presentations


Ads by Google