1 chapter 10 – database management 10.1 an introduction to databases 10.2 editing and designing...

68
1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Upload: annis-taylor

Post on 14-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

1

Chapter 10 – Database Management

10.1 An Introduction to Databases

10.2 Editing and Designing Databases

Page 2: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

2

10.1 An Introduction to Databases

• Accessing a Database Table

• Binding to Additional Tables

• Querying a Table with LINQ

• Primary and Foreign Keys

• The Join of Two Tables

Page 3: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

3

Sample Table – Cities Table

Page 4: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

4

Sample Table – Countries Table

Page 5: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

5

Database Terminology

• A table is a rectangular array of data.

• Each column of the table, called a field, contains the same type of information.

• Each row, called a record, contains all the information about one entry in the table.

Page 6: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

6

Database Management Software (DBMS)

• Used to create databases

• Databases contain one or more related tables

• Examples of DBMS are Access, Oracle, and SQL Server.

• The databases used in this chapter are found in the folder Programs\Ch10\Databases. They were created with Access and have the extension accdb.

Page 7: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Megacities.accdb

• Contains the two tables Cities and Countries shown earlier.

• This database will be used extensively in the examples for this chapter.

• Several steps are required to bind to a table of the database. (See the next sixteen slides.)

7

Page 8: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Binding to the Cities Table

Add a BindingSource control to the form. (The control is in the Data and All Windows Forms group of the Toolbox. It appears in the form’s component tray with the name BindingSource1.)

8

Page 9: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

DataSource Property of BindingSource1

9

click here

Page 10: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Choose Data Source Type

10

click on Next button

select

Page 11: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Choose Database Model

11

click on Next button

select

Page 12: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Choose Data Connection

12

click on New Connection button

Page 13: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Add Connection Dialog Box

13

click on Change button

Page 14: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Change Data Source Box

14

select

click on OK button

Page 15: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

15

Add Connection Dialog Box

click on Browse button

Page 16: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Select Database File

16

double-click on Megacities.accdb

Page 17: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Add Connection Dialog Box

17

click on OK button

Page 18: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Choose Data Connection

18

click on Next button

Page 19: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

19

click on Yes button

Page 20: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Save to File

20

check this box

click on Next button

Page 21: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Choose Database Objects

21

check on Tables box

click on Finish button

Page 22: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Changes in Properties Window and Form

22

Page 23: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

After Clicking on DataMember Down-Arrow

23

click on Cities

Page 24: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

VB Generated Items

24

new iconnew code

Page 25: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Binding Complete

• We are now bound to the Cities table via the MegacitiesDataSet and the CitiesTableAdapter.

• The next four slides show how to bind an additional table.

25

Page 26: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Connect an Additional Table

Add another BindingSource control to the form.

26

Page 27: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Set DataSource Property

27

click on MegacitiesDataSet

Page 28: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Set DataMember Property

28

click on Countries

Page 29: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

VB Generated Items

29

additional code shows in Load event procedureMe.CountriesTableAdapter.Fill(Me.MegacitiesDataSet.Countries)

new icon

Page 30: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 1: Form

30

txtTotalPop

Page 31: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 1: CodePrivate Sub btnDisplay_Click(...) Handles _ btnDisplay.Click Dim query1 = From city In MegacitiesDataSet.Cities

Where city.country = "India"

Order By city.pop2010 Descending

Select city.name

lstOutput.DataSource = query1.ToList

lstOutput.SelectedItem = Nothing

31

Page 32: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 1: Code (continued)

Dim query2 = From city In _ MegacitiesDataSet.Cities

Where city.country = "India"

Select city.pop2010

txtTotalPop.Text = CStr(query2.Sum)

End Sub

32

Page 33: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 1: Output

33

Page 34: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 2: Form

34

txtName

dgvOutput

Page 35: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 2: CodeDim query = From country In _ MegacitiesDataSet.Countries

Where country.name = txtName.Text

Select country.name, country.pop2010, country.monetaryUnit

If query.Count = 1 Then

dgvOutput.DataSource = query.ToList

dgvOutput.CurrentCell = Nothing

Else

MessageBox.Show("Country not found")

End If35

Page 36: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 2: Output

36

Page 37: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 3: Form

37

dgvOutput

Page 38: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 3: CodeDim query = From city In _ MegacitiesDataSet.Cities

Let popIncrease = city.pop2015 - city.pop2010

Let formattedIncr = FormatNumber(popIncrease, 1)

Where popIncrease > 1

Order By popIncrease Descending

Select city.name, formattedIncr

38

Page 39: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 3: Code (continued)

dgvOutput.DataSource = query.ToList

dgvOutput.CurrentCell = Nothing

dgvOutput.Columns("name").HeaderText = "City"

dgvOutput.Columns("formattedIncr").HeaderText = "Population Increase"

39

Page 40: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 3: Output

40

Page 41: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

41

Primary Keys

• A primary key is used to uniquely identify each record.

• Databases of student enrollments in a college usually use a field of student ID numbers as the primary key.

• Why wouldn't names be a good choice as a primary key?

Page 42: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

42

Primary Key Fields• Specified when database is created.• Every record must have an entry in the

primary-key field. • Two records cannot have the same entry in

the primary-key field.• This pair of requirements is called the Rule

of Entity Integrity.

Page 43: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

43

Two or More Tables• When a database contains two or more tables,

the tables are usually related. • For instance, the two tables Cities and

Countries are related by their country and name fields.

• Notice that every entry in Cities.country appears uniquely in Countries.name and Countries.name is a primary key.

• We say that Cities.country is a foreign key of Countries.name.

Page 44: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

44

Foreign Keys

• Foreign keys can be specified when a table is first created. Visual Basic will insist on the Rule of Referential Integrity.

• This Rule says that each value in the foreign key must also appear in the primary key of the other table.

Page 45: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

45

Join• A foreign key allows Visual Basic to link (or

join) two tables from a relational database

• When the two tables Cities and Countries from Megacities.accdb are joined based on the foreign key Cities.country, the result is the table in the next slide.

• The record for each city is expanded to show its country’s population and its monetary unit.

Page 46: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

46

A Join of Two Tables

Page 47: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

47

Beginning of Query to Join the Two Tables from Megacities

Dim query = From city In _ MegacitiesDataSet.Cities

Join country In MegacitiesDataSet.Countries

On city.country Equals country.name

Page 48: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 6: Form

48

Page 49: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 6: Code for Load Event

Me.CountriesTableAdapter.Fill(Me.MegacitiesDataSet.Countries)

Me.CitiesTableAdapter.Fill(Me.MegacitiesDataSet.Cities)

Dim query = From country In _ MegacitiesDataSet.Countries

Order By country.monetaryUnit Ascending

Select country.monetaryUnit

Distinct

lstCurrencies.DataSource = query.ToList

49

Page 50: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 6: Later CodeDim query = From city In _ MegacitiesDataSet.Cities

Join country In MegacitiesDataSet.Countries

On city.country Equals country.name

Where country.monetaryUnit = lstCurrencies.Text

Order By city.name Ascending

Select city.name

For Each city As String In query

lstCities.Items.Add(city)

Next50

Page 51: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Example 6: Sample Output

51

Page 52: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

52

10.2 Editing and Designing Databases

• A Program to Edit the Cities Table

• Designing the Form for the Table-Editing Program

• Writing the Table-Editing Program

• Principles of Database Design

Page 53: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Editing Program: Form

53

navigation toolbar

Page 54: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Navigation Toolbar

54

Page 55: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Editing Program: Output

55

Page 56: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Building the Form

• Start a new program and bind the Cities table.

• Add a BindingNavigator control (found in Data group of Toolbox) to the form. Note: BindingNavigator1 appears in component tray and a navigation toolbar appears at the top of the form.

56

Page 57: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Navigation Bar Added

57

button to be changed

Page 58: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Change Last Button

58

click here

Page 59: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Change Last Button (continued)

59

click on Button

Page 60: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Change Last Button (continued)

60

new button

• New button has name ToolStripButton 1• Use the Properties window for the button to

change the Name, Text, and Image settings to btnUpdate, Update, and Disk.bmp.

Page 61: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Change Last Button (continued)

61

Disk.bmp used for picture on button

Page 62: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Bind to Cities Table

62

click here

Page 63: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Bind Text Boxes to Cities Table Fields

63

drag these four items onto form

Page 64: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Text Boxes and Labels Generated by VB

64

Page 65: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Complete Design of Form

65

Page 66: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Index Numbers

• Each record of the table Cities has an index number ranging from 0 to 9.

• The value of BindingSource1.Position is the index number of the record currently displayed in the form.

66

Page 67: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Index Numbers (continued)

• The statement BindingSource1.Position = n

displays the record having index number n.

• The value of BindingSource1.Find("name", cityName)

is the index number of the specified city.

67

Page 68: 1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases

Principles of Database Design

• Data should often be stored in their smallest parts.

• Avoid redundancy.

• Avoid tables with intentionally blank entries.

• Strive for table cohesion.

• Avoid fields whose values can be calculated from existing fields.

68