Basic Database Design COSC 2328 – Web Programming
The Design Process Determine the purpose of the database Organize the information / divide the information into tables Turn information into columns Specify primary keys Setup table relationships Apply normalization rules
What is “Normalization” Organizing the columns (attributes) and tables (relations) of a relational database Why? Reduce data redundancy Reduce storage requirements Improve data dependencies
3 Normal Forms No repeating elements or groups of elements No partial dependencies on a concatenated key No dependencies on non-key attributes
Invoices
Tracking In A Spreadsheet How many 56” Blue Freens were sold in 2002? What was the total sales of 3” Red Freens came from Washington DC in 2016?
Filling In The Data Oops – Now we have repeating elements
Creating A Composite Key If we identify a primary key as the Invoice Number and Item ID, we get a unique key. There is no single column that identifies each row, but if we put the two columns together we now have a unique key.
No Partial Dependencies Each column in the table that is not part of the primary key must depend on the entire concatenated key If any column only depends on one part of the key, then we say that the entire table fails second normal form Steps -- Take each column not part of the key. Can this column exist without one or the other part of the primary key? If ‘yes’ – even once – then the table fails Second Normal Form
Table Concepts Break-up the spreadsheet into multiple tables in a database Think about one-to-many and many-to-one relationships Determine which fields belong to which tables Use IDs to identify rows when necessary
Designing The Tables orders --------------------- orderId (PK) customerId (FK) orderDate orderItems --------------------- orderId (PK) (FK) itemId (PK) (FK) itemQuantity items -------------------- itemId (PK) itemDescription itemPrice 1 n n 1 n 1 customers --------------------- customerId (PK) customerName customerAddress customerCity customerState