PART II Building Three-Tier Client–Server Applications 555 Chapter 8 Developing Java Web Applications to Access Databases 557
2.10 CREATE MICROSOFT SQL SERVER 2008
2.10.4 Create Relationships among Tables
Next, we need to set up relationships among these fi ve tables using the Primary and Foreign Keys. In the Microsoft SQL Server 2008 Express database environment, the relationship between tables can be set by using the Keys folder under each data table from the Object Explorer window. Now let ’ s begin to set up the relationship between the LogIn and the Faculty tables.
2.10.4.1 Create Relationship between the Log I n and the Faculty Tables
The relationship between the Faculty and the LogIn table is one - to - many, which means that the faculty_id is a primary key in the Faculty table, and it can be mapped to many faculty_id that are foreign keys in the LogIn table. To set up this relationship, expand the LogIn table and the Keys folder that is under the LogIn table. Currently, only one primary key, PK_LogIn , is existed under the Keys folder.
To add a new foreign key, right click on the Keys folder and select the New Foreign Key item from the pop - up menu to open the Foreign Key Relationships dialog, which is shown in Figure 2.32 .
The default foreign relationship is FK_LogIn_LogIn * , which is displayed in the
Selected Relationship box. Right now, we want to create the foreign relationship between the LogIn and the Faculty tables, so change the name of this foreign relation- ship to FK_LogIn_Faculty by modifying its name in the (Name) box that is under the Identity pane, and then press the Enter key from your keyboard. Then select two tables by clicking on the Tables And Columns Specification item that is under the General pane. Click the expansion button that is located on the right of the Tables And Columns Specification item to open the Tables and Columns dialog, which is shown in Figure 2.33 .
Click the drop - down arrow from the Primary key table combo box and select the Faculty table, since we need the primary key faculty_id from this table, then click the blank row that is just below the Primary key table combo box and select the
faculty_id column. You can see that the LogIn table has been automatically selected and displayed in the Foreign key table combo box. Click the drop - down arrow from the box that is just under the Foreign key table combo box and select the faculty_id
Figure 2.31. An error message when performing a paste job.
c02.indd 54
c02.indd 54 7/20/2011 11:10:51 AM7/20/2011 11:10:51 AM
www.traintelco.com
as the foreign key for the LogIn table. Your fi nished Tables and Columns dialog should match one that is shown in Figure 2.34 .
Click the OK button to close this dialog.
Before we can close this dialog, we need to do one more thing, which is to set up a cascaded relationship between the Primary key (faculty_id) in the parent table Faculty and the Foreign keys (faculty_id) in the child table LogIn. The reason we need to do this
Figure 2.32. The opened Foreign Key Relationships dialog box.
Figure 2.33. The opened Tables and Columns dialog box.
is because we want to simplify the data updating and deleting operations between these tables in a relational database, such as CSE_DEPT. You will have a better understanding about this cascading later on when you learn how to update and delete data against a relational database in Chapter 7 .
To do this cascading, scroll down along this Foreign Key Relationships dialog and expand the item Table Designer. You fi nd the INSERT And UPDATE Specifi ca- tions item. Expand this item by clicking the small plus icon; two subitems are displayed, which are:
• Delete Rule • Update Rule
The default value for both subitems is No Action. Click the No Action box for the Delete Rule item, and then click the drop - down arrow and select the Cascade item from the list. Perform the same operation for the Update Rule item. Your fi nished Foreign Key Relationships dialog should match one that is shown in Figure 2.35 .
In this way, we established the cascaded relationship between the Primary key in the parent table and the Foreign keys in the child table. Later on, when you update or delete any Primary key from a parent table, the related foreign keys in the child tables will also be updated or deleted without other additional operations. It is convenient! Click the Close button to close this dialog.
Go to the File|Save LogIn menu item to open the Save dialog and click the Yes button to save this relationship. You can select Yes or No to the Save Change Script dialog box if it appears.
Now right click on the Keys folder under the LogIn table from the Object Explorer window, and select the Refresh item from the popup menu to refresh this Keys folder.
Immediately, you can fi nd a new foreign key named FK_LogIn_Faculty , which appears
Figure 2.34. The fi nished Tables and Columns dialog box.
c02.indd 56
c02.indd 56 7/20/2011 11:10:51 AM7/20/2011 11:10:51 AM
www.traintelco.com
under this Keys folder. This is our new created foreign key that sets the relationship between our LogIn and Faculty tables. You can confi rm and fi nd this new created foreign key by right clicking on the Keys folder that is under the Faculty table.
2.10.4.2 Create Relationship between the Log I n and the Student Tables In a similar way, you can create a foreign key for the LogIn table and set up a one - to - many relationship between the Student and the LogIn tables.
Right click on the Keys folder that is under the dbo.LogIn table and select the New Foreign Key item from the popup menu to open the Foreign Key Relationships dialog. Change the name to FK_LogIn_Student and press the Enter key from your keyboard. Go to the Tables And Columns Specification item to open the Tables and Columns dialog, then select the Student table from the Primary key table combo box and student_id from the box that is under the Primary key table combo box. Select the student_id from the box that is under the Foreign key table combo box. Your fi nished Tables and Columns dialog should match one that is shown in Figure 2.36 .
Click the OK button to close this dialog box. Do not forget to establish the cascaded relationship for Delete Rule and Update Rule items by expanding the Table Designer and the INSERT And UPDATE Specifi cations items, respectively. Click the Close button to close the Foreign Key Relationships dialog box.
Go to the File|Save LogIn menu item to save this relationship. Click Yes for the following dialog box to fi nish saving. Now, right click on the Keys folder that is under the
Figure 2.35. The fi nished Foreign Key Relationships dialog.
dbo.LogIn table, and select Refresh item to show our new created foreign key
FK_LogIn_Student .
2.10.4.3 Create Relationship between the Faculty and the Course Tables The relationship between the Faculty and the Course tables is one - to - many, and the
faculty_id in the Faculty table is a Primary key, and the faculty_id in the Course table is a Foreign key.
Right click on the Keys folder under the dbo.Course table from the Object Explorer window and select the New Foreign Key item from the popup menu. On the opened
Foreign Key Relationships dialog, change the name of this new relationship to
FK_Course_Faculty in the (Name) box and press the Enter key from the keyboard.
In the opened Tables and Columns dialog box, select the Faculty table from the
Primary key table combo box and select the faculty_id from the box that is just under the Primary key table combo box. Then select the faculty_id from the box that is just under the Foreign key table combo box. Your fi nished Tables and Columns dialog should match one that is shown in Figure 2.37 .
Click the OK to close this dialog and set up the cascaded relationship for the Delete Rule and the Update Rule items, and then click the Close button to close the Foreign Key Relationships dialog box. Go to the File|Save Course menu item and click
Yes for the following dialog box to save this setting.
Now right click on the Keys folder under the dbo.Course table, and select the Refresh item. Immediately, you can fi nd our new created relationship key
FK_Course_Faculty .
Figure 2.36. The completed Tables and Columns dialog.
c02.indd 58
c02.indd 58 7/20/2011 11:10:52 AM7/20/2011 11:10:52 AM
www.traintelco.com
2.10.4.4 Create Relationship between the Student and the Student C ourse Tables
The relationship between the Student and the StudentCourse tables is one - to - many, and the student_id in the Student table is a Primary key and the student_id in the
StudentCourse table is a Foreign key.
Right click on the Keys folder under the dbo.StudentCourse table from the Object Explorer window and select the New Foreign Key item from the popup menu. On the opened Foreign Key Relationships dialog, change the name of this new relationship to FK_StudentCourse_Student in the (Name) box and press the Enter key from the keyboard. In the opened Tables and Columns dialog box, select the Student table from the Primary key table combo box, and select the
student_id from the box that is just under the Primary key table combo box.
Then select the student_id from the box that is just under the Foreign key table combo box. The fi nished Tables and Columns dialog should match one that is shown in Figure 2.38 .
Click the OK button to close this dialog and set up the cascaded relationship for Delete Rule and the Update Rule items, and then click the Close button to close the
Foreign Key Relationships dialog box. Go to the File|Save StudentCourse menu item and click Yes for the following dialog box to save this relationship.
Now right click on the Keys folder under the dbo.StudentCourse table, and select the Refresh item. Immediately you can fi nd our new created relationship key
FK_StudentCourse_Student .
Figure 2.37. The fi nished Tables and Columns dialog.
2.10.4.5 Create Relationship between the Course and the Student C ourse Tables
The relationship between the Course and the StudentCourse tables is one - to - many, and the course_id in the Course table is a Primary key and the course_id in the
StudentCourse table is a Foreign key.
Right click on the Keys folder under the dbo.StudentCourse table from the Object Explorer window and select the New Foreign Key item from the popup menu. On the opened Foreign Key Relationships dialog, change the name of this new relationship to FK_StudentCourse_Course in the (Name) box and press the Enter key from the keyboard. In the opened Tables and Columns dialog box, select the Course table from the Primary key table combo box and select the
course_id from the box that is just under the Primary key table combo box. Then select the course_id from the box that is just under the Foreign key table combo box. Your fi nished Tables and Columns dialog should match one that is shown in Figure 2.39 .
Click the OK button to close this dialog and do not forget to establish a cascaded relationship for the Delete Rule and the Update Rule items, and then click the Close button to close the Foreign Key Relationships dialog box. Then go to the File|Save StudentCourse menu item and click Yes for the following dialog box to save this relationship.
Now right click on the Keys folder under the dbo.StudentCourse table, and select the Refresh item. Immediately, you can fi nd our new created relationship key
FK_StudentCourse_Course .
At this point, we complete setting the relationships among our fi ve data tables.
Figure 2.38. The fi nished Tables and Columns dialog.
c02.indd 60
c02.indd 60 7/20/2011 11:10:52 AM7/20/2011 11:10:52 AM
www.traintelco.com
A completed Microsoft SQL Server 2008 sample database fi le CSE_DEPT.mdf can be found from the folder SQLServer that is located at the website: http://www.xxx.org/
bai/database . The completed relationships for these tables are shown in Figure 2.40 .