This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View What Is A Database Link Oracle as PDF for free.
What is a Database Link and how do I configure one? A Database Link is a named object created in one database which describes how to connect to another, completely different, database. Using a Database Link, it is possible, whilst connected to, say, the DB8 database to issue the command SELECT * FROM EMP@DB9, and the data that is returned will be that from the EMP table in the DB9 database. Links are thus a means of performing distributed database processing –the ability to distibute data around many different databases, on many different machines, in many different physical locations. Such an approach gives you redundancy (if the Melbourne machine blows up, it takes the Melbourne data with it, but Perth and Sydney continue working as before). It also gives you, potentially, performance benefits –with tables distributed amongst many machines, no one machine has to do all the hard work of resolving Users’ queries. (On the other hand, with data having to pass back and forth across the network, which is usually relatively slow, the performance benefits of distributed processing can easily be swamped by awful networking performance). Database Links are thus at the heart of Advanced Replication, which is Oracle’s complete infrastructure for distributed database processing. Links are relatively easy to set up, but they do depend on having first configured all aspects of inter-machine and inter-database networking and communication successfully. If you don’t know one end of a LISTENER.ORA from a SQLNET.ORA, then getting Links to work reliably is going to be awkward.
Instance Configuration Technically, when you come to create your Database Links, you can call them anything you like. But Oracle strongly recommends that you follow the ‘global names’ convention, because Advanced Replication (and some other advanced features) won’t work unless you do. What that convention means is that your links are named in the format .<domain name>. To do that might require some editing of your init.ora, however. Three parameters are crucial. DB_NAME is pretty self-explanatory, and has to be set whenever you create a new database. Not a lot of choice here, therefore –ours will be set to “DB9”. DB_DOMAIN is not compulsory though, yet it’s of extreme importance for linking databases. It defaults to a null string so in our case we’ll need to set it explicitly to “aldeburgh.local”. If you’ve got a registered Internet domain, then that’s probably what you’d use (I don’t, which is why the ‘.local’ is there). If you put DB_NAME and DB_DOMAIN together, you’ve got yourself a Global Database Name. In our case, it will be “db9.aldeburgh.local” –though this isn’t actually set as an init.ora parameter in its own right, but is merely constructed from the other two whenever needed. It’s important that the global database name as implied in the DB9 init.ora is what the GLOBAL_DBNAME setting in MOZART’s listener.ora is set to, and also what the SERVICE_NAME is set to in MONTEVERDI’s tnsnames.ora. If you check all these entries above, you’ll see that this is indeed the case here. Failure to make sure that they match will mean that the DB8 database will request a connection to an Instance which can’t be identified in the first place or, if identification is possible, can’t be connected to. One last parameter now needs to be set. It’s GLOBAL_NAMES, and it needs to be set to TRUE. There’s no absolute requirement for the parameter to be set in this way: but Advanced Replication won’t work without it. What it does is to force that all Database Links must have names which match the global name of the database they’re connecting to. In our case, since we’re connecting from DB8 to DB9, it needs to be set to TRUE in the init.ora for the DB8 database on MONTEVERDI. One final warning: databases are created (or should be –the default appears to be “us.oracle.com”!) with a global name that needs to match what you configure as the Global Database Name in the way described above. If you change your init.ora settings so that the DB_NAME+DB_DOMAIN combination no longer matches what was set as the global name at database creation, you’ll need to rename the database. That’s relatively easy to do: Alter database rename global_name to <something>;
In our case, we may therefore need to issue this command whilst connected as SYS to the DB9 database: Alter database rename global_name to db9.aldeburgh.local; Once all that is set up, we should be able to create our links without too much trouble.
Creating the Database Link This is the easy part. The basic syntax for creating a Database Link is: Create database link Connect to <schema> identified by <password> Using ; The real question is what goes in all those variable entries –the link name and so on? Well, the link name is the fully-qualified global database name that was discussed earlier. In our case, that’s “db9.aldeburgh.local”. There’s no rule that says it must match the global name, remember –except that, following Oracle’s advice, we’ve set GLOBAL_NAMES to be TRUE for the DB8 database. If we therefore specified anything other than “db9.aldeburgh.local” as the link name, the first time we tried to fetch data across the link we’d get the following error message: ORA-02085:
DATABASE LINK
DB9.MADEUPNAME
CONNECTS TO
DB9.ALDEBURGH.COM
The <schema> and <password> entries of the syntax are fairly self-explanatory: which bit of the DB9 database do we want to connect to? If you just want to select from Scott’s tables over the link, for example, then ‘Scott’ and ‘Tiger’ would be sensible entries. You might not want to limit yourself in this way, though: suppose you want to be able to select from tables that happen to reside in multiple schemas? Well, that can be done, too… but I’ll describe that in my paper “What are the different types of Database Links that can be created?”. For now, let’s keep it simple. Finally, the entry is hopefully pretty obvious. It’s whatever is in the tnsnames.ora file on the machine where DB8 resides as the alias for the connection to “db9.aldeburgh.local”. Being an alias, you could have set it to anything –but if you refer back to the discussion about our specific tnsnames.ora file, you’ll see that we’ve set it to “db9”. Bear in mind that it’s what is on the DB8 machine that’s important. It’s this machine, after all, that has to resolve the connection request, and pass it on to the Listener running on the DB9 machine. All of which means that we can now issue the following command:
Create database link db9.aldeburgh.local Connect to scott identified by tiger Using ‘db9’; Notice how the tnsnames.ora alias goes inside single quotes, but nothing else does.
appear. (Again, in case you were wondering, a “create table newemp as select * from [email protected]” will work fine: the DDL bit of the statement is taking place in the local DB8 database, and only the select is taking place across the link). So the general rule is that queries and DML across a link are permitted, but DDL isn’t. When DML is performed, by the way, all referential integrity constraints are checked and applied as normal (though if it happens that the parent table, say, is in a different schema than the one referred to in the ‘create database link’ command you’ll have trouble). Finally, what does the link look like as far as the DB9 database is concerned? If we query v$session after the link is created, but before it is actually used, we get this sort of result: SQL> connect system/manager@db9 Connected. SQL> select sid, serial#, username from v$session 2 where username is not null; SID SERIAL# USERNAME ---------- ---------- -----------------------------8 12 SYSTEM 1 row selected. Here we see only SYSTEM’s session, in which this query has just been issued. If we now (in a different session) connect as SYSTEM on the DB8 database, and issue a select across the database link, we’ll see this: SID SERIAL# USERNAME ---------- ---------- -----------------------------7 5 SCOTT 8 12 SYSTEM Notice the new session here: it’s listed as Scott’s even though I just stressed the fact that I’d connected to the DB8 database as SYSTEM. The reason for that again lies in the way we issued the ‘create database link’ command. To remind you, it was: Create database link db9.aldeburgh.local Connect to scott identified by tiger Using ‘db9’; So the link itself dictates that, whoever we happen to be connected as on DB8, the connection to the DB9 database must be made as Scott.
Notice that the OSUSER column contains the name of the Windows 2000 machine hosting the DB8 database (MONTEVERDI), not (as in the last line displayed) the actual operating system name of an individual user. This is about the only way to spot the difference between a ‘real’ user connected to the remote database in the normal fashion and a user connecting to it via a database link.
Managing Database Links Having created a Database Link, you need to be able to find out which links have been created, and how to modify them or get rid of them altogether. To view which links exist, select from the DBA_DB_LINKS views (there are equivalent USER_ and ALL_ views, of course): SQL> select * from dba_db_links; OWNER DB_LINK USERNAME HOST CREATED ------- --------------------- ----------- --------- --------SYSTEM DB9.ALDEBURGH.LOCAL SCOTT db9 03/MAR/02 Notice that the ‘host’ column doesn’t tell you the host at all (not in the sense of the machine being connected to, at least). Rather, it tells you the tnsnames alias being used to make the connection. Be aware that selecting from the same view in the DB9 database produces no rows at all. In other words, you can’t tell from within a database what other databases might have links connecting to it (unless those links are used, of course, at which point –as we’ve seen- it becomes obvious in v$session). You can’t actually modify a Database Link once it’s been created. Instead, you have to drop the entire thing, and re-create it from scratch: SQL> drop database link db9.aldeburgh.local; Database link dropped.
SQL> Alter session close database link db9.aldeburgh.local; Session altered. That could prove a bit awkward to remember if, like me, you’ve grown accustomed to ordinary select statements not requiring commits to make them do their stuff! Obviously, it’s much less of a hassle with ordinary DML statements. Notice, too, that the command is an ‘alter session’ one. That means the User wishing to close the link has to possess the alter session privilege. It also means that a link that one User happens to be using to perform a substantial piece of DML cannot be summarily closed by someone else (thus causing general consternation, transaction failure and massive rollback all round!). The link is session-specific, and only your own session can be used to close it.
Making Links Easier to Use One final point I’ll mention briefly here. It’s frankly a bit of a pain always to have to qualify your select statements (or DML) with long-winded fully qualified link names. Remember that public synonyms and views created in the local database can come to your rescue: SQL> 2 3 4
create view fred as select empno, ename, job from [email protected] where sal>500;
View created. SQL> select * from fred; EMPNO ---------7499 7521
Both of these approaches to what is laboriously known as ‘location transparency’ mean that Users don’t get RSI everytime they want to access remotely-stored data!