Presentation is loading. Please wait.

Presentation is loading. Please wait.

P ROGRAMMING L OGIC AND D ESIGN S IXTH E DITION Chapter 14 Using Relational Databases.

Similar presentations


Presentation on theme: "P ROGRAMMING L OGIC AND D ESIGN S IXTH E DITION Chapter 14 Using Relational Databases."— Presentation transcript:

1 P ROGRAMMING L OGIC AND D ESIGN S IXTH E DITION Chapter 14 Using Relational Databases

2 O BJECTIVES In this chapter, you will learn about: Relational database fundamentals Creating databases and table descriptions Primary keys Database structure notation Adding, deleting, updating, and sorting records within a table 2 Programming Logic & Design, Sixth Edition

3 O BJECTIVES ( CONTINUED ) Creating queries Relationships between tables Poor table design Anomalies, normal forms, and normalization Database performance and security issues 3 Programming Logic & Design, Sixth Edition

4 U NDERSTANDING R ELATIONAL D ATABASE F UNDAMENTALS Data hierarchy Characters Fields Records Files Database Holds files organization needs to support operations Called tables 4 Programming Logic & Design, Sixth Edition

5 U NDERSTANDING R ELATIONAL D ATABASE F UNDAMENTALS ( CONTINUED ) Figure 14-1 A telephone book table 5 Programming Logic & Design, Sixth Edition

6 U NDERSTANDING R ELATIONAL D ATABASE F UNDAMENTALS ( CONTINUED ) Primary key Uniquely identifies a record Often defined as a single table column Can be compound or composite Database management software functions Create table descriptions Identify keys Add, delete, and update records within a table 6 Programming Logic & Design, Sixth Edition

7 U NDERSTANDING R ELATIONAL D ATABASE F UNDAMENTALS ( 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 7 Programming Logic & Design, Sixth Edition

8 C REATING D ATABASES AND T ABLE D ESCRIPTIONS Planning and analysis 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 8 Programming Logic & Design, Sixth Edition

9 C REATING D ATABASES AND T ABLE D ESCRIPTIONS ( CONTINUED ) Figure 14-2 Customer table description 9 Programming Logic & Design, Sixth Edition

10 C REATING D ATABASES AND T ABLE D ESCRIPTIONS ( 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 10 Programming Logic & Design, Sixth Edition

11 I DENTIFYING P RIMARY K EYS Primary key Column that makes each record different from all others Examples customerID Student ID number Important for several reasons Should be immutable 11 Programming Logic & Design, Sixth Edition

12 I DENTIFYING P RIMARY K EYS ( CONTINUED ) Figure 14-3 Table containing residence hall student records 12 Programming Logic & Design, Sixth Edition

13 U NDERSTANDING D ATABASE S TRUCTURE N OTATION 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 13 Programming Logic & Design, Sixth Edition

14 A DDING, D ELETING, U PDATING, AND S ORTING R ECORDS WITHIN T ABLES Entering data Requires time and accuracy Method depends on database software Deleting and modifying data Keeping records up to date is vital 14 Programming Logic & Design, Sixth Edition

15 S ORTING THE R ECORDS IN A T ABLE 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 15 Programming Logic & Design, Sixth Edition

16 C REATING Q UERIES 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 School administrator might only be interested in looking at names and grade point averages Query Question using the syntax that the database software can understand 16 Programming Logic & Design, Sixth Edition

17 C REATING Q UERIES ( CONTINUED ) Query by example Create a query by filling in blanks Write statements in Structured Query Language, or SQL SELECT-FROM-WHERE Basic form of the SQL statement Example SELECT custId, lastName FROM tblCustomer WHERE state = "WI" 17 Programming Logic & Design, Sixth Edition

18 C REATING Q UERIES ( CONTINUED ) Can use comparison operators Wildcards Examples SELECT * from tblCustomer WHERE state = "WI“ SELECT * FROM tblCustomer 18 Programming Logic & Design, Sixth Edition

19 C REATING Q UERIES ( CONTINUED ) Figure 14-5 Sample SQL statements and explanations 19 Programming Logic & Design, Sixth Edition

20 U NDERSTANDING R ELATIONSHIPS BETWEEN T ABLES 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 Virtual table Table that is displayed as the result of the query Three types of relationships 20 Programming Logic & Design, Sixth Edition

21 U NDERSTANDING R ELATIONSHIPS BETWEEN T ABLES ( CONTINUED ) Figure 14-6 Sample customers and orders 21 Programming Logic & Design, Sixth Edition

22 U NDERSTANDING O NE - TO -M ANY R ELATIONSHIPS One row in a table can be related to many rows in another table 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 22 Programming Logic & Design, Sixth Edition

23 U NDERSTANDING O NE - TO -M ANY R ELATIONSHIPS ( CONTINUED ) 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 23 Programming Logic & Design, Sixth Edition

24 U NDERSTANDING M ANY - TO -M ANY R ELATIONSHIPS Another example of a one-to-many relationship tblItems(itemNumber, itemName, itemPurchaseDate, itemPurchasePrice, itemCategoryId) tblCategories(categoryId, categoryName, categoryInsuredAmount) 24 Programming Logic & Design, Sixth Edition

25 U NDERSTANDING M ANY - TO -M ANY R ELATIONSHIPS ( CONTINUED ) Figure 14-7 Sample items and categories: a one-to-many relationship 25 Programming Logic & Design, Sixth Edition

26 U NDERSTANDING M ANY - TO -M ANY R ELATIONSHIPS ( CONTINUED ) 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 Simplest way to support a many-to-many relationship Remove the itemCategoryId attribute 26 Programming Logic & Design, Sixth Edition

27 U NDERSTANDING M ANY - TO -M ANY R ELATIONSHIPS ( CONTINUED ) Example tblItems(itemNumber, itemName, itemPurchaseDate, itemPurchasePrice) tblCategories(categoryId, categoryName, categoryInsuredAmount) New table tblItemsCategories(itemNumber, categoryId) 27 Programming Logic & Design, Sixth Edition

28 U NDERSTANDING M ANY - TO -M ANY R ELATIONSHIPS ( CONTINUED ) Figure 14-8 Sample items, categories, and item categories: a many-to-many relationship 28 Programming Logic & Design, Sixth Edition

29 U NDERSTANDING O NE - TO -O NE R ELATIONSHIPS Row in one table corresponds to exactly one row in another table Least frequently encountered Common reason you create a one-to-one relationship is security 29 Programming Logic & Design, Sixth Edition

30 U NDERSTANDING O NE - TO -O NE R ELATIONSHIPS ( CONTINUED ) Figure 14-9 Employees and salaries tables: a one-to-one relationship 30 Programming Logic & Design, Sixth Edition

31 R ECOGNIZING P OOR T ABLE D ESIGN Need to know the following information Students’ names Students’ addresses Students’ cities Students’ states Students’ zip codes ID numbers for classes in which students are enrolled Titles for classes in which students are enrolled Potential problems with simple table design 31 Programming Logic & Design, Sixth Edition

32 R ECOGNIZING P OOR T ABLE D ESIGN ( CONTINUED ) Figure 14-10 Students table before normalization 32 Programming Logic & Design, Sixth Edition

33 U NDERSTANDING A NOMALIES, N ORMAL F ORMS, AND N ORMALIZATION Normalization Helps you reduce data redundancies and anomalies Types of anomalies Update Delete Insert 33 Programming Logic & Design, Sixth Edition

34 U NDERSTANDING A NOMALIES, N ORMAL F ORMS, AND N ORMALIZATION ( CONTINUED ) Three normal forms First normal form 1NF Second normal form 2NF Third normal form 3NF Each normal form is structurally better than the one preceding 34 Programming Logic & Design, Sixth Edition

35 F IRST N ORMAL F ORM Unnormalized Table that contains repeating groups 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 combined key of studentId and class 35 Programming Logic & Design, Sixth Edition

36 F IRST N ORMAL F ORM ( CONTINUED ) Figure 14-11 Students table in 1NF 36 Programming Logic & Design, Sixth Edition

37 F IRST N ORMAL F ORM ( CONTINUED ) Atomic attributes Small as possible, containing an undividable piece of data 37 Programming Logic & Design, Sixth Edition

38 S ECOND N ORMAL F ORM 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 38 Programming Logic & Design, Sixth Edition

39 S ECOND N ORMAL F ORM ( CONTINUED ) Figure 14-12 Students table in 2NF 39 Programming Logic & Design, Sixth Edition

40 S ECOND N ORMAL F ORM ( CONTINUED ) 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 40 Programming Logic & Design, Sixth Edition

41 T HIRD N ORMAL F ORM Table must be in 2NF, and it has no transitive dependencies Transitive dependency 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 41 Programming Logic & Design, Sixth Edition

42 T HIRD N ORMAL F ORM ( CONTINUED ) Figure 14-13 The complete Students database 42 Programming Logic & Design, Sixth Edition

43 D ATABASE P ERFORMANCE AND S ECURITY I SSUES Major issues Providing data integrity Recovering lost data Avoiding concurrent update problems Providing authentication and permissions Providing encryption 43 Programming Logic & Design, Sixth Edition

44 P ROVIDING D ATA I NTEGRITY Data integrity Set of rules that makes the data accurate and consistent Can enforce integrity between tables 44 Programming Logic & Design, Sixth Edition

45 R ECOVERING L OST D ATA Organization’s data can be destroyed in many ways Recovery 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 45 Programming Logic & Design, Sixth Edition

46 A VOIDING C ONCURRENT U PDATE P ROBLEMS Concurrent update Problem occurs when two database users need to modify the same record at the same time Lock 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 to the database all at once, or in a batch 46 Programming Logic & Design, Sixth Edition

47 P ROVIDING A UTHENTICATION AND P ERMISSIONS 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 47 Programming Logic & Design, Sixth Edition

48 P ROVIDING E NCRYPTION Encryption Process of coding data into a format that human beings cannot read Only authorized users see the data in a readable format 48 Programming Logic & Design, Sixth Edition

49 S UMMARY Database holds a group of files that an organization needs to support its applications Create tables Identify primary key Database operations Sort, add, edit, delete, query Table relationships One-to-many, many-to-many, one-to-one Normalization Database issues 49 Programming Logic & Design, Sixth Edition


Download ppt "P ROGRAMMING L OGIC AND D ESIGN S IXTH E DITION Chapter 14 Using Relational Databases."

Similar presentations


Ads by Google