
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf
118 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
Table 7-1 Displaying Data in a Sales Table
City |
Sales |
Date |
New York |
23600 |
Jul-14-2002 |
Atlanta |
16400 |
Jul-12-2002 |
Seattle |
17300 |
Jul-11-2002 |
Chicago |
19700 |
Jul-14-2002 |
San Francisco |
24200 |
Jul-14-2002 |
In this table, City, Sales, and Date are tableYcolumns. The rows contain five |
||
records each about sales at five cities onLa particular date. |
||
You can extract this data from the databaseFby using SQL query statements. The |
||
next section will revise basic SQL query statements. |
|
|
|
M |
|
|
A |
|
E
The Select StatementT
A database can be queried using SQL Select statements. Here is an example of a simple SQL Select statement.
Select * from Sales
This statement extracts all the records from the Sales table.The syntax of an SQL Select statement is as follows:
Select [select-list] from [table name]
You can further modify the Select statement to extract data from the table based on a condition by using the where keyword. For example, consider that you need to only view the sales on July 14, 2002. The following SQL statement provides the required output.
select city, sales from Sales where date = ‘jul-14-2002’
The syntax for the Select statement where you extract data based on a condition is as follows:
Select [select-list] from [table name] where [search condition]
The search operators that you can use with the where keyword are described in Table 7-2.
Team-Fly®

PROJECT CASE STUDY |
Chapter 7 |
119 |
|
|
|
Table 7-2 Operators Used in a Search Condition
Operator |
Description |
= |
Equal |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
<> |
Not equal |
Between |
Between an inclusive range |
Like |
Search for a pattern in a column |
|
|
Table 7-3 shows the Students table that contains information about students.
Table 7-3 The Students Table
FirstName |
LastName |
EmailAddress |
DOB |
City |
Sandra |
Lewis |
slewis@aol.com |
Jan-04-1971 |
Atlanta |
Elaine |
Thorn |
ethorn@yahoo.com |
Oct-27-1979 |
Chicago |
George |
Thomas |
gthomas@freemail.com |
Aug-25-1976 |
Atlanta |
Simon |
Watson |
swatson@fastmail.com |
Mar-18-1978 |
Memphis |
Larry |
Gates |
lgates@mymail.com |
Jun-12-1981 |
Atlanta |
Michael |
Brown |
mbrown@aol.com |
Feb-02-1972 |
Memphis |
Sarah |
Judd |
sjudd@zipmail.com |
Oct-04-1982 |
Chicago |
Joshua |
Johnson |
jjohnson@slowmail.com |
Apr-24-1977 |
Detroit |
Daniel |
Allison |
dallison@aol.com |
Dec-07-1975 |
Chicago |
Nicholas |
Harvey |
nharvey@buzz.com |
Mar-13-1979 |
Detroit |
Laura |
Hansen |
lhansen@hotmail.com |
Sep-12-1973 |
Memphis |
|
|
|
|
|
Now, I’ll create some SQL statements that will refresh your memory. To select only the students who live in Chicago, modify the Select statement, as follows:
Select * from Students where city = ‘Chicago’

120 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
Table 7-4 displays the result of the previous query.
Table 7-4 Students Who Live in Chicago
FirstName |
LastName |
EmailAddress |
DOB |
City |
Daniel |
Allison |
dallison@aol.com |
Dec-07-1975 |
Chicago |
Elaine |
Thorn |
ethorn@yahoo.com |
Oct-27-1979 |
Chicago |
Sarah |
Judd |
sjudd@zipmail.com |
Oct-04-1982 |
Chicago |
|
|
|
|
|
Consider the following SQL statement. The statement returns all rows with last names ending with n.
Select * from Students where LastName like ‘%n’
Table 7-5 displays the result of the previous query.
Table 7-5 Students Whose Last Name Ends With N
FirstName |
LastName |
EmailAddress |
DOB |
City |
Daniel |
Allison |
dallison@aol.com |
Dec-07-1975 |
Chicago |
Elaine |
Thorn |
ethorn@yahoo.com |
Oct-27-1979 |
Chicago |
Joshua |
Johnson |
jjohnson@slowmail.com |
Apr-24-1977 |
Detroit |
Laura |
Hansen |
lhansen@hotmail.com |
Sep-12-1973 |
Memphis |
Michael |
Brown |
mbrown@aol.com |
Feb-02-1972 |
Memphis |
Simon |
Watson |
swatson@fastmail.com |
Mar-18-1978 |
Memphis |
|
|
|
|
|
To alphabetically sort names of all students between Joshua and Michael, use the following SQL statement.
Select * from Students where FirstName between ‘Joshua’ and ‘Michael’
The result of the previous statement is displayed in Table 7-6.

|
|
PROJECT CASE STUDY |
Chapter 7 |
121 |
||
|
|
|
||||
Table 7-6 Students Whose First Name Is between Joshua and Michael |
||||||
|
|
|
|
|
|
|
FirstName |
LastName |
EmailAddress |
DOB |
City |
||
|
|
|
|
|
|
|
Joshua |
Johnson |
jjohnson@slowmail.com |
Apr-24-1977 |
Detroit |
||
Larry |
Gates |
lgates@mymail.com |
Jun-12-1981 |
Atlanta |
||
Laura |
Hansen |
lhansen@hotmail.com |
Sep-12-1973 |
Memphis |
||
Michael |
Brown |
mbrown@aol.com |
Feb-02-1972 |
Memphis |
||
|
|
|
|
|
|
|
Similarly, you can also display only specific columns. For example, to extract only FirstName, LastName, and City, you can use the following Select statement.
Select FirstName, LastName, City from Students
The result of the above SQL statement is shown in Table 7-7.
Table 7-7 List of First Names, Last Names, and Cities
FirstName |
LastName |
City |
Daniel |
Allison |
Chicago |
Elaine |
Thorn |
Chicago |
George |
Thomas |
Atlanta |
Joshua |
Johnson |
Detroit |
Larry |
Gates |
Atlanta |
Laura |
Hansen |
Memphis |
Michael |
Brown |
Memphis |
Nicholas |
Harvey |
Detroit |
Sandra |
Lewis |
Atlanta |
Sarah |
Judd |
Chicago |
Simon |
Watson |
Memphis |
|
|
|

122 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT
The Insert Statement
The Insert statement inserts a new row in a table. For example, to insert a new record in the Students table, you can use the following SQL statement.
Insert into Students values (‘Sarah’, ‘Lee’, ‘slee@yahoo.com’, ‘Mar-22-1977’, ‘Detroit’)
The preceding SQL statement inserts a new record in the Students table with the values mentioned in the SQL statement.
You can also insert data into specific columns. For example, to add only first and last names, you can use the following Insert statement.
Insert into students (FirstName, LastName) values (‘Jessica’, ‘Parker’)
The Update Statement
The Update statement modifies the data in a table. For example, consider that Laura Hansen has changed her last name to Brown. The following update statement can help you make the change:
Update Students set LastName = ‘Brown’ where FirstName = ‘Laura’ and LastName =
‘Hansen’
The Delete Statement
The Delete statement removes some or all rows from a table. For example, in the Students table, to delete the records of all students in Detroit, you use the following SQL statement:
Delete from Students where City = ‘Detroit’
The Create Statement
The Create statement can be used to create a database, a table in a database, and indexes in a table. For example, the following Create statement can be used to create a database containing customers.

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.

PROJECT CASE STUDY |
Chapter 7 |
127 |
|
|
|
Now, consider a situation where you need to change the rate for Candle stand. You would need to change the rate for Ord_Id 0045 also. This problem is similar to the update anomaly; however, you noticed this only when you added this record. This kind of problem is referred to as an insertion anomaly.
Therefore, to design a database without having to encounter these anomalies, you need to normalize the database.
To summarize, the following rules help you design a robust database:
A table should have a unique identifier.
A table should not have repeating values or columns.
A table should store data for only a single type of entity.
A table should avoid columns with null values.
Designing a Database
After applying all the concepts discussed so far, you would arrive at the database structure shown in Figure 7-1 for the CMS database.
FIGURE 7-1 Structure of the CMS database
The following section discusses the details of each table in the CMS database.