Create the Constraints Between Tables

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

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

2.11.3 Create the Constraints Between Tables

Now it is the time for us to set up the relationships between our fi ve tables using the Primary and Foreign keys. Since we have already selected the Primary key for each table when we create and build those tables, therefore, we only need to take care of the Foreign keys and connect them with the associated Primary keys in the related tables. Let ’ s start from the fi rst table, LogIn table.

2.11.3.1 Create the Constraints between the Log I n and Faculty Tables

Now let ’ s create the constraints between the LogIn and the Faculty tables by using a foreign key. Exactly, create a foreign key for the LogIn table and connect it to the primary key in the Faculty table. The faculty_id is a foreign key in the LogIn table but it is a primary key in the Faculty table. A one - to - many relationship is existed between the faculty_id in the Faculty table and the faculty_id in the LogIn table.

Figure 2.56. The completed Course table.

Figure 2.57. The completed Student table.

Figure 2.58. The completed StudentCourse table.

c02.indd 78

c02.indd 78 7/20/2011 11:10:55 AM7/20/2011 11:10:55 AM

www.traintelco.com

Log on the Oracle Database 10g XE using the customer username, CSE_DEPT and the customer database password, and then open the home page of the Oracle Database 10g XE. Click the Object Browser icon and select Browse|Table to list all tables. Select the LogIn table from the left pane to open it, click the Constraints tab, and then click the Create button that is the fi rst button in the second row. Enter LOGIN_FACULTY_FK into the Constraint Name box, and select the Foreign Key from the Constraint Type box, which is shown in Figure 2.59 . Check the On Delete Cascade checkbox. Then select the FACULTY_ID from the LogIn table as the foreign key column. Select the FACULTY table from the Reference Table Name box as the reference table, and select the FACULTY_

ID from the Reference Table Column List as the reference table column. Your fi nished Add Constraint window should match one that is shown in Figure 2.59 .

Click the Next button to go to the next window, and then click the Finish button to confi rm this foreign key ’ s creation.

2.11.3.2 Create the Constraints between the Log I n and Student Tables

The relationship between the Student table and the LogIn table is a one - to - many rela- tionship. The student_id in the Student table is a primary key, but the student_id in the LogIn table is a foreign key. Multiple student_id can be existed in the LogIn table, but only one or unique student_id can be found from the Student table.

To create a foreign key from the LogIn table and connect it to the primary key in the Student table, open the LogIn table if it is not opened, and click the Constraints tab, and then click the Create button that is the fi rst button in the second row to open the

Figure 2.59. Create the foreign key between the LogIn and the Faculty table.

Add Constraint window. Enter LOGIN_STUDENT_FK into the Constraint Name box, and select the Foreign Key from the Constraint Type box, which is shown in Figure 2.60 . Check the On Delete Cascade checkbox. Then select the STUDENT_ID from the LogIn table as the foreign key column. Select the STUDENT table from the Reference Table Name box as the reference table, and select the STUDENT_ID from the Reference Table Column List as the reference table column. Your fi nished Add Constraint window should match one that is shown in Figure 2.60 .

Recall that when we created the LogIn table in Section 2.11.2.1 , we emphasized that for the blank fi elds in both faculty_id and student_id columns, don ’ t place a NULL into these fi elds and just leave those fi elds blank. The reason for this is that an ALTER TABLE command will be issued when you create a foreign key for the LogIn table, and the NULL cannot be recognized by this command; therefore, an error ORA - 02298 occurs, and your creation of foreign key will fail.

Click the Next button to go to the next window, and then click the Finish button to confi rm this foreign key ’ s creation. Your fi nished foreign key creation window for the LogIn table should match one that is shown in Figure 2.61 .

2.11.3.3 Create the Constraints between the Course and Faculty Tables The relationship between the Faculty table and the Course table is a one - to - many rela- tionship. The faculty_id in the Faculty table is a primary key, but it is a foreign key in the Course table. This means that only unique faculty_id is existed in the Faculty table but

Figure 2.60. Create the foreign key between the LogIn and the Student table.

c02.indd 80

c02.indd 80 7/20/2011 11:10:55 AM7/20/2011 11:10:55 AM

www.traintelco.com

Figure 2.61. The fi nished foreign key creation window for the LogIn table.

multiple faculty_id can be existed in the Course table since one faculty can teach multiple courses.

Open the Course table by clicking it from the left pane. Click the Constraints tab and then click the Create button. Enter COURSE_FACULTY_FK into the Constraint Name box, and select the Foreign Key from the Constraint Type box, which is shown in Figure 2.62 . Check the On Delete Cascade checkbox. Then select the FACULTY_ID from the Course table as the foreign key column. Select the FACULTY table from the Reference Table Name box as the reference table, and select the FACULTY_ID from the Reference

Figure 2.62. Create the foreign key between the Course and the Faculty table.

Table Column List as the reference table column. Your fi nished Add Constraint window should match one that is shown in Figure 2.62 .

Click the Next button to go to the next window, and then click the Finish button to confi rm this foreign key ’ s creation. Your fi nished foreign key creation window for the Course table should match one that is shown in Figure 2.63 .

2.11.3.4 Create the Constraints between the Student C ourse and Student Tables

The relationship between the Student table and the StudentCourse table is a one - to - many relationship. The primary key student_id in the Student table is a foreign key in the StudentCourse table, since one student can take multiple different courses. In order to create this relationship by using the foreign key, fi rst, let ’ s open the StudentCourse table.

Click the Constraints tab and then click the Create button that is the fi rst button on the second row. Enter STUDENTCOURSE_STUDENT_FK into the Constraint Name box, and select the Foreign Key from the Constraint Type box, which is shown in Figure 2.64 . Check the On Delete Cascade checkbox. Then select the STUDENT_ID from the StudentCourse table as the foreign key column. Select the STUDENT table from the Reference Table Name box as the reference table, and select the STUDENT_ID from the Reference Table Column List as the reference table column. Your fi nished Add Constraint window should match one that is shown in Figure 2.64 .

Click the Next button to go to the next window, and then click the Finish button to confi rm this foreign key ’ s creation.

2.11.3.5 Create the Constraints between the Student C ourse and Course Tables

The relationship between the Course table and the StudentCourse table is one - to - many relationship. The primary key course_id in the Course table is a foreign key in the

Figure 2.63. The fi nished foreign key creation window for the Course table.

c02.indd 82

c02.indd 82 7/20/2011 11:10:55 AM7/20/2011 11:10:55 AM

www.traintelco.com

StudentCourse table, since one course can be taken by multiple different students. By using the StudentCourse table as an intermediate table, a many - to - many relationship can be built between the Student table and the Course table.

To create this relationship by using the foreign key, open the StudentCourse table by clicking it from the left pane. Click the Constraints tab and then click the Create button, which is the fi rst button on the second row. Enter STUDENTCOURSE_COURSE_FK into the Constraint Name box, and select the Foreign Key from the Constraint Type box, which is shown in Figure 2.65 . Check the On Delete Cascade checkbox.

Then select the COURSE_ID from the StudentCourse table as the foreign key column.

Select the COURSE table from the Reference Table Name box as the reference table, and select the COURSE_ID from the Reference Table Column List as the reference table column. Your fi nished Add Constraint window should match one that is shown in Figure 2.65 .

Click the Next button to go to the next window, and then click the Finish button to confi rm this foreign key ’ s creation. Your fi nished foreign key creation window for the StudentCourse table should match one that is shown in Figure 2.66 .

Our customer database creation for Oracle Database 10g Express Edition is com- pleted. A completed Oracle 10g XE sample database CSE_DEPT that is represented by a group of table fi les can be found from the folder Oracle that is located at the site http://

www.xxxxxx.org/bai/database .

At this point, we have fi nished developing and creating all sample databases we need to use later. All of these sample databases will be utilized for the different applications we will develop in this book.

Figure 2.64. Create the foreign key between the StudentCourse and the Student table.

Since the Oracle Database 10g XE is very different with other databases, such as Microsoft Access and SQL Server 2008, you need to refer to Appendix G to get a clear picture about how to use this CSE_DEPT Oracle database fi les. Refer to Appendix F to get the knowledge in how to use the Utilities of Oracle Database 10g XE to Unload the fi ve tables to fi ve Text fi les, and how to Load those fi ve table fi les into a new customer Oracle database to create a new customer Oracle database easily.

Figure 2.65. Create the foreign key between the StudentCourse and the Course table.

Figure 2.66. The fi nished foreign key creation window for the StudentCourse table.

c02.indd 84

c02.indd 84 7/20/2011 11:10:56 AM7/20/2011 11:10:56 AM

www.traintelco.com

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

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

(791 trang)