简体   繁体   中英

Oracle 11g: How to override default schema tables with test tables for my test user

I know this can be done, because I've seen it at my last workplace, but I don't know how to replicate!

Basically, there is a MASTER user who has write privileges for all our tables. In our application's DB adapter connection settings, we use DEFAULT_SCHEMA: MASTER

I have created a new test user for myself (on the same database as the master user, not using a database link) so that I can freely create test data without messing with real data. Then copied a table for my test user so that I can freely manipulate data: create table SIMMBOT.real_data_table as select * from MASTER.real_data_table

The problem is that I don't know how to set up the connection so that Oracle knows to override MASTER.real_data_table with my own SIMMBOT.real_data_table . I have a hunch that you can't actually do that in the connection settings... so starting from square one, what would I have to do to set up test tables like this? Something like a shared schema?

If your code is using fully qualified table names (ie MASTER.real_data_table or SIMMBOT.real_data_table ) then there is no way from a configuration standpoint to change what object is being referred to.

Assuming, however, that your code is not using a fully qualified table name-- if it is just selecting from real_data_table , then Oracle will first look for an object in the current schema with that name then look for a public synonym with that name.

If you connect as MASTER , you can change the current schema

ALTER SESSION SET current_schema = SIMMBOT

Once you've done that, all unqualified references to a table name will resolve to tables in the SIMMBOT schema. Note that the MASTER user would need to be granted appropriate access on the objects in the SIMMBOT schema separately-- setting the current schema only affects name resolution, not privileges. The SIMMBOT schema would also need to have every table that the code wants to reference-- there is no way to specify a hierarchy for resolving unqualified names. You can't tell Oracle to first resolve unqualified names in the SIMMBOT schema and then the MASTER schema.

An alternative would be to create synonyms for each table and manipulate the synonyms to reference your table for some or all users. If your application logged in as a third user that did not own any objects-- APP_USER for example-- you could create either private synonyms in the APP_USER schema that pointed to different objects in different schemas--

CREATE SYNONYM app_user.real_data_table FOR simmbot.real_data_table;
CREATE SYNONYM app_user.some_other_table FOR master.some_other_table;

or you could create public synonyms that would apply to all users (other than those that owned the objects)

CREATE PUBLIC SYNONYM real_data_table FOR simmbot.real_data_table;
CREATE PUBLIC SYNONYM some_other_table FOR master.some_other_table;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM