building a database. implementation of desirable features integrity –a field’s validation can be...
Post on 19-Dec-2015
215 views
TRANSCRIPT
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
Stock
Stock CodeStock DescriptionUnit Price
* Supplier IdUnitCostPriceStock levelReorder level
COrder
COrderNoOrder Date
* Customer IdStaffPaid
* StaffIssued* Staff no
COrderLine
* COrderNoQuantityRequired
Customer
Customer IdCustomer NameCustomer Address
Supplier
Supplier IdSupplier NameSupplier AddressAmount Owed
Staff
Staff noStaff nameStaff role
SupplierOrder
SupplierOrderNo* Supplier IdSupplierOrderDateDeliveredDate
SupplierOrderLine
* SupplierOrderNo* Stock CodeStockRequired
Stock
Stock CodeStock DescriptionUnit Price
* Supplier IdUnitCostPriceStock levelReorder level
COrder
COrderNoOrder Date
* Customer IdStaffPaid
* StaffIssued* Staff no
COrderLine
* COrderNoQuantityRequired
Customer
Customer IdCustomer NameCustomer Address
Supplier
Supplier IdSupplier NameSupplier AddressAmount Owed
Staff
Staff noStaff nameStaff role
SupplierOrder
SupplierOrderNo* Supplier IdSupplierOrderDateDeliveredDate
SupplierOrderLine
* SupplierOrderNo* Stock CodeStockRequired
Hierarchy of data structure
Layers of tables
• The tables Customer, Staff and Supplier have only primary keys. They are the foundation layer. Layer 1.
• The tables COrder, Stock and SOrder have foreign keys that only reference the foundation layer. They are Layer 2.
• COrderline and Sorderline depend on the tables in Layer 2. They are layer 3.
Analogous to building bricks
Layer 1
Custom
erC
ustomer
Staff
Staff
SupplierSupplier
The customer table is added, with key CustomerId, the Staff with key StaffNo and the Supplier with key SupplierId.
Layer 2
COrder
Stock
SOrder
CustomerStaffSupplier
The stock and the sOrder depend on the Supplier, both having foreign key SupplierId.
The COrder depends on BOTH Staff and Customer, having foreign keys StaffPaid, StaffIssued and CustomerId.
Layer 3
Supplier Staff Customer
COrderStock
SOrder
SOrder SOrder lineline
COrder COrder lineline
•Both the sorder line and the corder line depend on the stock, having stockcode as a foreign key and part of their key.
•cOrder line depends on Corder.
•Sorder line depends on SOrder
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