2. Objectives
2
In this chapter, you will learn about:
• Relational database fundamentals
• Creating databases and table descriptions
• Primary keys
• Database structure notation
• Working with records within a table
• Creating queries
Programming Logic and Design, Eighth Edition
3. Objectives (continued)
3
• Relationships between tables
• Poor table design
• Anomalies, normal forms, and normalization
• Database performance and security issues
Programming Logic and Design, Eighth Edition
4. Understanding Relational Database
Fundamentals
4
• Data hierarchy
– Characters
– Fields
– Records
– Files
• Database
– Holds files that an organization needs to support
operations
– Files are called tables
Programming Logic and Design, Eighth Edition
5. Understanding Relational Database
Fundamentals (continued)
• Entity
– One record or row
• Attribute
– A column, or field
5
Figure 14-1 A telephone book table
Programming Logic and Design, Eighth Edition
6. Understanding Relational Database
Fundamentals (continued)
6
• Primary key
– Uniquely identifies a record
– Often defined as a single table column
– Can be a compound key or composite key (made up of
multiple fields)
• Database management software functions
– Create table descriptions
– Identify keys
– Add, delete, and update records within a table
Programming Logic and Design, Eighth Edition
7. Understanding Relational Database
Fundamentals (continued)
7
• Database management software functions (continued)
– Arrange records within a table so they are sorted by
different fields
– Write questions that combine information from multiple
tables
– Create reports
– Keep data secure
• Relational database
– A group of database tables from which you can make
these connections
Programming Logic and Design, Eighth Edition
8. 8
Creating Databases and Table
Descriptions
• Planning and analysis are critical
• Create the database itself
– Name it and indicate the physical location
• Save a table
– Provide a name that begins with the prefix “tbl”
• tblCustomers
• Design the table
– Decide what columns your table needs and name them
– Provide a data type for each column
Programming Logic and Design, Eighth Edition
9. 9
Creating Databases and Table
Descriptions (continued)
Figure 14-2 Customer table description
Programming Logic and Design, Eighth Edition
10. 10
Creating Databases and Table
Descriptions (continued)
• Text columns
– Hold any type of characters: letters or digits
• Numeric columns
– Hold numbers only
• Other possible column types
– Numeric subtypes
– Boolean
– Currency
• Can add descriptions
Programming Logic and Design, Eighth Edition
11. 11
Identifying Primary Keys
• Primary key
– A column that makes each record different from all others
• Examples
– Customer ID, Student ID number, Part number
• Candidate keys – multiple fields in a record that are
unique and could be primary keys
• Alternate keys – candidate keys that were not
chosen as the primary key
• Primary keys should be immutable (values do not
change)
Programming Logic and Design, Eighth Edition
12. 12
Identifying Primary Keys (continued)
Figure 14-3 Table containing residence hall student records
Programming Logic and Design, Eighth Edition
13. 13
Understanding Database
Structure Notation
• A shorthand way to describe a table
– Table name followed by parentheses containing all the
field names
– Primary key underlined
• Example
tblStudents(idNumber, lastName,
firstName, gradePointAverage)
• Provides a quick overview of the table’s structure
– Does not provide information about data types or range
limits on values
Programming Logic and Design, Eighth Edition
14. Working With Records Within
Tables
14
• Entering data
– Requires time and accuracy
– Method depends on database software
• Deleting and modifying data
– Keeping records up to date is vital
Programming Logic and Design, Eighth Edition
15. Working With Records Within
Tables (continued)
15
• Database management software allows you to:
– Sort a table based on any column, or on multiple columns
– Group rows after sorting
– Add subtotals
– Create displays in the format that suits your needs
Programming Logic and Design, Eighth Edition
16. Creating Queries
16
• View subsets of data from a table you have created
– Examine only those customers with an address in a
specific state
• Limit the columns that you view
– A school administrator might only be interested in looking
at names and grade point averages
• Query
– A question using the syntax that the database software
can understand
Programming Logic and Design, Eighth Edition
17. Creating Queries (continued)
17
• Query by example
– Create a query by filling in blanks
• Write statements in Structured Query Language, or
SQL
• SELECT-FROM-WHERE
– Select the columns you want to see
– From the tables that have those columns
– Where one or more conditions are met
Programming Logic and Design, Eighth Edition
18. Creating Queries (continued)
18
– Example
SELECT custId, lastName
FROM tblCustomer
WHERE state = "WI"
• View - a particular way of looking at a database by
selecting specific fields and records, or placing
records in a selected order
– Wildcard
• Examples
• SELECT * FROM tblCustomer
WHERE state = "WI"
• SELECT * FROM tblCustomer
Programming Logic and Design, Eighth Edition
20. Understanding Relationships
between Tables
20
• Most database applications require many related
tables
• Relationship
– Connection between two tables
• Join operation
– Connecting two tables based on the values in a common
column called the join column
• Virtual table
– A table that is displayed as the result of the query
Programming Logic and Design, Eighth Edition
21. Understanding Relationships
Between Tables (continued)
21
• Three types of relationships:
– One-to-many
– Many-to-many
– One-to-one
Programming Logic and Design, Eighth Edition
23. Understanding One-To-Many
Relationships
23
• One-to-many relationship
• One row in a table can be related to many rows in
another table
• The most common type of relationship between tables
• Example
tblCustomers(customerNumber, customerName)
tblOrders(orderNumber, customerNumber,
orderQuantity, orderItem, orderDate)
– One row in the tblCustomers table can correspond to, and
be related to, many rows in the tblOrders table
Programming Logic and Design, Eighth Edition
24. Understanding One-To-Many
Relationships (continued)
24
• Base table: tblCustomers
• Related table: tblOrders
• customerNumber attribute links the two tables
together
• Nonkey attribute
• Foreign key
– When a column that is not a key in a table contains an
attribute that is a key in a related table
Programming Logic and Design, Eighth Edition
26. Understanding Many-To-Many
Relationships (continued)
26
• Many-to-many relationship
– Multiple rows in each table can correspond to multiple
rows in the other
• One specific row in the tblItems table can link to
many rows in the tblCategories table
– Cannot continue to maintain the foreign key
itemCategoryId in the tblItems table
• The simplest way to support a many-to-many
relationship is to remove the itemCategoryId
attribute
Programming Logic and Design, Eighth Edition
27. Understanding Many-To-Many
Relationships (continued)
27
• Example
tblItems(itemNumber, itemName,
itemPurchaseDate, itemPurchasePrice,
itemCategoryId)
tblCategories(categoryId, categoryName,
categoryInsuredAmount)
• New table
tblItemsCategories(itemNumber,
categoryId)
Programming Logic and Design, Eighth Edition
29. Understanding One-To-One
Relationships
29
• One-to-one relationship
• A row in one table corresponds to exactly one row
in another table
• The least frequently encountered
• A common reason to create a one-to-one
relationship is security
Figure 14-9 Employees and salaries tables: a one-to-one relationship
Programming Logic and Design, Eighth Edition
30. Recognizing Poor Table Design
30
• The design must support the needs of the
application
• It must not be cumbersome to use
• Potential problems with simple table design
• Avoid repeating fields (class/classTitle)
Figure 14-10 Students table before normalization
Programming Logic and Design, Eighth Edition
31. Understanding Anomalies, Normal
Forms, and Normalization
• Normalization
– Helps you reduce
data redundancies
and anomalies
• Types of anomalies:
– Update
– Delete
– Insert
31
• Three normal forms:
– First normal form, or 1NF
(eliminate repeating
groups)
– Second normal form, or
2NF (eliminate partial key
dependencies)
– Third normal form, or 3NF
(eliminate transitive
dependencies)
Programming Logic and Design, Eighth Edition
32. First Normal Form
32
• Unnormalized
– A table that contains repeating groups (a subset of rows
that depend on the same key)
• 1NF
– Contains no repeating groups of data
• Sample table
– class and classTitle attributes repeat multiple
times for some of the students
– Repeat the rows for each repeating group of data
– Create a combined key of studentId and class
Programming Logic and Design, Eighth Edition
33. First Normal Form (continued)
33
Figure 14-11 Students table in 1NF
• Atomic attributes
– As small as possible, containing an undividable piece of
data
Programming Logic and Design, Eighth Edition
34. Second Normal Form
34
• Eliminate all partial key dependencies
• No column should depend on only part of the key
• Must be in 1NF
• All nonkey attributes must be dependent on the
entire primary key
• Create multiple tables
– Each nonkey attribute of each table is dependent on the
entire primary key for the specific table within which the
attribute occurs
Programming Logic and Design, Eighth Edition
35. 35
Figure 14-12 Students table in 2NF
Second Normal Form (continued)
Programming Logic and Design, Eighth Edition
36. Second Normal Form (continued)
36
• When breaking up a table into multiple tables:
– Consider the type of relationship among the resulting
tables
– Determine what type of relationship exists between the
two tables
Programming Logic and Design, Eighth Edition
37. Third Normal Form
37
• The table must be in 2NF, and it has no transitive
dependencies
• Transitive dependency
– The value of a nonkey attribute determines, or predicts,
the value of another nonkey attribute
– Example: zip code determines city and state
• Remove the attributes that are determined by, or
are functionally dependent on, the zip attribute
Programming Logic and Design, Eighth Edition
38. Third Normal Form (continued)
38
Figure 14-13 The complete Students database
Programming Logic and Design, Eighth Edition
39. 39
Database Performance
and Security Issues
• Major issues
– Providing data integrity
– Recovering lost data
– Avoiding concurrent update problems
– Providing authentication and permissions
– Providing encryption
Programming Logic and Design, Eighth Edition
40. 40
Providing Data Integrity
• Data integrity
– A set of rules that makes the data accurate and consistent
• Can enforce referential integrity between tables
– Example
• A city cannot be entered as data unless the state that city
belongs to is already created
Programming Logic and Design, Eighth Edition
41. 41
Recovering Lost Data
• An organization’s data can be destroyed in many
ways
• Recovery
– The process of returning the database to a correct form
that existed before an error occurred
• Periodically make a backup copy of a database and
keep a record of every transaction
Programming Logic and Design, Eighth Edition
42. Avoiding Concurrent Update
Problems
42
• Concurrent update problem
– Occurs when two database users need to modify the
same record at the same time
• Lock and persistent (long-term) lock
– A mechanism that prevents changes to a database for a
period of time
• Do not allow users to update the original database
at all
– Store transactions and then later apply them to the
database all at once, or in a batch
Programming Logic and Design, Eighth Edition
43. Providing Authentication and
Permissions
43
• Authentication techniques include:
– Storing and verifying passwords
– Using physical characteristics
• Permissions assigned
– Indicate which parts of the database the user can view,
modify, or delete
Programming Logic and Design, Eighth Edition
44. 44
Providing Encryption
• Encryption
– The process of coding data into a format that human
beings cannot read
• Only authorized users see the data in a readable
format
Programming Logic and Design, Eighth Edition
45. Summary
45
• A database holds a group of files that an
organization needs to support its applications
• Create tables
– Identify primary key
• Use shorthand to describe a table
– tblItemsCategories(itemNumber,
categoryId)
• Database operations
– Sort, add, edit, delete, query
Programming Logic and Design, Eighth Edition
46. Summary (continued)
46
• Table relationships
– One-to-many, many-to-many, one-to-one
• Normalization
– Reduce data redundancy, update anomalies, delete
anomalies, insert anomalies,
– First, Second, Third Normal Form
• A database is a company’s most valuable asset
– Must provide security and integrity
Programming Logic and Design, Eighth Edition