PART II Building Three-Tier Client–Server Applications 555 Chapter 8 Developing Java Web Applications to Access Databases 557
2.11.2 Add New Data Tables into the Oracle User Database
Since we just created a new database CSE_DEPT without any table attached with this database, therefore, a blank table list is displayed under the Tables item, as shown in Figure 2.43 . We need to create and add fi ve new tables, LogIn, Faculty, Course, Student , and StudentCourse , into this new database.
Figure 2.42. Create new user dialog box.
c02.indd 64
c02.indd 64 7/20/2011 11:10:52 AM7/20/2011 11:10:52 AM
www.traintelco.com
2.11.2.1 Create the Log I n Table
Make sure that the content of the textbox in the left column is Tables, which is the default value; click the drop - down arrow of the Create button and select Tables to create our fi rst new table, LogIn , which is shown in Figure 2.44 .
A fl owchart of developing the table is shown in the left pane. This means that you need to follow these fi ve steps to fi nish the creation of your data table, and each step is mapped to one page. The middle pane contains the most components that allow us to create and edit our data table. Enter LogIn as the table name into the Table Name box.
The fi rst step in the fl owchart is the Columns , which means that you need to create each column based on the information of your data table, such as the Column Name, Type, Precision, Scale, and Not Null. For our LogIn table, we have four columns: user_
name , pass_word , faculty_id , and student_id . The data type for all columns is VARCHAR2(15), since this data type is fl exible, and it can contain varying - length char- acters. The upper bound of the length is 15, which is determined by the number you entered in the Scale box, and it means that each column can contain up to 15 characters.
Since the user_name is selected as the primary key for this table, check the Not Null checkbox next to this column to indicate that this column cannot contain a blank value.
Your fi nished fi rst step is shown in Figure 2.44 .
Click the Next button to go to the next page to assign the primary key for this table, which is shown in Figure 2.45 .
Figure 2.43. The opened Object Browser window.
Figure 2.45. The second step — assign the primary key.
66
c02.indd 66
c02.indd 66 7/20/2011 11:10:52 AM7/20/2011 11:10:52 AM
www.traintelco.com
Figure 2.46. The fourth step — setup constraints.
To assign a primary key to our new LogIn table, select the Not Populated from the Primary Key selection list because we don ’ t want to use any Sequence object to assign any sequence to our primary key. The Sequence object in Oracle is used to automatically create a sequence of numeric number for the primary key. In our case, our primary key is a string, and therefore we cannot use this object. Keep the Primary Key Name, LOGIN_
PK , unchanged, and select the USER_NAME(VARCHAR2) from the Primary Key box. In this way, we select the user_name as the primary key for this table. Since we do not have any Composite Primary Key for this table, just keep this box unchanged. Your fi nished second step should match one that is shown in Figure 2.45 . Click the Next button to continue to the next page — Set the foreign key page.
Since we have not created any other table, therefore, we cannot select our foreign key for this LogIn table right now. We leave this job to be handled later. Click the Next button to go to the next page. The next page allows you to set up some constraints on this table, which is shown in Figure 2.46 .
No constraint is needed for this sample database at this moment, so you can click the Finish button to go to the last page to confi rm our LogIn table. The opened Confirm page is shown in Figure 2.47 .
Click the Create button to create and confi rm this new LogIn table. Your created LogIn table should match one that is shown in Figure 2.48 if it is successful. The new created LogIn table is also added into the left pane.
After the LogIn table is created, the necessary editing tools are attached with this table and displayed at the top of this table. The top row of these tools contains object
Figure 2.47. The last step — confi rmation.
Figure 2.48. The created LogIn table.
c02.indd 68
c02.indd 68 7/20/2011 11:10:53 AM7/20/2011 11:10:53 AM
www.traintelco.com
Figure 2.49. The opened Data page.
editing tools, and the bottom line includes the actual editing methods. The editing methods include Add Column, Modify Column, Rename Column, and Drop Column, and these methods are straightforward in meaning without question.
To add data into this new LogIn table, you need to use and open the Data object tool in the top row. Click the Data tool to open the Data page, which is shown in Figure 2.49 .
Click the Insert Row button to open the data sheet view of the LogIn table, which is shown in Figure 2.50 .
Add the following data into the fi rst row: User Name — abrown , Pass Word — America , Faculty Id — B66750. Since this user is a faculty, leave the Student Id column blank ( don ’ t place a NULL in here, otherwise you will have trouble when you create a foreign key for this table later! ). Your fi nished fi rst row is shown in Figure 2.50 .
Click the Create and Create Another button to create the next row. In the similar way, add each row that is shown in Table 2.23 into each row on the LogIn table.
You can click the Create button after you add the fi nal row into your table. Your fi nished LogIn table should match one that is shown in Figure 2.51 .
Next, let ’ s create our second table — Faculty table.
2.11.2.2 Create the Faculty Table
Click the Table tool on the top raw and click the Create button to create another new table. Select the Table item to open a new table page. Enter Faculty into the Table Name box as the name for this new table, and enter the following columns into this new table:
Figure 2.50. The opened data sheet view of the LogIn table.
Table 2.23. The data in the LogIn table
user_name pass_word faculty_id student_id abrown america B66750
ajade tryagain A97850
awoods smart A78835
banderson birthday A52990
bvalley see B92996
dangles tomorrow A77587
hsmith try H10210
jerica excellent J77896 jhenry test H99118
jking goodman K69880 sbhalla india B86590 sjohnson jermany J33486 ybai reback B78880
• faculty_id — VARCHAR2(10) • faculty_name — VARCHAR2 (20) • offi ce — VARCHAR2 (10) • phone — CHAR(12) • college — VARCHAR2 (50)
c02.indd 70
c02.indd 70 7/20/2011 11:10:53 AM7/20/2011 11:10:53 AM
www.traintelco.com
• title — VARCHAR2 (30) • email — VARCHAR2 (30)
The popular data types used in the Oracle database include NUMBER, CHAR, and VARCHAR2. Each data type has its upper bound and low bound. The difference between the CHAR and the VARCHAR2 is that the former is used to store a fi xed - length string, and the latter can provide a varying - length string, which means that the real length of the string depends on the number of real letters entered by the user. The data types for all columns are VARCHAR2 with one exception, which is the phone column that has a CHAR type with an upper bound of 12 letters, since our phone number is composed of 10 digits, and we can extend this length to 12 with two dashes. For all other columns, the length varies with the different information, so the VARCHAR2 is selected for those columns.
The fi nished design view of your Faculty table is shown in Figure 2.52 . You need to check the Not Null checkbox for the faculty_ id column, since we selected this column as the primary key for this table.
Click the Next button to go to the next page to add the primary key for this table, which is shown in Figure 2.53 .
Check the Not Populated from the Primary Key list since we don ’ t want to use any Sequence object to automatically generate a sequence of numeric number as our primary key, and then select the FACULTY_ID(VARCHAR2) from the Primary Key
Figure 2.51. The completed LogIn table.
Figure 2.53. The opened Primary Key window.
72
c02.indd 72
c02.indd 72 7/20/2011 11:10:54 AM7/20/2011 11:10:54 AM
www.traintelco.com
textbox. In this way, the faculty_id column is selected as the primary key for this table.
Keep the Composite Primary Key box untouched, since we do not have that kind of key in this table, and click the Next button to go to the next page.
Since we have not created all other tables to work as our reference tables for the foreign key, click the Next to continue and we will do the foreign key for this table later. Click the Finish button to go to the Confi rm page. Finally, click the Create button to create this new Faculty table. Your completed columns in the Faculty table are shown in Figure 2.54 .
Now click the Data object tool to add the data into this new table. Click the Insert Row button to add all rows that are shown in Table 2.24 into this table.
Click the Create and Create Another button when the fi rst row is done, and continue to create all rows with the data shown in Table 2.24 . You may click the Create
Figure 2.54. The completed columns in the Faculty table.
Table 2.24. The data in the Faculty table
faculty_id faculty_name office phone college title email
A52990 Black Anderson MTC-218 750-378-9987 Virginia Tech Professor banderson@college.edu A77587 Debby Angles MTC-320 750-330-2276 University of Chicago Associate Professor dangles@college.edu B66750 Alice Brown MTC-257 750-330-6650 University of Florida Assistant Professor abrown@college.edu B78880 Ying Bai MTC-211 750-378-1148 Florida Atlantic University Associate Professor ybai@college.edu B86590 Satish Bhalla MTC-214 750-378-1061 University of Notre Dame Associate Professor sbhalla@college.edu H99118 Jeff Henry MTC-336 750-330-8650 Ohio State University Associate Professor jhenry@college.edu J33486 Steve Johnson MTC-118 750-330-1116 Harvard University Distinguished Professor sjohnson@college.edu K69880 Jenney King MTC-324 750-378-1230 East Florida University Professor jking@college.edu
Figure 2.55. The fi nished Faculty table.
button for your last row. Your fi nished Faculty table should match one that is shown in Figure 2.55 .
2.11.2.3 Create Other Tables
In the similar way, you can continue to create the following three tables: Course , Student , and StudentCourse based on the data shown in Tables 2.25 – 2.27 .
The data types used in the Course table are:
• course_id: VARCHAR2(10) — Primary Key • course: VARCHAR2(40)
• credit: NUMBER(1, 0) — precision = 1, scale = 0 (1 - bit integer) • classroom: CHAR(6)
• schedule: VARCHAR2(40)
• enrollment: NUMBER(2, 0) — precision = 2, scale = 0 (2 - bit integer) • faculty_id VARCHAR2(10)
The data types used in the Student table are:
• student_id: VARCHAR2(10) — Primary Key • student_name: VARCHAR2(20)
• gpa: NUMBER(3, 2) — precision = 3, scale = 2 (3 - bit fl oating point data with 2 - bit after the decimal point)
• credits: NUMBER(3, 0) — precision = 3, scale = 0 (3 - bit integer) • major: VARCHAR2(40)
c02.indd 74
c02.indd 74 7/20/2011 11:10:54 AM7/20/2011 11:10:54 AM
www.traintelco.com
Table 2.25. The data in the Course table
course_id course credit classroom schedule enrollment faculty_id CSC-131A Computers in Society
CSC-131B Computers in Society CSC-131C Computers in Society CSC-131D Computers in Society CSC-131E Computers in Society CSC-131F Computers in Society CSC-132A Introduction to Programming CSC-132B Introduction to Programming CSC-230 Algorithms & Structures CSC-232A Programming I CSC-232B Programming I CSC-233A Introduction to Algorithms CSC-233B Introduction to Algorithms CSC-234A Data Structure & Algorithms CSC-234B Data Structure & Algorithms CSC-242 Programming II
CSC-320 Object Oriented Programming CSC-331 Applications Programming CSC-333A Computer Arch & Algorithms CSC-333B Computer Arch & Algorithms CSC-335 Internet Programming CSC-432 Discrete Algorithms CSC-439 Database Systems CSE-138A Introduction to CSE CSE-138B Introduction to CSE CSE-330 Digital Logic Circuits CSE-332 Foundations of Semiconductors CSE-334 Elec Measurement & Design CSE-430 Bioinformatics in Computer CSE-432 Analog Circuits Design CSE-433 Digital Signal Processing CSE-434 Advanced Electronics Systems CSE-436 Automatic Control and Design CSE-437 Operating Systems CSE-438 Advd Logic & Microprocessor CSE-439 Special Topics in CSE
TC-109 M-W-F: 9:00-9:55 AM TC-114 M-W-F: 9:00-9:55 AM TC-109 T-H: 11:00-12:25 PM TC-119 M-W-F: 9:00-9:55 AM TC-301 M-W-F: 1:00-1:55 PM TC-109 T-H: 1:00-2:25 PM TC-303 M-W-F: 9:00-9:55 AM TC-302 T-H: 1:00-2:25 PM TC-301 M-W-F: 1:00-1:55 PM TC-305 T-H: 11:00-12:25 PM TC-303 T-H: 11:00-12:25 PM TC-302 M-W-F: 9:00-9:55 AM TC-302 M-W-F: 11:00-11:55 AM TC-302 M-W-F: 9:00-9:55 AM TC-114 T-H: 11:00-12:25 PM TC-303 T-H: 1:00-2:25 PM TC-301 T-H: 1:00-2:25 PM TC-109 T-H: 11:00-12:25 PM TC-301 M-W-F: 10:00-10:55 AM TC-302 T-H: 11:00-12:25 PM TC-303 M-W-F: 1:00-1:55 PM TC-206 T-H: 11:00-12:25 PM TC-206 M-W-F: 1:00-1:55 PM TC-301 T-H: 1:00-2:25 PM TC-109 T-H: 1:00-2:25 PM TC-305 M-W-F: 9:00-9:55 AM TC-305 T-H: 1:00-2:25 PM TC-212 T-H: 11:00-12:25 PM TC-206 Thu: 9:30-11:00 AM TC-309 M-W-F: 2:00-2:55 PM TC-206 T-H: 2:00-3:25 PM TC-213 M-W-F: 1:00-1:55 PM TC-305 M-W-F: 10:00-10:55 AM TC-303 T-H: 1:00-2:25 PM TC-213 M-W-F: 11:00-11:55 AM TC-206 M-W-F: 10:00-10:55 AM
A52990 B66750 A52990 B86590 B66750 A52990 J33486 B78880 A77587 B66750 A77587 H99118 K69880 B78880 J33486 A52990 B66750 H99118 A77587 A77587 B66750 B86590 B86590 A52990 J33486 K69880 K69880 H99118 B86590 K69880 H99118 B78880 J33486 A77587 B78880 J33486 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
28 20 25 30 25 32 21 21 20 28 17 18 19 25 15 18 22 28 22 15 25 20 18 15 35 26 24 25 16 18 18 26 29 17 35 22
Table 2.26. The data in the student table
student_id student_name gpa credits major schoolYear email
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 3.26
3.57 3.52 3.87 3.95
108 116 102 78 127
Table 2.27. The data in the StudentCourse table
s_course_id student_id course_id credit major H10210 CSC-131D CE B92996 CSC-132A CS/IS J77896 CSC-335 CS/IS A78835 CSC-331 CE H10210 CSC-234B CE J77896 CSC-234A CS/IS B92996 CSC-233A CS/IS A78835 CSC-132A CE A78835 CSE-432 CE A78835 CSE-434 CE J77896 CSC-439 CS/IS H10210 CSC-132A CE H10210 CSC-331 CE A78835 CSC-335 CE A78835 CSE-438 CE J77896 CSC-432 CS/IS A97850 CSC-132B ISE A97850 CSC-234A ISE A97850 CSC-331 ISE A97850 CSC-335 ISE J77896 CSE-439 CS/IS B92996 CSC-230 CS/IS A78835 CSE-332 CE B92996 CSE-430 CE J77896 CSC-333A CS/IS H10210 CSE-433 CE H10210 CSE-334 CE B92996 CSC-131C CS/IS B92996 CSC-439 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
• schoolYear: VARCHAR2(20) • email: VARCHAR2(20)
The data types used in the StudentCourse table are:
• s_course_id: NUMBER(4, 0) — precision = 4, scale = 0 (4 - bit integer) Primary Key • student_id: VARCHAR2(10)
• course_id: VARCHAR2(10)
• credit: NUMBER(1, 0) — precision = 1, scale = 0 (1 - bit integer) • major: VARCHAR2(40)
c02.indd 76
c02.indd 76 7/20/2011 11:10:54 AM7/20/2011 11:10:54 AM
www.traintelco.com
Your fi nished Course , Student , and StudentCourse tables are shown in Figures 2.56 – 2.58 , respectively.