This means a clientconnected to local database EAST_PROD can use a link stored in database EAST_PROD to access information in a remote database MID_PROD, but users connected to base MID_
Trang 1employees COLUMN OBJECT(emp_name) CHAR(30),emp_age INTEGER EXTERNAL(3),emp_id CHAR(7) NULLIF emps.emps.emp_id=BLANKS)),projects NESTED TABLE SDF(CONSTANT ’pr.txt’ “fix 71”)(project_id char(5) INTEGER EXTERNAL(5),project_name char(30) CHAR
NULLIF projects.project_name=BLANKS,proj_desc LOBFILE( proj_desc.txt) CHAR(2000)
TERMINATED BY “<>\n”))
In this example we have a VARRAY of employees and a nested table of projects Ifthe filler column EMP_CNT is 0, then the array is initialized to EMPTY; and if theCOUNT is greater then 0, then the VARRAY is created with the number of elementsequal to EMP_CNT As for the nested table of projects, we just load the data into thenested table, since it has no limitations on rows as does the VARRAY
Listing 22.14 has two examples of loading LOBs in SQL*Loader In the first, theLOBs are loaded from a delimited field; the second example loads LOBs from LOB files
LOADING OBJECTS, COLLECTIONS, AND LOBS
P A R T
III
Trang 2CHAPTER 22 • SQL*LOADER
1000
Listing 22.14: Loading LOBs
LOAD DATAINFILE ‘lob_example.dat’ “str ‘|’”
INTO TABLE candidateFIELDS TERMINATED BY ‘,’
(name CHAR(25),
“RESUME” CHAR(507) ENCLOSED BY ‘<beg_lob>’ AND ‘<end_lob>’)
lob_example.datMark Blomberg,<startlob> Mark D Blomberg
1234 East Park Vistamblomberg@here.comResume info here <endlob>
|Robert Freeman,<startlob> Robert G Freeman
1234 West Bay Driverfreeman@east.comResume info here <endlob>
LOAD DATAINFILE ‘lob2_example.dat’
INTO TABLE candidateFIELDS TERMINATED BY ‘,’
(name CHAR(20),ext_fname FILLER CHAR(40),
“RESUME” LOBFILE(ext_fname) TERMINATED BY EOF)
Mark Blomberg,mdb_resume.txt,Robert Freeman,’/candidate/rgf_resume.txt’,
Mdb_rsume.txt
Mark D Blomberg
1234 East Park Vistamblomberg@here.comResume info here
Rgf_resume.txt
Robert G Freeman
1234 West Bay Driverfreeman@east.com
Trang 3Loading Partitioned Objects
As fully discussed in Chapter 24, in Oracle a partitioned table is one that is separated(grouped) into logical sets SQL*Loader supports loading of partitioned tables with allload types (conventional path, direct path, and parallel direct path) SQL*Loader hasmade this transparent and you will not need to do any extra control file setup or dataprocessing to handle this type of table All the topics covered in this chapter apply topartitioned tables, as well
Loading of the following partitioned objects is supported in Oracle 8i SQL*Loader:
• A single partition of a partitioned table
• Multiple partitions of a partitioned table
• All partitions of a partitioned table
• Nonpartitioned table
Running SQL*Loader
You can run SQL*Loader from the command line, from a script, or using a parameterfile In this section, all examples demonstrate execution of SQL*Loader in a Unixenvironment
SQL*Loader from the Command Line
Command-line execution of SQL*Loader is easy From the system prompt, you enter
sqlldr with the required series of keywords and values (Refer to your Oracle
docu-mentation for the specific command to execute on your operating system.) If you
issue sqlldr with no keywords, SQL*Loader displays all the available keywords and
RUNNING SQL*LOADER
P A R T
III
Trang 4CHAPTER 22 • SQL*LOADER
1002
Listing 22.15: SQL*Loader Command Options (All Platforms)
Sqlldruserid Oracle username/passwordcontrol Control file namelog Log file namebad Bad file namedata Data file namediscard Discard file namediscardmax Number of discards to allow
(Default all)skip Number of logical records to skip(Default 0)
load Number of logical records to load(Default all)
errors Number of errors to allow
(Default 50)rows Number of rows in conventional path bind array
or between direct path data saves(Default: Conventional Path 64, Direct path all)bindsize Size of conventional path bind array in bytes
(System-dependent default)silent Suppress messages during run
(header, feedback, errors, discards, partitions, all)direct Use direct path
(Default FALSE)parfile Parameter file: name of file that contains
parameter specificationsparallel Perform parallel load
(Default FALSE)readsize Size (in bytes) of the read bufferfile File to allocate extents from
Here is an example of starting SQL*Loader from the command line:
sqlldr userid=mark/mark_sql control=1461c1.ctl log=servtype.logdata=servtype.csv bad=servtype.bad discard=servtyp.disparallel=true errors=10000 rows=1000 bindsize=100000000
Trang 5Using a Parameter File
Parameter file execution of SQL*Loader is easy as well You create a file that will holdthe parameters and their values Then you can specify that file when issuing theSQLLDR command
SQLLDR PARFILE=par_file_examp.par
Par_file_examp.paruserid=mark/mark_sqlcontrol=1461c1.ctllog=servtype.logdata=servtype.csvbad=servtype.baddiscard=servtyp.disparallel=trueerrors=10000rows=1000bindsize=100000000
NOTE Depending on your operating system, you may not be allowed to have spacesbetween the equals sign and the parameter value
SQL*Loader Script File Execution
Executing SQL*Loader from a script file is similar to using the command line and the
parameter file Within the script, you call SQL*Loader (sqlload), and then you
desig-nate a combination of command-line parameters as well as the parameter file
./load_sql.sh
contents of load_sql.shsqlldr mark/mark_sql
\control=1461c1.ctllog=servtype.log
RUNNING SQL*LOADER
P A R T
III
Trang 6First, here is the overall syntax for SQL*Loader:
The infile_clause is used to identify the files that actually hold the data to beloaded into the Oracle table This clause also identifies the files for bad records anddiscarded rows
Trang 7The concatenation clause creates one logical row from one or more physicalrecords from the input data The two options for this clause are CONCATENATE andCONTINUEIF
Each and every table to be loaded is specified with a separate into_table_clause,shown in the next two diagrams
RUNNING SQL*LOADER
P A R T
III
Trang 8CHAPTER 22 • SQL*LOADER
1006
The field_condition_clause, generally part of the column_specification, comparesthe named field to a given value Optionally, a null can be inserted into the column,utilizing the NULLIF or DEFAULTIF clause of column_specification The delimiterspecification designates how the data is to be parsed in the input file, including termi-nation characters and enclosure identifiers The column_specification tells
SQL*Loader how each field in the input record is to be interpreted
The data_type_specification is used by the column_specification for mapping theexternal representations of data in the input file into the internal representationswithin the database table
Trang 9RUNNING SQL*LOADER
P A R T
III
Trang 10data-• Use Java with Oracle8i
Trang 12CHAPTER 23
Oracle Database Links
Database link architecture 1012
Administering database links 1018
Using database links 1022
Trang 13A database link is exactly what its name implies: a connection between two
database servers allowing clients to access both servers as one logical unit
This is the central concept in distributed database systems This chapter
cov-ers the basic concepts of database links: how they work, how to manageand name them, and how to use them
Database Link Architecture
What is a database link and why would you use it? Simply put, a database link is apointer from one physical database to another physical database The two databases
can be on the same or separate physical computers The database link is only a
one-way communication from the originating database to the destination database.Keep in mind that the link is merely a pointer defined in the data dictionary table,and that is what makes the connection between the databases This means a clientconnected to local database EAST_PROD can use a link stored in database EAST_PROD
to access information in a remote database MID_PROD, but users connected to base MID_PROD cannot use the same link to access data in database EAST_PROD.When MID_PROD local users want to access data on EAST_PROD, they must define alink that is stored in the data dictionary of database EAST_PROD
data-The database links provide a means for users to manipulate objects within theremote database Users can issue DML statements such as SELECT, INSERT, UPDATE,and DELETE The remote server executes all operations on objects in the remote data-base, and only the results of those operations are passed back to the server that issuedthe call through the link Thus, database links make distributed database processing areality in the Oracle world
In order for a connection through a database link to occur, each database in the
distributed system must have a unique global database name in the network domain.
This global database name uniquely identifies a database server in a distributed tem You’ll find complete coverage of the global name concept in Chapter 9
sys-In Figure 23.1, a user can access four different distributed databases from a singlecentral corporate database The user will be able to access the tables that are on thePROD (Production) database on any of the four divisions (NW_DIV, NE_DIV, SW_DIV,and SE_DIV), depending on how links are established
Trang 14FIGURE 23.1
An arrangement of
database links
Advantages Offered by Database Links
One advantage of a database link is that it enables connection to a remote databaseserver and allows limited access (or unlimited, depending on how you configure theremote server account) to outside users You don’t have to keep the same set of data
on two different databases to support an enterprise with multiple locations Databaselinks allow query access of data on the remote system Thus MARK, a local user on thecorporate production database (PROD in the example in Figure 23.1), can link to andaccess the data at remote database PROD.NE_DIV without having to be a user on thatdatabase
Let’s consider a project-related example of linked databases, using Figure 23.1 as aguide Say we have projects established in local databases around the nation, eachtracking all information for its particular region We have databases named NW_DIV,SW_DIV, NE_DIV, and SE_DIV You’re working at corporate headquarters (the databasenamed CORP_DIV) and you have the responsibility of checking the status of all the
Database=PRODDivision=NW_DIVBranch=MEADDomain=COM
Database=PRODDivision=NE_DIVBranch=MEADDomain=COM
Database=PRODDivision=SW_DIVBranch=MEADDomain=COM
Database=PRODDivision=SE_DIVBranch=MEADDomain=COM
Database=PRODDivision=CORP_DIVBranch=MEADDomain=COM
User=MARKTables=PEOPLEPROJECTSTASKS
Trang 15CHAPTER 23 • ORACLE DATABASE LINKS
1014
national projects Instead of having to keep a set of data from each region, you canestablish a link to each of the _DIV locations By accessing their data, you can runreports and queries on the most current version of each region’s data This is known
as distributed processing.
Database Link Types
When you want to create a database link, you need to decide what type of link youwish to create The three types of database links are private, public, and global Table 23.1 gives a brief description of each type, and you’ll learn more about them asyou work through this chapter The table also summarizes the pros and cons of eachlink type You’ll want to consider these against the requirements of your own distrib-uted environment
TABLE 23.1: TYPES OF DATABASE LINKS
Disadvantages arethe same as for apublic link
Many users canaccess the link
Management isglobal and thuseasier to do
All users andPL/SQL programsPUBLIC user
Global
Many users canaccess the link, sonot as secure Likeall public entities,
it is accessible to alarge audience,which can meanheavy networktraffic
Many users canaccess the link
All users andPL/SQL programsPUBLIC user
Public
Only the user whocreated the linkcan access it
Most secure linkOnly the user who
created the link
User who ated the linkPrivate
Trang 16NOTE If your database is running multithreaded server (MTS), your public, private, and
global database links can all be established as shared links The primary purpose of a
shared database link is to take advantage of MTS’s ability to handle multiple database nections through one server process This reduces the overall load on the database server
con-Connection Methods
Having decided what link type you wish to create, you then choose a type of
connec-tion method to be used when you create the link The connecconnec-tion method of a link
determines how Oracle accesses the remote host through the database link The nection method also determines what objects your link has access to In addition,your each connection option has particular influence on the security of the remotedatabase to which you connect
con-• In a connected user link, the originating user connects as him/herself The user
on the originating database must have an account on the destination databasewith the same username as that on the originating database
• A fixed user link makes the connection as the user identified in the link That is,
if MARK (a user at the originating site) connects with a fixed link to the Projectsdatabase with the username/password combination of DEN_REM/GUEST, thenMARK will have all the rights and privileges granted to DEN_REM
• The current user link is an aspect of the Oracle Advanced Security Option and
should be employed with caution In a current user link, the user always nects as a global user and can thus access the remote database without the user’sglobal password being stored in the link definition For example, ROBERT canaccess a procedure that MARK wrote, accessing MARK’s account and MARK’sschema on the Projects database
con-WARN I NG The most secure of these connection methods is the connected userlink, which grants limited access on remote databases to local users Of course, the mostsecure method is, by virtue of its secure nature, more difficult to use We find that fixeduser links are employed most often in the real world Beware of fixed user links, however,
because the user account name and password are stored unencrypted in the SYS.LINK$
data dictionary view Although this view is only accessible to DBA-level accounts, this stillrepresents a major security hole
DATABASE LINK ARCHITECTURE
Trang 17CHAPTER 23 • ORACLE DATABASE LINKS
1016
Connected User Links
The following statement creates a public link to the NW_DIV database In thisarrangement, when user APP_OWN accesses the public link to NW_DIV in a query,the connected user will be APP_OWN
CREATE PUBLIC DATABASE LINK NW_DIV USING ‘NW_DIV’ ;
As you can see, the CREATE statement for a connected user link does not have aCONNECT string The CONNECT string is used to establish connection to the remotedatabase as a specified user One advantage of using this link is that the user connects
to the remote database as himself/herself, keeping the same username on both thesource and destination databases Better yet, no password is used and therefore can-not be intercepted This is a relatively secure method for establishing the connection
For a connected user link, the user must have privileges on the remote database This
can be considered both an advantage and disadvantage On the positive side, therequirement means the user cannot accomplish anything without the privileges Onthe negative side, the DBA has to establish and maintain those privileges
One more item concerning the connected user link: Oracle does allow a connecteduser to be authenticated by the host system or through other authentication services
If you intend to use connected user links for connecting to a remote database, you’llhave to set the REMOTE_OS_AUTHENT initialization parameter to TRUE (thedefault) To see the value of this parameter, use the following SQL:
SELECT name, value, isdefault, ismodified, descriptionFROM v$parameter
WHERE name = ‘remote_os_authent’ ;
Fixed User Links
With a fixed user link, the user connects to the remote database with a username and
password For instance, MARK (the DBA) creates a public database link in ROBERT’sschema and specifies a fixed user of RODNEY/CONSULTANT Now any user on thesource system can connect to the remote system using the proper username/pass-word; however, they will be connecting as RODNEY/CONSULTANT This allows theconnection to be enabled and disabled quickly and easily, leaving the remote link uponly as long as necessary
Here’s an example of the fixed user link:
CREATE DATABASE LINK my_link
Trang 18In this example, our database link is named MY_LINK We will connect to a Net8 vice called PROJ_US We will connect as the ROBERT user, with ENTERPRISE as thepassword
ser-One big drawback to the fixed user connection is that usernames and passwordsare stored in the LINK$ table and are unencrypted As stated earlier, this opens a gap
in the security arrangements Any user with SELECT ANY TABLE access can easily gainaccess if the 07_DICTIONARY_ACCESSIBILITY parameter is set to TRUE (which is thedefault) You can look at this parameter with the following SQL:
SELECT name, value, isdefault, ismodified, descriptionFROM v$parameter
WHERE name = ‘07_DICTIONARY_ACCESSIBILITY’ ;
NOTE We strongly suggest that O7_DICTIONARY_ACCESSIBILITY be set to FALSE in yourOracle8i database to avoid this security nightmare
Current User Links
A current user link always connects as a global user A global user must be
authenti-cated through an X.509 certificate and a private key, and the user must be established
on both the remote database and the source database
Following is an example of a current user link:
CREATE PUBLIC DATABASE LINK my_linkCONNECT TO CURRENT_USER USING ‘proj_us’ ;
In the previous example we are creating a database link called MY_LINK Since we usethe CURRENT_USER keyword, this is a current user link and will attach to the remotedatabase (PROJ_US) as the currently logged-in user
Following are the drawbacks of current user links:
• The current user is the same as the connected user when access is not lished from a stored function, procedure, or other object—that is, when the link
estab-is used directly (SQL statement) If MARK accesses the link with a SQL ment, then MARK could actually look like ROBERT if the current user is estab-lished as ROBERT
state-• The current user is the user who owns the object when the link is establishedthrough a function, procedure, or other object For example, if MARK accessesthe link through the function GET_PAYROLL_INFO, which was created byROBERT, then the CURRENT_USER is ROBERT
DATABASE LINK ARCHITECTURE
Trang 19CHAPTER 23 • ORACLE DATABASE LINKS
1018
• You cannot use a current user link through an object’s owner when connected
as an enterprise user, if that object’s owner exists in a shared global schema Forexample, if ROBERT is the owner of object GET_HELP and exists in the sharedglobal schema, when MARK executes GET_HELP he cannot gain access as theCURRENT_USER ROBERT
Schema Object Names on a Distributed System
Oracle uses the following syntax to name objects and keep them unique across a tributed system:
dis-schema.schema_object@global_database_name
where the variables have the following definitions:
• schema is a collection of tables, views, and grants making up a logical grouping.Each user owns a single schema
• schema_object is the table, view, index, or other object within the schema
• global_database_name is the unique name of the remote database Remember
that this name must match the DB_NAME and DB_DOMAIN concatenated
together, unless GLOBAL_NAMES is set to FALSE
Take a look at this example of a link and the SQL to access data on that database:
PROJ.NE_DIV.MEAD.COM
SELECT Proj_num, AmountFROM robert.proj@PROD.NE_DIV.MEAD.COMWHERE amount <= 200000 ;
This will get the project number and amount from the PROJ table in the ROBERTschema, where the amount is less than or equal to 200000 Note that if GLOBAL_NAMES were set to FALSE, then the following statement could be used We can substi-tute the global name with any name we want, as shown here:
SELECT Proj_num, AmountFROM robert.proj@ENTERPRISEWHERE amount <= 200000 ;
Administering Database Links
Trang 20documen-Creating Database Links
To create a database link, use the CREATE DATABASE LINK statement For creating apublic database link, use the optional syntax CREATE PUBLIC DATABASE LINK Oncethe link is created, you can use the link in your SQL code to access objects in theremote database Table 23.2 lists the SQL statements necessary to establish variousconnections and links to a remote database
TABLE 23.2: LINK COMMANDS
Type of Link Type of Connection SQL Command to Create the Link
Private Connected user CREATE DATABASE LINK Prod
USING ‘proj_us’ ;
Private Fixed user CREATE DATABASE LINK Prod
CONNECT TO robert IDENTIFIED BY enterprise USING ‘proj_us’ ;
Private Current user CREATE DATABASE LINK cur_test
CONNECT TO CURRENT_USER USING ‘prod_us’ ;
Public Connected user CREATE PUBLIC DATABASE LINK nw_div
USING ‘nw_div’ ;
Public Fixed user CREATE PUBLIC DATABASE LINK proj
CONNECT TO robert IDENTIFIED BY enterprise USING ‘prod_us’;
NOTE Keep in mind that you will need to establish Net8 connectivity to the remotedatabase before you can create a database link to it The process of configuring databasesfor Net8 is discussed in detail in Chapter 9
ADMINISTERING DATABASE LINKS