Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Relational Databases.

Similar presentations


Presentation on theme: "Chapter 4 Relational Databases."— Presentation transcript:

1 Chapter 4 Relational Databases

2 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
Learning Objectives Explain the importance and advantages of databases Describe the difference between database systems and file-based legacy systems. Explain the difference between logical and physical views of a database. Explain fundamental concepts of database systems such as DBMS, schemas, the data dictionary, and DBMS languages. Describe what a relational database is and how it organizes data. Create a set of well-structured tables to store data in a relational database. Perform simple queries using the Microsoft Access database.

3 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
Data Hierarchy Field Attributes about an entity Record Related group of fields File Related group of records Database Related group of files

4 Data Redundancy / Independence
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Data Redundancy / Independence File Approach Database Approach A, B, C, D Sales A, B, C, D, E, F, G A, C, E, F Shipping Database Management System A, D, E, G Billing Sales Shipping Billing

5 Advantages of Database Systems
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Advantages of Database Systems Data Integration Files are logically combined and made accessible to various systems. Data Sharing With data in one place it is more easily accessed by authorized users. Minimizing Data Redundancy & Data Inconsistency Eliminates Reduces the same data being stored in multiple files, thus reducing inconsistency in multiple versions of the same data. Data Independence Data is separate from the programs that access it. Changes can be made to the data without necessitating a change in the programs and vice versa. Cross-Functional Analysis Relationships between data from various organizational departments can be more easily combined.

6 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
Database Terminology Database Management System (DBMS) Interface between software applications and the data in files. Database Administrator (DBA) Person responsible for maintaining the database Data Dictionary Information about the structure of the database Field names, descriptions, uses

7 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
Logical Vs. Physical Physical View Depends on explicitly knowing: How is the data actually arranged in a file Where is the data stored on the computer Logical View Conceptual Organization of Data Separates storage of data from use of the data Unnecessary to explicitly know how and where data is stored.

8 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
Schemas Describe the logical structure of a database Conceptual Level Organization wide view of the data External Level Individual Users view of the data Each view is a subschema Internal Level Describes how data are stored and accessed Description of: Records, Definitions, Addresses & Indexes

9 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
DBMS Languages Data Definition Language (DDL) Builds the data dictionary Creates the database Describes the subschema Specifies record and/or field security constraints Data Manipulation Language (DML) Changes the content in the database Updates, Insertions and Deletions Data Query Language (DQL) Enable the retrieval, sorting, & display of data from the database

10 Types of Database Structures
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Types of Database Structures Hierarchical Data is organized in a tree like structure Each Child has only 1 parent, but each parent can have many children Network allows each record to have multiple parent and child records Object-Oriented information is represented in the form of objects database and applications are not independent Associated Relational

11 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
Relational Database Relational Data model represents the conceptual and external level schemas as if data are stored in tables Table Each Row, a Tuple, contains data about one instance of an entity This is equivalent to a Record Each Column, contains data about one attribute of an entity This is equivalent to a Field

12 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
A Relational Table Row (Record) Column (Field) Each row contains multiple attributes describing an instance of the entity. In this case inventory. Same type of data

13 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
Attributes Primary Key An attribute or combination of attributes that can be used to Uniquely identify a specific row(record) in a table. Foreign Key An attribute in one table that is a Primary key in another table. Used to link the two tables.

14 Each row is called a tuple, which rhymes with “couple.”

15 Each row contains data about a specific occurrence of the type of entity in the table.

16 Each column in a table contains information about a specific attribute of the entity.

17 A primary key is the attribute or combination of attributes that uniquely identifies a specific row in a table.

18 In some tables, two or more attributes may be joined to form the primary key.

19 STUDENTS Student ID Last Name First Name Phone No. Advisor No. Simpson Alice 1418 Sanders Ned Moore Artie 1503 ADVISORS Advisor No. Last Name First Name Office No. 1418 Howard Glen 420 1419 Melton Amy 316 1503 Zhang Xi 202 1506 Radowski J.D. 203 A foreign key is an attribute in one table that is a primary key in another table.

20 Foreign keys are used to link tables together.
STUDENTS Student ID Last Name First Name Phone No. Advisor No. Simpson Alice 1418 Sanders Ned Moore Artie 1503 ADVISORS Advisor No. Last Name First Name Office No. 1418 Howard Glen 420 1419 Melton Amy 316 1503 Zhang Xi 202 1506 Radowski J.D. 203 Foreign keys are used to link tables together.

21 STUDENTS Student ID Last Name First Name Phone No. Advisor No. Simpson Alice 1418 Sanders Ned Moore Artie 1503 ADVISORS Advisor No. Last Name First Name Office No. 1418 Howard Glen 420 1419 Melton Amy 316 1503 Zhang Xi 202 1506 Radowski J.D. 203 Other non-key attributes in each table store important information about the entity.

22 Database Design Errors
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Database Design Errors If the database is not designed properly data errors can occur: Update Anomaly Changes to existing data are not correctly recorded Due to multiple records with the same data attributes Insert Anomaly Unable to add a record to the database Delete Anomaly Removing a record also removes unintended data from the database

23 In the above, simplified example, a number of problems arise.
Student ID Last Name First Name Phone No. Course No. Section Day Time Simpson Alice ACCT-3603 1 M 9:00 AM FIN-3213 3 Th 11:00 AM MGMT-3021 11 12:00 PM Sanders Ned ACCT-3433 2 T 10:00 AM 5 W 8:00 AM ANSI-1422 7 F Moore Artie In the above, simplified example, a number of problems arise.

24 This problem is referred to as an update anomaly.
Student ID Last Name First Name Phone No. Course No. Section Day Time Simpson Alice ACCT-3603 1 M 9:00 AM FIN-3213 3 Th 11:00 AM MGMT-3021 11 12:00 PM Sanders Ned ACCT-3433 2 T 10:00 AM 5 W 8:00 AM ANSI-1422 7 F Moore Artie Suppose Alice Simpson changes her phone number. You need to make the change in three places. If you fail to change it in all three places or change it incorrectly in one place, then the records for Alice will be inconsistent. This problem is referred to as an update anomaly.

25 This problem is referred to as an insert anomaly.
Student ID Last Name First Name Phone No. Course No. Section Day Time Simpson Alice ACCT-3603 1 M 9:00 AM FIN-3213 3 Th 11:00 AM MGMT-3021 11 12:00 PM Sanders Ned ACCT-3433 2 T 10:00 AM 5 W 8:00 AM ANSI-1422 7 F Moore Artie What happens if you have a new student to add, but he hasn’t signed up for any courses yet? Or what if there is a new class to add, but there are no students enrolled in it yet? In either case, the record will be partially blank. This problem is referred to as an insert anomaly.

26 This problem is referred to as a delete anomaly.
Student ID Last Name First Name Phone No. Course No. Section Day Time Simpson Alice ACCT-3603 1 M 9:00 AM FIN-3213 3 Th 11:00 AM MGMT-3021 11 12:00 PM Sanders Ned ACCT-3433 2 T 10:00 AM 5 W 8:00 AM ANSI-1422 7 F Moore Artie If Ned withdraws from all his classes and you eliminate all three of his rows from the table, then you will no longer have a record of Ned. If Ned is planning to take classes next semester, then you probably didn’t really want to delete all records of him. This problem is referred to as a delete anomaly.

27 This approach is also fraught with problems:
Student ID Last Name First Name Phone No. Class 1 Class 2 Class 3 Class 4 Simpson Alice ACCT-3603 FIN-3213 MGMT-3021 Sanders Ned ACCT-3433 ANSI-1422 Moore Artie This approach is also fraught with problems: How many classes should you allow in building the table? The above table is quite simplified. In reality, you might need to allow for 20 or more classes (assuming a student could take many 1-hour classes). Also, more information than just the course number would be stored for each class. There would be a great deal of wasted space for all the students taking fewer than the maximum possible number of classes. Also, if you wanted a list of every student taking MGMT-3021, notice that you would have to search multiple attributes.

28 The solution to the preceding problems is to use a set of tables in a relational database.
Each entity is stored in a separate table, and separate tables or foreign keys can be used to link the entities together.

29 Note that within each table, there are no duplicate primary keys and no null primary keys.
Consistent with the entity integrity rule.

30 STUDENTS Student ID Last Name First Name Phone No. Advisor No. Simpson Alice 1418 Sanders Ned Moore Artie 1503 ADVISORS Advisor No. Last Name First Name Office No. 1418 Howard Glen 420 1419 Melton Amy 316 1503 Zhang Xi 202 1506 Radowski J.D. 203 Advisor No. is a foreign key in the STUDENTS table. Every incident of Advisor No. in the STUDENTS table either matches an instance of the primary key in the ADVISORS table or is null.

31 Add a student here. Leaves no blank spaces. Add a course here. Leaves no blank spaces. When a particular student enrolls for a particular course, add that info here.

32 Ned still exists in the student table.
Even if Ned was the only student in the class, ACCT-3603 still exists in the course table. If Ned Sanders drops ACCT-3603, remove Ned’s class from this table.

33 Design Requirements for Relational Database
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Design Requirements for Relational Database Every column must be single valued. Primary keys must contain data (Not Null). Entity Integrity Foreign Keys must contain the same data as the primary key in another table. Referential Integrity All other attributes must identify a characteristic of the table identified by the primary key.

34 Normalizing Relational Databases
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Normalizing Relational Databases Initially one table is used for all the data in a database Following rules, the table is decomposed into multiple tables related by: Primary Key – Foreign key integration Decomposed set of tables are in third normal form (3NF) First Normal Form: A relation is in first normal form if the domain of each attribute contains only atomic values, and the value of each attribute contains only a single value from that domain. Second Normal Form: No non-prime attribute in the table is functionally dependent on a proper subset of any candidate key Third Normal Form: Every non-prime attribute is non-transitively dependent on every candidate key in the table. The attributes that do not contribute to the description of the primary key are removed from the table. In other words, no transitive dependency is allowed.

35 Semantic Data Modeling
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Semantic Data Modeling Using Knowledge of Business Processes Diagram is created of the Database System Entity-Relationship Diagram or E-R Diagram In accounting we use a special form: Resource, Event, Agent or REA diagram Diagram becomes Blue-print of Database structure Database designed this way must still follow rules of a Normalized Database to eliminate design errors

36 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall
Creating Queries

37 Microsoft Access Query#1
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Microsoft Access Query#1

38 Microsoft Access Query#2
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Microsoft Access Query#2

39 Microsoft Access Query#3
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Microsoft Access Query#3

40 Microsoft Access Query#4
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Microsoft Access Query#4

41 Microsoft Access Query#5
Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall Microsoft Access Query#5


Download ppt "Chapter 4 Relational Databases."

Similar presentations


Ads by Google