download

Building a database
Implementation of desirable features
• Integrity
– A field’s validation can be declared when the field is
declared. If this validation is used, then the integrity of
the field remains intact.
• E.g. date, haulage container, payroll/student number.
– Entity integrity - No attribute participating in the
primary key of a base relation is allowed to accept null
values.
• You cannot make a phone call without a phone number.
– Domain constraints - what are the possible valid values
that can be used?
Referential integrity
– Through the propagation and use of foreign
keys, no detail can be created where a master is
needed, nor can a master be deleted without
consent to the deletion of the details
– E.g. I cannot order stock unless I know who the
supplier is and how to contact the company. If
I add a stock item to my database without a
supplier, I cannot order stock.
Implementation of desirable
features
• Data independence
– The implementation of relational databases
causes the external and conceptual schema to
be data independent. The internal schema and
the physical level are data dependent.
• Controlled redundancy
– The relational model reduces redundancy at the
conceptual level, because it is in third normal
form.
Building tables
• Use primary keys
• Put the tables with ONLY primary keys in first.
– This is the first layer.
• Put the tables that reference those tables in next.
– This is the second layer.
– This layer uses the keys of the first layer as FOREIGN
keys.
– The second layer cannot be placed until the first layer is
complete.
• See the Joe’s Yard example following:
Required new data structure
Customer
Customer Id
Customer Name
Customer Address
Staff
Supplier
Staff no
Staff name
Staff role
Supplier Id
Supplier Name
Supplier Address
Amount Owed
Stock
Docket
DocketNo
Order Date
*Customer Id
DocketPaidSig
DocketRcvdSig
*Staff no
Order Line
*DocketNo
*Stock Code
QuantityRequired
Stock Code
Stock Description
Unit Price
*Supplier Id
UnitCostPrice
Stock level
Reorder level
SupplierOrder
SupplierOrderNo
*Supplier Id
SupplierOrderDate
DeliveredDate
SupplierOrderLine
*SupplierOrderNo
*Stock Code
StockRequired
Hierarchy of data structure
Customer
Customer Id
Customer Name
Customer Address
Staff
Supplier
Staff no
Staff name
Staff role
Supplier Id
Supplier Name
Supplier Address
Amount Owed
Stock
Docket
DocketNo
Order Date
*Customer Id
DocketPaidSig
DocketRcvdSig
*Staff no
Order Line
*DocketNo
*Stock Code
QuantityRequired
Stock Code
Stock Description
Unit Price
*Supplier Id
UnitCostPrice
Stock level
Reorder level
SupplierOrder
SupplierOrderNo
*Supplier Id
SupplierOrderDate
DeliveredDate
SupplierOrderLine
*SupplierOrderNo
*Stock Code
StockRequired
Layers of tables
• The tables Customer, Staff and Supplier
have only primary keys. They are the
foundation layer. Layer 1.
• The tables Docket, Stock and Supplier
Order have foreign keys that only reference
the foundation layer. They are Layer 2.
• Order line and Supplier order line depend
on the tables in Layer 2. They are layer 3.
Analogous to building bricks
Staff
Layer 1
The
customer
table is
added, with
key
CustomerId,
the Staff
with key
StaffNo and
the Supplier
with key
SupplierId.
Layer 2
Docket
Supplier Order
Stock
Supplier
Staff
Customer
The stock and the
supplier Order depend
on the Supplier, both
having foreign key
SupplierId.
The Docket depends
on BOTH Staff and
Customer, having
foreign keys
CustomerPdSig,
CustomerRcvdSig and
CustomerId.
Layer 3
Supplier
Order
line
Order line
Stock
Docket
Supplier
Order
Supplier
Staff
Customer
•Both the supplier
order line and the
order line depend
on the stock,
having stockcode
as a foreign key
and part of their
key.
•Order line
depends on docket.
•Supplier order
line depends on
Supplier Order
The built database
Recap
• Look back at the blocks.
– The table creates are the structure or the framework - i.e.
the architect’s drawing
– The inserts are like the bricks. You cannot insert into a
table unless there is supporting data in the table on which
it depends.
• Do
– Creates starting with the one(s) with no dependents
– Inserts starting with the one(s) with no dependents
– Deletes starting with the one(s) on which no other
depends
– Drops starting with the one(s) on which no other depends