Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2000 Deitel & Associates, Inc. All rights reserved. Chapter 25 – Database: SQL, ADO and RDS Outline 25.1Introduction 25.2Relational Database Model 25.3Relational.

Similar presentations


Presentation on theme: " 2000 Deitel & Associates, Inc. All rights reserved. Chapter 25 – Database: SQL, ADO and RDS Outline 25.1Introduction 25.2Relational Database Model 25.3Relational."— Presentation transcript:

1  2000 Deitel & Associates, Inc. All rights reserved. Chapter 25 – Database: SQL, ADO and RDS Outline 25.1Introduction 25.2Relational Database Model 25.3Relational Database Overview: Books.mdb 25.4Structured Query Language 25.4.1Basic SELECT Query 25.4.2WHERE Clause 25.4.3ORDER BY Clause 25.4.4Using INNER JOIN to Merge Data from Multiple Tables 25.4.5 TitleAuthor Query from Books.mdb 25.4.6Inserting a Record 25.4.7Updating a Record 25.5Registering Books.mdb as an ODBC Source 25.6ActiveX Data Objects (ADO) 25.7Remote Data Services (RDS

2  2000 Deitel & Associates, Inc. All rights reserved. 25.1 Introduction Database –Integrated collection of data Database Management System (DBMS) –Has mechanisms for storing and organizing data –Sophisticated queries and manipulations of data Relational Databases –Most popular database system in use today Structured Query Language (SQL – pronounced “sequel”) –Almost universally used with relational databases –Makes queries (request information) and manipulate data

3  2000 Deitel & Associates, Inc. All rights reserved. 25.1 Introduction (II) In this chapter –Presentation of basic SQL queries using a database –Introduction of two Microsoft technologies ActiveX Data Objects (ADO) Remote Data Services (RDS) These two technologies –Enable client browser to Retrieve information from a database on a Web server Process that information on the client computer Return modifications of the data to the Web server so that the data can be updated in the database Manipulation of data in the client –Increases performance of Web-based database applications –Reduces overall load on the server computer

4  2000 Deitel & Associates, Inc. All rights reserved. 25.2 Relational Database Model Relational Database Model –Logical representation of the data –Consider relationships between data without worrying about physical implementation Relational Database –Composed of tables –Any row of the table is called a record –The first field is used as the primary key for referencing Records are normally unique (by primary key) Primary key can be composed of more than one field or column –Each column represents a different field (or attribute)

5  2000 Deitel & Associates, Inc. All rights reserved. 25.2 Relational Database Model (II) Relational Database Structure NumberNameDepartmentSalaryLocation 23603 24568 35761 34589 47132 78321 JONES, A. KERWIN, R. LARSON, P. MYERS, B. NEUMANN, C. STEPHENS, T. 413 642 611 413 611 1100 2000 1800 1400 9000 8000 NEW JERSEY LOS ANGELES ORLANDO NEW JERSEY ORLANDO Table: Employee A record A columnPrimary Key

6  2000 Deitel & Associates, Inc. All rights reserved. 25.2 Relational Database Model (III) Different users interested in different parts of the table –SQL statements specify the data to select from the table SQL –Provides complete set of keywords –Smaller databases can also be combined to form larger ones –Results of a query called result sets (or record sets) Example: Result set to show where departments are located Department Location 413 642 611 NEW JERSEY LOS ANGELES ORLANDO

7  2000 Deitel & Associates, Inc. All rights reserved. 25.3 Relational Database Overview: Books.mdb Overview of Database Books.mdb –This database will be used throughout the chapter to introduce Various database concepts Use of SQL to obtain useful information from the database and to manipulate the database –Books.mdb consists of four tables: Authors, Publishers, AuthorISBN and Titles Primary key for each table is shown in italics

8  2000 Deitel & Associates, Inc. All rights reserved. 25.3 Relational Database Overview: Books.mdb (II) Authors table description –AuthorID Integer representing author’s ID number in database (primary key) –FirstName String representing author’s first name –LastName String representing author’s last name –YearBorn String representing author’s year of birth Authors table data

9  2000 Deitel & Associates, Inc. All rights reserved. 25.3 Relational Database Overview: Books.mdb (III) Publishers table description –PublisherID Integer representing publisher’s ID number in database (primary key) –PublisherName String representing abbreviated name for publisher Publishers table data

10  2000 Deitel & Associates, Inc. All rights reserved. AuthorISBN table description –ISBN String representing ISBN number for a book (linking table - no primary key) –AuthorID Integer representing author’s ID number 25.3 Relational Database Overview: Books.mdb (IV)

11  2000 Deitel & Associates, Inc. All rights reserved. 25.3 Relational Database Overview: Books.mdb (V) AuthorISBN table data

12  2000 Deitel & Associates, Inc. All rights reserved. 25.3 Relational Database Overview: Books.mdb (VI) Titles table description –ISBN String representing ISBN number of the book (primary key) –Title String representing title of the book –EditionNumber String representing edition of the book –YearPublished String representing year in which book was published –Description String representing description of the book –PublisherID Integer representing publisher’s ID number (corresponds to ID number in Publishers table)

13  2000 Deitel & Associates, Inc. All rights reserved. Titles table data 25.3 Relational Database Overview: Books.mdb (VII)

14  2000 Deitel & Associates, Inc. All rights reserved. Table relationships in books.mdb Lines between tables represent relationships –Example: Line between Publishers and Titles One-to-many relationship Every publisher can have an infinite number of books –PublisherID is the foreign key in the Titles table Must correspond to primary key in Publishers table Maintains Rule of Referential Integrity AuthorISBN table- –No primary key –Linking table between Titles and Authors

15  2000 Deitel & Associates, Inc. All rights reserved. 25.4 Structured Query Language Structured Query Language (SQL) keywords used to –Query a database –Insert records into a database –Update existing records in a database

16  2000 Deitel & Associates, Inc. All rights reserved. 25.4.1 Basic SELECT Query SELECT * FROM TableName –TableName specifies table in database where data is located –* selects all rows and fields from TableName SELECT * FROM Authors –Selects the entire contents of the Authors table SELECT AuthorID, LastName FROM Authors –To select fields from table, replace * with comma-separated list of field names to select –Returns the following:

17  2000 Deitel & Associates, Inc. All rights reserved. 25.4.1 Basic SELECT Query (II) Order of fields –Specifying field names ensures that the fields are returned in the same order, even if in different order in the table Allows result set to be processed more efficiently by the application –SQL statement using * does not ensure that fields will be returned in the expected order Do not use to specify field names to select from one or several table Field name with spaces –If contains spaces, must be enclosed in square brackets ( [] ) Error if you forget to enclose in brackets Therefore, try to avoid field names with spaces

18  2000 Deitel & Associates, Inc. All rights reserved. 25.4.2 WHERE Clause WHERE clause –Most cases, only necessary to locate records that satisfy certain selection criteria –SQL uses the WHERE clause to specify the selection criteria –Can contain operators, =, =, <> and LIKE Simplest form: SELECT fieldName1, fieldName2, … FROM TableName WHERE Criteria SQL is case-sensitive on some systems –Check your database system document to determine the syntax to be used for keywords All uppercase, all lowercase, combination?

19  2000 Deitel & Associates, Inc. All rights reserved. 25.4.2 WHERE Clause (II) Example SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors WHERE YearBorn > 1960 Returns

20  2000 Deitel & Associates, Inc. All rights reserved. 25.4.2 WHERE Clause (III) LIKE Operator –Used for pattern matching with wildcard characters asterisk ( * ) question mark ( ? ) –Allows SQL to search for similar strings that “match a pattern” –Asterisk Indicates any number of characters in a row at location in pattern Surrounded by single-quote characters –Question Mark Indicates a single character at that position in the pattern Surrounded by single-quote characters –Not supported by all database systems

21  2000 Deitel & Associates, Inc. All rights reserved. 25.4.2 WHERE Clause (IV) LIKE Operator Example SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors WHERE Lastname LIKE ‘d*’ –Returns records of all authors whose last name’s begin with the letter d

22  2000 Deitel & Associates, Inc. All rights reserved. 25.4.2 WHERE Clause (V) LIKE Operator Example II SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors WHERE Lastname LIKE ‘?i*’ –Returns records of all authors whose last name second letter is the letter i

23  2000 Deitel & Associates, Inc. All rights reserved. 25.4.2 WHERE Clause (VI) LIKE Operator Example III –Query can be specialized to allow any character in a range of characters in one position in the string [ startValue - endValue ] SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors WHERE Lastname LIKE ‘?[a-i]*’ –Returns records of all authors in table because all fit range

24  2000 Deitel & Associates, Inc. All rights reserved. 25.4.3 ORDER BY Clause ORDER BY Clause –Sorts results of query into ascending or descending order SELECT fieldName1, fieldName2, … FROM TableName ORDER BY fieldName ASC SELECT fieldName1, fieldName2, … FROM TableName ORDER BY fieldName DESC Example SELECT AuthorID, FirstName, LastName, YearBorn FROM Authors ORDER BY LastName ASC –Returns authors sorted by last name in ascending order

25  2000 Deitel & Associates, Inc. All rights reserved. 25.4.3 ORDER BY Clause (II) The WHERE and ORDER BY clauses can be combined –Example: SELECT ISBN, Title, EditionNumber, YearPublished, PublisherID FROM Titles WHERE Title LIKE ‘*How to Program’ ORDER BY Title ASC –Returns:

26  2000 Deitel & Associates, Inc. All rights reserved. 25.4.3 ORDER BY Clause (III) ORDER BY can be applied to multiple fields ORDER BY fieldName1 SortingOrder, fieldName2 SortingOrder, … When we construct a query –Create one long string containing query –Multiple lines and indentation only used for readability when query statement is displayed in text

27  2000 Deitel & Associates, Inc. All rights reserved. 25.4.4 Using INNER JOIN to Merge Data from Multiple Tables INNER JOIN Clause –Merges data from multiple tables into single view –Referred to as joining the tables –Placed in the FROM clause of an SQL query Simplest form SELECT fieldName1, fieldName2, … FROM Table1 INNER JOIN Table2 ON Table1.field = Table2.field

28  2000 Deitel & Associates, Inc. All rights reserved. 25.4.4 Using INNER JOIN to Merge Data from Multiple Tables (II) ON part of INNER JOIN –Specifies fields from each table to be compared to determine records to be selected –“TableName.” syntax required if fields have same name in both tables –Example: SELECT FirstName, LastName, ISBN FROM Authors INNER JOIN AuthorISBN ON Authors.AuthorID = AuthorISBN.AuthorID ORDER BY LastName, FirstName

29  2000 Deitel & Associates, Inc. All rights reserved. 25.4.4 Using INNER JOIN to Merge Data from Multiple Tables (III)

30  2000 Deitel & Associates, Inc. All rights reserved. 24.4.5 TitleAuthor Query from Books.mdb Predefined query: TitleAuthor –Table containing book title, ISBN number, author’s first and last names, year published, and publisher’s name –Note the nested INNER JOIN structures –Note: spacing and indenting only for readability SELECT Titles.Title, Titles.ISBN, Authors.FirstName, Authors.Lastname, Titles.YearPublished, Publishers.PublisherName FROM ( Publishers INNER JOIN Titles ON Publishers.PublisherID = Titles.PublisherID ) INNER JOIN ( Authors INNER JOIN AuthorsISBN ON Authors.AuthorID = AuthorISBN.AuthorID ) ON Titles.ISBN = AuthorsISBN.ISBN ORDER BY Titles.Title

31  2000 Deitel & Associates, Inc. All rights reserved. 24.4.5 TitleAuthor Query from Books.mdb (II) Portions of TitleAuthor query output

32  2000 Deitel & Associates, Inc. All rights reserved. 25.4.6 Inserting a Record INSERT INTO operation –Inserts data into the table (e.g; adds a record) –Simplest form: –The single quote character should be used as a delimiter for strings to be inserted into the database INSERT INTO TableName ( fieldName1, fieldName2, …, fieldNameN ) Values ( value1, value 2, …, valueN ) Values to be inserted into fields – in order of fields listed before Table into which record will be inserted List of field names into which to insert values (not required if inserting complete record) KEYWORDS

33  2000 Deitel & Associates, Inc. All rights reserved. 25.4.6 Inserting a Record Sample insert operation –Note: AuthorID field not specified because it has been set up in Microsoft Access, our database software, as an auto-numbered field INSERT INTO Authors ( FirstName, LastName, YearBorn ) Values ( ‘Sue’, ‘Smith’, 1960 ) Result

34  2000 Deitel & Associates, Inc. All rights reserved. 25.4.7 Updating a Record Modify a record with the UPDATE operation UPDATE TableName SET fieldName1 = value1, fieldName2 = value2, …, fieldNameN = valueN WHERE criteria Example: UPDATE Authors SET YearBorn = 1969 WHERE LastName = ‘Deitel’, FirstName = ‘Paul’ –Changes YearBorn value for Paul Deitel in Authors table to 1969

35  2000 Deitel & Associates, Inc. All rights reserved. 25.5 Registering Books.mdb as an ODBC Data Source Open DataBase Connectivity (ODBC) Application Programming Interface (API) –Developed by Microsoft to allow Windows applications to communicate in a uniform manner with relational databases To execute an SQL query –Program must be able to access the database Database must be given a System Data Source Name (DSN) on the server Database must be registered as an ODBC source ODBC Driver written by vendors –Uses ODBC API to provide uniform access to the database For specific instructions –See section 25.5 in your textbook

36  2000 Deitel & Associates, Inc. All rights reserved. 25.6 ActiveX Data Objects (ADO) Microsoft’s Universal Data Access (UDA) –Architecture designed for high-performance data access to Relational data sources Non-relational data sources Mainframe/legacy data sources UDA Architecture: Application or Browser ADO ODB C OLE DB Relational data sources Non-relational data sources Mainframe/legacy data

37  2000 Deitel & Associates, Inc. All rights reserved. 25.6 ActiveX Data Objects (ADO) UDA consists of three primary components –OLE DB Core of UDA architecture Provides low-level access to any data source –ODBC C programming language library Uses SQL to access data –ActiveX Data Controls Simple object model Provides uniform access to any data source by interacting with OLE DB

38  2000 Deitel & Associates, Inc. All rights reserved. 25.6 ActiveX Data Objects (ADO) ADO object model –Provides objects and collections

39  2000 Deitel & Associates, Inc. All rights reserved. 25.7 Remote Data Services (RDS) RDS –Emerging Microsoft technology for client-side database manipulation over the internet –Recordsets retrieved on the server using ADO and sent to client browser Called disconnected recordset –Client can execute queries against the recordset –Similar to Tabular Data Control RDS provides mechanism for sending updated records to Web server Tabular Data Control does not RDS is still developing –Future releases will work with Personal Web Server (PWS) –Server-side setup complex and developing rapidly

40  2000 Deitel & Associates, Inc. All rights reserved. 25.7 Remote Data Services (RDS) (II) RDS Architecture RDS still developing –Future releases will work with PWS –Server-side setup complex and developing rapidly ADO ODB C OLE DB Relational data sources Non-relational data sources Mainframe/legacy data PWS/IIS Server Client Browser RDS

41  2000 Deitel & Associates, Inc. All rights reserved. 25.7 Remote Data Services (RDS) (III) RDS implemented as client-side ActiveX control –Named RDS.Datacontrol Properties Table Methods Table

42  2000 Deitel & Associates, Inc. All rights reserved. 25.7 Remote Data Services (RDS) (IV) RDS.Datacontrol –Transfers data asynchronously Transfer occurs in parallel with other executing code ReadyState property stores control’s current state –Uses constants to declare state s used in the program –state can have one of three values 0 – no user activity 1 – download state 2 – upload state

43  2000 Deitel & Associates, Inc. All rights reserved. Outline 1.1 Define OBJECT 2.1 Open and define tag 2.2 Set predefined ReadyState constants 2.3 Set Initial StatusText.Value 3.1 Define deitelDC_OnReadySt ateChange() Sub procedure 3.2 Open Select structure inside if structure for downloading actions 1 2 3 4 5 6<OBJECT CLASSID = "clsid:BD96C556-65A3-11D0-983A-00C04FC29E33" 7 ID = deitelDC WIDTH = 0 HEIGHT = 0> 8 9 10 11<!-- 12Option Explicit 13 14 15Const adcReadyStateLoaded = 2 16Const adcReadyStateInteractive = 3 17Const adcReadyStateComplete = 4 18 19Dim state 20 21Public Sub window_OnLoad() 22 ’ Set the state machine at the first state 23 StatusText.Value = "Click Find." 24 state = 0 25End Sub 26 27Public Sub deitelDC_OnReadyStateChange() 28 If state = 1 Then 29 Select Case deitelDC.ReadyState 30 ’ Just started downloading, there is no 31 ’ data to look at.

44  2000 Deitel & Associates, Inc. All rights reserved. Outline 3.3 set StatusText.value downloading 3.4 Fill in fields for updating 3.5 Set final StatusText.Value downloading value 4.1 Open select structure inside ElseIf structure for uploading 4.2 Set first StatusText.value output for uploading Case 32 Case adcReadyStateLoaded 33 StatusText.Value = "Downloading..." 34 35 ’ Partially downloaded the data, 36 ’ there is more to come. 37 Case adcReadyStateInteractive 38 StatusText.Value = "Still downloading..." 39 40 ’ Completely downloaded the data, 41 ’ there’s no more coming. 42 Case adcReadyStateComplete 43 44 ’ Fill in the fields for updating 45 ’ Get ISBN field to prove we got the right one 46 FoundISBN.Value = deitelDC.Recordset( "ISBN" ) 47 48 ’ The title we want to modify 49 FoundTitle.Value = deitelDC.Recordset( "Title" ) 50 51 ’ OK for updating. Everything worked. 52 StatusText.Value = "Finished downloading." 53 state = 2 54 End Select 55 56 ElseIf state = 2 Then 57 58 Select Case deitelDC.ReadyState 59 ’ Started uploading, there is no data sent. 60 Case adcReadyStateLoaded 61 ’ OK for updating. Everything worked. 62 StatusText.Value = "Uploading..."

45  2000 Deitel & Associates, Inc. All rights reserved. Outline 4.3 Finish setting StatusText.value ’s for uploading 4.4 Close all structures 5.1 Define OnClick() Sub procedure 5.2 Validate input values 63 64 ’ Partially uploaded the data, there is more to send. 65 Case adcReadyStateInteractive 66 ’ OK for updating. Everything worked. 67 StatusText.value = "Still Uploading..." 68 69 ’ Completely downloaded the data, 70 ’ there is no more coming. 71 ’ Goto readystate = complete if it works 72 Case adcReadyStateComplete 73 ’ OK for updating. Everything worked. 74 StatusText.value = "Finished updating." 75 state = 0 76 End Select 77 End If 78End Sub 79 80Public Sub Find_OnClick() 81 ’ Validate the input values. Never assume valid 82 ’ data is in the text boxes. 83 84 ’ Server.Value will be used to designate the 85 ’ server with the database 86 If Server.Value = "" Then 87 Call Msgbox( "Please specify a web server. " & _ 88 "Suggest: http://xxx.xxx.xxx.xxx" ) 89 90 ’ ISBN.Value is the record we want to search for 91 ElseIf ISBN.value = "" Then 92 Call MsgBox( "Please specify an ISBN to examine. " & _ 93 "Suggest: 0-13-226119-7" )

46  2000 Deitel & Associates, Inc. All rights reserved. Outline 5.3 Begin data download 5.4 Set values 5.5 Tell server to begin sending data - call deitelDC.Refresh() Sub procedure 5.6 Close structures 6.1 Define Update_ OnClick() Sub procedure 6.2 Fetch value from text box, setup to go back to server 94 95 ’ All data is probably valid so begin the data download. 96 Else 97 ’ Request the data from. 98 deitelDC.Server = Server.Value 99 100 ’ Set the SQL query. 101 deitelDC.SQL = "SELECT ISBN, " & _ 102 "Title FROM Titles WHERE ISBN = ’" & ISBN.Value & "’" 103 104 ’ Set the DSN to fetch the data. 105 deitelDC.Connect = "DSN=Books;" 106 107 ’ Tell the server to begin sending the data to us. 108 Call deitelDC.Refresh() 109 state = 1 110 End If 111End Sub 112 113Public Sub Update_OnClick() 114 ’ If everything worked above, we can change 115 ’ the record in the database 116 If state = 2 Then 117 ’ We are only updating this field in the database. 118 ’ so we fetch the value from the text box and set it to go 119 ’ back to the server for update 120 deitelDC.Recordset( "Title" ) = FoundTitle.Value 121

47  2000 Deitel & Associates, Inc. All rights reserved. Outline 6.3 Save changes and refresh 6.4 Close all structures and tag 7.1 Insert and define META tag 7.2 Insert and define searching INPUT s 7.3 Insert button INPUT: “ Find ” 123 Call deitelDC.SubmitChanges() 124 125 ’ Refresh after submit operation 126 Call deitelDC.Refresh() 127 End If 128End Sub 129--> 130 131 132 133 134 135 136 137RDS Example--Correct titles in a remote database 138Record to Find 139 140 141 Server: 142 <INPUT ID= server NAME= server VALUE= "http://xxx.xxx.xxx.xxx" 143 TYPE = "text" SIZE = 60> 144 145 146 ISBN: 147 <INPUT ID = ISBN NAME = ISBN VALUE = "0-13-226119-7" 148 TYPE = "text"> 149 150 151 152 153Results 122 ’ Save these changes

48  2000 Deitel & Associates, Inc. All rights reserved. Outline 7.4 Insert and define Results INPUT elements 7.5 Insert button INPUT : “ Update Title ” 7.6 Insert StatusText INPUT textbox 154 155 156 Found ISBN( readonly ): 157 158 159 160 161 Found Title: 162 163 164 165<INPUT TYPE = "button" ID = "Update" NAME = "Update" 166 VALUE = "Update Title"> 167 168Status:<INPUT TYPE = "text" ID = "StatusText" NAME = "StatusText" 169 SIZE = 30 READONLY VALUE = "Click Find."> 170 171

49  2000 Deitel & Associates, Inc. All rights reserved. 25.7 Remote Data Services (RDS) (V) Document Output 1 The page is loaded

50  2000 Deitel & Associates, Inc. All rights reserved. 25.7 Remote Data Services (RDS) (VI) Document Output 2 The use has clicked the Find button which results in the Found ISBN and Found Title fields being populated

51  2000 Deitel & Associates, Inc. All rights reserved. 25.7 Remote Data Services (RDS) (VII) Document Output 3 User has edited the Found Title field

52  2000 Deitel & Associates, Inc. All rights reserved. 25.7 Remote Data Services (RDS) (VIII) Document Output 4 User has clicked Update Title

53  2000 Deitel & Associates, Inc. All rights reserved. 25.7 Remote Data Services (RDS) (IX) Document Output 5 User has clicked Find


Download ppt " 2000 Deitel & Associates, Inc. All rights reserved. Chapter 25 – Database: SQL, ADO and RDS Outline 25.1Introduction 25.2Relational Database Model 25.3Relational."

Similar presentations


Ads by Google