34
C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy [email protected]

C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy [email protected]

Embed Size (px)

Citation preview

Page 1: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

C H A P T E R 4

Designing Database

E-CommerceHassanin M. Al-Barhamtoshy

[email protected]

Page 2: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

How This Chapter Is Structured

The main topics we’ll touch on in this chapter are:

1.Analyzing the requirement of the database and the functionality it should support

2.Creating the database structures for the application

3.Implementing the business tier objects required to make the system run, and putting a basic but functional error-handling strategy in place

4.Implementing a functional UI for the system

Page 3: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

What Does a Product Catalog Look Like?

We need the following file entities: Products Categories Customers Orders

Page 4: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Database Designing

The major tables in e-Commerce database, as the following:

1- Products TableName

Category Name

Description

Vendor name

Vendor address

Vendor phone number

Price

Image file name

Page 5: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Database Designing

2- CategoryName

3- CustomersLast Name

First Name

Address

City

State

Zip Code

Phone Number

E-mail

Credit Card Number

Page 6: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Database Designing

4- OrderOrder number

Date

Customer

Product

Quantity

Price

Subtotal

Shipping

Tax

Total

Page 7: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

• After the key columns have been added, the list looks like this:Products

Product ID (primary key)

Name

Category ID (foreign key)

Category Name

Description

Price

Image file name

Vendor ID (foreign key)

VendorVendor ID (primary key)

Name

Address

City

State

Zip Code

Phone Number

E-mail

Page 8: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

CategoryCategory ID (primary key)

Name

CustomersLast Name

First Name

Address

City

State

Zip Code

Phone Number

E-mail (primary key)

Credit Card Number

OrderOrder number (primary key)

Date

Page 9: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

OrderOrder number (primary key)

Date

Customer ID (foreign key)

Product ID (foreign key)

Quantity

Price

Subtotal

Shipping

Tax

Total

Page 10: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Entity Relation Database

Page 11: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Building a Product Catalog Application

This application’s user interface has just three pages:

1.Default.aspx displays a list of products for a category selected by the user.

2.Product.aspx displays details about a specific product selected by the user.

3.Cart.aspx is displayed when the user chooses to purchase a product.

Page 12: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

The user interface for the Product Catalog application.

Page 13: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

The Product List page.

Page 14: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

The Product Detail page.

Page 15: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

The Cart page

Page 16: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Designing the Product Database

It consists of three tables:

Categories

Products

FeaturedProducts

Page 17: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

The Categories table

Page 18: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

The Products table

Page 19: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

The Featured Products table

Page 20: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Creating the database

sqlcmd -S localhost\SQLExpress -i CreateProducts.sql

The CreateProducts.sql script

USE master

GO

IF EXISTS(SELECT * FROM sysdatabases

WHERE name=’Products’)

DROP DATABASE Products

GO

CREATE DATABASE Products 3➝ ON (NAME= Product, FILENAME= ‘C:\APPS\Products.mdf’,

SIZE=10 )

GO

Page 21: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Creating the database

USE Products

GO

CREATE TABLE Categories ( catid VARCHAR(10) NOT NULL,

name VARCHAR(50) NOT NULL, [desc] VARCHAR(MAX) NOT NULL,

PRIMARY KEY(catid) )

GO

CREATE TABLE Products (productid VARCHAR(10) NOT NULL,

catid VARCHAR(10) NOT NULL, name VARCHAR(50) NOT NULL,

shorttext VARCHAR(MAX) NOT NULL, longtext VARCHAR(MAX) NOT NULL,

price MONEY NOT NULL, thumbnail VARCHAR(40) NOT NULL,

image VARCHAR(40) NOT NULL, PRIMARY KEY(productid),

FOREIGN KEY(catid) REFERENCES Categories(catid) )

GO

Page 22: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Creating the database

CREATE TABLE FeaturedProducts (

productid VARCHAR(10) NOT NULL,

featuretext VARCHAR(MAX) NOT NULL,

saleprice MONEY NOT NULL,

PRIMARY KEY(productid),

FOREIGN KEY(productid) REFERENCES Products(productid) )

GO

Page 23: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Adding Logic to the Site

The following paragraphs describe the highlights of this script:

1.Sets the database context to master. This is usually the default context, but it’s a good idea to set it just in case.

2.Deletes the existing Products database if it exists.

3.Creates a database named Products. The database file will be created in the C:\Apps directory. You should change this location if you want to place the database file in a different folder.

4.Creates the Categories table.

5.Note that the column name desc is a SQL keyword, so it must be enclosed in brackets.

6.Creates the Products table.

7.Creates the FeaturedProducts table.

Page 24: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

sqlcmd -S localhost\SQLExpress -i InsertProducts.sql• Once again, you’ll need to change the server name if

you’re not running SQL Server Express on your own computer.

• You’ll know the script works if you see a series of messages like this one: (1 rows affected)

Adding some test data

Page 25: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Adding some test data

Page 26: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

The application must perform the following queries:

• Retrieve all rows from the Categories table to fill the drop-down list on the Default.aspx page so the user can select a product.

• Retrieve all rows from the FeaturedProducts table to display at the top of the Default.aspx page. Note that some data is also required from the Products table, so this query requires a join.

• Retrieve all products for a given category, including the sale price indicated in the FeaturedProducts table.

• Retrieve all data for a specified product to display on the Product.aspx page. Note that this query must also retrieve the sale price from the FeaturedProducts table.

Querying the database

Page 27: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

• The query to retrieve all rows from the Categories table uses this SQL statement:

SELECT catid, name,

[desc]

FROM Categories

ORDER BY name

Querying the database

Page 28: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

• The query to retrieve the featured product rows requires a join to retrieve data from the FeaturedProducts table as well as the Products table:

SELECT FeaturedProducts.productid,

FeaturedProducts.featuretext,

FeaturedProducts.saleprice,

Products.name,

Products.price

FROM FeaturedProducts

INNER JOIN Products

ON FeaturedProducts.productid = Products.productid

Querying the database

Page 29: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

• The query to retrieve the products for a given category also requires a join:

SELECT Products.productid,

Products.catid,

Products.name,

Products.shorttext,

Products.longtext,

Products.price,

Products.image,

Products.thumbnail,

FeaturedProducts.saleprice

FROM Products

LEFT OUTER JOIN FeaturedProducts

ON Products.productid = FeaturedProducts.productid

WHERE (Products.catid = @catid)

Querying the database

Page 30: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

• The last query used by the program retrieves the data for a specific product:

SELECT Products.productid,

Products.catid,

Products.name,

Products.shorttext,

Products.longtext,

Products.price,

Products.image,

FeaturedProducts.saleprice,

FeaturedProducts.featuretext

FROM Products

LEFT OUTER JOIN FeaturedProducts

ON Products.productid = FeaturedProducts.productid

WHERE (Products.productid = @productid)”

Querying the database

Page 31: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

The connection string used to access the Products database is stored in the application’s web.config file, like this:

<connectionStrings>

<add name=”ConnectionString”

connectionString=”Data

Source=localhost\SQLExpress;

Initial Catalog=Products;Integrated Security=True”/>

</connectionStrings>

Connecting to the database

Page 32: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

• The last query used by the program retrieves the data for a specific product:

SELECT Products.productid,

Products.catid,

Products.name,

Products.shorttext,

Products.longtext,

Products.price,

Products.image,

FeaturedProducts.saleprice,

FeaturedProducts.featuretext

FROM Products

LEFT OUTER JOIN FeaturedProducts

ON Products.productid = FeaturedProducts.productid

WHERE (Products.productid = @productid)”

Querying the database

Page 33: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Downloading the Code

• Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at: www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

• You can also download the examples from the Wrox Books Web site at:

http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0764588079.html

Page 34: C H A P T E R 4 Designing Database E-Commerce Hassanin M. Al-Barhamtoshy hassanin@kau.edu.sa

Quiz # 2

• Design an ERD (Entity Relation Database) for e-Learning Application), includes the following:

• E-Learning Table names.

• Tables Structure.

• Tables Relation.