Creating Oracle 10g Xe Database
Creating Oracle 10g Xe Database
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.
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
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
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.
faculty id – VARCHAR2(10)
name – VARCHAR2(20)
office – VARCHAR2(10)
phone – CHAR(12)
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
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
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.
73
74 Introduction to Databases
75
76 Introduction to Databases
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.
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.
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.