Overheads Chap. 4

Transcription

Overheads Chap. 4
4-1
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Chapter
McGraw-Hill
4
Windows Database Using
Related Tables
This chapter examines techniques for establishing
relationships among tables and extracting data
from the tables.
Objectives (1 of 2)
ˆ Explain
the types of table relationships
master-detail records
ˆ Display a field from a second table using a lookup
ˆ Create a search using a parameterized query and
write a filter to retrieve specific data
ˆ Assign data values to unbound controls
ˆ Display
McGraw-Hill
4-3
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
1
Objectives (2 of 2)
ˆ Retrieve
and display the parent row for a selected
child row
ˆ Retrieve and display an array of child rows for a
selected parent row
ˆ Create an application that displays from a
many-to-many relationship
ˆ Select the correct locations for handling and
formatting data in a multitier application
McGraw-Hill
4-4
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Data Relationships
ˆ Data
stored in multiple related tables
table Î parent or master
ˆ Second table Î child or detail
ˆ The primary key of one table is included as a field in
a related table to link the two tables
ˆ The key included in the related table is called a
foreign key
ˆ Primary
McGraw-Hill
4-5
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
One-to-Many Relationships
ˆ Most
common type of relationship
record in the parent table relates to one or more
records in the child table
ˆ One
„ Examples
„
„
McGraw-Hill
include:
Customer with multiple order
Student with multiple courses
4-6
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
2
Many-to-Many Relationships
ˆ Example:
One author can
write many books; one
book can have many
authors
ˆ SQL Server and Access
require a junction table
to join two tables
ˆ The junction table’s
primary key consists of
the foreign keys from
both tables
McGraw-Hill
4-7
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
One-to-One Relationships
ˆ Least
common relationship
record in the parent matches one record in the
child table
ˆ Usually the two table could be combined but may be
kept separate for security reasons or the child table
contains short-term information
ˆ One
McGraw-Hill
4-8
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Constraints
ˆA
unique constraint specifies that no duplicate
entries are allowed in a column
ˆ Foreign-key constraints ensure that parent and
child tables remain synchronized when records are
deleted or changed
The
Thereferential
referentialintegrity
integrityconcept
concept
isisenforced
enforcedby
bythe
theDBMS
DBMSusing
using
foreign
key
constraints
to
ensure
foreign key constraints to ensurethat
that
changes
changesand
anddeletions
deletionsininone
onetable
table
are
made
to
all
affected
tables.
are made to all affected tables.
McGraw-Hill
4-9
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
3
Creating a Dataset with More
Than One Table
ˆ The
DataSet object can
hold more than one table
and the relationships
between the tables
ˆ To generate a DataSet
with multiple tables, check
the boxes for both tables
in the Data Source
Configuration Wizard
McGraw-Hill
4-10
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Viewing or Setting a Relationship
ˆA
DataRelation object
describes the
relationship between
the tables
ˆ Use the XML schema
(.xsd) file for the
dataset to create the
relationship
McGraw-Hill
4-11
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Specifying the Parent, Child, and
Foreign Key
ˆ When
adding a relationship to a DataSet’s schema,
it’s important to get the parent/child relationship right
ˆ Determine which table is the “one side” and which is
the “many side” in a one-to-many relationship
ˆ The “one side” should always be the parent and the
“many side” should be the child
McGraw-Hill
4-12
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
4
Queries and Filters
ˆA
parameterized query creates a new DataSet that
holds the selected records
ˆ A filter selects records from an existing DataSet
„ Use
McGraw-Hill
a filter when you already have a complete DataSet
4-13
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Using a Parameterized Query
ˆ SQL
statement that retrieves selected record(s)
based on a value that the user enters at run time
ˆ The value supplied by the user is called a parameter
which is the basis for the term parameterized query
McGraw-Hill
4-14
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Selecting Specific Records
ˆ If
a DataSet is to contain only selected record(s), the
SQL SELECT used by the TableAdapter can be
modified
ˆ Use a WHERE clause in an SQL query to specify
which records to select, called the criteria
SELECT
SELECT Title,
Title, Author,
Author, ISBN
ISBN FROM
FROM Books
Books
WHERE
WHERE Title
Title == 'Romeo
'Romeo and
and Juliette'
Juliette'
SELECT
SELECT Name,
Name, AmountDue
AmountDue FROM
FROM OverdueAccounts
OverdueAccounts
WHERE
WHERE AmountDue
AmountDue >> 100
100
SELECT
SELECT emp_id,
emp_id, lname,
lname, fname
fname FROM
FROM employee
employee
WHERE
WHERE lname
lname == 'Jones'
'Jones'
McGraw-Hill
4-15
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
5
Query Builder
Enter
Enterthe
theparameter
parameterininthe
the
Filter
FilterBox
Boxand
andthe
theWhere
Where
clause
clauseisiscreated.
created.
The
Thegrid
gridfills
fillswith
withthe
the
selected
selectedrecords
records
McGraw-Hill
4-16
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Writing a Filter
ˆ Rules
for creating a filter are the same as for a
WHERE clause of an SQL statement
ˆ Specify the field, a comparison operator (usually the
equals sign), and the value to match
""LastName
LastName == Jones
Jones""
""SalesAmount
SalesAmount == 1000
1000""
""Quantity
Quantity >> 00""
ˆ Use
single quotes to enclose the value to match
when a string contains spaces
""Title
Title
McGraw-Hill
4-17
== 'A'A Great
Great Book
Book'"'"
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Useful Comparison Operators
Operator Meaning
Examples
=
Equal to
"Subject = 'Business'"
"Subject = '" & subjectTextBox.Text &
"'"
>
Greater
than
"Sales > 1000"
"Sales > " & salesTextBox.Text
<
Less than
"Sales < 1000"
"Sales < " & salesTextBox.Text
Like
Pattern
match
"Subject Like ('B%')" (SQL Server)
"Subject Like 'B*'" (Access)
McGraw-Hill
4-18
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
6
Binding a List at Run Time
ˆ List
usually appears empty until a selection is made
the control in code rather than at design time
ˆ When the DataSource is set, the list fills with the data
from the BindingSource
ˆ Bind
' 'Bind
Bind the
the list
list box
box with
with the
the filtered
filtered data.
data.
With
With employeeListBox
employeeListBox
.DataSource
.DataSource == BindingSource1
BindingSource1
.DisplayMember
Name""
.DisplayMember == ""Name
End
End With
With
McGraw-Hill
4-19
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Completed Filter Exercise
ˆ User
selects a job description from the combo box
corresponding job_id is used to filter the data
ˆ All matching records display in the list box
ˆ The
McGraw-Hill
4-20
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Unbound Data Fields
Controls that do not have data bindings are called
unbound controls
ˆ Assign a field of data at run time, in code
ˆ Must reference individual database fields from the
dataset
ˆ
McGraw-Hill
4-21
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
7
Referring to Records and Fields
ˆ Actual
data values are held in DataRow objects
Table object in a DataSet has a DataRows
collection made up of DataRow objects
ˆ The data values are held in the DataRow.Items
collection
ˆ Each
McGraw-Hill
4-22
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Referring to Fields
ˆ Reference
the DataRow Items collection
firstNameString
firstNameString == employeeDataRow.Item(1).ToString
employeeDataRow.Item(1).ToString
oror
firstNameString
firstNameString == employeeDataRow.Item(“fname”).ToString
employeeDataRow.Item(“fname”).ToString
or
or
firstNameString
firstNameString == employeeDataRow!fname.ToString
employeeDataRow!fname.ToString
McGraw-Hill
4-23
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Working With Parent and Child
Records
DataRelation objects relate
parent and child records
ˆ Use the DataRelation to
retrieve the parent of a child
record or find an array of
matching child row(s) for a
given parent
ˆ Remember the rule—the one
side of a one-to-many relation
is the parent; the many side is
the child
ˆ
McGraw-Hill
4-24
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
8
Retrieving a Related Parent Row
From the selected
employee name, the
program retrieves
the correct
employee DataRow
and the matching
Jobs parent
DataRow to display
the job description
McGraw-Hill
4-25
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Find the Parent Row for a Child
Record
ˆ Find
the row in the child table (employee) that
matches the combo box selection
Dim
Dim employeeDataRow
employeeDataRow As
As DataRow
DataRow
employeeIDString
employeeIDString == employeeComboBox.SelectedValue.ToString
employeeComboBox.SelectedValue.ToString
employeeDataRow
employeeDataRow == PubsDataSet.employee.FindByemp_id(
PubsDataSet.employee.FindByemp_id( __
employeeIDString)
employeeIDString)
ˆ Get
the parent row from the parent (jobs) table
Dim
Dim jobDataRow
jobDataRow As
As DataRow
DataRow
jobDataRow
jobDataRow == employeeDataRow("EmployeeToJobsRelation")
employeeDataRow("EmployeeToJobsRelation")
ˆ Retrieve
specific field from the parent data row
jobTitleTextBox.Text
jobTitleTextBox.Text == jobDataRow!job_desc.ToString
jobDataRow!job_desc.ToString
McGraw-Hill
4-26
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Retrieving Related Child Rows
ˆ Retrieve
an array of related
child rows
ˆ The GetChildRows
method is similar to
retrieving a related parent
row
ˆ The difference is that the
GetChildRows method
returns an array of rows
instead of a single row
The
TheComplete
CompleteFind
FindParent
ParentProgram
Program––p.
p.170
170
The
TheComplete
CompleteFind
FindChild
ChildProgram
Program––p.
p.171-172
171-172
McGraw-Hill
4-27
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
9
Many-to-Many Relationships
ˆ In
an M:N relationship, two 1:M relationships must be
set up. The junction table is the child table in each
of the two 1:M relationships
McGraw-Hill
4-28
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Multitier Considerations
ˆ Be
sure to think through the goals of OOP
presentation tier should provide only the user
interface
ˆ All user input and output formatting belongs in the
form
ˆ Data retrieval and any processing should be handled
in other classes
ˆ The user interface should be able to completely
change without modifying the other classes
ˆ Filtering or data retrieval method changes should not
affect the interface
ˆ The
McGraw-Hill
4-29
© 2007 The McGraw-Hill Companies, Inc. All rights reserved.
10