 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
Advertisements

 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
1 Introduction to Web Application Introduction to Data Base.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
Database Lecture # 1 By Ubaid Ullah.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 25 – Database: SQL, ADO and RDS Outline 25.1Introduction 25.2Relational Database Model 25.3Relational.
Copyright © 2003 by Prentice Hall Module 4 Database Management Systems 1.What is a database? Data hierarchy and data organization Field, record, file,
ASP.NET Programming with C# and SQL Server First Edition
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Database Technical Session By: Prof. Adarsh Patel.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 10: Database Application Programming Interface Xiang Lian The University of Texas – Pan American Edinburg,
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC.
Database: SQL and MySQL
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Databases November 15, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg. Published.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Relational Databases Database Driven Applications Retrieving Data Changing Data Analysing Data What is a DBMS An application that holds the data manages.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
IST 220 Introduction to Databases Course Wrap-up.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
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,
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
Session 1 Module 1: Introduction to Data Integrity
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
Database Connectivity with ASP.NET. 2 Introduction Web pages commonly used to: –Gather information stored on a Web server database Most server-side scripting.
IS6146 Databases for Management Information Systems Lecture 1: Introduction to IS6146 Rob Gleasure robgleasure.com.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
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.
Introduction to Database Programming with Python Gary Stewart
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Fundamentals of DBMS Notes-1.
Database, SQL and ADO.NET
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Database Programming in Java
 2012 Pearson Education, Inc. All rights reserved.
JDBC.
Databases Intro (from Deitel)
Chapter 4 Relational Databases
Structured Query Language
Chapter 8 Working with Databases and MySQL
Chapter 22 - SQL, MySQL, DBI and ADO
Presentation transcript:

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational Database Overview 22.4 Structured Query Language Basic SELECT Query WHERE Clause GROUP BY Clause ORDER BY Clause Merging Data from Multiple Tables Inserting a Record Updating a Record DELETE FROM Statement TitleAuthor Query from Books.mdb 22.5 MySQL

 2001 Prentice Hall, Inc. All rights reserved. 2 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.6 Introduction to DBI Perl Database Interface Python DB-API PHP dbx module 22.7 ActiveX Data Objects (ADO) 22.8 Internet and World Wide Web Resources

 2001 Prentice Hall, Inc. All rights reserved Introduction Database –Integrated collection of data Database management system (DBMS) –Provides mechanisms for storing and organizing data –Allows users to access and store data without addressing internal representation of databases Relational databases –Consist of data corresponding to one another –Most popular database systems in use –Uses Structured Query Language (SQL) to create queries –Examples: Oracle, MS SQL Server, MySQL, Informix

 2001 Prentice Hall, Inc. All rights reserved Relational Database Model Relational database model –Logical representation of data allowing users to consider relationships between data –Consists of tables Following figure illustrates Employee table –Might exist in personnel system –Table row called record –Table column called field –Number field is primary key Contains unique data that cannot be duplicated Identifies the record Examples: social security number, employee ID number, etc.

 2001 Prentice Hall, Inc. All rights reserved Relational Database Model Fig. 22.1Relational database structure.

 2001 Prentice Hall, Inc. All rights reserved Relational Database Model SQL statements –Use to obtain table subsets –Complete set of keywords enable programmers to define complex queries Results of query called result sets (or record sets) –Following table shows results of SQL query (Fig. 22.2) Provides geographical location of several departments Fig. 22.2Result set formed by selecting data from a table.

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview Overview SQL using Books.mdb database –Consists of four tables Authors, Publishers, AuthorISBN and Tables Primary key fields in italics Authors table (Figs and 22.4) –Consists of four fields Unique ID number, first name, last name and year of birth –Contains null value for YearBorn field Not primary key, so can contain null values FirstName and LastName can contain null values also

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview Publishers table (Figs and 22.6) –Consists of two fields Unique ID and publisher name

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview Titles table (Figs and 22.8) –Consists of six fields ISBN number, title, edition number, year published, book description and publisher ID number

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview AuthorISBN table (Figs and 22.10) –Consists of two fields ISBN number and author ID number –Links names of authors with respective book titles

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview Microsoft Access diagram (Fig ) –Illustrates relationships between tables in database –Lines represent table relationships –One-to-many relationship Example: line between Publishers and Titles tables Single publisher can have many books in Titles table

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview Fig Table relationships in Books.mdb.

 2001 Prentice Hall, Inc. All rights reserved Relational Database Overview Rule of Entity Integrity –Every record must have value in primary key field –Primary key values must be unique Foreign key field (or constraints) –References primary key field in another table –Specified when creating tables –Maintains Rule of Referential Integrity Every foreign key field value must appear in another table’s primary key field Example: PublisherID field in Titles table –Enables information from multiple tables to be joined for analysis

 2001 Prentice Hall, Inc. All rights reserved Structured Query Language Overview SQL using Books.mdb database Following table lists some SQL keywords

 2001 Prentice Hall, Inc. All rights reserved Basic SELECT Query Extracts information from one or more tables Simplest form –SELECT * FROM TableName Asterisk ( * ) notifies query to select all rows and columns from table TableName specifies a table in database –Example: SELECT * FROM Authors Selecting specific fields –Replace asterisk ( * ) with field names –Example: SELECT AuthorID, LastName FROM Authors

 2001 Prentice Hall, Inc. All rights reserved Basic SELECT Query

 2001 Prentice Hall, Inc. All rights reserved WHERE Clause Optional clause in SELECT query Selects records satisfying selection criteria Basic form –SELECT fieldName1, fieldName2 FROM TableName WHERE criteria –Example: SELECT * FROM Authors WHERE YearBorn > 1960 Result set contains two authors born after 1960

 2001 Prentice Hall, Inc. All rights reserved WHERE Clause Can contain operators –, =, =, <> and LIKE LIKE operator –Performs pattern matching with wildcard characters asterisk ( * ) and question mark ( ? ) –Pattern matching allows SQL to search for particular string or string of characters –Asterisk ( * ) indicates string can have zero or more characters at it’s position Example: SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors WHERE LastName LIKE ‘D*’ –Result set contains two records

 2001 Prentice Hall, Inc. All rights reserved WHERE Clause

 2001 Prentice Hall, Inc. All rights reserved WHERE Clause LIKE operator, cont. –Question mark ( ? ) indicates single character can occupy it’s position Example: SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors WHERE LastName LIKE ‘?i*’ –Result set contains one author

 2001 Prentice Hall, Inc. All rights reserved WHERE Clause Specifying range of characters –Use [startValue-endValue] startValue is first character in range endValue is last character in range

 2001 Prentice Hall, Inc. All rights reserved GROUP BY Clause Groups result set by a particular column Basic form –SELECT fieldName, COUNT ( * ) FROM TableName GROUP BY fieldName –COUNT returns number of records selected by query Example: SELECT AuthorID, COUNT ( * ) AS Count FROM AuthorISBN GROUP BY AuthorID COUNT ( * ) AS Count assigns name to column that contains total count values

 2001 Prentice Hall, Inc. All rights reserved GROUP BY Clause Combining WHERE and GROUP BY clauses –Example: SELECT AuthorID, COUNT ( * ) AS Count FROM AuthorISBN WHERE AuthorID <= 3 GROUP BY AuthorID Result set contains three records

 2001 Prentice Hall, Inc. All rights reserved ORDER BY Clause Sorts result set by given criteria –In ascending order ( ASC ) or descending order ( DESC ) Basic form –SELECT fieldName1, fieldName2 FROM TableName ORDER BY fieldName ASC Can replace ASC with DESC –Example: SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors ORDER BY LastName ASC

 2001 Prentice Hall, Inc. All rights reserved ORDER BY Clause Basic form, cont. –To obtain same list in descending order Example: SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors ORDER BY LastName DESC

 2001 Prentice Hall, Inc. All rights reserved ORDER BY Clause Sorts multiple fields –Example: SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors ORDER BY LastName, FirstName ASC keyword is default sorting order, so is optional First sorts by last name, then by first name

 2001 Prentice Hall, Inc. All rights reserved ORDER BY Clause Combining WHERE and ORDER BY clauses –Example: SELECT ISBN, Title, EditionNumber, YearPublished, PublisherID FROM Titles WHERE Title LIKE ‘*How to Program’ ORDER BY Title ASC Selects titles ending in “How to Program” Sorts them in ascending order

 2001 Prentice Hall, Inc. All rights reserved ORDER BY Clause

 2001 Prentice Hall, Inc. All rights reserved Merging Data from Multiple Tables May need to combine data from different tables Merging data = joining tables –Combines records from different tables –Extracts records common to tables Basic form –SELECT fieldName1, fieldName2 FROM TableName1, TableName2 WHERE TableName1.fieldName = TableName2.fieldName –WHERE clause specifies fields to be compared from each table Normally represent primary key field in one table and foreign key field in other table

 2001 Prentice Hall, Inc. All rights reserved Merging Data from Multiple Tables Basic Form, cont. –Example: SELECT FirstName, LastName, ISBN FROM Authors, AuthorISBN WHERE Authors. AuthorID = AuthorISBN. AuthorID ORDER BY LastName, FirstName Merges FirstName and LastName fields from Authors table with ISBN field from AuthorISBN table Result set contains list of authors and ISBN numbers of books each author wrote Sorts in ascending order by LastName and FirstName

 2001 Prentice Hall, Inc. All rights reserved Merging Data from Multiple Tables

 2001 Prentice Hall, Inc. All rights reserved Merging Data from Multiple Tables Fully qualified name –TableName.fieldName specifies fields to be compared –Fields with same name in tables require “TableName.” syntax –Cross database queries require database name before TableName

 2001 Prentice Hall, Inc. All rights reserved Inserting a Record Use INSERT INTO operation Basic Form –INSERT INTO TableName (fieldName1, fieldName2) VALUES (value1, value2) TableName specifies table that receives new records Comma-separated list of field names specify the fields of TableName –Specifies columns that receive new records VALUES specifies data to be inserted into table –Example: INSERT INTO Authors ( FirstName, LastName, YearBorn ) VALUES ( ‘Sue’, ‘Smith’, 1960 ) Inserts three values into three columns of Authors table

 2001 Prentice Hall, Inc. All rights reserved Inserting a Record

 2001 Prentice Hall, Inc. All rights reserved Updating a Record Modifies data in tables (updates records) Basic form: –UPDATE TableName SET fieldName = value1 WHERE criteria SET assigns values to certain fields –Example: UPDATE Authors SET YearBorn = ‘1969’ WHERE LastName = ‘Deitel’ AND FirstName = ‘Paul’ AND states all components of selection criteria must be satisfied Can replace WHERE clause with WHERE AuthorID = 2

 2001 Prentice Hall, Inc. All rights reserved Updating a Record

 2001 Prentice Hall, Inc. All rights reserved DELETE FROM Statement Removes data from tables Basic form –DELETE FROM TableName WHERE criteria –Example: DELETE FROM Authors WHERE LastName = ‘Smith’ AND FirstName = ‘Sue’ Can replace WHERE clause with WHERE AuthorID = 5

 2001 Prentice Hall, Inc. All rights reserved TitleAuthor Query from Books.mdb Books.mdb contains one predefined query –TitleAuthor Produces table containing book title, ISBN number, last name, book’s year published and publisher’s name Figure shows query, Figure shows result set

 2001 Prentice Hall, Inc. All rights reserved. Outline 42 TitleAuthor 1 SELECT Titles.Title, Titles.ISBN, Authors.FirstName, 2 Authors.LastName, Titles.YearPublished, 3 Publishers.PublisherName 4 FROM Publishers, Titles, Authors, AuthorISBN 5 WHERE Publishers.PublisherID = Titles.PublisherID 6 AND Authors.AuthorID = AuthorISBN.AuthorID 7 AND Titles.ISBN = AuthorISBN.ISBN 8 ORDER BY Titles.Title Fig TitleAuthor query from the Books.mdb database. Fully qualified names for clarity Lines 1-3 indicate fields that query selects Joins tables provided that PublisherID in Publishers table matches PublisherID in Titles table Combines two preceding result sets on condition that ISBN field in Titles table matches ISBN field in AuthorISBN table

 2001 Prentice Hall, Inc. All rights reserved TitleAuthor Query from Books.mdb

 2001 Prentice Hall, Inc. All rights reserved MySQL Pronounced “My Ess Que Ell” Robust and scalable RDBMS Multiuser, multithreaded server –Performs multiple commands concurrently Uses SQL to interact with data Supports various programming languages –C, C++, Java, Python, Perl, PHP, etc Supports various operating systems –Windows, Linux and Unix Access multiple databases with single query

 2001 Prentice Hall, Inc. All rights reserved Introduction to DBI Databases part of distributed applications –Divides work across multiple computers Retrieves result set and displays results Driver –Helps programs access databases –Each can have different syntax –Each database requires its own driver Interface –Provides uniform access to all database systems Database interface –Programmatic library for accessing relational database

 2001 Prentice Hall, Inc. All rights reserved Perl Database Interface Perl DBI –Enables users to access relational databases from Perl programs –Database independent –Most widely used interface in Perl –Uses handles (Fig ) Object-oriented interfaces Driver handles, database handles, statement handles

 2001 Prentice Hall, Inc. All rights reserved Python DB-API Python DB-API –Database application programming interface –Portable across several databases –Consists of Connection and Cursor data objects Connection data object (Fig ) –Accesses database through four methods Cursor data object (Fig ) –Manipulates and retrieves data

 2001 Prentice Hall, Inc. All rights reserved Python DB-API

 2001 Prentice Hall, Inc. All rights reserved PHP dbx module dbx module –Consists of seven functions that interface to database modules –Supports MySQL, PostgreSQL and ODBC databases

 2001 Prentice Hall, Inc. All rights reserved ActiveX Data Objects (ADO) Microsoft Universal Data Access (UDA) –Supports high-performance data access to relational, non-relational and mainframe data sources –Consists of three primary components OLE DB –Core of UDA architecture –Provides low-level access to data source Open Database Connectivity (ODBC) –C programming-language library –Uses SQL to access data ActiveX Data Objects –Simple object modules –Provide uniform access to data source through OLE DB

 2001 Prentice Hall, Inc. All rights reserved ActiveX Data Objects (ADO) Fig Microsoft’s UDA architecture. Fig Portion of the ADO object model.

 2001 Prentice Hall, Inc. All rights reserved ActiveX Data Objects (ADO) ADO object model –Provides objects and collections Collections –Containers that hold one or more objects of specific type –Following table lists some ADO objects and collections

 2001 Prentice Hall, Inc. All rights reserved ActiveX Data Objects (ADO)