Databases & SQL Basics Kurtis D. Leatham

Slides:



Advertisements
Similar presentations
Database Design Week 10.
Advertisements

Computer Concepts BASICS 4th Edition
CSE 1561 A Brief MySQL Primer Stephen Scott. CSE 1562 Introduction Once you’ve designed and implemented your database, you obviously want to add data.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Introduction to Structured Query Language (SQL)
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1 Created By Martin Schedlbauer
WEEK 11 Database Design. Agenda Hybrid Review Create Tables Add, Edit Data Create Relationships in MS Access 2010 Queries.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
With Microsoft Office 2007 Introductory© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Introductory.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
1 MySQL and phpMyAdmin. 2 Navigate to and log on (username: pmadmin)
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
WEEK 11 Database Design. TABLE INSTANCE CHARTS Create Tables.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
Stored Procedure. Objective At the end of the session you will be able to know :  What are Stored Procedures?  Create a Stored Procedure  Execute a.
Improving Database Performance Derrick Rapley
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Maintaining a Database Access Project 3. 2 What is Database Maintenance ?  Maintaining a database means modifying the data to keep it up-to-date. This.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
Web Programming MySql JDBC Web Programming.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Course Contents Overview: Database basics Lesson 1: Benefits of using a database Lesson 2: Table that data Lesson 3: Analyzing, viewing, and reporting.
Access Lessons 1, 2 and 3 ©2009 M and K Solutions, LLC – All Rights Reserved.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Chapter 5 Introduction to SQL.
 2012 Pearson Education, Inc. All rights reserved.
Larry Reaves October 9, 2013 Day 16: Access Chapter 2 Larry Reaves October 9, 2013.
Writing Basic SQL SELECT Statements
Using SQL to Prepare Data for Analysis
Chapter 8 Working with Databases and MySQL
Creating Tables & Inserting Values Using SQL
Shelly Cashman: Microsoft Access 2016
Kurtis D. Leatham Databases & SQL Basics Kurtis D. Leatham
Arrays, Structures, Multidimensional Arrays, Arrays of Structures
Presentation transcript:

Databases & SQL Basics Kurtis D. Leatham

June 28 th – July 1 st 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 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!)

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st  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

June 28 th – July 1 st 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

June 28 th – July 1 st  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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

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

June 28 th – July 1 st 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

June 28 th – July 1 st  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

June 28 th – July 1 st  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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st 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)

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st 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#

June 28 th – July 1 st 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

June 28 th – July 1 st  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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st  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

June 28 th – July 1 st 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

June 28 th – July 1 st  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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st  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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st 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

June 28 th – July 1 st Q&A ?