Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://www.princeton.edu/~rcurtis/ultradev/ecommdatabase2.html
This is the second installment in an explanation of Database Design for Ultradev E-commerce application developers. It's based on my database experience (and all the mistakes I made along the way) with building E-commerce apps in Drumbeat 2000. I hope it's helpful
ProductDescription [nvarchar](255)
SupplierID
[int]
Integer
CategoryID
[int]
Integer
QuantityPerUnit
[int]
Integer
Text(20)
1 of 8
03/11/2013 10:46 PM
http://www.princeton.edu/~rcurtis/ultradev/ecommdatabase2.html
discounts off MSRP. AvailableSize AvailableColors SizeID ColorID [nvarchar](50) [nvarchar](100) [int] [int] Text(50) Text(100) Integer Text(50) SizeID - used to link to separate Sizes Table SizeID - used to link to separate Colors Table Discount percentage per item. If you have multiple discount levels (for example with different membership levels) you might have Discount1, Discount2, Discount 3. Item weight for shipping calculations Integer Integer Units currently in stock - used in inventory Units on order - this should be incremented by the shopping cart when orders are placed. Reorder Level - When to Reorder products. Drumbeat E-commerce used UnitsInStock - UnitsonOrder = X If X is > ReorderLevel then "Item is in Stock" If X is <= ReorderLevel then "Item is Out of Stock" This is helpful to display to customers and useful for inventory purposes Used to turn a product on or off in the store. Note: I also have a SupplierAvailable Yes/No field in my Suppliers Table. My AvailableProducts query looks at both. So I can turn on or off ALL items from one Supplier by changing the bit value in the Suppliers Table or individual Products by using the bit value in the Products Table. Turns Discount on or off Similar to ProductAvailable and duplicates the function. Allows an item to be available or not. Link to an Image file of the product or a URL reference to the image a product ranking used for displaying item specials or showing certain items as higher in a sort Notes on the product - like only available between December and January Available Sizes
Discount
[decimal]
ReorderLevel
[smallint]
Integer
ProductAvailable
[bit]
Yes/No
Note
[varchar](255)
Text(255)
Okay, now you have your Products Table all sketched out. Let's quickly go through the Suppliers Table.
2 of 8
03/11/2013 10:46 PM
http://www.princeton.edu/~rcurtis/ultradev/ecommdatabase2.html
Here is a basic Suppliers Table. Modify it for your own purposes. The Suppliers Table has a One-to-Many Relationship to the Products Table (one Supplier can have many Products). Field Name SupplierID (Primary Key) CompanyName ContactFName ContactLName ContactTitle Address1 Address2 City State PostalCode Country Phone Fax Email WebSite SQL 7 Data Type [int] [nvarchar](50) [nvarchar](30), [nvarchar](50) [nvarchar](30) [nvarchar](60) [nvarchar](50), [nvarchar](15) [nvarchar](25) [nvarchar](15) [nvarchar](50) [nvarchar](25) [nvarchar](25) [nvarchar](75) [nvarchar](100) Access 2000/97 Data Type Integer Text(40) Text(30) Text(50) Text(30) Text(60) Text(50) Text(15) Text(25) Text(15) Text(50) Text(25) Text(25) Text(75) Text(100) Description of how you pay the Supplier (check, Purchase order, credit card, Net 30, etc.). This can be held as text or connected to a separate PaymentTypes Table using a PaymentID in both the Suppliers Table and the PaymentTypes Table. Description of Types of Discounts available from the Supplier If there is a standard discount percentage, you can set it here and then apply it to All Products from this Supplier. Description of types of goods available from the Supplier. This can be held as text or connected to a separate GoodsCategory Table using a CategoryID in both the Suppliers Table and the GoodsCategory Table. Sets Discount available on or off Reorder Level - When to Reorder products. Drumbeat E-commerce used UnitsInStock - UnitsonOrder = X Notes This is set as an Identity field in SQL 7 with an increment of 1 and as an Autonumber field in Access. Can be used to link to Product Table. Company Name Company Contact First Name Company Contact Last Name
PaymentMethods
[nvarchar](100)
Text(100)
DiscountType
[nvarchar](100)
Text(100)
DiscountRate
TypeGoods
[nvarchar](255)
Text(255)
DiscountAvailable CurrentOrder
[bit], [bit]
Yes/No Yes/No
3 of 8
03/11/2013 10:46 PM
http://www.princeton.edu/~rcurtis/ultradev/ecommdatabase2.html
If X is > ReorderLevel then "Item is in Stock" If X is <= ReorderLevel then "Item is Out of Stock" This is helpful to display to customers and useful for inventory purposes CustomerID SizeURL [varchar](50) [varchar](100) Text(50) Text(100) Your customer ID with the Supplier. A URL to the Supplier Web Page with sizing info on their products (helpful for your customers) A URL to the Supplier Web Page with color info on their products (helpful for your customers) Link to an Image file of the Supplier's Logo or a URL reference to the image a product ranking used for displaying Supplier specials or showing certain items as higher in a sort. Like the individual item ranking, here you can set one Supplier to show higher on a list than another (regardless of alphabet) Notes on the Supplier
SizeURL Logo
[varchar](100) [nvarchar](75)
Text(100) Text(75)
Ranking
[int]
Integer
Note
[varchar](255)
Text(255)
Here is a sample Orders Table. For each order a new row is created in the table. Since a customer may order multiple items at one time, the actual product information for each order (quantity, size color, ProductID, etc.) are stored in a separate OrderDetails Table. The two Tables are linked by the OrderID (which in most cases would be an Autonumber field in Access or an Identify field in SQL 7). The Orders Table has a One-to-Many Relationship to the OrderDetails Table (one Order can have many OrderDetails) Field Name OrderID Primary Key SQL 7 Data Type [int] Access 2000/97 Data Type Integer Notes This is set as an Identity field in SQL 7 with an increment of 1 and as an Autonumber field in Access. Can be used to link to ProductDetails Table. Used as the Foreign Key to your Customers Table. This can be a text datatype or an Integer depending on your preference as long as each CustomerID is unique. In one application I use a University ID number which is unique for each student but which uses leading zeroes so I have to store it as text (same with Social Security Number)
CustomerID
[nvarchar](50)
Text (50)
4 of 8
03/11/2013 10:46 PM
http://www.princeton.edu/~rcurtis/ultradev/ecommdatabase2.html
PaymentID
[int]
Integer
This is a Foreign Key to a PaymentTypes Table so that the customer can select payment options from a List Box driven by the PaymentTypes Table (e.g. Check, Credit Card, Purchase Order) Date the order was placed. Usually set at the Database level Now() in Access and GetDate() in SQL 7. As soon as the record is written the current date from the Server the database is on is written. For international ordering you may want to think about Greenwich Mean Time. Date the items are required by the customer. Date the items where shipped. If you are not shipping all items together then you may want to have a ShippingDate field in the OrderDetails Table so you can track exactly what items shipped on what day. This is the Foreign Key to the Shippers Table that says what shipping company is used. If you use more than one company and do not ship complete then you may want to have a ShipperID in the OrderDetails table so you can reference that one item went on Date X Federal Express and the other items went on Date Y UPS Ground. Freight Charges. Again it is in this table only if things are shipped complete. If not you would need to track individual shipping charges in the OrderDetails Table. Sales Tax on the entire order A time stamp Used by CyberCash for credit card transaction approval Used by CyberCash for credit card transaction approval Used by CyberCash for credit card transaction approval
OrderDate
[datetime]
Date
RequiredDate
[datetime]
Date
ShipDate
[datetime]
Date
ShipperID
[int]
Integer
Freight
[money]
Currency
Currency Text (50) Text (50) Text (50) Text (250) Yes/No Yes/No Currency Date
The OrderDetails Table stores the information about each particular product that is being ordered. If
5 of 8
03/11/2013 10:46 PM
http://www.princeton.edu/~rcurtis/ultradev/ecommdatabase2.html
the person only orders one item there will be one row added to the Orders Table for the new order and one row added to the OrderDetails Table. If the person orders 25 items there will be one row added to the Orders Table for the new order and twenty-five rows added to the OrderDetails Table. There is an important distinction that comes up here between Access and SQL Server that is critical to your table design and future expandability. Access does not comply with the ANSI SQL specifications and therefore allows you to have a table that does not have a Unique Row Identifier. SQL Server will not let you do this. In SQL Server you must have a field that uniquely identifies each row. In SQL 7 you need an OrderDetailID as the Primary Key to uniquely identify each row in the table. In addition you have the OrderID field which serves as the Foreign Key to the Orders table. This lets you create your One-to-Many Relationship between the Orders Table and the OrderDetails Table. I would strongly urge you to set up Access the same way. That way if you upsize your Access database to SQL Server, you won't have to add the OrderDetailID later. Field Name OrderDetailID Primary Key SQL 7 Data Type [int] Access 2000/97 Data Type Integer Notes This is set as an Identity field in SQL 7 with an increment of 1 and as an Autonumber field in Access. Can be used to link to ProductDetails Table. This is the Foreign Key to the OrderDetails Table. This is the Foreign Key to the Products Table. Price per item Number of items ordered Any discount applied to the individual item Currency Text (50) Text (50) Yes/No Date This is typically a calculated field based on Price * Quantity * Discount Size Color This part of the order has been fulfilled. Date that the bill was issued for the item. Useful if you bill in separate increments based on when you ship. Date the items where shipped. If you are not shipping all items together then you may want to have a ShippingDate field in the OrderDetails Table so you can track exactly what items shipped on what day. This is the Foreign Key to the Shippers Table that says what shipping company is used. If you use more than one company and do not ship complete then you may want to have a ShipperID in the OrderDetails table so you can reference that one item went on Date X Federal Express and the other items went on Date Y UPS Ground.
OrderID - Foreign Key to Orders [int] Table ProductID Price Quantity Discount Total Size Color Fulfilled BillDate [int] [money] [smallint] [real] [money] [nvarchar](50) [nvarchar](50) [bit] [datetime]
ShipDate
[datetime]
Date
ShipperID
[int]
Integer
6 of 8
03/11/2013 10:46 PM
http://www.princeton.edu/~rcurtis/ultradev/ecommdatabase2.html
Freight
[money]
Currency
Freight Charges. Again it is in this table only if things are shipped complete. If not you would need to track individual shipping charges in the OrderDetails Table. Sales Tax on the entire order
SalesTax
[money]
Currency
Now that we have our four basic tables defined there are a few more things to think about before starting your Web application. Think a little bit about the types of products you are selling and the types of orders you may get from your customers. If you are selling books like Amazon or other items that don't have any other special attributes you are done. But let's go back to the Size & Color issue. Let's say that Sue orders 3 T-shirts: one small blue, one medium red, and one large white. How will your E-commerce app handle this? Will she need to make three separate selections in the shopping cart or can she select 3 for the T-shirt quantity and then identify what Size/Color combo she needs for each one? (For simplicities sake we will assume that all three items are the same price but if not it gets even more complicated). Let's go back to your Products Table. If all three shirts have the same ProductID regardless of the color or size then the assumption would be that she enters 3 into the Quantity edit box on the Shopping Cart. How are you going to get the other information from her about correct size/color? Now you know why you want to spend some up front time designing your database properly.
Well, let's start with the easy approach, simple products and colors with no price variations. The table itself is quite simple. Field Name SQL 7 Data Type Access 2000/97 Data Type Integer Notes This is set as an Identity field in SQL 7 with an increment of 1 and as an Autonumber field in Access. Can be used to link to ProductDetails Table. This is the Foreign Key to the Products Table. Size Size
ProductDetailID [int] - Primary Key ProductID Foreign Key to Products Table Color Size
Here is some sample data. As you can see since we have two attributes, size and color, we have to show all possible combinations for each product in the ProductDetails Table. ProductDetailID ProductID 1 2 3 4 5 6 136 136 136 136 136 136 Color Red Black White Red Black White Size XS XS XS S S S
If we run a query that combines the Products Table and the Product Details Table (more on queries later) we would see something like this.
7 of 8
03/11/2013 10:46 PM
http://www.princeton.edu/~rcurtis/ultradev/ecommdatabase2.html
ProductID Product Name 136 136 136 136 136 136 Alpine Tundra Rain Jacket Alpine Tundra Rain Jacket Alpine Tundra Rain Jacket Alpine Tundra Rain Jacket Alpine Tundra Rain Jacket Alpine Tundra Rain Jacket
Size XS XS XS S S S
Copyright 2000 All rights reserved Rick Curtis, Princeton, NJ, USA Macromedia and UltraDev are trademarks of the Macromedia Corporation.
8 of 8
03/11/2013 10:46 PM