Presentation is loading. Please wait.

Presentation is loading. Please wait.

DAT602 Database Application Development Lecture 4 Database Design.

Similar presentations


Presentation on theme: "DAT602 Database Application Development Lecture 4 Database Design."— Presentation transcript:

1 DAT602 Database Application Development Lecture 4 Database Design

2 Database Application Development - Lecture 4 Basic structure of database application Three ties model Database is the foundation of whole system. Generally, we should design database at very beginning. Database Application server (Business logic) Client (User interface)

3 Database Application Development - Lecture 4 Analyze Requirement Requirement analysis is complicated. For database design you should consider which kinds of data are necessary for the expecting system? The more careful you consider it, the less risk of modifying database you’ll take in the future.

4 Database Application Development - Lecture 4 In this lecture, we’ll use a Mini online shop as a example to demonstrate how to design a simple database. Basic requirement: Sellers can put products online. Buyers can view and buy available products. Details of transaction should be kept.

5 Database Application Development - Lecture 4 Mini Online shop How many entities should be involved in this system? Customers, sellers, products, transaction. Create one table for each entity. CustomerSellerProductTransaction Tables

6 Database Application Development - Lecture 4 Design a table Which data is needed for each table? What types of these data? How long of data is appropriate? Which column should be primary key?

7 Database Application Development - Lecture 4 Table design Which data is needed for each table? Only cover necessary and close related data. ColumnType NameChar IDChar EmailChar TelephoneChar HeightFloat Unnecessary

8 Database Application Development - Lecture 4 Table design What types of these data? Float, integer, char, date, etc.. Different databases have different type systems, but they are similar. char vs. varchar The char is a fixed-length character data type, the varchar is a variable-length character data type.

9 Database Application Development - Lecture 4 Table design How long of data is appropriate? 8bit, 32bit, 128bit? Longer data costs more storage. Consider carefully. 1 Chinese character = 2 byte = 16 bit

10 Database Application Development - Lecture 4 Table design Which column should be chosen as primary key? Chose the column will never have two same values. Primary key is used to identify different records.

11 Database Application Development - Lecture 4 Customer table. Column NameTypeLength(Byte) UserNamechar16 Addresschar128 TelephoneNOchar16 UserIDchar16 Emailchar32 Balancefloat16 Primary Key

12 Database Application Development - Lecture 4 Transaction table Column NameData typeLength TransactionIDchar16 Datedate16 BuyerIDchar16 SellerIDchar16 ItemIDchar16 Amountfloat16 TotalPricefloat16 Primary Key Foreign Key

13 Database Application Development - Lecture 4 Relationship between tables ColumnType TransactionIDchar BuyerIDchar SellerIDchar …… ColumnType UserIDchar UserNamechar …… ColumnType SellerIDchar SellerNamechar …… Primary key Foreign key

14 Database Application Development - Lecture 4 Normalization Normalization eliminates redundancies and to ensure consistent dependencies. Simply, normalization refines database design.

15 Database Application Development - Lecture 4 Normalization guideline There must be no repeated fields. The table cannot contain fields that are not related to the primary key. For a multiple keys table, the table should only contain data related to one entity, and that entity should be described by its primary key. The table cannot contain two or more independent multivalued facts about an entity.

16 Database Application Development - Lecture 4 There must be no repeated fields. ColumnType UserIDChar FullNameChar SurnameChar …… Fullname and Surname are conflict, remove Surname or keep Surname and use GivenName to replace Fullname. ColumnType UserIDChar GivenNameChar SurnameChar …… Customer table Normalized Customer table

17 Database Application Development - Lecture 4 The table cannot contain fields that are not related to the primary key. ColumnType UserIDChar UserNameChar TransactionIDChar …… For customer table, TransactionID is not a part of customer information, but a part of transaction, you should remove it. ColumnType UserIDChar UserNameChar …… Customer table Normalized Customer table

18 Database Application Development - Lecture 4 For a multiple keys table, the table should only contain data related to one entity, and that entity should be described by its primary key. ColumnType TransactionIDChar BuyerIDChar SellerIDChar BuyerNameChar …… Primary key Foreign key Transaction Table ColumnType TransactionIDChar BuyerIDChar SellerIDChar …… Normalized Transaction Table

19 Database Application Development - Lecture 4 The table cannot contain two or more independent multi-valued facts about an entity. UserID (Primary Key) Telephone 007123456 007345678 008389207 …… Customer Table UserID (Primary Key) Telephone1Telephone2 007123456345678 008389207 …… Normalized Customer Table Primary key can not has two records with same value.

20 Database Application Development - Lecture 4 View Views are implemented in a relational database system by allowing the user to select data from the database to create temporary tables, known as views.

21 Database Application Development - Lecture 4 TransactionIDDateUserIDUserNameUserTelephoneSeller…. TransactionCustomerSeller View of detailed transaction Tables

22 Database Application Development - Lecture 4 Keep each table as simple as possible, just assign necessary columns to it, you can create views by combining data from multiple tables.

23 Database Application Development - Lecture 4 Literature “Java Database Programming Bible”, by John O'Donahue ISBN:0764549243 John Wiley & Sons © 2002 Chapter 2


Download ppt "DAT602 Database Application Development Lecture 4 Database Design."

Similar presentations


Ads by Google