Create Relationships among Tables

Một phần của tài liệu practical database aprogramming with java (Trang 63 - 68)

PART II Building Three-Tier Client–Server Applications 555 Chapter 8 Developing Java Web Applications to Access Databases 557

2.9 CREATE MICROSOFT ACCESS SAMPLE DATABASE

2.9.4 Create Relationships among Tables

All fi ve tables are completed, and now we need to set up the relationships between these fi ve tables by using the primary and foreign keys. Go to the Database Tools|

Table 2.16. The data in the Student table

student_id student_name gpa credits major schoolYear email 3.26 108

3.57 116 3.52 102 3.87 78 3.95 127

A78835 Andrew Woods Computer Science Senior awoods@college.edu A97850 Ashly Jade Information System Engineering Junior ajade@college.edu B92996 Blue Valley Computer Science Senior bvalley@college.edu H10210 Holes Smith Computer Engineering Sophomore hsmith@college.edu

J77896 Erica Johnson Computer Science Senior ejohnson@college.edu

Table 2.17. The data in the StudentCourse table s_course_id student_id course_id credit major

CSC-131D CSC-132A CSC-335 CSC-331 CSC-234B CSC-234A CSC-233A CSC-132A CSE-432 CSE-434 CSC-439 CSC-132A CSC-331 CSC-335 CSE-438 CSC-432 CSC-132B CSC-234A CSC-331 CSC-335 CSE-439 CSC-230 CSE-332 CSE-430 CSC-333A CSE-433 CSE-334 CSC-131C CSC-439

H10210 CE B92996 CS/IS

J77896 CS/IS

A78835 CE H10210 CE J77896 CS/IS

B92996 CS/IS

A78835 CE A78835 CE A78835 CE J77896 CS/IS

H10210 CE H10210 CE A78835 CE A78835 CE J77896 CS/IS

A97850 ISE A97850 ISE A97850 ISE A97850 ISE J77896 CS/IS

B92996 CS/IS

A78835 CE B92996 CE J77896 CS/IS

H10210 CE H10210 CE B92996 CS/IS

B92996 CS/IS

1000 3

1001 3

1002 3

1003 3

1004 3

1005 3

1006 3

1007 3

1008 3

1009 3

1010 3

1011 3

1012 3

1013 3

1014 3

1015 3

1016 3

1017 3

1018 3

1019 3

1020 3

1021 3

1022 3

1023 3

1024 3

1025 3

1026 3

1027 3

1028 3

c02.indd 40

c02.indd 40 7/20/2011 11:10:49 AM7/20/2011 11:10:49 AM

www.traintelco.com

Relationships menu item to open the Show Table dialog. Keep the default tab Tables selected, and select all fi ve tables by pressing and holding the Shift key on the keyboard and clicking the last table — StudentCourse . Click the Add button, and then the Close button to close this dialog box. All fi ve tables are added and displayed in the Relationships dialog box. The relationships we want to add are shown in Figure 2.19 .

The P.K and F.K. in Figure 2.19 represent the Primary and Foreign keys, respectively.

For example, the faculty_id in the Faculty table is a primary key, and it can be

Figure 2.16. The completed Course table.

Figure 2.17. The completed Student table

Figure 2.18. The completed StudentCourse table.

Figure 2.19. Relationships between tables.

user_name pass_word faculty_id student_id LogIn Table

faculty_id name office college

student_id name major gpa course_id course credits

s_course_id

faculty_id

student_id course_id

P.K. P.K.

P.K. P.K.

P.K.

F.K. F.K.

F.K.

F.K. F.K.

one-to-many

one-to-many

many-to-many one-to-many

one-to-many one-to-many

Faculty Table

Course Table Student Table

StudentCourse Table

c02.indd 42

c02.indd 42 7/20/2011 11:10:50 AM7/20/2011 11:10:50 AM

www.traintelco.com

connected with the faculty_id that is a foreign key in the LogIn table. The relationship between these two tables are one - to - many, since the unique primary key faculty_id in the Faculty table can be connected to multiple foreign key that is faculty_id located in the LogIn table.

To set this relationship between these two tables, click faculty_id from the Faculty table and drag to the faculty_id in the LogIn table. The Edit Relationships dialog box is displayed, which is shown in Figure 2.20 .

Select the Enforce Referential Integrity checkbox to set up this reference integrity between these two fi elds. Also check the following two checkboxes:

• Cascade Update Related Fields • Cascade Delete Related Records

The purpose of checking these two checkboxes is that all fi elds or records in the cascaded or child tables will be updated or deleted when the related fi elds or records in the parent tables are updated or deleted. This will greatly simplify the updating and delet- ing operations for a given relational database that contains a lot of related tables. Refer to Chapters 6 and 7 for more detailed discussions about the data updating and deleting actions.

Click the Create button to create this relationship. In the similar way, you can create all other relationships between these fi ve tables. One point you need to remember when you perform this dragging operation is that always starting this drag from the Primary key in the parent table and ending it with the Foreign key in the child table. As shown in Figure 2.20 , the table located in the left of the Edit Relationships dialog is considered as the parent table, and the right of this dialog is the child table. Therefore, the faculty_id in the left is the Primary key, and the faculty_id in the right is the Foreign key, respectively.

The fi nished relationships dialog should match the one that is shown in Figure 2.21 .

Figure 2.20. Edit Relationships dialog box.

A completed Microsoft Access 2007 database fi le CSE_DEPT.accdb can be found from the folder Database\Access that is located at the Wiley ftp site. Refer to Appendix G if you want to use this sample database in your applications.

Một phần của tài liệu practical database aprogramming with java (Trang 63 - 68)

Tải bản đầy đủ (PDF)

(791 trang)