Download presentation
Presentation is loading. Please wait.
Published byYanti Liani Budiman Modified over 6 years ago
1
Navya Thum navya.thum@mail.wvu.edu March 04, 2013
Microsoft ACCESS Navya Thum March 04, 2013
2
Attendance Go to to record attendance. Enter your MyID username and password. 11/18/2018
3
Databases & Relationships
Now that we know what a table is, let’s talk again about databases … A Database is a collection of tables If Tables are related, it is called a “Relational Database” A Common Field between tables is what allows a relationship to exist. This works by way of “Primary” and “Foreign” keys… 11/18/2018
4
Primary Keys - revisited
Primary Key – Field in every table whose field values are unique for every record Examples might be things like: Student ID numbers Cell Phone numbers Anything that will not have duplicates as additional records are entered 11/18/2018
5
Relationships Foreign Key – Similar field in a related table
Primary key field in relationship will have similar data type and content to foreign key field The two tables are joined together on the primary and foreign keys and form a Common Field. 11/18/2018
6
Relationships The link formed through this Common Field which is dubbed in one case as a “primary key”, and in another as a “foreign key”, allows tables to share data Note that the “Look Ahead” database has 3 tables, but we will only look at 2 today… 11/18/2018
7
1: Employees Table… EmployeeID is the Primary Key as all numbers are unique for employee records 11/18/2018
8
This will tie back to the Employees table.
2: Locations Table… LocationID is the primary key and each record has a unique LocationID designator. This will tie back to the Employees table. 11/18/2018
9
Relationships Databases are all about efficiency and not having to store the same information more than once if multiple records can use it In this example, we know that multiple people work at the same office address. One address can have many related Employees (One to Many)… 11/18/2018
10
Relationships Instead of typing the same address in each record for every Atlanta office employee: “450 Peachtree Road…” We can assign a simple relationship to a “Locations” table and reference this information to a given location with a short common code of “L01” between the tables… 11/18/2018
11
One to Many Relationship
(One instance here) (Many here) Typing “L01” when entering an employee and having it equate to the entire address saves MANY characters in databases as it is reused over and over. “L01” is 3 characters and the entire address is over 30 ! 11/18/2018
12
Summing up what was just mentioned
Each table has a primary key that has a unique field value In one of the tables, there is a foreign key In Employees table Has the “Employee ID” as the primary key Locations table Has the “Location ID” as the primary key That “Location ID” ties back to a “Location ID” field in the Employees table (where it is considered the Foreign key) What we need to do now is to join them 11/18/2018
13
Making the Relationship
These tables need to be tied together (related) to work this way… Open the Relationships Window through: Database Tools ribbon > Relationships Right click blank space and select “Show Table” Double click “Employees” & “Locations” tables to be able to establish the relationship between them. 11/18/2018
14
The Relationships Window
Click the “Close” button The Relationship window allows us to see structures representing each added table Within the structures, we see the fields that each table has Primary keys are shown with a key icon … 11/18/2018
15
Making the Relationships
Drag “LocationID” from the “Locations” table to “LocationID” in the “Employees” table. Notice the “One-To-Many” reference Click to Enforce Referential Integrity Click “Create”… 11/18/2018
16
Relationship Formed (Relationship Primary Key)
Note the One to Many (1 to infinity symbol) that formed on the join line… Close and save the relationship Run the “Employee Information” query to see data pulled together from both tables… (Relationship Foreign Key) Note, the relationship we made was already created in the query we ran and we did this only for increased understanding of how to create relationships. 11/18/2018
17
Query Results Information is pulled from the “Employee” and “Locations” tables through their relationship and shown united within the query results to give us a complete picture of the information ! The LocationID field was used behind the scenes to pull it all together. Everything is stored efficiently and we are able to see everything we need together all at once. 11/18/2018
18
Access Chapter 2 Doing it from Scratch
Creating a Database Creating Tables Creating Fields Field Properties 11/18/2018
19
Premise We are going to create a school database to keep information on students and programs at WVU Table Creation Fields, Data types, other attributes Establishing Relationships Entering Records 11/18/2018
20
Building the Framework
Open Access Select “Blank Database” For “File Name”, Call it lastname_firstname_accessexample4.accdb Save to Desktop (This is important) Click “Create” Set the Objects bar to “Object Type” to see “All Access Objects” * Note that nothing exists in the objects area yet 11/18/2018
21
Designing Databases When designing a database, try to think of all the fields of data that need to be stored Group the fields into tables Each table will contain a group of fields Select a field in each table as a primary key where each field value will be unique … 11/18/2018
22
Designing Databases We want to break out information that may be redundant and include it in separate tables… When tables will be related to one another, you need to include a common field in each of the tables that will be used to form the relationship Primary key in one table Foreign key in the other table 11/18/2018
23
Planning We need to consider what types of information our database will hold Assuming that we wish to keep records on students, we will want to have the following information: First and Last names, address, phone, major, and student number… 11/18/2018
24
Drawing it all out To be versatile, we will want to make multiple fields for name and address elements to split the information up: FirstName LastName StAddress City State Zip … breaking things up helps us in searching later on 11/18/2018
25
Drawing it all out We will also have fields for these items:
StudentNum, PhoneNum Major, will be used over and over One major to Many people This screams “One to Many relationship” and we can make a separate table for the majors with a common field of “MajorID” Second table fields: MajorID and MajorName 11/18/2018
26
Assigning Field “Data Types”
Data Types specify types of field values you can enter for the field, how they can be used, and what other properties the field will have They essentially control what kind of information may be entered as field values for a given field Let’s look at what kinds are available… 11/18/2018
27
Access Field Types … 11/18/2018
28
Access Field Types 11/18/2018
29
Naming Fields Fields and Tables need to be named
It is good to pick meaningful names Names can be up to 64 characters Names need to be unique Can contain letters, numbers, and some special characters Spaces generally not used Can put capitol letters at the start of words to show separation. ex: TwoWords 11/18/2018
30
Field Sizes Access has default field sizes that can be overwritten
Setting a Field Size tells Access how much space to allocate for fields It should be only as large as the maximum-size piece of data needing to be held Making it too large will waste disk space as it is consumed whether filled or not. 11/18/2018
31
Field Captions The Caption Property allows us to customize the way a field name will show up in a report or form. If we do not specify one, the regular field name will be used Because we do not use spaces in actual field names, they may not be easy to understand in reports. The caption lets us make names appear more like regular English words if we choose 11/18/2018
32
Creating the First Table
With “Table1” selected in the Objects bar, Click the ruler to enter “Design View” Name the table StudentInfo and click OK We now see the following… 11/18/2018
33
New Table in Design View
Note that a primary key field is already created We can change the name of this field and add others… 11/18/2018
34
Making the Fields Create the following Fields:
Replace “ID” with StudentNum of Data Type Number FirstName of type Text LastName of type Text PhoneNum of type Text … 11/18/2018
35
Making the Fields MajorID of type Number StAddress of type Text
City of type Text State of type Text Zip of type Text … 11/18/2018
36
Making the Fields Change Text Field Sizes to be “20”
Put descriptions on StudentNum & MajorID Students ID Number Foreign Key to related table Put captions in fields that have abbreviated names 11/18/2018
37
Making the Majors Table
We said that we would like to create a separate table to have the majors listed as many students will have the same major and this will save a lot of space Close the StudentInfo table & Save the changes Select the Create Ribbon Choose Table 11/18/2018
38
Majors Table * The first field will be MajorID and the data type needs to be Number to match the MajorID field in the first table that we will relate it to Go to Design View and name it Majors Create the MajorID field of type Number Description: Primary Key related to StudentInfo table Create the MajorName field of type Text … 11/18/2018
39
Majors Table Completed
Verify MajorID is the Primary Key Flip to Datasheet view, saving changes, and enter the following records in this table… 11/18/2018
40
Entering Data in Majors Table
Close the table 11/18/2018
41
Enter Records in StudentInfo Table
Open the StudentInfo table in datasheet view Create 4 student records and enter all data for them (make up names and such) Make 2 of them MajorID 2 Make 1 of them MajorID 1 Make 1 of them Major ID 3… Close The Table! 11/18/2018
42
Reasons to Relate Establishing relationships between tables can:
Eliminate redundancy Waste Less Space Reduce data errors / Inconsistencies in duplication… 11/18/2018
43
Including a Common Field
When one table needs to be related to another table, we include a common field The common field will be the primary key in one table and foreign key in the related table In our example the MajorID fields are common Primary in Majors table and Foreign in StudentInfo … 11/18/2018
44
Let’s Relate Our Tables!
Make sure both tables are closed Always close tables when working on them directly, otherwise you may get errors when things are in use Database Tools ribbon Select Relationships Double-click each table name to add them … 11/18/2018
45
Creating the Relationship
Drag MajorID from Majors table to MajorID in the StudentInfo Table Enforce Referential Integrity Click Create Close the Relationships window & save Close Access 11/18/2018
46
Making a Multi-table Query
Create ribbon > Other group Query Design Add both tables and close “Show Table” Expand the StudentInfo table to see all fields * Notice the relationship showing 11/18/2018
47
What will happen?! We want the query to show the student number, first and last names, phone number, full address and name of their major (not number) We will not use the MajorID field because remember that we don’t want to see the number The relationship that we established already will use the common field to get the MajorName for us by it using the MajorID number! 11/18/2018
48
Add Fields to the Query Double click to add the fields in the order we want them to be displayed StudentInfo Table StudentNum, FirstName, LastName, PhoneNum, StAddress, City, State, Zip Majors Table MajorName 11/18/2018
49
Run the Query! Click the ! Up top to run the query
Notice that the information was pulled together out of both tables to be able to get you the words for the MajorName and all the other data together 11/18/2018
50
Keep the query to use again
Close the Query Save the query as StudentQuery OK “Queries” is added to the Objects Bar In StudentInfo add a 5th student Close the StudentInfo table Rerun the Query 11/18/2018
51
In-Class EXTRA CREDIT ! ! ! Close Access & Go to the CS101 Web site
Use Homework Submission Tool Browse and upload for 2 points of credit. Submissions NOT ACCEPTED outside of class! >> Extra credit exercises are to be done in class. Those submitted out of class will be considered violating the academic dishonesty agreement. 11/18/2018
52
For next class, 03.18.2013 – MyITLab Lesson C due
Walk through today’s slides to better understand the “Relationships” concept. – MyITLab Lesson C due – Homework 4 is due HW4 is posted now on the assignments page. 11/18/2018
53
Questions? 11/18/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.