IS 4420 - Access Lab 1 Objectives: A. Launch Microsoft Access and create a new database B. Understand Microsoft Access environment C. Create tables and insert data D. Setup ODBC E. Connect Oracle database using ODBC F. Use query by example A. Launch Microsoft Access and Create New Database 1. Launch Microsoft Access using the Start menu 2. Select Blank Access database and click OK (Of course, in the future when you want to open an existing database, you should choose open existing database) 3. Change the file name to myfirst.mdb or any other name you want and click Create 4. Congratulations! You’ve just created your first Access database when you see the window below: 1 B. Understand Microsoft Access Environment 1. Explore the menu 2. Explain key objects: tables, queries, forms, reports, and macros 3. Understand Open view (to run an object) and Design view (to design an object) C. Create Tables and Insert Data We are going to create a relation database according to the logical schema below. But to save time, we will just create the Department and Dept_Locations table and their relationship. Employee FName Minit LName SSN BDate Address Sex Salary SuperSSN DepNo Department DName DepNo MgrSSN MgrSDate Dept_Locations DepNo DLocation Project PName PNumber PLocation DepNo Works_On ESSN PNo Hours Dependent ESSN Dependent_Name Sex BDate Relationship 1. Double-click Create table in Design view 2. Type DName as the Field Name, and choose Text as the Data Type 3. In the General tab a. Change Field Size to 15 b. Select Yes for Required (Not Null) 2 c. Select Yes (No Duplicates) for Indexed (Unique) 4. Do the similar for the other three attributes in the Department table according to the requirements below. Attribute Department Name Department Number Manager SSN Manage Start Date Data Type VARCHAR(15) INT CHAR(9) DATE Primary UNIQUE Foreign Constraint NOT NULL NOT NULL NOT NULL 5. Right-click the DepNo field and select Primary Key 6. Save the table as Department and close the table. 7. Create the dept_locations table similar according the requirements below. Attribute Data Type Primary Department Number INT Department Location VARCHAR(15) Foreign Constraint Department (DepNo) ON NOT NULL DELETE CASCADE NOT NULL 8. Create relationship between the two tables. a. Click Relationships under the Tool menu b. Select the two tables, click Add, and then Close c. Select DepNo in the Dept_Locations table, hold the mouse left key, drag it onto the DepNo in the Department table, and release the left key. d. Check Enforce Referential Integrity e. Check Cascade Delete Related Records f. Click Create and you should be able to the window below 9. Double-click to open the Department table, enter the following records: 3 Table Name: Department DName Manufacture Administration DepNo 1 2 MgrSSN 888665555 543216789 MgrDate 19-JUN-71 04-JAN-99 10. Close the Department table and enter the following records for Dept_Locations table. Table Name: Dept_Locations DepNo 1 1 2 3 DLocation Houston Chicago New York Salt Lake City The system won’t allow you to enter the last record, why? Ignore the last record and carry on. Close myfirst.dmb. D. Setup ODBC You have just learned how to create and populate tables in Microsoft Access. Alternatively, you can access Oracle database from Microsoft Access. But you will need to setup ODBC first. 1. Click Control Panel from the Start menu 2. Double click Administrative Tools (For Windows XP, you may need to switch from Category View to Classic View). 3. Double click Data Sources and then click Add button 4. Scroll down and select Oracle in OraHome92 5. Click Finish and Enter the following parameter values in the window below Data Source Name: is4420 (or anything you want) Description: Database Fundamentals (or anything you want) TNS Service Name: select IS4420 User ID: your user id (example: user8) 4 6. Click OK and then OK to exit, and you have successfully created an ODBC E. Connect Oracle Database Using ODBC The connection between a Microsoft Access and Oracle tables allows a linkage or reference to the data stored in the Oracle tables. The data displayed in a Linkage Table (in Access) are actually stored on the Oracle database server. Thus, all data manipulations (e.g., data insertion and data deletion) in Access will be reflected to the actual table on Oracle database server. 1. Launch Microsoft Access, and select "Blank Access database" 2. Then, change the File Name to myodbc.mdb (or any other name you want) 3. To connect an Oracle table to Access, select "File", then "Get External Data", and then click on "Link Tables" 4. A Link Table dialogue screen will pop up (see the following screenshot). To indicate the table that you would like to link is from ODBC, please select "ODBC Databases" in the "Files of Type" section, and click Link 5. Select the Machine Data Source tab, and select your data source (example: is4420). If the data source if not available, refer to the “How to setup ODBC” instruction. 5 6. Upon clink on “OK” for data source, an "Oracle ODBC Driver Connect" dialogue screen will appear and ask you for "User Name" and "Password" for accessing the Oracle database server. Please enter your user name and password (when you log on to SQL Plus Worksheet) and then click on OK to continue. 7. Please select the tables you want to link, which are all the tables you created in Lab 2. Remember to check the "Save password". By checking "Save password", you are exempted from re-entering your password when attempting to connect Oracle tables to Access databases in the future. Click OK 6 8. You should be able to see the new tables with "Earth-like" icon in the Tables section (see the following screenshot). If so, you have successfully connected a table on an Oracle database server to the Access database environment. 9. Double click on one table you have just linked and you should be able to see all the data (records) stored in that table (as shown in following screenshot). 7 F. Use Query by Example 1. List department number, department name, and number of employees in each department, ordered by number of employees in each department. We have done this in Oracle SQL Plus in Assignment 5. Now let’s try it using Microsoft Access QBE. 2. Select Queries under Objects, and double click Create query in Design view 3. Select the Department and Employee table, and click Add 4. Design the query as shown below. Note: you can click the sign to show or not to show the aggregate functionality Total 8 5. Click the ! sign to run the query, you should see the result below. DEPNO 1 2 3 4 5 DNAME SSNOfCount Manufacture 1 Administration 1 Headquarter 1 Finance 3 Research 4 6. You can save this query as countemployee for future use, and you should see this query under the Queries tab. 7. Select the SQL View under the View menu, you will see the SQL command below: SELECT USER22_DEPARTMENT.DEPNO, USER22_DEPARTMENT.DNAME, Count(USER22_EMPLOYEE.SSN) AS SSNOfCount FROM USER22_DEPARTMENT INNER JOIN USER22_EMPLOYEE ON USER22_DEPARTMENT.DEPNO = USER22_EMPLOYEE.DEPNO GROUP BY USER22_DEPARTMENT.DEPNO, USER22_DEPARTMENT.DNAME ORDER BY Count(USER22_EMPLOYEE.SSN); 9 8. Let’s do another query, list the name of employees who both lives (address) and works (department location) in Salt Lake City. 9. Select Queries under Objects, and double click Create query in Design view 11. Select the Dept_Locations and Employee table, and click Add. 12. You will find unlike the previous example, there is no connection between those two tables. 13. Drag the DEPNO attribute in the Dept_locations table unto the DEPNO in the Employee table to create the join, and then design the query as below 10 14. Run the query and here is the result: FNAME LNAME ADDRESS DLOCATION Doug Gilbert 11 S 59 E, Salt Lake City, UT Salt Lake City 15. Go to the SQL View to see the SQL code: SELECT USER22_EMPLOYEE.FNAME, USER22_EMPLOYEE.LNAME, USER22_EMPLOYEE.ADDRESS, USER22_DEPT_LOCATIONS.DLOCATION FROM USER22_DEPT_LOCATIONS INNER JOIN USER22_EMPLOYEE ON USER22_DEPT_LOCATIONS.DEPNO = USER22_EMPLOYEE.DEPNO WHERE (((USER22_EMPLOYEE.ADDRESS) Like "%Salt%") AND ((USER22_DEPT_LOCATIONS.DLOCATION) Like "%Salt%")); 16. Please save and keep a copy of the myodbc.mdb file for next lab. 11