Chapter 3 Part1
Chapter 3 Part1
Content
3.1. How to design a database
3.2. How to using SQL to create a MySQL database
3.3. How to using SQL to work with a MySQL database
3.4. Professional PHP for working with MySQL
3.5. A database-driven website
C1, Slide 2
3.1. How to design a database
Objectives
Applied
1. Given the specifications for a database modeled on a real-world
system, design the database. That is, identify the tables, columns,
keys, relationships, and indexes for the new database.
2. Given a diagram for an unnormalized database, normalize the
structure to the third normal form.
3. Use MySQL Workbench to create database diagrams.
C 16, Slide 3
Objectives (continued)
Knowledge
1. Describe the process of designing a database in terms of tables,
columns, keys, and relationships.
2. Describe referential integrity, and explain how enforcing it
assures that the database isn’t corrupted when rows are inserted,
updated, or deleted.
3. Explain how normalizing a database to the third normal form
improves database performance.
4. In general terms, describe the criteria for indexing a column.
C 16, Slide 4
A database system is modeled
after a real-word system
C 16, Slide 5
The six basic steps for designing a data
structure
Step 1: Identify the data elements
Step 2: Subdivide each element into its smallest useful components
Step 3: Identify the tables and assign columns
Step 4: Identify the primary and foreign keys
Step 5: Review whether the data structure is normalized
Step 6: Identify the indexes
C 16, Slide 6
An invoice that can be used
to identify data elements
C 16, Slide 7
The data elements identified on the invoice
Vendor name Invoice date Item extension
Vendor address Invoice terms Vendor contact name
Vendor phone number Item part number Vendor contact ext.
Vendor fax number Item quantity Vendor AR contact name
Vendor web address Item description Vendor AR contact ext.
Invoice number Item unit price Invoice total
C 16, Slide 8
A name that’s divided into first and last names
C 16, Slide 9
An address that’s divided into street address, city,
state, and zip code
C 16, Slide 10
Possible tables and columns for an A/P
system
Vendors table
Vendor name Vendor contact first name
Vendor address Vendor contact last name
Vendor city Vendor contact phone
Vendor state Vendor AR first name
Vendor zip code Vendor AR last name
Vendor phone number Vendor AR phone
Vendor fax number Terms*
Vendor web address Account number*
C 16, Slide 11
Possible tables and columns for an A/P system
(continued)
Invoices table Invoice line items table
Invoice number* Invoice number*
Invoice date Item part number
Terms* Item quantity
Invoice total Item description
Payment date Item unit price
Payment total Item extension
Invoice due date Account number*
Credit total Sequence number
Account number*
C 16, Slide 12
The relationships between the tables
in the accounts payable system
C 16, Slide 13
Two tables with a many-to-many relationship
C 16, Slide 14
Operations that can violate referential integrity
C 16, Slide 15
A table that contains repeating columns
C 16, Slide 16
The accounts payable system in third normal
form
C 16, Slide 17
About indexes
An index provides a way for a database management system to
locate information more quickly.
MySQL automatically creates an index for a primary key.
You can create composite indexes of two or more columns.
Because indexes must be updated each time you add, update, or
delete a row, don’t create more indexes than you need.
C 16, Slide 18
The seven normal forms
First (1NF)
Second (2NF)
Third (3NF)
Boyce-Codd (BCNF)
Fourth (4NF)
Fifth (5NF)
Domain-key (DKNF) or Sixth (6NF)
C 16, Slide 19
The benefits of normalization
Since a normalized database has more tables than an unnormalized
database, and since each table has an index on its primary key, the
database has more indexes. That makes data retrieval more efficient.
Since each table contains information about a single entity, each
index has fewer columns (usually one) and fewer rows. That makes
data retrieval and insert, update, and delete operations more
efficient.
Each table has fewer indexes, which makes insert, update, and delete
operations more efficient.
Data redundancy is minimized, which simplifies maintenance and
reduces storage.
C 16, Slide 20
The invoice data with a column
that contains repeating values
C 16, Slide 21
The invoice data in first normal form
C 16, Slide 22
The invoice data in first normal form
with keys added
C 16, Slide 23
The invoice data in second normal form
C 16, Slide 24
The A/P system in second normal form
C 16, Slide 25
The A/P system in third normal form
C 16, Slide 26
The accounts payable system in fifth normal
form
C 16, Slide 27
When to denormalize
When a column from a joined table is used repeatedly in search
criteria, you should consider moving that column to the primary key
table if it will eliminate the need for a join.
If a table is updated infrequently, you should consider
denormalizing it to improve efficiency. Because the data remains
relatively constant, you don’t have to worry about data redundancy
errors once the initial data is entered and verified.
Include columns with derived values when those values are used
frequently in search conditions. If you do that, you need to be sure
that the column value is always synchronized with the value of the
columns it’s derived from.
C 16, Slide 28
MySQL Workbench…
Lets you create and edit diagrams.
Lets you define the tables, columns, and indexes for a database.
Lets you define the relationships between the tables in a database.
Lets you generate a diagram from a SQL creation script.
Lets you generate a SQL creation script from a diagram.
C 16, Slide 29
The Welcome tab of the Home page
C 16, Slide 30
MySQL Workbench
C 16, Slide 31