RELATIONAL DATABASES AND XML 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 1
Objectives Compare the following data storage technologies: Describe a relational database. Describe XML. Write a program that accesses data from a relational database. Design relational database tables. Use Structured Query Language (SQL) Access a database from Visual Basic .NET Access a database using Java Database Connectivity (JDBC) Write a program that accesses data from an XML file. Create an XML document. Identify the .NET XML classes 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 2
Tables and Relationships 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 3
One-To-One Relationships Almost always expressed as an attribute of a table. Customer 1 FirstName 1 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 4
One-To-Many Relationships A table has 0, 1, or many related records Usually expressed through a foreign key relationship Primary key is on the “one” side of the relationship. Order1 FK Order2 Customer FK PK Customer3 FK 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 5
Many-to-Many Relationships 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 6
Data Access Classes for Bound Controls SqlConnection Defines connection SqlDataAdapter Retrieves data from the database DataSet Stores the data DataGrid Bound control 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 7
Data Bound Controls 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 8
Updating Data 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 9
Deleting Records 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 10
Validating Data 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 11
SELECT results SELECT FirstName, LastName FROM tblCustomer 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 12
SELECT with Where Clause SELECT FirstName, LastName, City, State FROM tblCustomer WHERE State = 'CO' OR State = 'CA' 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 13
SELECT with BETWEEN SELECT * FROM tblOrder WHERE OrderDate BETWEEN '1/4/2001' an '6/5/2001' 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 14
SELECT With LIKE SELECT * FROM tblCustomer WHERE FirstName LIKE 'J%' Wildcard characters * ? % _ [a-m]% 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 15
SELECT With IN SELECT FirstName, LastName, State FROM tblCustomer WHERE State IN ('CO','WI') 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 16
ORDER BY SELECT * FROM tblOrder ORDER BY OrderDate Desc SELECT TOP 3 * 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 17
JOINS SELECT FirstName, LastName, OrderDate FROM tblOrder INNER JOIN tblCustomer ON tblOrder.CustomerID = tblCustomer.ID 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 18
Data Returned by Join 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 19
Aggregates SELECT tblOrder.CustomerID, FirstName, LastName, COUNT(dbo.tblOrder.CustomerID) AS TotalOrders FROM tblOrder INNER JOIN tblCustomer ON tblOrder.CustomerID = tblCustomer.ID GROUP BY FirstName, LastName, CustomerID 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 20
Results of Previous Query 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 21
HAVING SELECT tblOrder.CustomerID, FirstName, LastName, COUNT(dbo.tblOrder.CustomerID) AS TotalOrders FROM tblOrder INNER JOIN tblCustomer ON tblOrder.CustomerID = tblCustomer.ID GROUP BY FirstName, LastName, CustomerID HAVING (COUNT(tblOrder.CustomerID) > 1 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 22
Aggregate Functions 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 23
UPDATE UPDATE tblItem SET Price = Price * 1.1 UPATE tblInventory WHERE RetailPrice > 100 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 24
DELETE DELETE * FROM tblOrder WHERE OrderDate < '10/31/98' 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 25
INSERT INSERT INTO tblOrder (CustomerID, OrderDate) VALUES (119, '6/16/2001') INSERT INTO tblOrderArchive SELECT * FROM tblOrder WHERE OrderDate < #6/1/2001# 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 26
XML Element with Subelements <Person firstName="John" lastName="Doe" address1="123 Main Street" address2 = "' '" city="Sometown" state="OH" zip="22222" phone="111-242-5512"> <orders> <order id="111" itemid="2923" itemdesc="Super Foo Widget"> </order> </orders> </Person> 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 27
XML/XSL Hierarchy 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 28
XML and XSL Example 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 29
XML Classes in VB.NET xmlNode xmlNodeList xmlDocument 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 30
Summary In this unit the students learned to: Compare the following data storage technologies: Describe a relational database. Describe XML. Write a program that accesses data from a relational database. Design relational database tables. Use Structured Query Language (SQL) Access a database from Visual Basic .NET Access a database using Java Database Connectivity (JDBC) Write a program that accesses data from an XML file. Create an XML document. Identify the .NET XML classes 12/3/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 9 Slide 31