142 CHAPTER 7 An Example of Logical Database DesignFigure 7.2 Conceptual data model diagram for UML Table 7.2 Results of the Analysis of the Conceptual Data Model Customermany: Jobone cu
Trang 1142 CHAPTER 7 An Example of Logical Database Design
Figure 7.2 Conceptual data model diagram for UML
Table 7.2 Results of the Analysis of the Conceptual Data Model
Customer(many): Job(one) cust-no -> job-title
Order(many): Customer(one) order-no -> cust-no
Salesperson(many): Department(one) sales-id -> dept-no
Item(many): Department(one) item-no -> dept-no
Order(many): Item(many):
Salesperson(one)
order-no,item-no->sales-id
Order(many): Department(many):
Salesperson(one)
order-no,dept-no-> sales-id
Customer
Salesperson Order
Job
Department order-dept-sales
Item
*
has
1 1
*
*
*
1
* places
hires 1
contains
order- item-sales
Trang 27.2 Logical Design 143
create table customer
(cust_no char(6), job_title varchar(256), primary key (cust_no),
foreign key (job_title) references job
on delete set null on update cascade);
create table job
job_title varchar(256), primary key (job_no));
create table order
(order_no char(9), cust_no char(6) not null, primary key (order_no),
foreign key (cust_no) references customer
on delete set null on update cascade);
create table salesperson
(sales_id char(10) sales_name varchar(256), dept_no char(2), primary key (sales_id),
foreign key (dept_no) references department
on delete set null on update cascade);
create table department
(dept_no char(2), dept_name varchar(256), manager_name varchar(256), primary key (dept_no));
create table item
(item_no char(6), dept_no char(2), primary key (item_no),
foreign key (dept_no) references department
on delete set null on update cascade);
Trang 3144 CHAPTER 7 An Example of Logical Database Design
create table order_item_sales
(order_no char(9),
sales_id varchar(256) not null, primary key (order_no, item_no),
foreign key (order_no) references order
on delete cascade on update cascade,
foreign key (item_no) references item
on delete cascade on update cascade,
foreign key (sales_id) references salesperson
on delete cascade on update cascade);
create table order_dept_sales
(order_no char(9),
sales_id varchar(256) not null, primary key (order_no, dept_no),
foreign key (order_no) references order
on delete cascade on update cascade,
foreign key (dept_no) references department
on delete cascade on update cascade,
foreign key (sales_id) references salesperson
on delete cascade on update cascade);
Note that it is often better to put foreign key definitions in separate (alter) statements This prevents the possibility of getting circular defini-tions with very large schemas
This process of decomposition and reduction of tables moves us closer to a minimum set of normalized (BCNF) tables, as shown in Table 7.3
The reductions shown in this section have decreased storage space and update cost and have maintained the normalization of BCNF (and thus 3NF) On the other hand, we have potentially higher retrieval cost—given the transaction “list all job_titles,” for example—and have increased the potential for loss of integrity because we have eliminated simple tables with only key attributes Resolution of these trade-offs depends on your priorities for your database
The details of indexing will be covered in the companion book, Phys-ical Database Design However, during the logPhys-ical design phase of
defin-ing SQL tables, it makes sense to start considerdefin-ing where to create indexes At a minimum, all primary keys and all foreign keys should be
Trang 47.3 Summary 145
indexed Indexes are relatively easy to implement and store, and make a significant difference in reducing the access time to stored data
7.3 Summary
In this chapter, we developed a global conceptual schema and a set of SQL tables for a relational database, given the requirements specification for a retail store database The example illustrates the database life cycle steps of conceptual data modeling, global schema design, transforma-tion to SQL tables, and normalizatransforma-tion of those tables It summarizes the techniques presented in Chapters 1 through 6
Table 7.3 Decomposition and Reduction of Tables
customer cust_no job_title, cust_name,
cust_address
order order_no cust_no, item_no,
date_of_purchase, price
salesperson sales_id dept_no, sales_name, phone_no
item item_no dept_no, color, model_no
order_item_sales order_no,item_no sales_id
order_dept_sales order_no,dept_no sales_id