Download presentation
Presentation is loading. Please wait.
Published byReynold Harrell Modified over 9 years ago
1
CHAPTER 4 Relational Databases
2
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. Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-2
3
Data Hierarchy Field Attributes about an entity Record Related group of fields File Related group of records Database Related group of files Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-3
4
Data Redundancy / Independence File ApproachDatabase Approach Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-4 A, B, C, D Sales A, C, E, F Shipping A, D, E, G Billing A, B, C, D, E, F, G Database Management System Database Management System Sales Shipping Billing
5
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 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 combines. Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-5
6
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 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-6
7
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. Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-7
8
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 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-8
9
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 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-9
10
Types of Database Structures Hierarchical Network Object-Oriented Associated … Relational Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-10
11
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 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-11
12
A Relational Table Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-12 Row (Record) Column (Field) Each row contains multiple attributes describing an instance of the entity. In this case inventory. Same type of data
13
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. Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-13
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
ADVISORS Advisor No.Last NameFirst NameOffice No. 1418HowardGlen420 1419MeltonAmy316 1503ZhangXi202 1506RadowskiJ.D.203 STUDENTS Student IDLast Name First NamePhone No. Advisor No. 333-33-3333SimpsonAlice333-33331418 111-11-1111SandersNed444-44441418 123-45-6789MooreArtie555-55551503 A foreign key is an attribute in one table that is a primary key in another table.
20
ADVISORS Advisor No.Last NameFirst NameOffice No. 1418HowardGlen420 1419MeltonAmy316 1503ZhangXi202 1506RadowskiJ.D.203 Foreign keys are used to link tables together. STUDENTS Student IDLast Name First NamePhone No. Advisor No. 333-33-3333SimpsonAlice333-33331418 111-11-1111SandersNed444-44441418 123-45-6789MooreArtie555-55551503
21
ADVISORS Advisor No.Last NameFirst NameOffice No. 1418HowardGlen420 1419MeltonAmy316 1503ZhangXi202 1506RadowskiJ.D.203 Other non-key attributes in each table store important information about the entity. STUDENTS Student IDLast Name First NamePhone No. Advisor No. 333-33-3333SimpsonAlice333-33331418 111-11-1111SandersNed444-44441418 123-45-6789MooreArtie555-55551503
22
Database Design Errors If 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 records to the database Delete Anomaly Removing a record also removes unintended data from the database Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-22
23
Student ID Last Name First NamePhone No.Course No.SectionDayTime 333-33-3333SimpsonAlice333-3333ACCT-36031M9:00 AM 333-33-3333SimpsonAlice333-3333FIN-32133Th11:00 AM 333-33-3333SimpsonAlice333-3333MGMT-302111Th12:00 PM 111-11-1111SandersNed444-4444ACCT-34332T10:00 AM 111-11-1111SandersNed444-4444MGMT-30215W8:00 AM 111-11-1111SandersNed444-4444ANSI-14227F9:00 AM 123-45-6789MooreArtie555-5555ACCT-34332T10:00 AM 123-45-6789MooreArtie555-5555FIN-32133Th11:00 AM In the above, simplified example, a number of problems arise.
24
Student ID Last Name First NamePhone No.Course No.SectionDayTime 333-33-3333SimpsonAlice333-3333ACCT-36031M9:00 AM 333-33-3333SimpsonAlice333-3333FIN-32133Th11:00 AM 333-33-3333SimpsonAlice333-3333MGMT-302111Th12:00 PM 111-11-1111SandersNed444-4444ACCT-34332T10:00 AM 111-11-1111SandersNed444-4444MGMT-30215W8:00 AM 111-11-1111SandersNed444-4444ANSI-14227F9:00 AM 123-45-6789MooreArtie555-5555ACCT-34332T10:00 AM 123-45-6789MooreArtie555-5555FIN-32133Th11:00 AM 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
Student ID Last Name First NamePhone No.Course No.SectionDayTime 333-33-3333SimpsonAlice333-3333ACCT-36031M9:00 AM 333-33-3333SimpsonAlice333-3333FIN-32133Th11:00 AM 333-33-3333SimpsonAlice333-3333MGMT-302111Th12:00 PM 111-11-1111SandersNed444-4444ACCT-34332T10:00 AM 111-11-1111SandersNed444-4444MGMT-30215W8:00 AM 111-11-1111SandersNed444-4444ANSI-14227F9:00 AM 123-45-6789MooreArtie555-5555ACCT-34332T10:00 AM 123-45-6789MooreArtie555-5555FIN-32133Th11:00 AM 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
Student ID Last Name First NamePhone No.Course No.SectionDayTime 333-33-3333SimpsonAlice333-3333ACCT-36031M9:00 AM 333-33-3333SimpsonAlice333-3333FIN-32133Th11:00 AM 333-33-3333SimpsonAlice333-3333MGMT-302111Th12:00 PM 111-11-1111SandersNed444-4444ACCT-34332T10:00 AM 111-11-1111SandersNed444-4444MGMT-30215W8:00 AM 111-11-1111SandersNed444-4444ANSI-14227F9:00 AM 123-45-6789MooreArtie555-5555ACCT-34332T10:00 AM 123-45-6789MooreArtie555-5555FIN-32133Th11:00 AM 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: –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. Student ID Last Name First Name Phone No.Class 1Class 2Class 3Class 4 333-33-3333SimpsonAlice333-3333ACCT-3603FIN-3213MGMT-3021 111-11-1111SandersNed444-4444ACCT-3433MGMT-3021ANSI-1422 123-45-6789MooreArtie555-5555ACCT-3433FIN-3213
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
Design Requirements for Relational Database 1. Every column must be single valued. 2. Primary keys must contain data (Not Null). Entity Integrity 3. Foreign Keys must contain the same data as the primary key in another table. Referential Integrity 4. All other attributes must identify a characteristic of the table identified by the primary key. Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-29
30
Note that within each table, there are no duplicate primary keys and no null primary keys. Consistent with the entity integrity rule.
31
ADVISORS Advisor No.Last NameFirst NameOffice No. 1418HowardGlen420 1419MeltonAmy316 1503ZhangXi202 1506RadowskiJ.D.203 STUDENTS Student IDLast Name First NamePhone No. Advisor No. 333-33-3333SimpsonAlice333-33331418 111-11-1111SandersNed444-44441418 123-45-6789MooreArtie555-55551503 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.
32
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.
33
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.
34
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)normal form Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-34
35
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 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-35
36
CREATING QUERIES Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-36
37
Microsoft Access Query#1 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-37
38
Microsoft Access Query#2 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-38
39
Microsoft Access Query#3 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-39
40
Microsoft Access Query#4 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-40
41
Microsoft Access Query#5 Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-41
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.