Database Design Tutorial

数据库是什么?An organized collection of data

数据库的类型
Relational (Oracle, SQL Server, DB2)
Data Warehouse
NoSQL (Many subtypes of NoSQL databases)
Object
Many other kinds including older architectures such as Hierarchical

For our purposes we are interested in Relational Databases
Relational Databases are defined by relationships in the data - usually
primary and foreign keys. These types of database are very useful for transaction (often called OLTP - online transaction processing).
Data is contained in tables that looks much like an Excel spreadsheet.

A poor Design example:

Customer
PK CustomerID
   CustomerName
   Address
   City
   State
   PhoneNumber1
   PhoneNumber2
   EmployerName

Order
PK OrderID
   OrderDate
   OrderDescription
   OrderComments
   ProductID
   ProductName

Product
PK ProductID
   ProductName
   Size
   ProductComments

For Customer table:
CustomerName可能含有First name, last name, Middle name,这里用CustomerName包含这三者,违反了1NF
PhoneNumber1, PhoneNumber2,如果想加入类似PhoneNumber3,需要修改表的结构
EmployerName和CustomerID(key)没有直接关系,不符合2NF, 如果想把客户和雇主联系,可以用EmployerID,和Employer表关联,而且,存在无业人员的情况

For Order table:
因为通过ProductID就能直接找到ProductName,所以存在传递依赖关系,不符合3NF,这种传递依赖关系当一个Order含有多个Product的时候,能够清晰的看到很多的重复的数据。

从业务上来说,这种表设计,没法通过Customer找到Order的信息

Order和Product表直接存在多对多的关系,需要引入第三个表
从业务逻辑上来看:
一个Customer可以有多个Order
一个Order也可以有多个Product,一个Product可以属于多个Order


A Solid Design example:
Customer
PK CustomerID
   LName
   FName
   DOB
   Address
   City
   State

CustomerPhone
PK CustomerID
PK AreaCd
PK PhoneNbr
   Type
   Comments
   Private

Order
PK OrderID
   CustomerID
   Date
   Comments

Product
PK ProductID
   Name
   Description
   Price
   Size
   Units
   Comments

OrderProducts
PK OrderID
PK ProductID
   Qty
   Comments
请使用浏览器的分享功能分享到微信等