All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

18
All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003

Transcript of All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Page 1: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

All Powder Board and Ski

Oracle 9i WorkbookChapter 3: Database TablesJerry PostCopyright © 2003

Page 2: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

DBDesign: An Expert System

http://time-post.com/dbdesign Benefits

Makes it easy to create database diagrams Saves data in central location, so changes can be

made from almost any computer Provides immediate detailed feedback on the

design Requirements

Instructors must ask for a free account Instructors and students need a Java-enabled Web

browser

Page 3: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Identifying Key Columns

If you are uncertain about which columns to key. Write them down and evaluate the business rules.

OrderID CustomerID

For a given order, can there ever be more than one customer?If yes, then key CustomerID.In most businesses, only one customer per order, so do not key it.

For a given customer, can there ever be more than one order?If yes, then key OrderID, otherwise, do not key it.All businesses hope to get more than one order from a customer, so OrderID must be key. Underline it. Since OrderID is the only key,these columns belong in the Order table.

CustomerOrder(OrderID CustomerID, … )

Page 4: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Sequences: Introduction

Sometimes it is best to let the DBMS generate a guaranteed unique key value

The table must contain a single column primary key that is assigned a numeric data type, such as NUMBER(38)

Later, you will create a SEQUENCE to generate the numbers

Later, you will create a trigger to automatically obtain and use the generated key value

Page 5: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Customer Skill Level

CustomerID, LastName, … Style, SkillLevel

CustomerID, LastName, … Style, SkillLevel

Business rule: Each customer can have one skill in many styles.Business rule: Each style can apply to more than one customer.Need a table with both attributes as keys.

CustomerID, LastName, … Style, SkillLevel

But you cannot include LastName, FirstName and so on, because then you would have to re-enter that data for each customer skill.

Consider what happens if you (incorrectly) try to place Style and SkillLevel in the Customer table:

Page 6: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Customer Style Skills

Customer

CustomerIDLastNameFirstNamePhoneAddressCityStateZIP

CustomerSkill

CustomerIDStyleSkillLevel

Style

StyleStyleDescription

SkillLevel

SkillLevelSkillDescription

Page 7: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Creating Tables with the Editor

Table name

Data typeData size

Column name

Primary key

Page 8: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Primary Key Constraint

Create unique name

Select Primary constraint type

Select the columns that make up the primary key

Page 9: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Constraints

Unique name Acceptable values

CHECK constraint type

Page 10: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Create Tables with SQL

CREATE TABLE Customer(

CustomerID NUMBER(38),LastName NVARCHAR2(25),FirstName NVARCHAR2(25),Phone NVARCHAR2(25),Email NVARCHAR2(120),Address NVARCHAR2(50),City NVARCHAR2(50),State NVARCHAR2(25),ZIP NVARCHAR2(15),Gender NVARCHAR2(15),DateOfBirth DATE,CONSTRAINT pk_Customer PRIMARY KEY (CustomerID),CONSTRAINT ck_CustGender

CHECK (Upper(Gender) IN ('FEMALE', 'MALE', 'UNIDENTIFIED')));

Page 11: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Table in SQL: ProductCategory

CREATE TABLE ProductCategory(

Category NVARCHAR2(50),CategoryDescription NVARCHAR2(250),

CONSTRAINT pk_ProductCategory PRIMARY KEY (Category));

Page 12: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

ANALYZE TABLE

ANALYZE TABLE PaymentMethod COMPUTE STATISTICS;

One command for each table

Substantial performance improvement

Page 13: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Relationships: Department and Employee

Employee

EmployeeIDTaxpayerIDLastNameFirstNameAddressPhoneCityStateZIPDepartment

Department

DepartmentDescription

1…1

1…*Foreign Key

Reference Table

Page 14: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Relationship: Foreign Key Constraint

Employee table

Department table must be created first

Foreign key constraint

Column in the Employee table Column in the

Department table

Reference table Cascade is important

Page 15: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Relationships in SQL

CREATE TABLE Department(

Department NVARCHAR2(50),Description NVARCHAR2(150),

CONSTRAINT pk_Department PRIMARY KEY (Department));CREATE TABLE Employee(

EmployeeID INTEGER,TaxpayerID NVARCHAR2(50),LastName NVARCHAR2(25),FirstName NVARCHAR2(25),Address NVARCHAR2(50),Phone NVARCHAR2(25),City NVARCHAR2(50),State NVARCHAR2(15),ZIP NVARCHAR2(15),Department NVARCHAR2(50)

DEFAULT ‘Sales’,CONSTRAINT pk_Employee PRIMARY KEY (EmployeeID),CONSTRAINT fk_DepartmentEmployee FOREIGN KEY (Department)

REFERENCES Department(Department)ON DELETE CASCADE

);

Page 16: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Estimating Database Size

CustomerID Long 4LastName Text(50) 30FirstName Text(50) 20Phone Text(50) 24Email Text(150) 50Address Text(50) 50State Text(50) 2ZIP Text(15) 14Gender Text(15) 10DateOfBirth Date 8

Average bytes per customer 212Customers per week (winter) *200Weeks (winter) *25Bytes added per year 1,060,000

Page 17: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Data Assumptions

200 customers per week for 25 weeks

2 skills per customer

2 rentals per customer per year

3 items per rental

20 percent of customers buy items

4 items per sale

100 manufacturers

20 models per manufacturer

5 items (sizes) per model

Page 18: All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.

Database Table Sizes

Table Bytes Rows Comments TotalCustomer 212 5000 200 per week * 25 weeks 1,060,000CustomerSkill 68 10000 2 per customer 680,000Rental 44 10000 2 per customer 440,000RentItem 78 30000 3 items per rental 2,340,000PaymentMethod 20 5 basic methods 100Sale 152 1000 20 pct of customers buy 152,000SaleItem 36 4000 4 items per sale average 144,000Employee 248 35 Est. per year 8,680Department 70 5 basic list 350BindingStyle 120 15 main ones 1,800Manufacturer 262 100 should be enough 26,200ItemModel 174 2000 20 per manufacturer 348,000Inventory 48 10000 5 per model average 480,000ProductCategory 70 25 should be enough 1,750SkiBoardStyle 110 50 should be enough 5,500SkillLevel 74 10 10 main skill levels 740

5,689,120