Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

PROJECT CASE STUDY

Chapter 7

123

 

 

 

Create database Customers

The statement to create a table is slightly different from the Select statement. Consider a situation where you want to create a table named Customers with five

columns: cust_no, cust_name, cust_addr, cust_phone, and cust_email. To create

the table Customers, you need to use the following SQL statement:

Create table Customers (cust_no char(4), cust_name char(25), cust_addr varchar(50), cust_phone char(12), cust_email char(20))

The first step toward creating a database is to create the design of a database. The following section discusses the fundamentals of a database design.

Primary and Foreign Keys

To access data stored in a table, you need a way to identify each row stored in the table. For example, consider that George Thomas has changed his e-mail address to georget@aol.com and you need to update the same in the Students table. You can execute the following SQL statement to update the information:

Update Students set EmailAddress = ‘georget@aol.com’ where FirstName = ‘George’ and LastName = ‘Thomas’

In this case, the FirstName and LastName columns identify the rows uniquely in the Students table. However, this is not the best way to identify a row because more than one person could have the same combination of the first name and the last name. Therefore, the identifier must uniquely identify all data in the table. In the case of the Students table, you can create another column, StudentID, that will be unique for every row. Such a unique identifier is called a primary key.

Consider the Customers table discussed earlier. It has five columns: cust_no,

cust_name, cust_addr, cust_phone, and cust_email. In this table, cust_no is the best

column to be set as the primary key. This is because this key is unique for each

124 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

record and can, therefore, identify each row uniquely. You can make a column a primary key when creating the table in the following manner:

Create table Customers (cust_no char(4) primary key, cust_name char(25),

cust_addr varchar(50), cust_phone char(12), cust_email char(20))

A foreign key is a column or a combination of columns that creates a link between two tables. Adding the primary key column of one table to another table creates a relationship between the tables. This primary key column becomes the foreign key in the other table.

Consider the Orders table with the columns order_no, order_price, order_quan-

tity, and order_date. To process an order, a customer who ordered the goods must be tracked. To do this, you need to add the cust_no column to the Orders table. The cust_no column,which is the primary key in the Customers table, becomes the foreign key in the Orders table. You can create a foreign key at the time of creating the table in the following manner:

Create table Orders

(order_no char(4) primary key, order_price int,

order_quantity int, order_date datetime, cust_no char(4) not null

references Customers (cust_no))

Referential Integrity

You learned that foreign keys are used to establish relationships. However, you may be wondering why you need to establish these relationships. To appreciate the need for creating relationships between tables, consider the following scenario. Table 7-8 displays the records in the Orders table and Table 7-9 displays the records in the Customers table.

 

 

 

PROJECT CASE STUDY

Chapter 7

125

Table 7-8 The Orders Table

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

cust_no

cust_name

cust_addr

cust_phone cust_email

 

 

 

 

 

 

 

 

C001

Lee, Lynn &

#106, Crosswood St.,

901-458-4233

lee@lla.com

 

 

Associates

Memphis, TN

 

 

 

 

 

 

C023

Korex copiers

#286 Central Avenue,

901-362-7615

webmaster@korex.com

 

 

 

Memphis, TN

 

 

 

 

 

 

C035

Sellmart

#2136 S White Station

901-497-5256

liz@sellmart.com

 

 

 

Memphis, TN

 

 

 

 

 

 

C017

Plasco & Sons

#1176 South Central

901-362-2661

bcroft@aol.com

 

 

 

Avenue, Memphis, TN

 

 

 

 

 

 

C034

Plex Cables, Inc.

#1054 Poplar Avenue,

901-497-0763

sales@plexcables.com

 

 

 

Memphis, TN

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Table 7-9 The Customers Table

 

 

 

 

 

 

 

 

 

 

 

 

 

 

order_no

order_price

order_quantity

order_date

cust_no

 

 

 

 

 

 

 

 

 

 

O762

625

 

2

 

1-12-2002

C023

 

O023

2175

 

4

 

3-3-2002

C035

 

O136

175

 

1

 

2-2-2002

C001

 

O174

550

 

2

 

3-22-2002

C017

 

O382

1050

 

4

 

1-22-2002

C023

 

 

 

 

 

 

 

 

 

 

 

Now, if the record for the customer with cust_no C017 is deleted, there will be an order, O174, that will not have a valid cust_no. In order to avoid such a condition, you need to establish relationships. When any two tables are related, you cannot delete a record in one table if there is a related record for it in the other table.This is known as referential integrity.

Referential integrity provides the following benefits. It prevents users from:

Adding records to a related table if there is no associated record in the primary table

126Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

Changing values in a primary table when there are related records in the related table

Deleting records from a primary table if there are related records in the related table

Normalization

Normalization refers to the process of reducing data redundancy. It usually involves splitting data into two or more tables until repeating groups of data are placed in separate tables. The first step in building a database is to examine the data and then break it down into a row and column format.To appreciate the need for normalization, consider the following example.

Consider Table 7-10, which displays the records in the Product_Orders table.

Table 7-10 The Product_Orders Table

Ord_Id

Ord_Date

Ord_Qty

Ord_Amt

Prod_Id

Prod_Name

Prod_rate

0014

03-13-02

3

$24

P012

Soft toys

$8

0045

03-10-01

2

$12

P003

Candle stand

$6

0033

02-17-02

4

$32

P012

Soft toys

$8

0021

01-25-01

1

$11

P007

Pen

$11

 

 

 

 

 

 

 

Consider a situation where you need to reduce the rate of soft toys to $6 because you have a large stock of soft toys.However, in reducing the rate, you had to make changes in two rows. Imagine the effort required to make changes in a table with a large number of records. So, you decided to split this table into two tables, Orders and Products. Whereas the Orders table has the orders that were booked by customers, the Products table has the list of products sold by the company.

This type of problem, where the same information needs to be changed in more than one record, is referred to as an update anomaly.

The order with Ord_Id 0045 was cancelled. So, you decide to delete that record. However, you realized that the details of Candle stand would also be lost. The solution to this problem is to split the table into two, Orders and Products. This type of problem is referred to as a deletion anomaly.