Sei sulla pagina 1di 11

MIS 2000 Information Systems for Management * Asper School of Business

Lab on Databases Database Tutorial - Part 3

Updated Fall 2010


The purpose of this exercise is to advance your knowledge of using MS Access 2007 (Access). This lab is a continuation of the lecture on TPS, and MIS, and the tutorial on data diagramming. The practical purpose of the lab is to enable you to complete your Database Assignment. A basic level of using Access is addressed here, which means that most procedures use Access Wizards. You will practice how to create: Tables (a database) Data entry forms (user interface) these two makes a TPS Queries Management reports these two using TPS data create an MIS You will work with the data diagram shown in Figure 1. In fact, this is the solution to the exercise 2 in the data diagramming tutorial discussed in the class.

Figure 1. Data diagram for Course Registration System (Note: Read relationships from left to right)

1. Designing Tables

To start using MS Access, select or click on its icon on your screen. You will then be presented with some choices.

1.1 Creating a new database To create a new database, do the following: 1. Click Office Button (the circle in the upper-most left corner) 2. Select New 3. The right side of the screen should show text Blank Database and a field entitled File Name. If a default name is specified, replace the part of it before the period with - Course Registration System. Your complete file name should be - Course Registration System.accdb. 4. You also want to specify where your database application will be. Click the folder icon nearby and navigate to your locker or perhaps a flash drive (if you have it). Click the button Create. You are done!

1.2 Creating tables Access stores all data in objects called tables (the concept from relational database technology). First you want to create the table Student. Once you started your database, Access is ready to create your first table. In fact, it can already be started for you. If it is not, do the following to create a table: 1. In the main menu across the top of the screen (called Ribbon), click Create 2. Click Table

1.3 Creating columns Whether the table was automatically opened or you had to run the above procedure, Access immediately opens up the first column. Then, you need to name it and add more columns. 2

To create columns in your table Student, use the following procedure: 1. Double-click the name of the first column and type StudentID; press enter 2. Double-click the name of the second column and type - StuFirstName; press enter 3. Use step 2 to create remaining columns StuLastName, StuMajor, and StuFaculty. You want to make all the columns that are represented as attributes in your data diagram in Figure 1. 4. Once done, click Office Button and choose Save. Your table Student is made and saved.

1.4 Setting up data types Your table is just missing a finishing touch. It is very important to specify whether the data in a column is number, text, date, or something else. If fact, Access has already did some job for you, using default data types. But you need to make sure these are what you really need. To set up data types, use the following procedure: 1. Have your table Student opened on the screen, and then in the main menu (Ribbon) choose View 2. Datasheet view should be marked as active. You should switch to Design View by clicking it. 3. Your table is different now, showing three columns named Field Name, Data Type, and Description. Dont get confused, as the columns in the table you just created are now listed in rows! You can see that data types are already set up. All Text types are good, and you only need to change the data type for StudentID from Auto Number to Number. Auto Number makes Access automatically enter numbers 1, 2, 3. This can be useful, but may create problems when you are changing database design. 4. Click in the column where Auto Number is written, and then click the downward arrowhead. In the drop-down list that shows up, click Number. 5. Save the table design (Office Button/Save).

1.5 Defining Primary Key You know from the data diagram in Figure 1 that each entity must have a primary key attribute. When you implement your data diagram in Access, a primary key attribute becomes the primary key column. Access may have automatically done this for you. Check it out via the Design View of the table Student. If yes, there should be a symbol of key assigned to the column StudentID. If not, use the procedure below. To set up a primary key column, do this: 1. Open the table Student in Design View 2. Click anywhere in the row StudentID 3. In the main menu (Ribbon), click the Primary key button 1.6 Closing a table To close a table after doing some work on its design 1. Save a design change by clicking Office Button and selecting Save 2. Click the x in the upper-right corner of the table displayed (either in the Design of Datasheet View). Alternatively, right-click the tab with the table name, and select Close in the pop-up menu. Use the procedures above for creating tables SectionList and Section. Table 1 shows all the tables, including data types; compare it with Figure 1.
Table 1. Database tables column names and data types Student SectionList Section StudentID, number StuFirstName, text StuMidName, text StuLastName, text StuMajor, text StuFaculty, text StudentID, number SectionID, number RegistrationDate, date/time StudentGrade, number To set StudentID and SectionID as the key, press Ctrl and click each column; then in Ribbon click the Primary key button. SectionID, number CourseID, text SectionYear, text SectionSemester, text SectionNumber, text SectionDay, text SectionTime, date/time Course CourseID, text CourseTitle, text Credits, number

Important! Look for a moment at SectionList. This is the only table that represents an artificial entity that does not correspond to a real object. As discussed in the class, you can think of this table (and its entity) in several ways. (1) The registration dates and grades are attributes that belong to both Student and Section because only a student enrolled in a particular section (and finishing the coursework) gets a grade. (2) Because one student can enroll in many sections, and each section can have many students, SectionList is needed as a bridging table that will show clearly what sections a student has enrolled and what grade in each of these the student has got. (3) Technically, the multiplicity between Student and Section is many-tomany. The relational database technology cant work with this relationship. So, you must link these tables through the table SectionList, and produce two one-to-many relationships ideal for relational database technology. The only thing you still need to do is a small intervention in the data type for Grade in the SectionList table. It must be able to accept numbers with decimal places. To fix this, use the following procedure:

1. Place the cursor in the Data Type column of the Grade column 2. In the lower part of the design screen called Field Properties, click the cell
named Field Size and select Single. Notice that the cell Decimal Places is now set to Auto. 3. Save your design. 2. Establishing Relationships between Tables

You need to establish relationships between tables (or link them), which will reflect the relationships in your data diagram. Establishing relationships takes a few simple procedures. 2.1 Showing tables for linking To get to tables you want to link (Student and SectionList), you must have the tables closed. The whole procedure is: 1. Close all tables 2. In the main menu, click Database Tools 2. Click SectionList 3. In the Show Table form, you see names of your tables listed. Mark Student and click the button Add. 4. Mark SectionList and click the button Add 5. Click Close button of the Show Table window. The Relationships tab should now be on the screen. 5

Note: If for some reason the Relationships window does not show both tables, you can quickly add the missing table by clicking the right mouse button and selecting Show Table in the pop-up window. You can move the tables in the Relationships window by placing the cursor on a table and then dragging it to a desired location while the button is pressed. Arrange tables so that the Students table is on the left and SectionList table is right of it in the same line. (Access may have already done this for you.)
2.2. Linking tables Tables must be closed before getting linked. Then, use this procedure:

1. In the Ribbon, click Database Tools and then Relationship 2. Click StudentID in Students table and, while keeping the mouse button
pressed, drive the mouse to StudentID in SectionList table. Release the mouse button.

3. In the form called Edit relationships, check the box "Enforce referential
integrity". Notice that Access already figured what relationship you are creating On--to-Many. As soon as you export a key into another table where it is the foreign key, the source table becomes one and the destination table becomes many.

4. Enable also the other two options of activating cascading update and deletion.
This means that the change (or deletion) of primary keys will automatically change (or delete) the foreign keys in linked tables.

5. Click the button Create.


Once you are done, you should have the tables displayed with a line linking them on the StudentID field and showing 1 next to Students and the infinity sign next to SectionList. That's it! Use the procedure above to link other tables as shown in Figure 1. In the end, save the content of the Relationships tab (click Office Button/Save).

3. Entering Data into Tables

Enter some data into your tables (see Figure 2 for ideas). For this, you need to use the Datasheet View of tables. In this format, a table looks like an MS Excel sheet, and entering data is also like with Excel. You first enter data in tables without foreign keys, then in those with the foreign keys. The values of foreign keys must be just those that already exist in tables where these attributes are primary keys. This constraint ensures consistency in data and it is called

referential integrity. Therefore, referential integrity means that the foreign key values must match the primary key values. Make sure that you follow the rule that a certain student takes one or more course sections. Of course, each section takes a number of students. Examine the values for StudentID and Section ID in SectionList, in Figure 2.
Student StudentID StuFirstNa StuMidNa StuLastNa StuMajor me me me V. A. Dobbs Lakich Mackowitz MIS MIS StuFaculty Asper School Asper School

6234560 Lou 7234567 Mike 7234568 Hana

Accounting Asper School

7777777 Joe

D.

Doe

Engineeri Science ng

Section Section Course SectionY SectionSeme SectionNum Section SectionTi ID ID ear ster ber Day me 10115 MIS200 2009 0 21110 ACC200 2009 1 10116 MIS200 2009 0 Winter Fall Fall A01 A02 A02 MW TTH TTH 10:00:00 AM 1:00:00 PM

SectionList Studen Sectio Registration tID nID Date 623456 0 623456 0 723456 8 10115 21110 10115 6/15/2008 4/5/2008 12/3/2008 Grade 4 3.5 3

Course Course ID CourseTitle Credi ts 3 4 3 3

ACC20 Introductory 01 Accounting ENG20 Engineering I 00 MIS200 Info Systems for 0 Management MIS350 DBMS 0

Figure 2. Tables in Course Registration System (example data, yours may differ)

4. Creating Data Entry Forms You can enter more data in your tables as you already did though the Datasheet View. But you can also ease data entry by using special data entry forms. To create a data entry form for the Student table, do the following:

1. If the table Student is closed, mark its name appearing in the pane on left
side of the screen. If the table is opened, skip to step 2.

2. In the main menu, click Create, and then Form. The newly created form
Student appears on the screen. You can now enter data in the Student table by using the form Student. Once you enter some data, click Office Button/Save, and then check if your data are really entered in the table Student. Use the procedure above to create forms on the other two tables.

5. Creating Queries Queries retrieve data from a database in order to answer users questions about business. In other words, queries (and reports that usually draw on queries) use a TPS and altogether represent an MIS. When you run a query, the output it generates is not saved permanently. Only the query statement is saved permanently. Queries use Structured Query Language that the DBMS (its query processor) interprets. Specialists write query statements directly in SQL. Fortunately, you dont

have to learn SQL in order to write queries. Access supports a simplified query creation via a Wizard function.

5.1 Creating a query from one table This procedure will create a query on the table Student, which will output a list of faculty and majors of students in the database:

1. In the main menu, click Create, and then Query Wizard


2. A New Query form pops up; choose Simple Query Wizard

3. In the form Simple Query Wizard that pops up, select the table Student 4. The form lets you select fields you wish (the > button) or altogether (>>);
choose fields StudentID, StuMajor and StuFaculty and click Next

5. On the next screen, accept the proposed name for the query by clicking
Finish. The system should respond by displaying the output. Your output may not be sorted, and therefore it may be difficult to estimate how many students belong to particular faculties. A quick way to sort rows is by right-clicking a column, and selecting sorting (A to Z, or Z to A) in the pop-up menu. Below is an alternative procedure that should work with different versions of Access:

1. While the output is displayed on the screen, click View and Query Design in
the main menu. A new tab opens up showing your query in a different format.

2. In the column StuMajor, click in the Sort line. A drop down menu is enabled.
Click the option ascending.

3. In the main menu, click the Run button with an exclamation symbol on it. The
system should respond by outputting your list sorted alphabetically on the majors.

4. Save the query (Office Button/Save) under the existing name.


Assume that the users question is to list just the science students. You can modify this to answer the question: 1. 2. 3. 4. Open the query in Design View In the row Criteria, column StuFaculty, type the word Science Click the exclamation mark in the Ribbon to run the query Save the query by to read something like Student Science Query

5.2 Creating a query from two tables and using a calculated field

You may want to find the average grades per faculty. To get this answer, you need to combine two tables Student, where the faculty is listed, and SectionList, where grades are stored. To find average grades per faculty, 1. In the main menu, click Create, and then Query Wizard 2. A New Query form pops up; choose Simple Query Wizard 3. In the form Simple Query Wizard select the table Student, and then the field StuFaculty; click Next

4. In the form Simple Query Wizard select the table SectionList, and then the
field Grade; click Next 5. In the next view of the same form, click Summary and then Summary Options

6. In the next view, check the box Avg to get the average statistics on Grade;
click buttons OK and then Finish 7. To save the query, click the Office Button, then Save As, and type in Average Grades per Faculty for the query name If all went well you should get a list of faculties and the average grade for each. The calculation function of averaging stored its output in a new filed called Avg Of Grade.

6. Creating Reports Reports have formatting capabilities that queries lack. On the other hand, queries usually do most of the retrieval work and then reports are built upon queries. Reports can be built in different ways. Here is one quick method:

1. In the main menu click Create, and then select Report Wizard 2. In the Report Wizard form, choose the query Average Grades per Faculty you
created in the exercise above 3. Choose both available fields

4. Select grouping per faculty


5. For sorting, select Avg of Grade descending

6. For the layout, select Stepped 7. For the style, select whatever you like 8. Preview the report. You will get a nicer format than a query output but not
everything may be as you wish. Close the Preview tab, and get your report in Design View. You can now change the size of text boxes and other details. This step is where the bulk of report design work is. Follow some useful formatting procedures.

10

To resize a text box,

1. Place the cursor on an edge of a text box; the cursors becomes an arrow
2. Press the mouse button and drag the cursor to a desired point To move a text box,

1. Place the cursors in the text box and move it around until it becomes a cross
composed of arrows pointing to four directions 2. Press the mouse button and drag the cursor to a desired location. Here is another quick method for making reports: 1. Open a table 2. In the main menu click Create, and then select Report You will have a report created and displayed on the screen. But you again need to do some formatting work.

Thats all folks!

11

Potrebbero piacerti anche