Вы находитесь на странице: 1из 15

06.04.

2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

IE 304R Tutorial VI Introduction to Access Tables & Relationships 1 Access vs Excel


Excel is a spreadsheet, a very good one; Access is a relational database. Excel is a great tool for doing what if calculations, statistical comparisons, data analysis and graphing. Access is good for storing a very large amount of data in an orderly, organized way, searching for data with great speed and flexibility, sorting data, assembling multiple related sets of data into coherent logical structures. Access does calculations well and Excel manages data. However, these two programs cannot be used interchangeably: If you are primarily dealing with calculations go with Excel. If you are primarily dealing with data management go with Access.

For more detailed explanation, you may see: http://office.microsoft.com/en-us/access-help/using-access-or-excel-to-manage-your-dataHA001042918.aspx

Actually, a better idea is to use the benefits of both. Access and Excel communicate easily; and hence, one can use Access for data management and Excel for carrying out what if analysis. http://office.microsoft.com/en-us/excel-help/top-10-reasons-to-use-access-with-excelHA010264081.aspx

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

1/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

2 Creating an Access Database


Step 1
Start MS Access. Choose Blank Database in the Available Templates menu. Choose the folder that you want to save your database in the File Name field. Name your database in the File Name field (with .accdb extension). Finally, press Create button.

Your database will be created with a single table Table1 with a single field named as ID. Data in a database is kept within tables. Hence, our database should have at least one table in MS Access.

3 Creating a Table
3.1 Datasheet View
Step 2
In this step, we are going to modify Table1 and create a table to keep course information and rename it as Courses.

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

2/15

Double click on the field name ID and rename it as CourseID. The data type of CourseID field is set to AutoNumber. Select any cell in the field and change data type to Text using the dropdown list in the DataType field in the Fields menu in the ribbon.

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

The AutoNumber data type stores an integer that MS Access increments (adds to) automatically as you add new records. You can use the AutoNumber data type as a unique record identification for tables having no other unique value.

Step 3
-

Click to Add the second field, CourseTitle as Text. Click to Add the third field, CourseCredit as Number. Click on the Save icon in the menu (or Ctrl+S) and name your table as Courses.

3.2 Pasting Data from Excel


Step 4
Suppose that you have already created a table in MS Excel for Courses and you want to copy it to your new table in MS Access. Open the file Courses.xlsx. Select range A2:C5 and copy. Open your Courses table in MS Access, select the first three columns:

Right click and Paste (or CTRL+V). A warning dialog box will appear, click Yes. 4 records should be copied to your Courses table from MS Excel.

3.3 Entering Data using Datasheet View


Step 5
Enter the following course manually: CourseID: ME351 CourseTitle: Thermodynamics of Heat Power CourseCredit: 3

Then,save your table.

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

3/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

3.4 Design View


Step 6
In this step we are going to create a new table, Students, using the Design View. Click on Table Design in the Create menu of the ribbon. A new table, Table1, will be displayed in Design View. Insert the following field names: StuID StuFName StuLName Section as as as as Text Text Text (field size = 7) (field size = 50) (field size = 50)

Number (integer)

CTRL+S, save your table as Students. You will get the following warning message:

The primary key of a table uniquely identifies each record (row) in the table. It can either be a normal attribute that is guaranteed to be unique (such as CourseID or StudentID) or it can be generated by the DBMS (such as AutoNumber data type).

Press Cancel. In this table, StuID is our primary key. Hence, select the first row (StuID), right click and press Primary Key (or select the first row, press Primary Key button in the Design menu in the ribbon). Now we can save our table as Students. Check the Datasheet View of your new table by selecting Datasheet View in the dropdown list of the View field within the Design or Home menu in the ribbon. You can switch between different views using this dropdown list.

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

4/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

4 Importing Data
One can easily import data from other sources into Access tables.

4.1 Importing Data from Excel into an Existing Table


We have already created Students table in the previous recitations and we do not want to re-enter the data. We may paste it as we have done in Step 3; or we may import.

Step 7

- Open the file Students.xlsx to check the fields. - Click on Excel in the Import & Link part of External Data menu in the ribbon.

Select the path of your Students.xlsx file in the File Name field. Select the second option (Append a copy of the records to the table), and select Students table in the dropdown list. Then, click OK.

You will get the following warning message if Students table is open:

Click Yes. Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

5/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

Import Spreadsheet Wizard will appear. In the first step, the wizard asks if the first row of your table contains column header. Check the option if it is not already checked and click Next. Then click Finish in the next step and Close in the following. Then, open your Students table in the Datasheet View in order to check if you have successfully imported the data.

4.2 Importing Data from Excel into a New Table


Suppose that we have also kept the data of Instructors in a spreadsheet, Instructors.xlsx, and we want to create a new table, Instuctors, in our database which exactly the same as our table in MS Excel.

Step 8

Click on Excel in the Import & Link part of External Data menu in the ribbon. Select the path of your Instructors.xlsx file in the File Name field. Select the first option (Import the source data into a new table in the current database). Then, click OK.

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

6/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

In the first step, check First Row Contains Column Headings option and click Next. In the second step, you will see the data types of each field automatically assigned to the fields. You may change the data types or rename the fields in this step. For this example, just click Next. Third step is about the primary key choice. If you let Access add primary key, a new field called ID with AutoNumber data type will be added. However, since we already have a primary key, InsID, select the second option (choose my own primary key) and select InsID. Then, click Next. In the last step, name of the table is typed in the Import to table field. Type Instructors and click Finish. In the following window, click Close. Your table with

the fields togehter with their data types, primary key, and entries

will be created.

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

7/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

5 Relationships
Up to now, we have created three tables for our three entities: Courses, Students and Instructors. From now on we want to create relational tables such as: 1) Performance scores of the instructors obtained from the evaluation forms filled by the students: Note that this table will keep the assignment of courses to instructors. Hence, we will use the data from two different tables: Courses and Instructors. This is a many-to-many relationship: one course can be instructed by one or more instructors (in different sections or semesters) and one instructor can teach many courses. Hence, we need to create a new table (*) to keep this assignment.

* (Wikipedia) In systems analysis, a many-to-many relationship is a type of cardinality that refers to the relationship between two entities, A and B in which A may contain a parent row for which there are many children in B and vice versa. For instance, think of A as Authors, and B as Books. An Author can write several Books, and a Book can be written by several Authors. Because most database management systems only support one-to-many relationships, it is necessary to implement such relationships physically via a third and fourth junction table, say, AB with two one-to-many relationships A -> AB and B -> AB.

2) Grades of the students: This table will keep the courses that each student takes. Hence, again we will require the information of two entities from two different tables: Students and Courses. One student may take many courses while one course will be taken by many students. Hence, again we have many-to-many relationship.

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

8/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

5.1 Using Lookup Wizard


Step 9
In this step we are going to create PerformanceScore table. the Design View AutoNumber Text Text Number with the following fields: (field size = 5) (field size = 8) (integer) a. (as in Step 6) Create a new table using CourseIns as CourseID as InsID as PerformanceScore as

Set CourseIns as the primary key and save your table as PerformanceScore. b. Open Datasheet View and enter the following row: CourseID IE304 InsID IE123456 /the ID of Cem yign PerformanceScore enter any number between 0 and 100. CourseID may be easy to remember; however, remembering InsID is hard. Actually, listing the names of the instructors in InsID field would help a lot while entering data. c. Go back to Design View. Change the data type of InsID field from Text to Lookup Wizard. Lookup Wizard will appear. Select the first option in the first step and click Next.

d. In the second step, select Table: Instructors since we want to lookup the names of the instructors within the Instructors table. Then, click Next.

e. In the third step, select the fields: InsID, InsLName, InsFName. Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

9/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

f.

In the fourth step, we may sort the list anyway we require.

g. In the fifth step, the wizards asks whether we want to display the key column (InsID) or not. For this example, let us not hide (uncheck the option) and click Next.

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

10/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

h. In the sixth step, select InsId (which is our primary key) in the Available Fields box and click Next. i. In the last step: i. Check Enable Data Integrity.

(Wikipedia) For referential integrity to hold, any field in a table that is declared a foreign key can contain only values from a parent table's primary key. For instance, deleting a record that contains a value referred to by a foreign key in another table would break referential integrity. Some relational database management systems (RDBMS) can enforce referential integrity, normally either by deleting the foreign key rows as well to maintain integrity (CASCADE DELETE), or by returning an error and not performing the delete (RESTRICT DELETE).

ii.

Select Restrict Delete. Then, press Finish button.

j.

Now open your CourseInstructor table in Datasheet View. In the InsID field, you will see a dropdown list icon. From the list select Cem yign. k. Using the + sign in this record in Instructors table, you can see the courses assigned to the selected instructor:

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

11/15

06.04.2012 l.

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

In order to check RESTRICT DELETE option, open your Instructors table in Datasheet View. Select the row which contains the record of Cem yign. Right click and Delete Record. If we have defined the relation properly, MS Access will not allow the deletion process. m. Finally, let us repeat the same procedure for CourseID field in order to completely define relationship between two tables.

5.2 Using Relationships


In this step we are going to create Grades table. a. (as in Step 6) Create a new table using CourseStu as CourseID as StuID as Grade as the Design View AutoNumber Text Text Number with the following fields: (field size = 5) (field size = 7) (integer)

Set CourseStu as as the primary key and save your table as Grades. b. Click on Relationships button in Database Tools menu in the ribbon.

c. Right click on the grey region and click Show Table.

Show Table dialog box will appear. Add every table that is not already displayed in the layout by double-clicking them in the field list. If you add a table more than once, select the duplicate and press Delete. Your tables with all of their fields will be displayed. Also a key icon will be displayed near the primary keys of each table. d. Place your tables as in the figure in part C. You will see that the relationships between Instructors-PerformanceScore-Courses tables are already defined (in Step 8).

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

12/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

One-to-many between Instructors- PerformanceScore, One-to-many between Courses-PerformanceScore.

This two relationships define a many-to-many relationship between Courses-Instructors. e. In order to define a similar relationship between tables Courses-Grades-Students: i. Drag and drop CourseID of Courses table onto CourseID of Grades table. Edit Relationships dialog box will appear. You have the following options: Enforce Cascade Cascade Delete Relational Update Related Integrity Related Fields Records Explanation - Integrity is not enforced: We allow to enter CourseIDs in the Grades ( side) 1 not checked table which do not exist within the CourseIDs of the Courses table (1 side). - Integrity is enforced: We may only enter CourseIDs to the Grades table that already exist in Courses table. 2 checked not checked not checked - Any CourseID that is used in a record in Grades table cannot be updated or deleted from the Courses table. - Integrity is enforced. - Moreover, whenever a CourseID in the Courses table is updated, CourseIDs of all records in Grades table having the updated 3 checked checked not checked CourseID will also be updated. - Any CourseID that is used in a record in Grades table cannot be deleted from the Courses table. - Integrity is enforced. - Any CourseID that is used in a record in Grades table cannot be updated. - We allow deletion of a CourseID from 4 checked not checked checked Courses table even if it is used in a record within Grades table. However, all records having the deleted CourseID will also be deleted from the Grades table. - Integrity is enforced. 5 checked checked checked - Update is allowed (as in 3). - Deletion is allowed (as in 4).

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

13/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships For our relation, third option seems proper:

Spring 2012

ii.

Drag and drop StuID of Students table onto StuID of Grades table. We do not think that any Student ID may be updated or deleted. Hence, for this relationship, select the second option:

f.

Save your relationships by pressing the save icon (or CTRL+S). Your relationships should look like:

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

14/15

06.04.2012

IE304R Tutorial VI: Introduction to Access Tables & Relationships

Spring 2012

5.3 Join Types


a. Double click on one of the relationship lines in the Relationships window to open Edit Relationships dialog box. Click Join Type button to see the options. You will see that there exists three options. Default join type is the first one (inner join). For now, this join type is proper for our example; hence, do not make any changes and close the dialog box. b. Read the document about join types in order to understand their meainings:

(MS Office) When you define a table relationship, the facts about the relationship inform your query designs. For example, if you define a relationship between two tables, and you then create a query that employs the two tables, Access automatically selects the default matching fields based upon the fields specified in the relationship. You can override these initial default values in your query, but the values supplied by the relationship will often prove to be the correct ones. Because matching and bringing together data from more than one table is something you will do frequently in all but the most simple databases, setting defaults by creating relationships can be time saving and beneficial. A multiple table query combines information from more than one table by matching the values in common fields. The operation that does the matching and combining is called a join. For instance, suppose you want to display customer orders. You create a query that joins the Customers table and the Orders table on the Customer ID field. The query result contains customer information and order information for only those rows where a corresponding match was found. One of the values you can specify for each relationship is the join type. The join type tells Access which records to include in a query result. For example, consider again a query that joins the Customers table and the Orders table on the common fields that represents the Customer ID. Using the default join type (called an inner join), the query returns only the Customer rows and the Order rows where the common fields (also called the joined fields) are equal. However, suppose you want to include all Customers even those who have not yet placed any orders. To accomplish this, you have to change the join type from an inner join to what is known as a left outer join. A left outer join returns all of the rows in the table on the left side of the relationship and only those that match in the table on the right. A right outer join returns all of the rows on the right and only those that match on the left. Note: In this case, "left" and "right" refer to the position of the tables in the Edit Relationships dialog box, not the Relationships window. You should think about the result you will most often want from a query that joins the tables in this relationship, and then set the join type accordingly.

Department of Industrial Engineering, Middle East Technical University IE304: Production and Service Information Systems

15/15

Вам также может понравиться