You are on page 1of 22

62 Introduction to Databases

Figure 2.38. The finished Tables and Columns dialog box.

2.11 CREATE ORACLE 10G XE SAMPLE DATABASE


After you finish downloading and installing Oracle Database 10g Express Edition
(refer to Appendix C), you can begin to connect your client computer to your server
to create, access, and manipulate your database. In this section, you will learn how
to create, edit, and manipulate your database using Oracle Database 10g Express
Edition (XE).
To connect your computer to your Oracle server, go to Start|All Program
Files|Oracle Database 10g Express Edition|Go To Database Home Page to open
the Login page, which is shown in Figure 2.40.

Figure 2.39. Relationships among tables.


2.11 Create Oracle 10g XE Sample Database 63

Figure 2.40. The Login page.

You can log on as an administrator by entering SYSTEM into the Username


box and the password you selected during your download and installation of Oracle
Database 10g XE into the Password box if you want to use the whole system. But
if you want to create customer databases, you must create a new user and log on
as that user. Refer to Appendix D for detailed information on how to create a new
user to create a new database.
The open home page is shown in Figure 2.41.

Figure 2.41. The home page.


64 Introduction to Databases

Figure 2.42. The opened Object Browser window.

You need to use Object Browser to create, access, and manipulate your database
in the Oracle Database 10g XE server. Click this icon to open the Object Browser
window, which is shown in Figure 2.42.
Some developed data tables are displayed in the left column. These tables are
created by the system, and you can use them as sample tables to test your applica-
tions. But we want to create a sample database ourselves, so we need to create five
new tables: LogIn, Faculty, Course, Student, and StudentCourse.

2.11.1 Create the LogIn Table


Make sure that the content of the text box in the left column is Tables, which is the
default value. Click the drop-down arrow on the Create button and select Tables to
create our first new table, LogIn, which is shown in Figure 2.43.
A flowchart for developing the table is shown in the left pane. This means that
you need to follow these five steps to finish creating your data table. Each step is
mapped to one page. The middle pane contains most of the components that allow
us to create and edit our data table. Enter LogIn as the table name in the Table
Name box.
The first step in the flowchart is Columns, which means that you need to cre-
ate each column by entering the information for your data table, such as the Col-
umn 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 (scale 10) since this data type is flexible and can contain
varying-length characters. The upper bound of the length is 10, which means that
each column can contain up to 10 characters. Since user name will be the primary
key for this table, check the Not Null check box next to this column to indicate that
this column cannot contain a blank value. Your finished first step is shown in Fig-
ure 2.44.
2.11 Create Oracle 10g XE Sample Database 65

Figure 2.43. Create a new table.

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.
To assign a primary key our new LogIn table, select Not Populated for the Pri-
mary Key because we don’t want to use a Sequence object to assign a sequence
to our primary key. The Sequence object in Oracle is used to automatically create
a sequence of numeric numbers for the primary key. In our case, the primary key

Figure 2.44. The finished first step.


66 Introduction to Databases

Figure 2.45. The second step – assign the primary key.

is a string; therefore, we cannot use a sequence. Keep the Primary Key Constraint
Name, LOGIN PK, unchanged, and select USER NAME(VARCHAR2) from the
Primary Key box. In this way, we select user name as the primary key for this table.
Your finished second step should match the one shown in Figure 2.45. Click the
Next button to continue to the next page to set the foreign key.
Since we have not yet created any other tables, 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 some constraints on
this table, as 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 confirm the LogIn table. The open
Confirm page is shown in Figure 2.47.
Click the Create button to create and confirm this new LogIn table. Your created
LogIn table should match the one shown in Figure 2.48. The newly created LogIn
table is also added to the left pane.
After the LogIn table is created, the necessary editing tools are displayed at the
top of this table. The top line of tools contains objects to be edited, and the next line
includes the actual edit methods. The edit methods Add Column, Modify Column,
Rename Column, and Drop Column are straightforward without question.
To add data to this new LogIn table, you need to open and use the Data
object tool. Click the Data tool to open the Data page, which is shown in Fig-
ure 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 first row: User Name – abrown, Pass Word –
america, Faculty Id – B66750. Since this user is faculty, leave the Student Id column
2.11 Create Oracle 10g XE Sample Database 67

Figure 2.46. The fourth step – set up constraints.

blank (don’t place a NULL in here, or you will have trouble when you cre-
ate a foreign key for this table later!). Your finished first row is shown in Fig-
ure 2.50.
Click the Create and Create Another button to create the next row. In a similar
way, add each row shown in Table 2.23 into the LogIn table.
You can click the Create button after you add the final row to your table. Your
finished LogIn table should match the one shown in Figure 2.51.
Next, let’s create our second table, Faculty.

Figure 2.47. The last step – confirmation.


68 Introduction to Databases

Figure 2.48. The created LogIn table.

2.11.2 Create the Faculty Table


Click the object tool Table and click the Create button to create another new table.
Select the Table item to open a new table page. Enter Faculty in the Table Name
box, and enter the following columns for this new table:

 faculty id – VARCHAR2(10)
 name – VARCHAR2(20)
 office – VARCHAR2(10)
 phone – CHAR(12)

Figure 2.49. The opened Data page.


2.11 Create Oracle 10g XE Sample Database 69

Figure 2.50. The data sheet view of the LogIn table.

 college – VARCHAR2(40)
 title – VARCHAR2(30)
 email – VARCHAR2(30)

Popular data types used in the Oracle database include NUMBER, CHAR, and
VARCHAR2. Each data type has an upper bound and a lower bound. The differ-
ence between CHAR and VARCHAR2 is that the former is used to store a fixed-
length string and the latter can store a varying-length string, which means that the
real length of the string depends on the number of actual letters assigned to it. The
data types for all columns are VARCHAR2 with one exception, the phone column,
which has a CHAR type with an upper bound of 12 letters since the phone numbers

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
70 Introduction to Databases

Figure 2.51. The completed LogIn table.

are composed of 10 digits and the length 12 with two dashes. For all other columns,
the length varies with the different information, so VARCHAR2 is selected for
those columns.
The finished design view of the Faculty table is shown in Figure 2.52. You need
to check the Not Null check box for the faculty id column since this column will be
the primary key for this table.
Click the Next button to go to the next page to add the primary key for this table,
as shown in Figure 2.53.
Check Not populated for Primary Key since we don’t want to use a Sequence
object to automatically generate a sequence of numeric numbers for our primary
key, and then select FACULTY ID(VARCHAR2) from the Primary Key text
box. In this way, the faculty id column is selected as the primary key for this
table.
Click the Next button to go to the next page.
Since we have not created enough tables to work as our reference tables for the
foreign key, we need to create one for this table. Click Next to continue. We will do
the foreign key for this table later. Click the Finish button to go to the Confirm page.
Finally, click the Create button to create this new Faculty table. The 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.
2.11 Create Oracle 10g XE Sample Database 71

Figure 2.52. The finished design view of the Faculty table.

Click the Create and Create Another button when the first row is done, and
continue to create all rows with the data shown in Table 2.24. You may click the
Create button after the last row. Your finished Faculty table should match the one
shown in Figure 2.55.

Figure 2.53. The open Primary Key window.


72 Introduction to Databases

Figure 2.54. The completed columns in the Faculty table.

2.11.3 Create Other Tables


In a similar way, you can create the following three tables: Course, Student, and
StudentCourse based on the data shown in Tables 2.25, 2.26, and 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)

The data types used in the Student table are:

 student id: VARCHAR2(10) – Primary Key


 name: VARCHAR2(20)
 gpa: NUMBER(3, 2) – precision = 3, scale = 2 (3-bit floating point
data with 2-bit after the decimal point)
 credits: NUMBER(3, 0) – precision = 3, scale = 0 (3-bit integer)
 major: VARCHAR2(40)
 schoolYear: VARCHAR2(20)
 email: VARCHAR2(20)
Table 2.24. The Data in the Faculty Table

faculty id 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 Assistant 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

73
74 Introduction to Databases

Figure 2.55. The finished Faculty table.

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)
Your finished Course, Student, and StudentCourse tables are shown in Fig-
ures 2.56, 2.57 and 2.58, respectively.

2.11.4 Create the Foreign Keys for Tables


2.11.4.1 Create the Foreign Key Between the LogIn and Faculty Tables
Now let’s create the 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 exists between the
faculty id in the Faculty table and the faculty id in the LogIn table.
Open the home page of Oracle Database 10g XE if it is not open, and then
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. Enter LOGIN FACULTY FK into the Constraint Name
box, and select Foreign Key from the Constraint Type box, as shown in Fig-
ure 2.59. Check the On Delete Cascade check box. Then select FACULTY ID
from the LogIn table as the foreign key column. Select the FACULTY table in
the Reference Table Name box as the reference table, and select FACULTY ID
Table 2.25. The Data in the Course Table
course id course credit classroom schedule enrollment faculty id
CSC-131A Computers in Society 3 TC-109 M-W-F: 9:00-9:55 AM 28 A52990
CSC-131B Computers in Society 3 TC-114 M-W-F: 9:00-9:55 AM 20 B66750
CSC-131C Computers in Society 3 TC-109 T-H: 11:00-12:25 PM 25 A52990
CSC-131D Computers in Society 3 TC-109 M-W-F: 9:00-9:55 AM 30 B86590
CSC-131E Computers in Society 3 TC-301 M-W-F: 1:00-1:55 PM 25 B66750
CSC-131I Computers in Society 3 TC-109 T-H: 1:00-2:25 PM 32 A52990
CSC-132A Introduction to Programming 3 TC-303 M-W-F: 9:00-9:55 AM 21 J33486
CSC-132B Introduction to Programming 3 TC-302 T-H: 1:00-2:25 PM 21 B78880
CSC-230 Algorithms & Structures 3 TC-301 M-W-F: 1:00-1:55 PM 20 A77587
CSC-232A Programming I 3 TC-305 T-H: 11:00-12:25 PM 28 B66750
CSC-232B Programming I 3 TC-303 T-H: 11:00-12:25 PM 17 A77587
CSC-233A Introduction to Algorithms 3 TC-302 M-W-F: 9:00-9:55 AM 18 H99118
CSC-233B Introduction to Algorithms 3 TC-302 M-W-F: 11:00-11:55 AM 19 K69880
CSC-234A Data Structure & Algorithms 3 TC-302 M-W-F: 9:00-9:55 AM 25 B78880
CSC-234B Data Structure & Algorithms 3 TC-114 T-H: 11:00-12:25 PM 15 J33486
CSC-242 Programming II 3 TC-303 T-H: 1:00-2:25 PM 18 A52990
CSC-320 Object Oriented Programming 3 TC-301 T-H: 1:00-2:25 PM 22 B66750
CSC-331 Applications Programming 3 TC-109 T-H: 11:00-12:25 PM 28 H99118
CSC-333A Computer Arch & Algorithms 3 TC-301 M-W-F: 10:00-10:55 AM 22 A77587
CSC-333B Computer Arch & Algorithms 3 TC-302 T-H: 11:00-12:25 PM 15 A77587
CSC-335 Internet Programming 3 TC-303 M-W-F: 1:00-1:55 PM 25 B66750
CSC-432 Discrete Algorithms 3 TC-206 T-H: 11:00-12:25 PM 20 B86590
CSC-439 Database Systems 3 TC-206 M-W-F: 1:00-1:55 PM 18 B86590
CSE-138A Introduction to CSE 3 TC-301 T-H: 1:00-2:25 PM 15 A52990
CSE-138B Introduction to CSE 3 TC-109 T-H: 1:00-2:25 PM 35 J33486
CSE-330 Digital Logic Circuits 3 TC-305 M-W-F: 9:00-9:55 AM 26 K69880
CSE-332 Foundations of Semiconductors 3 TC-305 T-H: 1:00-2:25 PM 24 K69880
CSE-334 Elec. Measurement & Design 3 TC-212 T-H: 11:00-12:25 PM 25 H99118
CSE-430 Bioinformatics in Computer 3 TC-206 Thu: 9:30-11:00 AM 16 B86590
CSE-432 Analog Circuits Design 3 TC-309 M-W-F: 2:00-2:55 PM 18 K69880
CSE-433 Digital Signal Processing 3 TC-206 T-H: 2:00-3:25 PM 18 H99118
CSE-434 Advanced Electronics Systems 3 TC-213 M-W-F: 1:00-1:55 PM 26 B78880
CSE-436 Automatic Control and Design 3 TC-305 M-W-F: 10:00-10:55 AM 29 J33486
CSE-437 Operating Systems 3 TC-303 T-H: 1:00-2:25 PM 17 A77587
CSE-438 Advd Logic & Microprocessor 3 TC-213 M-W-F: 11:00-11:55 AM 35 B78880
CSE-439 Special Topics in CSE 3 TC-206 M-W-F: 10:00-10:55 AM 22 J33486

75
76 Introduction to Databases

Table 2.26. The Data in the Student Table

student id name gpa credits Major schoolYear email


A78835 Andrew Woods 3.26 108 Computer Senior awoods@college.edu
Science
A97850 Ashly Jade 3.57 116 Information Junior ajade@college.edu
System
Engineering
B92996 Blue Valley 3.52 102 Computer Senior bvalley@college.edu
Science
H10210 Holes Smith 3.87 78 Computer Sophomore hsmith@college.edu
Engineering
J77896 Erica Johnson 3.95 127 Computer Senior ejohnson@college.edu
Science

Table 2.27. The Data in the StudentCourse Table

s course id student id course id credit major


1000 H10210 CSC-131D 3 CE
1001 B92996 CSC-132A 3 CS/IS
1002 J77896 CSC-335 3 CS/IS
1003 A78835 CSC-331 3 CE
1004 H10210 CSC-234B 3 CE
1005 J77896 CSC-234A 3 CS/IS
1006 B92996 CSC-233A 3 CS/IS
1007 A78835 CSC-132A 3 CE
1008 A78835 CSE-432 3 CE
1009 A78835 CSE-434 3 CE
1010 J77896 CSC-439 3 CS/IS
1011 H10210 CSC-132A 3 CE
1012 H10210 CSC-331 2 CE
1013 A78835 CSC-335 3 CE
1014 A78835 CSE-438 3 CE
1015 J77896 CSC-432 3 CS/IS
1016 A97850 CSC-132B 3 ISE
1017 A97850 CSC-234A 3 ISE
1018 A97850 CSC-331 3 ISE
1019 A97850 CSC-335 3 ISE
1020 J77896 CSE-439 3 CS/IS
1021 B92996 CSC-230 3 CSIS
1022 A78835 CSE-332 3 CE
1023 B92996 CSE-430 3 CE
1024 J77896 CSC-333A 3 CS/IS
1025 H10210 CSE-433 3 CE
1026 H10210 CSE-334 3 CE
1027 B92996 CSC-131C 3 CS/IS
1028 B92996 CSC-439 3 CS/IS
2.11 Create Oracle 10g XE Sample Database 77

Figure 2.56. The completed Course table.

Figure 2.57. The completed Student table.


78 Introduction to Databases

Figure 2.58. The completed StudentCourse table.

Figure 2.59. Create the foreign key between the LogIn and the Faculty table.
2.11 Create Oracle 10g XE Sample Database 79

from the Reference Table Column List box as the reference table column. Your
finished Add Constraint window should match the one shown in Figure 2.59.
Click Next to go to the next window, and then click the Finish button to confirm
this foreign key’s creation.

2.11.4.2 Create the Foreign Key Between the LogIn and Student Tables
The relationship between the Student table and the LogIn table is a one-to-many
relationship. The student id in the Student table is a primary key, and the student id
in the LogIn table is a foreign key. Multiple instances of a student id can exist in the
LogIn table, but only one, unique student id can be in the Student table.
To create a foreign key from the LogIn table and connect it to the primary key
in the Student table, you first need to open the home page of Oracle Database 10g
XE if it is not open. On the open home page window, 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.
Enter LOGIN STUDENT FK into the Constraint Name box, and select Foreign
Key from the Constraint Type box, which is shown in Figure 2.60. Check the On
Delete Cascade check box. Then select STUDENT ID from the LogIn table in the
Foreign Key Column box. Select the STUDENT table in the Reference Table Name
box as the reference table, and select STUDENT ID from the Reference Table Col-
umn List box as the reference table column. Your finished Add Constraint window
should match the one shown in Figure 2.60.
Recall that when we created the LogIn table in Section 2.11.1, we emphasized
that a NULL should not be placed into the blank fields in the faculty id and stu-
dent id columns and that these fields should be left blank. The reason for this is that
an ALTER TABLE command will be issued when you create a foreign key for the

Figure 2.60. Create the foreign key between the LogIn and the Student table.
80 Introduction to Databases

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

LogIn table, and NULL cannot be recognized by this command; therefore an error,
ORA-02298, would occur and your creation of the foreign key would fail.
Click Next to go to the next window, and then click the Finish button to confirm
this foreign key’s creation.
Your finished foreign key creation window for the LogIn table should match the
one shown in Figure 2.61.

2.11.4.3 Create the Foreign Key Between the Course and Faculty Tables
The relationship between the Faculty table and the Course table is one-to-many.
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 a unique faculty id exists in the Faculty table,
but multiple instances of a faculty id can exist in the Course table since one faculty
member can teach multiple courses.
Open the home page of Oracle Database 10g XE if it is not open. On the home
page window, click the Object Browser icon and select Browse|Table to list all
tables. Select the Course table from the left pane to open it, click the Constraints
tab, and then click the Create button. Enter COURSE FACULTY FK into the
Constraint Name box, and select Foreign Key in the Constraint Type box, which
is shown in Figure 2.62. Check the On Delete Cascade check box. Then select FAC-
ULTY ID from the Course table in the Foreign Key Column box. Select the FAC-
ULTY table from the Reference Table Name box as the reference table, and select
FACULTY ID from the Reference Table Column List box as the reference table
column. Your finished Add Constraint window should match the one shown in Fig-
ure 2.62.
Click the Next button to go to the next window, and then click the Finish button
to confirm this foreign key’s creation. Your finished foreign key creation window
for the Course table should match the one shown in Figure 2.63.

2.11.4.4 Create the Foreign Key Between the StudentCourse


and Student Tables
The relationship between the Student table and the StudentCourse table is one-
to-many. The primary key student id in the Student table is a foreign key in
2.11 Create Oracle 10g XE Sample Database 81

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

the StudentCourse table since one student can take multiple courses. To cre-
ate this relationship by using the foreign key, open the home page of Oracle
Database 10g XE if it is not open. On the open home page window, click the
Object Browser icon and select Browse|Table to list all tables. Select the Student-
Course table from the left pane to open it, click the Constraints tab, and then
click the Create button. Enter STUDENTCOURSE STUDENT FK into the Con-
straint Name box, and select Foreign Key from the Constraint Type box, which
is shown in Figure 2.64. Check the On Delete Cascade check box. Then select
STUDENT ID from the StudentCourse table in the Foreign Key Column box.
Select the STUDENT table in the Reference Table Name box as the reference table,
and select STUDENT ID in the Reference Table Column List box as the reference
table column. Your finished Add Constraint window should match the one shown
in Figure 2.64.

Figure 2.63. The finished foreign key creation window for the Course table.
82 Introduction to Databases

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

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

2.11.4.5 Create the Foreign Key Between the StudentCourse


and Course Tables
The relationship between the Course table and the StudentCourse table is one-to-
many. The primary key course id in the Course table is a foreign key in the Student-
Course 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 home page of
Oracle Database 10g XE if it is not open. On the opened home page window,
click the Object Browser icon and select Browse|Table to list all tables. Select the
StudentCourse table from the left pane to open it, click the Constraints tab, and
then click the Create button. 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 check box. Then
select COURSE ID from the StudentCourse table in the foreign key column. Select
the COURSE table in the Reference Table Name box as the reference table, and
select COURSE ID in the Reference Table Column List box as the reference table
column. Your finished Add Constraint window should match the one shown in Fig-
ure 2.65.
Click the Next button to go to the next window, and then click the Finish button
to confirm this foreign key’s creation. Your finished foreign key creation window
for the StudentCourse table should match the one shown in Figure 2.66.
2.11 Create Oracle 10g XE Sample Database 83

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

Our database creation for Oracle Database 10g XE is completed. The com-
pleted Oracle Database 10g XE sample database CSE DEPT that is represented
by a group of table files can be found in the folder database\Oracle, located at
www.cambridge.org/9780521712354. Refer to Appendix F to get more detailed
information if you want to use this sample database in your Visual Basic applica-
tions.
At this point, we have finished developing and creating all the sample databases
we will use later. All these sample databases will be utilized for different applica-
tions we will develop in the following chapters in this book.

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

You might also like