Directory listing of http: uap.unnes.ac.id ebook biblebook Visual Basic 6 com & Library Books

27
C H A P T E R

Creating
Dat abase
Object s with
Oracle8i

I

n this c hapter, I’m go ing to sho w yo u ho w to use the two
mo st c o mmo n utilities in Orac le8i, SQL*Plus and Enterprise
Manager, to ac c ess yo ur database. Then I’ll talk abo ut ho w to
c reate tables and indexes using Enterprise Manager. Finally, I’ll
c o ver ho w to manage yo ur sec urity using Enterprise Manager.










In This Cha pter
Intro ducing O racle8 i
SQ L* Plus
Intro ducing O racle8 i
Enterprise Manag er
Co nstructing
tablespaces, tables,
and indexes
Manag ing security
Creating ro les
and users

Introducing SQL*Plus
SQL*Plus c o mes in two flavo rs: a c o mmand-line driven utility
that runs under DOS and a windo w-o riented utility that pro vides
a pane where yo u c an enter SQL statements and a pane to sho w
the results. Bo th to o ls ac c ept the same set o f SQL statements

and will return the same results. The o nly differenc e between
the two is ho w the data is presented to the user. I’ll use the c o mmand-line variety in this sec tio n.

Command-Line SQL*Plus
The c o mmand-line versio n o f SQL*Plus is started fro m a
c o mmand pro mpt b y entering SQLPLUS. Yo u c an enter c o mmands direc tly fro m the keyb o ard o r enter them into a file
and exec ute the entire file. The results o f the c o mmands will
b e displayed immediately fo llo wing the c o mmand yo u enter.
Note

DOS and commands: In trying to eradicate the concept of
DOS from the user’s m ind, Microsoft engineers cleverly
renam ed the old DOS Window to Com m and Prom pt in
Window s 2000; how ever, it’s the sam e old com m and
interpreter you’ve com e to love and hate.










600

Part VII ✦ Oracle8i

Connecting to your database with SQL*Plus
These parameters inc lude the user name, the passwo rd, and the database yo u plan
to use. If yo u do n’t supply all o f the info rmatio n, yo u will be pro mpted fo r it. The
fo rmat yo u must use is sho wn belo w:

[/][@]
where is a valid user name fo r the database yo u wish to ac c ess,
is the passwo rd asso c iated with the user name, and
c o ntains the name o f the database yo u wish to ac c ess.
Fo r example, if yo u want to have a user name o f toy and vb6db.athena.justpc.
net as the name o f the database yo u wish to ac c ess, yo u c an start SQL*Plus using
the fo llo wing c o mmand:


SQLPLUS [email protected]
SQL*Plus will pro mpt yo u fo r the passwo rd, and assuming that yo u entered it c o rrec tly, yo u c o uld be pro mpted with an SQLPLUS> pro mpt (see Figure 27-1).

Figure 27-1: Logging onto a database w ith SQL*Plus.

Chapter 27 ✦ Creating Database Objects with Oracle8i

Note

There are other options also: SQL*Plus supports several other options on the
com m and line. See Oracle8i DBA Bible by Jonathan Gennick, and published by
IDG Books, for m ore inform ation.

Caution

It’s not very hidden: While you can enter your passw ord on the com m and line, I
don’t recom m end it. In Window s 2000, som eone can sim ply press the up arrow
until they see the com m and you executed to start SQL*Plus, even inside the
SQL*Plus program . After quitting SQL*Plus, this inform ation w ill rem ain in the

com m and history of both Window s 2000/ NT and Window s 98/ 95 system s w ith
DOSKEY running. If it w as the last com m and entered, it w ill be visible by sim ply
hitting the F3 key.

Entering commands
Onc e SQL*Plus has been started, an SQL> pro mpt will be displayed. Yo u c an enter
any c o mmand o r SQL statement at the pro mpt. If the syntax is c o rrec t, the c o mmand o r SQL statement will be immediately exec uted and the results displayed
belo w what yo u typed.
If the display is to o wide, it will auto matic ally be wrapped to the next line. No te
that the lines displayed in the DOS windo w will auto matic ally sc ro ll so that the
mo st rec ent info rmatio n is displayed. This c an c ause a pro blem if yo u are exec uting a Se le ct statement, whic h returns a large number o f ro ws. In this c ase, yo u
sho uld either use SQL*Plus Windo w o r inc rease the sc reen buffer size under the
DOS windo w’s pro perty settings.
Note

Confusing, isn’t it?: Com m ands and SQL statem ents are treated differently in
SQL*Plus. Com m ands occupy only a single line, w hile SQL statem ents m ay occupy
m ultiple lines. To indicate the end of an SQL statem ent, you m ust type a sem icolon (;). Typing a sem icolon at the end of a com m and w ill generate an error m essage. Also, if you don’t supply sufficient inform ation for a com m and, you m ay be
prom pted for the additional inform ation, or you m ay receive a m essage describing
how to use the com m and.


Useful commands
Table 27-1 lists so me o f the mo re useful c o mmands yo u will find in SQL*Plus. While
this is no t a c o mplete list, it sho uld give yo u a feel fo r the types o f c o mmands available. Yo u c an use the Help Index c o mmand to find a c o mplete list o f the c o mmands
that are available.

601

602

Part VII ✦ Oracle8i

Table 27-1
Useful Commands in SQL*Plus
Command

Description

Clear SQL


Clears the contents of the SQL buffer.

Connect

Logons to a different database server or the sam e database server
as a different user.

Define _Editor

Specifies the nam e of your favorite editor program that w ill be run
w hen you use the Edit com m and.

Describe

Provides a detailed description of a database object, such as a table
or stored procedure.

Edit

Edits the contents of the SQL buffer in your favorite editor.


Get

Loads the contents of a file into the SQL buffer.

Help

Provides basic help on SQL*Plus com m ands.

Host

Runs the specified DOS com m and and returns to SQL*Plus.

List

Lists the statem ents in the SQL buffer.

Quit

Exits SQL*Plus and returns to DOS.


Run

Lists the statem ents in the SQL buffer and executes them .

Save

Saves the SQL buffer to a disk file.

Set Linesize

Determ ines the w idth of the line.

Set Newpage

Determ ines the num ber of lines displayed betw een pages.

Set Pagesize

Determ ines the num ber of lines in a page.


Set Pause

When On, m eans that the output w ill be displayed in pages.

Set Serveroutput

When On, m eans that output w ill be directed to the console w hen
using the DBMS_OUTPUT package.

Spool

Directs output to an external file or to the system printer.

Start

Runs the SQL statem ents in a disk file.

One o f the mo st useful c o mmands in SQL*Plus is the Describe c o mmand. This allo ws
yo u to quic kly see the struc ture o f a table o r the statements in a sto red pro c edure. I

find this helpful if I do n’t have a piec e o f paper handy c o ntaining c o lumns and tables
in a database.

Chapter 27 ✦ Creating Database Objects with Oracle8i

After running an SQL statement, it is sto red in the SQL buffer. Yo u c an list the c o ntents o f this buffer, edit the statement, o r run the statement o ver again using the c o mmands sho wn in Table 27-1. The c o ntents remain in the SQL buffer until ano ther SQL
statement is entered o r the buffer is explic itly c leared using the Clear SQL c o mmand.
Yo u c an list the c o ntents o f the SQL buffer with the List c o mmand. The Save and Get
c o mmands will write a c o py o f the buffer to a disk file and lo ad it bac k again, respec tively. Use the Run c o mmand to exec ute the statements in the SQL buffer.
The Edit c o mmand c o pies the SQL b uffer to a file c alled afiedt.buf and lo ads it
into uses whatever edito r yo u spec ify in the De fine _Editor c o mmand. Yo u then
edit yo ur statements, save them and c lo se the edito r to return to SQL*Plus. By
default o n a Windo ws platfo rm, yo u’ll use Notepad. Yo u c o uld also use the Save
c o mmand to save the SQL b uffer into a file and then use the Host c o mmand to
perfo rm the same func tio n. No te that yo ur SQL*Plus sessio n is suspended until
the edito r o r ho st c o mmand is c o mplete.
SQL*Plus c o ntains c o mmands that c an make wo rking with yo ur SQL statements easier. The first thing yo u sho uld do if yo u are testing Select statements is to use the Set
Pause On c o mmand. This c o mmand will auto matic ally break yo ur o utput into pages
and pause the o utput at the end o f eac h page. The number o f lines in eac h page is
c o ntro lled by the Set Pagesize c o mmand. Yo u c an also c o ntro l the number o f lines
displayed between page breaks by using the Set Newpage c o mmand.
If yo u c ho o se, yo u c an direc t the o utput o f the SQL statements to a disk file o r a
printer by using the Spool c o mmand. To redirec t yo ur o utput to a file, spec ify Spool
fo llo wed by the name o f the disk file. To redirec t yo ur o utput to the printer, issue the
Spool Out c o mmand. Then yo u c an issue any c o llec tio n o f SQL statements o r c o mmands that generate o utput. When yo u’re finished, issue the Spool Off statement to
c lo se the o utput file o r send yo ur o utput to the printer.
Tip

M ake it fit: Use the Set Pagesize , Set Newpage, and Set Linesize com m ands to
adjust the characteristics of your printer.

Introducing Enterprise M anager
Enterprise Manager is a c o mprehensive to o l designed to help yo u manage yo ur
database using graphic al to o ls. It pro vides a c entral po int fro m whic h yo u c an manage
all o f yo ur Orac le8i database servers, even if they are o n multiple physic al c o mputers.
It c o mmunic ates with the Orac le Management Server, whic h in turn c o mmunic ates
with the database servers that yo u wish to ac c ess. While no t as easy to use as SQL
Server’s Enterprise Manager, Orac le8i Enterprise Manager is a big impro vement o ver
issuing SQL statements in SQL*Plus to perfo rm c o mmo n tasks suc h as c reating tasks
and users.

603

604

Part VII ✦ Oracle8i

The Enterprise M anager console
Rather than c o mbine all o f the to o ls into a single applic atio n, Enterprise Manager
c o nsists o f the Enterprise Manager c o nso le, plus a number o f o ther utilities that
c an be launc hed fro m the c o nso le o r in a stand-alo ne fashio n. So me o f the mo re
impo rtant to o ls inc lude:

✦ SQL*Plus Worksheet — a graphic al versio n o f SQL*Plus.
✦ DBA Studio — pro vides a graphic al way to c reate and maintain yo ur database
struc tures.

✦ Enterprise Security Manager — simplifies the pro c esses needed to c reate
users and map them o nto the pro per ro les.

✦ Net8 Assistant — makes it easy to c o nfigure yo ur c o mmunic atio ns netwo rk.
Tip

There’s no substitute for page count: If you are w orking w ith a database like
Oracle8 i w ith lots of tools and utilities, you should invest in a good reference. I
suggest the Oracle8 i DBA Bible by Jonathan Gennick published by IDG Books.

Logging onto the Enterprise M anager console
When yo u initially start the Enterprise Manager c o nso le (c ho o se Start ➪ Orac le –
OraHo me81 ➪ Enterprise Manager ➪ Co nso le), yo u will see the Enterprise Manager
Lo gin windo w (see Figure 27-2). In this windo w, yo u need to supply a valid administrato r ID and passwo rd, as well as c ho o se the lo c atio n o f the Orac le Management
Server.

Figure 27-2: Logging onto the Enterprise Manager console.

Note

Yet another ID: The adm inistrative ID used to log in to the Oracle Managem ent
Server is not the sam e thing as the user nam e you specify w hen you log in to a
database. The adm inistrative ID is used to m anage netw ork resources, not
database resources.

Chapter 27 ✦ Creating Database Objects with Oracle8i

Using the Enterprise Console
The Enterprise Co nso le presents a pac ked view o f the reso urc es and to o ls that are
available fo r yo u to mo nito r and manage yo ur c o llec tio n o f database servers (see
Figure 27-3).

Help
Remove
Hide pane
Create job
Create event
Create group

Navigator pane

Extended Database Application
Service M anagement

Jobs pane

Database Application
Application M anagement
Figure 27-3: Running the Enterprise Console.

Group pane

Event pane

605

606

Part VII ✦ Oracle8i

✦ Navigator pane c o ntains info rmatio n abo ut the o bjec ts that c an be managed
thro ugh the Enterprise Manager c o nso le.

✦ Group pane is c usto mized to display the info rmatio n yo u wish fro m the
Navigato r pane.

✦ Jobs pane lists the jo bs defined in the database that perfo rms tasks like
database bac kups and batc h pro c essing.

✦ Event pane c o ntains info rmatio n abo ut the events yo u define in yo ur
database.

✦ Hide pane pro vides a quic k way to hide any o f the fo ur main panes
(Navigato r, Gro up, Jo bs and Events).

✦ Create job walks yo u thro ugh the pro c ess to add a jo b to the system.
✦ Create event displays a windo w that yo u use to define an event.
✦ Create group defines a gro up that c an be displayed in the gro up pane.
✦ Remove allo ws yo u to delete an o bjec t in the c o nso le.
✦ Help invo kes the help subsystem.
✦ Application Management allo ws yo u to launc h the Orac le Applic atio ns
Manager.

✦ Database Applications inc ludes o ptio ns to start the DBA Studio and the
SQL*Plus Wo rksheet utilities.

✦ Extended Database Applications pro vides butto ns that bring up the Orac le
interMedia Text Manager, the Orac le Replic atio n Manager, and the Orac le
Spatial Index Adviso r.

✦ Service Management launc hes the Net8 Assistant, the OSA Manager, o r the
Orac le Internet Direc to ry Manager.

Connecting to databases
In the Navigato r windo w, yo u c an explic itly c o nnec t to a database to view the
info rmatio n abo ut it. Just expand the Databases ic o n to sho w the databases that
are available. Then right c lic k o n the database and c ho o se Co nnec t fro m the po pup menu. This will display a c o nnec tio n fo rm, as sho wn in Figure 27-4.
Fill in the user name and passwo rd that yo u wish to use to ac c ess that partic ular
database instanc e. Then c hec k the Save as Preferred Credential if yo u want to
make the user name and passwo rd the default when c o nnec ting to this database.
This means that whenever yo u expand the database ic o n, yo u wo n’t be pro mpted
fo r c o nnec tio n info rmatio n. Sinc e the Enterprise Manager kno ws whic h database
yo u want to ac c ess, it supplies the info rmatio n in the Servic e field and do esn’t
allo w yo u to c hange it.

Chapter 27 ✦ Creating Database Objects with Oracle8i

Figure 27-4: Connecting the Enterprise Manager
to a database.

Applic atio n develo pers typic ally need many o f the func tio ns and reso urc es available in the Orac le8i Enterprise Manager. Ho wever, the SQL*Plus Wo rksheet utility
and the DBA Studio are two to o ls that are wo rth disc ussing in mo re detail.

SQL*Plus Worksheet
Running func tio ns inside a DOS windo w isn’t fo r everyo ne. Orac le8i inc ludes an alternative to SQL*Plus, c alled SQL*Plus Wo rksheet. This utility is basic ally a graphic al
versio n o f SQL*Plus. The main impro vement is that the SQL buffer is displayed in o ne
pane, while the results o f its exec utio n are displayed in ano ther pane. Yo u c an start
this utility direc tly fro m the Enterprise Manager o r by c ho o sing Start ➪ Orac le –
OraHo me81 ➪ Database Administratio n ➪ SQLPlus Wo rksheet.
Note

SQL*Plus Window ain’t SQL*Plus Worksheet: There’s a third version of SQL*Plus,
called SQL*Plus Window (Start ➪ Oracle – OraHom e81 ➪ Application Developm ent ➪ SQL Plus). This utility is m erely SQL*Plus for DOS running in a w indow. You
still enter com m ands at a prom pt. Its prim ary advantage is the scrollbars that allow
you to look back at the com m ands and SQL statem ents you execute and their
results.

Connecting to your database
Yo u start SQL*Plus Wo rksheet fro m the Enterprise Manager b y right c lic king o n
the datab ase yo u wish to use and c ho o sing Datab ase Applic atio n ➪ SQL*Plus
Wo rksheet fro m the po p-up menu. If yo u were already c o nnec ted to the datab ase,
o r have a preferred c redential fo r this datab ase, yo u will auto matic ally b e c o nnec ted when SQL*Plus Wo rksheet starts. Otherwise, SQL*Plus Wo rksheet will b e
started witho ut an ac tive c o nnec tio n, and yo u will need to exec ute a Conne ct
statement to c o nnec t to yo ur datab ase.

607

608

Part VII ✦ Oracle8i

If yo u start SQL*Plus Wo rksheet using the Start butto n, yo u’ll be pro mpted with the
Enterprise Manager Lo gin fo rm. Yo u have a c ho ic e o f two ways to lo g in. Yo u c an lo g
in thro ugh the Orac le Management Server using yo ur administrato r ID and passwo rd,
o r yo u c an lo g in direc tly to the database using yo ur no rmal database passwo rd (see
Figure 27-5). If yo u wish, yo u c an c lic k o n the Lo gin to the Orac le Management Server
and lo g in to the Orac le Management Server first. Then the preferred c redentials will
be auto matic ally used fo r this partic ular database instanc e.

Figure 27-5: Login to
SQL*Plus Worksheet using
a norm al database user
nam e.

Running SQL*Plus Worksheet
After lo gging o nto SQL*Plus Wo rksheet, yo u’ll see a two -paned windo w with a set o f
ic o ns alo ng the left side, as sho wn in Figure 27-6. The to p pane ho lds the SQL statements yo u wish to exec ute, while the bo tto m pane c o ntains their results. When yo u
initially lo g in, yo u’ll see the results o f the Connect c o mmand that was used to
ac c ess the database.

✦ Command pane is the pane where yo u enter yo u SQL statements and/ o r c o mmands fo r exec utio n.

✦ Results pane is the pane where the results fro m exec uting a c o mmand are displayed.

✦ Connection allo ws yo u to lo g o ut fro m the database and bac k o n as a different
user.

✦ Execute starts pro c essing the SQL statements listed in the c o mmand area.
✦ History maintains a list o f the c o mmands and SQL statements yo u have
exec uted during this sessio n.

✦ Help displays the Enterprise Manager Help System

Chapter 27 ✦ Creating Database Objects with Oracle8i

Connection

Execute

Help

Command pane

Results pane

History
Figure 27-6: Running SQL*Plus Worksheet.

As yo u might expec t, all yo u need to do to exec ute a c o mmand o r SQL statement is
to enter it into the Co mmand pane and press the Exec ute butto n. One wo rd o f c autio n — o nc e yo u start exec uting a c o mmand, yo u c an’t sto p it. While this isn’t a pro blem fo r mo st c o mmands, c o nsider the pro blem that might o c c ur if yo u try to Select
several tho usand ro ws fro m a table.

609

610

Part VII ✦ Oracle8i

DBA Studio
The DBA Studio utility is designed to make yo ur life easier when yo u’re designing
an Orac le8i database. Many func tio ns that previo usly required yo u to spec ify very
c o mplex SQL statements, like Create Table o r Create Schema , are no w implemented in a muc h easier to use GUI.
Tip

A real Oracle DBA uses ERWin: While DBA Studio is a pow erful tool, you m ight
w ant to consider using ERWin by Com puter Associates. While this product is
som ew hat expensive, it can be w orth its w eight in gold w hen designing com plex
Oracle databases, and unlike database designer solutions from the database vendor, this tool allow s you to design databases that can be im plem ented for nearly
any database vendor.

Starting DBA Studio
Yo u c an start this utility direc tly fro m the Enterprise Manager, o r by c ho o sing
Start ➪ Orac le – OraHo me81 ➪ Database Administratio n ➪ DBA Studio . When yo u
start the DBA Studio direc tly fro m Windo ws, yo u will be pro mpted to launc h DBA
Studio standalo ne o r to lo g in to the Orac le Management Server. In either c ase, yo u
will want to c o nnec t to the database as SYSDBA (see Figure 27-7). This will ensure
that yo u have all o f the appro priate c apabilities yo u’ll need while designing yo ur
database.

Figure 27-7: Logging onto DBA Studio in
standalone m ode.

Caution

But it’s encrypted: If you choose to save your login inform ation as the preferred
credential w hen logging into DBA Studio in standalone m ode, you should know
that the passw ord is stored on your local hard disk. Even though your passw ord is
encrypted, anyone w ho has physical access to your m achine can access DBA
Studio by using your default login inform ation.

Chapter 27 ✦ Creating Database Objects with Oracle8i

Running DBA Studio
DBA Studio presents a two -paned view similar to that used by SQL Server
Enterprise Manager (see Figure 27-8). It also inc ludes a c o lumn o f ic o ns do wn the
left side o f the windo w that yo u c an use to perfo rm c o mmo nly used func tio ns.

Connect
Save list
Refresh
Show dependencies
Tree view

Remove
Help
Create like
Create
Figure 27-8: Running DBA Studio.

Details view

611

612

Part VII ✦ Oracle8i

✦ Tree view c o ntains a hierarc hic al list o f ic o ns that referenc e the database
o bjec ts that yo u c an manipulate with this utility.

✦ Details view pro vides additio nal info rmatio n abo ut the c urrently selec ted
ic o n in the tree view.

✦ Connect allo ws yo u to establish a database c o nnec tio n to ano ther database.
✦ Save List c reates a file that c o ntains the list o f o bjec ts, suc h as the tables that
are c o ntained in a partic ular sc hema.

✦ Refresh gets a fresh c o py o f the info rmatio n displayed in the details view and
the ic o n view.

✦ Show Dependencies displays the o bjec ts that the selec ted o bjec t depends o n,
as well as tho se o bjec ts that depend o n the selec ted o bjec t.

✦ Create c reates a brand new o bjec t in the c urrently selec ted c atego ry in the
ic o n view.

✦ Create Like c reates a new o bjec t based o n the c urrently selec ted o bjec t.
✦ Remove deletes the c urrently selec ted o bjec t.
✦ Help invo kes the help subsystem.

Creating Tablespaces
Sinc e c reating databases is a task best suited to a database administrato r, I want to
begin by walking yo u thro ugh the pro c ess to c reate a tablespac e. A table space represents a po o l o f disk sto rage that c an be used to ho ld tables and indexes.

Before you create a tablespace
Befo re yo u c reate a tablespac e, yo u sho uld c o nsider the fo llo wing questio ns:

✦ Whic h database instanc e sho uld ho ld the new tablespac e? Mo st servers that
run Orac le8i run multiple database instanc es. This allo ws the database administrato r to allo c ate tablespac e and tables to different database instanc es to
o ptimize perfo rmanc e.

✦ What name do yo u want to give the new tablespac e? Tablespac e names c an
be fro m 1 to 30 c harac ters in length and c an’t duplic ate the name o f ano ther
tablespac e o r an Orac le8i reserved wo rd.

✦ Whic h disk lo c atio n sho uld ho ld the files fo r the tablespac e? This info rmatio n
will vary depending o n the o perating system. On Windo ws-based systems,
yo u’ll have to pro vide the drive, direc to ry path, and file name fo r eac h file
that will ho ld the data in the tablespac e.

Chapter 27 ✦ Creating Database Objects with Oracle8i

✦ What sho uld the initial size o f eac h file be? If yo u need to gro w yo ur
tablespac e, yo u c an add additio nal files o r use the Alter Tablespace c o mmand to mo dify ho w the existing data files c an gro w.

Creating your tablespace
Start Orac le DBA Studio and fo llo w these steps:

1. Verify that yo u are c o nnec ted to the database where yo u wish to add a
tablespac e and c lic k o n the Create ic o n. This will display the Create On
windo w, as sho wn in Figure 27-9.

Figure 27-9: Select the tablespace
icon to create a new tablespace.

2. Cho o se Tablespac e and press the Create butto n. This will display the Create
Tablespac e pro perty windo w, as sho wn in Figure 27-10. Then yo u just need to
fill in the blanks. Enter a name fo r the tablespac e. This will auto matic ally fill in
a value fo r the file name. If yo u wish, yo u c an c hange the name o f the file. If
yo u plan to add additio nal files to this tablespac e, yo u sho uld inc lude a _01 to
indic ate that this is the first file. The File Direc to ry will default to the o ne c urrently used by the database. Yo u c an also c hange it, if yo u wish to plac e the
file o n ano ther disk drive. Finally, yo u need to fill in the size o f the tablespac e.
If yo u c lic k o n the area next to the numeric value under Size, yo u c an c hange
the units fro m megabytes ( MB) to kilo bytes ( KB).

3. Press the Create butto n to allo c ate the spac e fo r the tablespac e. A message
bo x will be displayed, letting yo u kno w when the tablespac e has suc c essfully
been c o mpleted.

613

614

Part VII ✦ Oracle8i

Figure 27-10: Provide the inform ation for the
tablespace.

Creating Tables and Indexes
Befo re yo u c reate the tab les fo r yo ur applic atio n, yo u sho uld dec ide o n a name
fo r yo ur datab ase sc hema. A sc hema name will help yo u gro up all o f the datab ase
o b jec ts yo u c reate into a single entity. While yo u do n’t explic itly c reate yo ur
sc hema in the DBA Studio , yo u referenc e its name eac h time yo u c reate a new
datab ase o b jec t.
Creating a table isn’t muc h mo re than spec ifying a name fo r yo ur table and the list
o f c o lumns yo u want inc luded. Ho wever, the mo re time yo u spend preparing to
build yo ur table, the better o ff yo u will be. Here are a few questio ns yo u may want
to c o nsider.

✦ What tablespac e will ho ld yo ur table?
✦ What is the name o f yo ur table? Yo ur table name sho uld be prefixed by the
sc hema name that yo u want to use. If yo u do n’t spec ify a sc hema name, yo ur
user name will be used. Bo th identifiers c an be up to 30 c harac ters in length.
While yo u c an use spac es and o ther spec ial c harac ters as part o f the table
name, yo u will have to enc lo se the table name in quo tatio n marks eac h time
yo u use it. Also , this will make the table name c ase-sensitive. Also , remember
that the table name must be unique within the sc hema yo u spec ify.

✦ What is the name o f eac h c o lumn? The rules fo r naming a c o lumn are the
same as naming the table.

Chapter 27 ✦ Creating Database Objects with Oracle8i

✦ What is the data type fo r eac h c o lumn? The data type sho uld be o ne o f the
data types listed in Chapter 26. Yo u may have to c ho o se the size o f the field
and the sc ale depending o n the data type yo u selec t.

✦ Sho uld the c o lumn ac c ept Null values? No te that Null values sho uld no t be
used in any c o lumn that is used as part o f a primary o r fo reign key.

✦ Is there a default value fo r the c o lumn? This is an o ptio nal field that allo ws yo u
to insert a spec ific value into the table instead o f marking the c o lumn as Null .
✦ Sho uld yo u perfo rm a referential integrity c hec k? This implies that eac h value
entered in this field must exist in ano ther table in this database.

Creating your table
Orac le DBA Studio allo ws yo u to c reate a table thro ugh a multi-step wizard o r by
displaying a wo rksheet-like fo rm to fill o ut. I’m go ing to step yo u thro ugh the wizard
sinc e it simplifies muc h o f the wo rk. Then I’ll sho w yo u ho w to mo dify the table
using the fo rm.

1. Start DBA Studio , selec t the appro priate database, and press the Create butto n to display the o bjec t list, as sho wn in Figure 27-11. Selec t Table fro m the
list, make sure the Use Wizard c hec k bo x is c hec ked, and then press the
Create butto n.

Figure 27-11: Starting the
Create Table w izard.

2. In the first step o f the Table Wizard (see Figure 27-12), yo u are asked to spec ify the name o f the table, the sc hema, and the tablespac e that sho uld be used.

615

616

Part VII ✦ Oracle8i

Figure 27-12: Entering basic inform ation about the table.

3. In the next step o f the wizard (see Figure 27-13), yo u will enter the c o lumns
yo u want to inc lude and their data type. Yo u c an also spec ify a default value
fo r eac h c o lumn as well. Simply enter the info rmatio n in the Pro perties o f
Co lumn sec tio n o f the fo rm and press the Add butto n. Yo u c an also remo ve a
c o lumn that yo u have added by selec ting it in the Co lumns defined area o f the
fo rm and pressing Remo ve.

Figure 27-13: Entering colum n inform ation.

Chapter 27 ✦ Creating Database Objects with Oracle8i

4. In step 3 o f the Table Wizard, yo u selec t the primary key (see Figure 27-14).
While Orac le permits yo u to c reate the table witho ut a primary key, I believe
that every table yo u c reate sho uld have a primary key. Cho o se the Yes, I Want
To Create a Primary Key radio butto n to display the list o f c o lumns fo r the
table, and then spec ify the o rder o f the c o lumns that c o mprise the primary
key. If a c o lumn is no t part o f the primary key, leave the o rder field blank.

Figure 27-14: Selecting the prim ary key for your table.

5. Pressing Next allo ws yo u to spec ify Null and Unique c o nstraints o n eac h field
in yo ur new table (see Figure 27-15). To mo dify a c o lumn, selec t the c o lumn in
the Co lumns Defined area o f the fo rm. Yo u sho uld always ensure that bo th c o nstraints are selec ted fo r the primary key. Ho wever, yo u sho uld o nly use Unique
fo r the primary key if yo u have a very go o d reaso n fo r do ing so bec ause eac h
Unique c lause yo u add after the primary key impo ses a lo t o f extra wo rk eac h
time yo u insert a new ro w.

6. Fo reign key c o nstraints are selec ted in step 5 o f the Tab le Wizard ( see
Figure 27-16) . Fo r eac h c o lumn that referenc es the primary key o f ano ther
tab le, c ho o se Yes, The Co lumn is a Fo reign Key and then spec ify the name
o f the Sc hema, Tab le, and Co lumn that c o mprise the fo reign key. If yo u have
no fo reign keys, then c ho o se No , The Co lumn is no t a Fo reign Key.

617

618

Part VII ✦ Oracle8i

Figure 27-15: Choosing Null and Unique constraints for each
colum n in your table.

Figure 27-16: Defining foreign key relationships.

7. Step 6 allo ws yo u to define c hec k c o nstraints fo r eac h c o lumn (see Figure
27-17). These are tests that the value yo u insert into the c o lumn must meet
befo re the ro w c an be inserted o r updated. Selec t the c o lumn in the Co lumns
defined sec tio n o f the fo rm, and then add the c o nstraint if desired.

Chapter 27 ✦ Creating Database Objects with Oracle8i

Figure 27-17: Creating check constraints.

8. The remaining steps o f the wizard allo w yo u to o ptimize ho w the tab le is
sto red. Unless yo u are a kno wledgeab le Orac le datab ase administrato r, I
suggest that yo u skip these steps. Pressing the Finish b utto n will c reate
yo ur tab le. When the wizard has finished, a message b o x will b e displayed
indic ating that yo ur tab le was suc c essfully c reated.
Tip

Like Create Like, man: If you have to create a table that is sim ilar to another table
that is already in your database, consider clicking on the Create Like button,
instead of the Create button. Select an existing table in tree view and press the
Create Like button. Your new table w ill include all of the definitions that are in the
table you previously selected. You m ay then m ake any m odifications you w ant —
add colum ns, delete colum ns, change nam es, data types, and so on. This can be
m uch easier than you think.

M odifying a table
Yo u c an mo dify a tab le b y right c lic king o n the tab le name fro m the tree view and
c ho o sing the Edit o ptio n fro m the po p-up menu. This will display the Edit Tab le
pro perty windo w, as sho wn in Figure 27-18. Yo u may c hange any o f the c o lumn’s
c harac teristic s, inc luding Datatype, Size, Sc ale, Fo reign Key Referenc es ( Ref) ,
Nulls, and Default Value, all o n this single fo rm. No tic e that yo u c an’t c hange any
o f the info rmatio n at the to p part o f the fo rm suc h as the name o f the tab le, its
sc hema o r tab le spac e.

619

620

Part VII ✦ Oracle8i

Figure 27-18: Modifying a
table’s characteristics.

Yo u c an make any c hanges yo u wish to a table as lo ng as it do esn’t have data in it. If
yo ur table do es c o ntain data, yo u c an make c hanges as lo ng as yo u fo llo w these rules:

✦ A new c o lumn must ac c ept Null values. If yo u wish to add a c o lumn that do esn’t permit Null values, yo u add the c o lumn permitting Null values, then use an
Update statement to assign a value to this c o lumn fo r every ro w in the table.
Then yo u c an mo dify the c o lumn to pro hibit Null values.
✦ Yo u always inc rease the size o f a c harac ter-based c o lumn ( Char, Varchar2 ,
etc .). If yo u want to dec rease the size, yo u must first c hange the value fo r eac h
ro w in the table to Null , then make the c hange.

✦ Yo u c an always inc rease the number o f digits o r inc rease o r dec rease the
number o f dec imal plac es in a Numbe r c o lumn. To dec rease the number o f
digits in the c o lumn, yo u must first ensure that eac h ro w c o ntains a Null
value fo r this c o lumn befo re making the c hange.

✦ Yo u c an c hange the data type assigned to a c o lumn o nly if the c o lumn’s value
fo r eac h ro w in the table is Null .
Tip

There’s always a way if you really want to do something: One w ay to change
the characteristics of a colum n is to add a tem porary colum n to your table that
accepts Null values, w ith all of the characteristics you w ant the changed colum n to
have. Then use an Update statem ent and assign the value from the original colum n to the tem porary colum n using w hatever functions are necessary to convert
the value properly. Then delete the old colum n from your table and give the original colum n’s nam e to the tem porary colum n.

Chapter 27 ✦ Creating Database Objects with Oracle8i

Creating an index
Yo u c an always add an index to yo ur table by c lic king the Create butto n and c ho o sing Index fro m the list o f available database o bjec ts. This will display the Create
Index pro perty windo w (see Figure 27-19). Simply fill in the values fo r the name o f
the index, the sc hema it will be asso c iated with, and the tablespac e it will use fo r
sto rage. Then c ho o se the sc hema and table name fo r the table where the index will
be applied. Next, c ho o se the c o lumns and the o rder they will appear in the index
and and press Create to build the index.

Figure 27-19: Creating an index.

M anaging Security
Managing sec urity is an impo rtant part o f every datab ase management system
( DBMS) . As yo u wo uld expec t, it invo lves three main pro c esses: c reating ro les,
c reating users, and assigning permissio ns to b o th users and ro les.

Creating roles
As yo u’ve seen befo re, a ro le is a way to bundle to gether permissio ns and o ther
ro les to fo rm a single entity that c an be asso c iated with a user. Creating a ro le isn’t
very diffic ult. Simply press the Create butto n, and selec t Ro le fro m the list o f

621

622

Part VII ✦ Oracle8i

database o bjec ts. This will display the Create Ro le pro perty windo w (see Figure
27-20). Yo u must enter a name fo r the ro le and c ho o se whether yo u want additio nal
authentic atio n fro m the user in o rder to enable the ro le.

Figure 27-20: Creating a new role.

On the Ro le tab, yo u c an c ho o se to add any existing ro les to this ro le. This means
yo u c an c reate a single ro le that enc o mpasses several o ther ro les, thus simplifying
sec urity management.

Creating users
Using DBA Studio , yo u c an c reate a user very easily b y pressing the Create b utto n
and selec ting Users fro m the list o f datab ase o b jec ts. This will display the Create
User pro perty windo w. This pro perty windo w has three tab s: General, Ro le, and
System Privileges, eac h o f whic h ho lds part o f the info rmatio n availab le ab o ut a
user. After filling o ut the info rmatio n in eac h tab , simply press the Create b utto n
to c reate the new user.
Tip

Create Like strikes again: Creating a new user is one of the m ost painful processes any com puter center has to m anage. One w ay to avoid som e of the pain is
to create a standard user and then use the Create Like process to duplicate it,
including all of its security roles and privileges. While you should still review everything to m ake sure that it is appropriate for the user you are creating, Create Like
w ill help you avoid m aking sim ple m istakes.

Chapter 27 ✦ Creating Database Objects with Oracle8i

Entering general information for a user
Figure 27-21 sho ws the General tab o f the Create User pro perty windo w. To c reate a
new user, simply fill o ut the user’s name and spec ify the passwo rd info rmatio n. Then
asso c iate the user with the appro priate Default and Tempo rary tablespac es. If yo u
c hec k the Expire Passwo rd No w c hec k bo x, the user will be required to c hange their
passwo rd the first time they lo g o n to the system. Also , selec ting Lo c ked means that
the user is pro hibited fro m using the user name until yo u selec t the Unlo c ked radio
butto n.

Figure 27-21: Entering
general inform ation about
a user.

Note

Three strikes and you’re locked: You can enable a feature in your database that
w ill lock a user out of the database if they specify an invalid passw ord three tim es
in a row.

Granting roles
In o rder fo r the user to perfo rm any useful wo rk, yo u must assign them to o ne o r
mo re ro les (see Figure 27-22). At a minimum, they sho uld be granted the CONNECT
ro le, whic h will permit them to Connect to the database server. Simply selec t the
ro les yo u wish to grant to the user and press the arro w that is po inting do wn. The
selec ted ro les are c o pies to the Granted pane. When the user is c reated, the user
will inherit the sec urity permissio ns o f eac h o f the ro les in the Granted pane.

623

624

Part VII ✦ Oracle8i

Figure 27-22: Selecting roles for a user.

Selecting system privileges
Fo r the mo st part, a user sho uldn’t be assigned any o f the privileges listed o n the
System Privileges tab (see Figure 27-23). These privileges sho uld be reserved fo r
database administrato rs and/ o r database o perato rs. (See Chapter 26, “Overview o f
Orac le8i,” fo r a mo re detailed disc ussio n abo ut the system privileges and what they
permit the user to do ).

Granting permissions
In o rder to sec ure yo ur database, yo u must use o ne o f the fo rms o f SQL*Plus and
enter the appro priate Grant and Revoke statements. Yo u c an’t do this fro m DBA
Studio . To simplify the permissio ns pro c ess, I suggest that yo u c reate separate ro les
fo r eac h type o f ac c ess yo u want to pro vide fo r a table and grant the reso urc es to the
ro le. This means yo u might have a ro le fo r read ac c ess, o ne fo r update ac c ess, and
ano ther fo r administrative use. I wo uld then repeat this pro c ess fo r eac h database
o bjec t (view, sto red pro c edure, etc .) that is part o f the applic atio n.

Chapter 27 ✦ Creating Database Objects with Oracle8i

I wo uld then c reate ano ther ro le fo r eac h ac c ess level fo r yo ur applic atio n and assign
eac h o f the lo wer-level ro les c reated earlier that are appro priate fo r the applic atio n.
This simplifies the pro c ess o f administratio n, sinc e yo u wo uld o nly have to asso c iate
o ne ro le fo r eac h applic atio n that the user has ac c ess to . Then if yo u c hange the
applic atio n slightly, yo u need o nly manage the permissio ns asso c iated with the
lo wer-level ro les.

Figure 27-23: Selecting
system privileges.

Thoughts on Creating Oracle8 i Database Objects
Creating database objects in Oracle8 i can be very sim ple or incredibly com plex —it’s your
choice. Oracle8 i includes m any features that you can exploit w hen you build your database
that I haven’t discussed here. These features can m ake you database m uch m ore efficient.
How ever using these features can be difficult. This is another reason that an experienced
database adm inistrator is w orth their w eight in m em ory chips.
Not all of the options available to you via SQL statem ents are available through the DBA
Studio. If you need to be able to m anage the details of how the data files are allocated in a
table space or define m ore com plex relationships betw een tables, you w ill need to use one
of the SQL*Plus variants to execute the specific SQL com m ands that perform the desired
function.

625

626

Part VII ✦ Oracle8i

Summary
In this c hapter yo u learned:

✦ abo ut SQL*Plus and so me useful c o mmands yo u may want to use.
✦ abo ut Enterprise Manager and ho w to use it to ac c ess multiple database
instanc es.

✦ abo ut SQL*Plus Wo rksheet and ho w it differs fro m SQL*Plus.
✦ ho w to c reate a tablespac e.
✦ ho w to c reate tables and indexes.
✦ ho w to manage sec urity by c reating users and granting ro les.