cis110 computer programming design chapter (14)

46
Programming Logic and Design Eighth Edition Chapter 14 Using Relational Databases

Upload: ahmed-al-zaidy-msis

Post on 14-Apr-2017

44 views

Category:

Education


2 download

TRANSCRIPT

Page 1: CIS110 Computer Programming Design Chapter  (14)

Programming Logic and DesignEighth Edition

Chapter 14Using Relational Databases

Page 2: CIS110 Computer Programming Design Chapter  (14)

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

Page 3: CIS110 Computer Programming Design Chapter  (14)

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

Page 4: CIS110 Computer Programming Design Chapter  (14)

Understanding Relational DatabaseFundamentals

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

Page 5: CIS110 Computer Programming Design Chapter  (14)

Understanding Relational DatabaseFundamentals (continued)

• Entity– One record or row

• Attribute– A column, or field

5

Figure 14-1 A telephone book tableProgramming Logic and Design, Eighth Edition

Page 6: CIS110 Computer Programming Design Chapter  (14)

Understanding Relational DatabaseFundamentals (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

Page 7: CIS110 Computer Programming Design Chapter  (14)

Understanding Relational DatabaseFundamentals (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

Page 8: CIS110 Computer Programming Design Chapter  (14)

8

Creating Databases and TableDescriptions

• 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

Page 9: CIS110 Computer Programming Design Chapter  (14)

9

Creating Databases and TableDescriptions (continued)

Figure 14-2 Customer table description

Programming Logic and Design, Eighth Edition

Page 10: CIS110 Computer Programming Design Chapter  (14)

10

Creating Databases and TableDescriptions (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

Page 11: CIS110 Computer Programming Design Chapter  (14)

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

Page 12: CIS110 Computer Programming Design Chapter  (14)

12

Identifying Primary Keys (continued)

Figure 14-3 Table containing residence hall student records

Programming Logic and Design, Eighth Edition

Page 13: CIS110 Computer Programming Design Chapter  (14)

13

Understanding DatabaseStructure Notation

• A shorthand way to describe a table – Table name followed by parentheses containing all the

field names– Primary key underlined

• ExampletblStudents(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

Page 14: CIS110 Computer Programming Design Chapter  (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

Page 15: CIS110 Computer Programming Design Chapter  (14)

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

Page 16: CIS110 Computer Programming Design Chapter  (14)

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

Page 17: CIS110 Computer Programming Design Chapter  (14)

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

Page 18: CIS110 Computer Programming Design Chapter  (14)

Creating Queries (continued)

18

– ExampleSELECT 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

Page 19: CIS110 Computer Programming Design Chapter  (14)

Creating Queries (continued)

19

Figure 14-5 Sample SQL statements and explanations

Programming Logic and Design, Eighth Edition

Page 20: CIS110 Computer Programming Design Chapter  (14)

Understanding Relationshipsbetween 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

Page 21: CIS110 Computer Programming Design Chapter  (14)

Understanding RelationshipsBetween Tables (continued)

21

• Three types of relationships:– One-to-many– Many-to-many– One-to-one

Programming Logic and Design, Eighth Edition

Page 22: CIS110 Computer Programming Design Chapter  (14)

22

Understanding RelationshipsBetween Tables (continued)

Figure 14-6 Sample customers and orders

Programming Logic and Design, Eighth Edition

Page 23: CIS110 Computer Programming Design Chapter  (14)

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

Page 24: CIS110 Computer Programming Design Chapter  (14)

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

Page 25: CIS110 Computer Programming Design Chapter  (14)

Understanding Many-To-Many Relationships

25

Figure 14-7 Sample items and categories: a one-to-many relationship

Programming Logic and Design, Eighth Edition

Page 26: CIS110 Computer Programming Design Chapter  (14)

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

Page 27: CIS110 Computer Programming Design Chapter  (14)

Understanding Many-To-Many Relationships (continued)

27

• ExampletblItems(itemNumber, itemName, itemPurchaseDate, itemPurchasePrice, itemCategoryId)tblCategories(categoryId, categoryName, categoryInsuredAmount)

• New tabletblItemsCategories(itemNumber, categoryId)

Programming Logic and Design, Eighth Edition

Page 28: CIS110 Computer Programming Design Chapter  (14)

Understanding Many-To-Many Relationships (continued)

28

Figure 14-8 Sample items, categories, and item categories: a many-to-many relationship

Programming Logic and Design, Eighth Edition

Page 29: CIS110 Computer Programming Design Chapter  (14)

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 relationshipProgramming Logic and Design, Eighth Edition

Page 30: CIS110 Computer Programming Design Chapter  (14)

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

Page 31: CIS110 Computer Programming Design Chapter  (14)

Understanding Anomalies, NormalForms, 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

Page 32: CIS110 Computer Programming Design Chapter  (14)

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

Page 33: CIS110 Computer Programming Design Chapter  (14)

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

Page 34: CIS110 Computer Programming Design Chapter  (14)

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

Page 35: CIS110 Computer Programming Design Chapter  (14)

35

Figure 14-12 Students table in 2NF

Second Normal Form (continued)

Programming Logic and Design, Eighth Edition

Page 36: CIS110 Computer Programming Design Chapter  (14)

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

Page 37: CIS110 Computer Programming Design Chapter  (14)

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

Page 38: CIS110 Computer Programming Design Chapter  (14)

Third Normal Form (continued)

38

Figure 14-13 The complete Students database

Programming Logic and Design, Eighth Edition

Page 39: CIS110 Computer Programming Design Chapter  (14)

39

Database Performanceand 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

Page 40: CIS110 Computer Programming Design Chapter  (14)

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

Page 41: CIS110 Computer Programming Design Chapter  (14)

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

Page 42: CIS110 Computer Programming Design Chapter  (14)

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

Page 43: CIS110 Computer Programming Design Chapter  (14)

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

Page 44: CIS110 Computer Programming Design Chapter  (14)

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

Page 45: CIS110 Computer Programming Design Chapter  (14)

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

Page 46: CIS110 Computer Programming Design Chapter  (14)

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