19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

mary key. Databases of student enrollments in a college usually use a field of social security<br />

numbers as the primary key. Names would not be a good choice because there could easily be<br />

two students having the same name.<br />

When a database is created, a field can be specified as a primary key. If so, <strong>Visual</strong> <strong>Basic</strong><br />

will insist that every record have an entry in the primary key field <strong>and</strong> that the same entry<br />

does not appear in two different records. If the user tries to enter a record with no data in the<br />

primary key, the error message “Index or primary key can’t contain a null record.” will be<br />

generated. If the user tries to enter a record with the same primary key data as another record,<br />

the error message “Duplicate value in index, primary key, or relationship. Changes were<br />

unsuccessful.” will be displayed.<br />

When a database contains two or more tables, the tables are usually related. For instance,<br />

the two tables Cities <strong>and</strong> Countries are related by their country field. Let’s refer to these two<br />

fields as Cities.country <strong>and</strong> Countries.country. Notice that every entry in Cities.country appears<br />

uniquely in Countries.country <strong>and</strong> Countries.country is a primary key. We say that Cities.country<br />

is a foreign key of Countries.country. Foreign keys can be specified when a table is first created.<br />

If so, <strong>Visual</strong> <strong>Basic</strong> will insist on the Rule of Referential Integrity, namely, that each<br />

value in the foreign key must also appear in the primary key of the other table.<br />

The CD accompanying this book contains a database named MEGACTY2.MDB. It has<br />

the same information as MEGACTY1.MDB except that Cities.city <strong>and</strong> Countries.country<br />

have been specified as primary keys for their respective tables, <strong>and</strong> Cities.country has been<br />

specified as a foreign key of Countries.country. If the user tries to add a city to the Cities<br />

table whose country does not appear in the Countries table, then the error message “Can’t<br />

add or change record. Referential integrity rules require a related record in table ‘Countries’.”<br />

will be displayed. The message will also be generated if the user tries to delete a country<br />

from the Countries.country field that appears in the Cities.country field. Due to the<br />

interdependence of the two tables in MEGACTY2.MDB, this database is called a relational<br />

database.<br />

A foreign key allows <strong>Visual</strong> <strong>Basic</strong> to link (or join) together two tables from a relational<br />

database in a meaningful way. For instance, when the two tables Cities <strong>and</strong> Countries from<br />

MEGACTY2.MDB are joined based on the foreign key Cities.country, the result is Table 11.4.<br />

The record for each city is exp<strong>and</strong>ed to show its country’s population <strong>and</strong> its currency. This<br />

joined table is very h<strong>and</strong>y if, say, we wanted to click on navigation arrows <strong>and</strong> display a city’s<br />

name <strong>and</strong> currency. We only have to create the original two tables; <strong>Visual</strong> <strong>Basic</strong> creates the<br />

joined table as needed. The request for a joined table is made in a language called SQL.<br />

Table 11.4<br />

A Join of Two Tables<br />

Cities. Cities. Countries. Country.<br />

city country pop1995 pop2015 country pop1995 currency<br />

Tokyo Japan 26.8 28.7 Japan 125.0 yen<br />

Sao Paulo Brazil 16.4 20.8 Brazil 155.8 real<br />

New York USA 16.3 17.6 USA 263.4 dollar<br />

Mexico City Mexico 15.6 18.8 Mexico 85.6 peso<br />

Bombay India 15.1 27.4 India 846.3 rupee<br />

Shanghai China 15.1 23.4 China 1185.2 yuan<br />

Los Angeles USA 12.4 14.3 USA 263.4 dollar<br />

Beijing China 12.4 19.4 China 1185.2 yuan<br />

Calcutta India 11.7 17.6 India 846.3 rupee<br />

Tianjin China 10.7 17.0 China 1185.2 yuan<br />

■ SQL<br />

Relational Databases <strong>and</strong> SQL 331<br />

Structured Query Language (SQL) was developed in the early 1970s at IBM for use with<br />

relational databases. The language was st<strong>and</strong>ardized in 1986 by ANSI (American National

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!