Pre-Lab Database Activity Tutorial: Creating and Linking Tables in Microsoft Access Must be completed by Tuesday April 5 at 11:00pm by students enrolled in Wednesday lab sessions Must be completed by Thursday April 7 at 11:00pm by students enrolled in Friday lab sessions Must be completed by Sunday April 10 at 11:00pm by students enrolled in Monday lab sessions FOLDER/FILE SUBMISSION TO THE APPROPRIATE KSU ONLINE Canvas Submission Location MUST BE COMPLETED BY THE DATE AND TIME LISTED ABOVE. If you meet the folder/file submission deadline listed above, you can hand in your packet of printouts at your regularly scheduled lab session the next day. Before you start, PLEASE print out this entire tutorial. Having a printed copy of the tutorial in front of you results in the activity taking less time than completing the activity while constantly switching back and forth between the instructions and the application you are working with. You will get finished with this activity quite a bit faster if you have the printed activity description in front of you. Prior to starting this activity, you need to visit the MANGT 366 course on KSU Online. After logging in, visit the MANGT 366 course on KSU Online. After logging in, visit Files and then locate the file called “Access Prelab Database.accdb” Download that file to your storage media. Rename the file so that its new name contains your own first and last name, followed by Database Prelab. For instance, my file would be called Brian Kovar Database Prelab Or (Brian Kovar Database Prelab.accdb) Database files created by Microsoft Access follow the Relational Database model. In the relational database model, data is stored in entity classes (tables) and these entity classes (tables) are linked together by a common field that connects the tables. Each table (entity class) stores information about related concepts. For instance, in your upcoming database project, you are storing data about a health club that you own. The database file that you will create will contain the following tables: an employee table, a membership table and a courses table. While you can just start by typing information into a table, that is actually not the way to create a database. Why?? If you just start typing in something, what you will end up with may not be what you need or what you expect. Instead, you need to design your table(s) first and then type in the data afterwards. Therefore, the first step in creating a database table is to create the structure, or skeleton, of the table first. This is just like creating the framework for a house. This structure, or skeleton, is also called the data dictionary. One of the first things that we do when we create the data dictionary is that we specify the attributes of the data. The attributes of the data are database fields and the characteristics of those fields. When we specify the characteristics of the fields, there are many things that we can specify. Three that I will focus on here are the primary key, input masks and field sizes. The primary key is a field that uniquely describes each and every record in a table. By saying that the field is unique, that means that no two records can have the same primary key. Good examples of primary keys include id number, part number, driver’s license number, student id number, eID, WID: no one else or nothing else can have that same id number. 1 Bad examples of primary keys include last name, first name, birthdate, phone number, zip code, etc. Why are those bad examples? Someone else could have the same last name as another person or they could have the same first name as another person. The same thing applies to birthdate, zip code or even the phone number. If someone can share that value/item/thing with another person, then it is not unique and can’t serve as a unique identifier. The primary key is also used to sort, or index, the database. If the primary key is a word, the records are sorted/arranged based upon that word, in alphabetical order. If the primary key is a number, the records are sorted/arranged based upon that number, from low number to high number order. Another field characteristic that you can specify is that you can create input masks. Input masks allow you to simplify data entry for fields that have a common format. ID numbers tend to have dashes in them (123-456789). Dates have slashes in them (12/25/2015). Phone numbers use the parenthesis and dashes (785)123-4567. Rather than always typing in the symbol, an input mask can be created so that the user simply types in the number and the computer puts the appropriate symbol in. When designing the table, the database designer simply creates a standard format (a mask) for a field. Although there are a number of other field characteristics that can be specified, the last one that will be mentioned here is the field size. The field size is an allocation of storage space, either in the memory of the computer or on a storage media. The default field size in Access is 256 characters. Do you really need 256 characters to store a person’s age? No, you will need 3 characters at the most. In fact, most people’s ages will be expressed as two characters. Anything more than 3 characters is a waste of space. When storing data, you should always consider field size, selecting the most appropriate field size given the data being stored. Now, after looking at some of the basics, it is now time to begin working with Access. Open the database file that you downloaded for this activity. Upon opening an Access file, you see a screen that is a little different than other software that you typically might work with. The Home tab contains some of the basic tools that we will use later on as part of our database activities. The Create tab is used to create various objects found in your database, including tables, queries, forms and reports. This activity deals with creating tables while your upcoming lab sessions will focus on creating queries, forms and reports. 2 Notice that the file currently contains two tables. For now, we will ignore the table called “Practice Sorting Table.” The sole reason that table is included in this file is that we will use the data contained in that table to practice sorting in Access later on in this activity, toward the end. Right now, just pretend that table does not even exist. We will use the Jobs table. Double click the Jobs table to open it. The table contains two fields, Job and Description. The table contains three records. Job 10 is Selling something. Job 20 is Making something. Job 30 is Cleaning something. We have jobs, now we just need some employees to do those jobs. For that, we need to create a new table (new entity class). As is seen in the picture above, close the Jobs Table using the X on the right-hand side of the screen. Once the Jobs table is closed, click on the Create tab. When creating a new table, the user has to decide whether to click on the Table option or the Table Design option. Clicking the Table option lets you just start typing in stuff while clicking on the Table Design option allows you to specify fields and field characteristics prior to data being typed into the table. When creating a new table, ALWAYS select the Table Design option. Click Table Design and the design screen should appear (as pictured). For each field that you want to include in your table, you will specify the name of the field (field name) and also the data type of the field. Available data types include: Short Text, which replaced the old Text data type, holds any type of character, letter, number or special symbol Long Text, which replaces the old Memo data type, allows data to be stored in a sentence or multiple sentences. Number, which stores data used in calculations Date/Time, which allows data to be stored as a date or time Currency AutoNumber, which generates a unique number for each record Yes/No, which allows for two options (Yes/No), (True/False), (Male/Female), etc. And there are other data types as seen 3 When specifying the data type, you want to consider how the piece of data will be used. You would not store a phone number such as (785)123-4567 as number because you would not use a phone number in a numerical calculation. You would store it as short text. Some sort of ID number would also be stored as short text as well, for the same reason. The third characteristic of each field that you might want to include is the Description, which indicates what an abbreviation for the field name might actually represent. For example, a field name of SSN might stand for Social Security Number while HP or WP might stand for Home Phone or Work Phone. In the workplace, providing a description of the data would be a form of documenting your work (for others who might use or modify your database). To move from one area of this screen to another, please use the tab key to advance your cursor. To go backwards, use shift tab. Using the picture seen to the right, please type in each Field Name and specify the relevant data type. The third column (Description) will be left empty. One item should be mentioned regarding the Job field and the data type of Number that we just assigned to that field. To be honest, a data type of number is not a good data type for this particular field because we are not going to use the job number in any sort of numeric calculation. However, in just a short time, we are going to link/join the Job table that we looked at previously with this new table, and in order to make this link (relationship) between the two tables, we have to make it based upon a common field found in both tables. In addition to the field having the same name, it also needs to have the same data and the same data type. Since the Job field is numeric in the Jobs Table, and since it is the field that is common in both tables, it has to be exactly the same in this new table as it is in the Jobs Table that already exists (or we have to change the Job field in the Jobs table to match this new field, which is more work than we want to do now). That is why the Job field has a Number data type in this table as well. Before moving on, the unique identifier (primary key) for records in this table should be specified. Let’s look at each of the fields and determine if that particular field would be a good primary key. Remember that this table will hold employee data. Job would not be a good primary key because several employees might have the same job number. Phone number would not be a good primary key because several employees might have the same phone number, perhaps a shared phone. Birthdate would not be a good primary key because several employees might have the same birthdate, just as some of your friends might have the same birthdate as you do. Several employees might share the same first name so that is not unique. Several employees might share the same last name so that is not unique either. ID Numbers are unique so that field should be our primary key. To specify that ID Number should be the primary key, click in the middle of the letters that make up the wording of the ID Number field name. The ID Number field is now selected. 4 Click the Primary Key button found on the Quick Access toolbar (see picture) and a “key” symbol should appear in the gray box to the left of the ID Number field. You can also accomplish this by right-clicking the field and selecting the Primary Key option as well. Next, click the Save Button. When asked to specify the name of the table, type Employee Table. You have now created the employees table (employees entity class). Your currently selected field should be the ID Number field. Please look toward the bottom of the design screen that you are currently using. For each field, you can specify additional properties, including: Field Size (see earlier discussion regarding field sizes) Input Mask (see earlier discussion regarding input masks) Default Value (what shows up as the initial value of a field prior to it being changed) Validation Rule. Validation rules are also called Integrity Constraints. Validation rules/integrity constraints are rules designed to ensure the validity of data being entered. Examples of validation rules include: o Setting up a minimum/floor values for a field (can’t be less than the minimum value). o Setting up a maximum value for a field (can’t go above the maximum value). o Creating a rule that indicates a birthdate can’t be in the future. Someone can be born right now (or this exact instant), but someone can’t be born in the future. o Saying that a student can’t enroll in a class if they haven’t met the prerequisites for the class. o Saying that a worker can’t be assigned to a job that doesn’t exist. o Setting up a primary key also sets up a validation rule (since no two records can have the same primary key) The validation text is the error message that someone sees if they violate the validation rule. If they go above the max, go below the min, assign a worker to a job that doesn’t exist, or give someone a birthdate in the future, etc. As part of your upcoming database project, you will set some field sizes and create input masks. You will next practice creating input masks used in the database project. 5 With ID Number being your currently selected field, at the bottom of the screen, click to the right of Input Mask. Look to the far right of the row and you should see 3 black dots. Click on the 3 black dots and the Input Mask wizard should appear. The Input Mask wizard will ask “which input mask matches how you want data to look?” The field you are working with is called ID Number, but ID Number does not appear in the listing. However, there is one that is fairly close, Social Security Number. When creating the input mask for id numbers in the upcoming project, use the following options: Click Social Security Number, then click Next. When asked “Do you want to change the input mask?” The answer is no, you don’t want to change it, so click Next. When asked “How do you want to store the data?” you have two options. Select the With Symbols option, then click Next. That is all the information the wizard needs to create the input mask so click Finish. After creating this input mask, a sequence of numbers and symbols should appear next to the Input Mask box. Click Save to save the changes to this field. Now, it is time to create the next input mask. This input mask will be for the Birthdate field. When creating the input mask for dates in the upcoming project, use the following options: First, click in the middle of the Birthdate field name (top portion of the design screen). Then, click to the right of the Input Mask box (bottom portion of the design screen). Then, click on the 3 black dots to invoke the input mask wizard. Since the data type for this field is date/time, only date and time input mask options appear. Select Short Date, then click Next. On the next screen, you don’t want to change the input mask, so click Next. That is all the information the wizard needs to create the input mask so click Finish. After creating this input mask, a sequence of numbers and symbols should appear next to the Input Mask box. Click Save to save the changes to this field. Now, it is time to create the final input mask. This input mask will be for the Phone Number field. When creating the input mask for phone numbers in the upcoming project, use the following options: First, click in the middle of the Phone Number field name (top portion of the design screen). Then, click to the right of the Input Mask box (bottom portion of the design screen). Then, click on the 3 black dots to invoke the input mask wizard. Notice that the first option is Phone Number. Since you are creating an input mask for the phone number, select that option, then click Next. 6 When asked “Do you want to change the input mask?” The answer is no, you don’t want to change it, so click Next. When asked “How do you want to store the data?” you have two options. Select the With Symbols option (always select that for this class), then click Next. That is all the information the wizard needs to create the input mask so click Finish. After creating this input mask, a sequence of numbers and symbols should appear next to the Input Mask box. Click Save to save the changes to this field. When working on your Access database project this semester, when you are instructed to create input masks, use the options that you just saw to create the required input masks. The three input masks that you just made use the various options that you will also need to select when working on your project. Next, close down the table, but don’t close down the file. You should be back at the main navigation screen where you can see the three tables that are part of your database file. When building a database that follows the relational model, data is stored in tables (entity classes). Each table is used to store information related to a particular concept. At the present time, you have two tables (ignore the table called Practice Sorting table for right now). The Jobs table stores information regarding different jobs. The employees table (which does not yet contain actual data) will eventually store information regarding employees who do those various jobs. In pull data from both tables at the same time, maybe to show information about the job assigned to each employee, or information about the employees who do each job, we need to link those two tables by creating a relationship. Recall earlier that it was mentioned that the basis of a relationship is a common field found in the tables (the same field found in multiple tables). Your next task is to make a relationship. First, make sure that that all tables are currently closed. If one of the tables is currently open/active, you will be unable to make the relationship. Assuming all tables are closed, click the tab for Database Tools. Then click Relationships. What you will see next will vary. You might see no tables, some tables or all tables. If you are missing tables, click the Show Table button. For any tables that are missing, double-click the table name in the Show Table dialog box. Once Employee and Jobs is visible, click the Close option. The field that is the same in both of the Jobs Table and the Employee table is the Job field (found in both tables). 7 Click and drag the Job field on the left to the Job field on the right. Let go and the Edit Relationships dialog box should appear. Make sure that the same field (Job) is on both sides of the Edit Relationships dialog box. Click the Enforce Referential Integrity checkbox to place a checkmark next to Enforce Referential Integrity. Doing this creates an integrity constraint, a rule, which says in order to enter a job number into the employees table (which currently does not have any data into it), that same number must also exist in the Jobs table (which is already populated with data). In essence, an employee cannot be assigned a job number that doesn’t exist in the jobs table. Click Create and the relationship is made. Your work should match the image. Click the Save Button to save your work. From time to time, you may be asked to print out your relationships. To do that, simply click on the Relationship Report option. Go ahead and click that option if you haven’t already done so. Click the Print button to print the relationship report. When finished printing the report, click the Save button. A SAVE AS dialog box should appear for the report. Click OK and the report should appear in the listing toward the left of your screen where you can see your tables and other objects that you create in Access. Close the Relationship Report and then close the Relationships Screen. Up to this point in time, you have been working in the Design view. Next, you will enter records into the Employee table using Datasheet view. Double click the Employee table to enter the datasheet view. Take a look at your screen. Make sure none of the field names/attributes are cut off. For any that are cutoff, widen out the column. For purposes of our assignments, every single word that is cut off is considered a spelling error on your part, so having 20 cutoffs happening in the same column is not a good thing. In the image, notice that Phone Number is cutoff. That column needs to be wider. Click underneath the ID Number field name. Type in the following ID Number: 123456789 Notice that dashes are appearing in between some of the numbers. That is the result of creating the input mask. 8 Press the Tab key to move to the Last Name field. Type in the following last name: Greene Press the Tab key to move to the First Name field. Type in the following first name: Kevin Press the Tab key to move to the Birthdate field. Type in the following date: 10102005 Notice that the date appears with slashes (10/10/2005). That is the result of creating the input mask. Press the Tab key to move to the Phone Number field. Type in the following phone number: 7855321098 Notice that the phone number appears as (785)532-1099 That is the result of creating the input mask. Press the Tab key to move to the Job field. Type in the following job number: 10 Press the Tab key. Access starts a new record for you and it saves the prior record. Create a 2nd record, using the following information: ID Number: type in whatever nine numbers that you wish. Last Name: type in your own last name. First Name: type in your own first name. Birthdate: type in your own birthdate. Phone Number: make up some sort of phone number to type in. Job: type in the number 50 Typing in a job number of 50 is a problem, as you can now see. Why is that? Recall that when you linked/joined the employees table with the jobs table, you specified that you wanted to enforce referential integrity. When referential integrity is enforced, you have created an integrity constraint that does not allow you to type a job number into the employees table if that same number does not already exist in the jobs table. Valid job numbers that exist in the jobs table are 10,20, 30. Job 50 is not a real job and does not exist in the jobs table. That is why you are seeing the message You cannot add or change a record because a related record is required in table Jobs Table This referential integrity stops invalid data from being entered into the employees table. In this case, an employee cannot be assigned to a job that does not exist. Since 50 is not a valid job, change the job number to 20 and then press the Tab key. Since 20 is a valid job number, everything works and you are now ready to type in the next record. Since you are done entering records, click the Save button and then close the table. Rather than typing in a bunch of additional records, you will now move on to your next task, sorting records. Double click the table called Practice Sorting Table in order to open the table. Widen out any columns that are cut off. As seen in the picture, there are two sorting buttons. A-Z (the top button) is the ascending order sort button. Ascending order is A-Z alphabetical order, as well as low to high number order. The Z-A button (bottom button) is the descending order sort button. Descending order is Z-A reverse alphabetical order, as well as high to low number order. 9 The first sort that you wish to do is to sort in alphabetical order by last name. Click the Last Name field name. This selects the entire last name field. Click the Ascending Order sort button and the records should be sorted in alphabetical order by Last Name (A-Z). Unlike Excel sorting where the columns are independent of each other (or at least can be independent of each other), the columns/fields in Access are all tied to their related records. When you sort by last name, or any field for that matter, then entire set of records is rearranged, as you will now see. Lets do an additional sort. Click the Salary field name. This selects the entire salary field. Click the Descending Order sort button and the records should be now sorted in high to low number order based on salary. Again, notice that all of the records are rearranged. Make a screenshot of your screen to show me the results of your descending order salary sort. Doing the screenshot will ensure that everything fits on 1 page rather than risking hitting the print button and having your work spill over to a second page. Print the screenshot. Save your work. While sorting by one column/field is pretty easy, sorting by more than one column is a little bit more challenging. In your first Access lab session, we will begin by doing multi-column sorts and seeing the rules related to sorting in Access. Although we will start with sorting, the primary focus of your first Access lab session will be making queries in Access. HANDING IN THE ACTIVITY 1) Submit your prelab file to the proper location in the KSU Online Canvas assignment submission location.. 2) Please assemble your packet of printouts. The printouts should be arranged in the following order: A cover page containing the usual required information. The printout of your Relationship report The printout of your screenshot showing the descending salary sort FOLDER/FILE SUBMISSION MUST BE COMPLETED BY THE DUE DATE/TIME LISTED ABOVE. If you meet the folder/file submission deadline listed above, you can hand in your packet of printouts at my office or in your regularly scheduled lab session the next day. 10