abderrahim_05
abderrahim_05

Reputation: 465

Query more than one database?

What's the way to query more than one database using SQL*Plus?

In MySQL it's possible to do something like this :

create table WK_LINK_JOINT_IDEOREQ AS
select k.constraint_name cn, k.table_name tl, l.column_name lc
     , k.referenced_table_name tg, k.column_name cg, l.referenced_table_name td
     , l.referenced_column_name cd
  from information_schema.KEY_COLUMN_USAGE k

It's just an example, it's not complete : but as you can see, we are working on two databases, INFORMATION_SCHEMA and another one.

I want to do somthing like this using SQL*Plus, but the problem is that when we connect using SQL*Plus, we specify the database (SID) which means that the others are not accessible.

Is there a way to do it?

Upvotes: 0

Views: 186

Answers (1)

APC
APC

Reputation: 146239

Oracle has a different interpretation of DATABASE from MySQL. In Oracle we have multiple users or schemas in the same database.

So, if what you really want is to access objects from a different schema all that has to happen is for that schema to grant you privileges. You can then reference the tables (or whatever) in your SQL.

User JOE grants you select on his table

SQL>  conn JOE/SOAP
SQL>  grant select on my_table to ABC;

You can then run queries on it:

SQL>  conn ABC/DEF
SQL>  select * from joe.my_table;

In your example you used INFORMATION_SCHEMA. The Oracle equivalent of this is the data dictionary, an enormous library of views. Find out more.

Public access is granted by default on most of them. So you can select from USER_TABLES, USER_CONSTRAINTS and USER_CONS_COLUMNS to re-create that query (assuming I have understood it correctly).

Upvotes: 1

Related Questions