0% found this document useful (0 votes)
21 views31 pages

Chapter 3 Part1

Uploaded by

tindepzai91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views31 pages

Chapter 3 Part1

Uploaded by

tindepzai91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

ADVANCED WEB TECHNOLOGY

Chapter 3. Master MySQL Programming


Chapter 3. Master SQL Programming

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*

 Data elements that were added


 *Data element related to two or more entities

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

Two tables with a one-to-one relationship

C 16, Slide 14
Operations that can violate referential integrity

This operation… Violates referential integrity if…


Delete a row from the primary key table
The foreign key table contains one or more
rows related to the deleted row
Insert a row in the foreign key table
The foreign key value doesn’t have a
matching primary key value in the related
table
Update the value of a foreign key
The new foreign key value doesn’t have a
matching primary key value in the related
table
Update the value of a primary key
The foreign key table contains one or more
rows related to the row that’s changed

C 16, Slide 15
A table that contains repeating columns

A table that contains redundant data

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.

When to create an index


 When the column is a foreign key
 When the column is used frequently in search conditions or joins
 When the column contains a large number of distinct values
 When the column is updated infrequently

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

The invoice data with repeating columns

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

Questions about the structure


1. Does the vendor information depend only on the invoiceID column?
2. Does the terms column depend only on the invoiceID column?
3. Does the accountNo column depend only on the invoiceID column?
4. Can the invoiceDueDate and lineItemAmount columns be derived
from other data?

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.

How to install MySQL Workbench


1. Go to the MySQL Workbench web site at:
http://www.mysql.com/products/workbench
2. Download the version for your system.
3. Run the installer or setup file and respond to the prompts.

C 16, Slide 29
The Welcome tab of the Home page

C 16, Slide 30
MySQL Workbench

C 16, Slide 31

You might also like