Home » SQL & PL/SQL » SQL & PL/SQL » create synonym for multiple tables
create synonym for multiple tables [message #12302] Fri, 14 May 2004 03:38 Go to next message
kim
Messages: 116
Registered: December 2001
Senior Member
hi,

I have several tables(63) in schema 'KIMCHOW'. I have created 3 users and they have been given DBA role. however, to manipulate the tables in the KIMCHOW schema, I need to write 'kimchow.table_name'. I've been told to write synonym for the tables. How may I create synonyms for each table and give it the same name in one go?

thanks. 
Re: create synonym for multiple tables [message #12308 is a reply to message #12302] Fri, 14 May 2004 08:19 Go to previous messageGo to next message
Satish Shrikhande
Messages: 167
Registered: October 2001
Senior Member
conn KIMCHOW

spool c:r.sql
select 'create synonym '||table_name ||' for '||table_name||'.KIMCHOW' from user_tables ;
spool off

conn user1
@c:r.sql

conn user2
@c:r.sql
Re: create synonym for multiple tables [message #12312 is a reply to message #12308] Fri, 14 May 2004 08:49 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
You will want to put the KIMCHOW reference in front of the table name (kimchow.table_name).

Also, you don't need to give those others the DBA role - that is way too extreme. Just give them select (and insert/update/delete if necessary) on those tables.

kimchow>grant select on table_name to user1;
Re: create synonym for multiple tables [message #12345 is a reply to message #12312] Sun, 16 May 2004 20:53 Go to previous messageGo to next message
kimchow
Messages: 16
Registered: May 2004
Junior Member
I've tried to execute this:
'create synonym planning for planning.kimchow'
It gives the error ORA-00955: object name already exists.

Todd, I don't want to put the schema name 'KIMCHOW' in front of table_name, when I am accessing kimchow's tables. E.g. if I log in as user1(I am in USER1 schema), I will want to access table 'PLANNING' simply as:
'select * from planning'
and not as:
'select * from kimchow.planning'
how may I do so?

concerning the DBA role, it's only for testing purposes.
Re: create synonym for multiple tables [message #12363 is a reply to message #12345] Mon, 17 May 2004 20:55 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9104
Registered: November 2002
Location: California, USA
Senior Member
I believe that when Todd said to put the schema name (kimchow) in front of the table name, that he intended that you do so when creating the synonym, not when selecting. Please see the example below.

scott@ORA92> CREATE USER user1 IDENTIFIED BY user1
  2  /

User created.

scott@ORA92> CREATE USER kimchow IDENTIFIED BY kimchow
  2  /

User created.

scott@ORA92> GRANT CONNECT, RESOURCE TO user1, kimchow
  2  /

Grant succeeded.

scott@ORA92> @ CONNECT kimchow/kimchow
scott@ORA92> set termout off
kimchow@ORA92> 
kimchow@ORA92> set termout on
kimchow@ORA92> CREATE TABLE planning
  2    (col1 NUMBER)
  3  /

Table created.

kimchow@ORA92> INSERT INTO planning (col1) VALUES (1)
  2  /

1 row created.

kimchow@ORA92> COMMIT
  2  /

Commit complete.

kimchow@ORA92> GRANT SELECT ON planning TO user1
  2  /

Grant succeeded.

kimchow@ORA92> @ CONNECT user1/user1
kimchow@ORA92> set termout off
user1@ORA92> 
user1@ORA92> set termout on
user1@ORA92> <b>CREATE SYNONYM planning FOR kimchow.planning
  2  /</b>

Synonym created.

user1@ORA92> <b>SELECT * FROM planning
  2  /</b>

      COL1
----------
         1
Re: create synonym for multiple tables [message #12365 is a reply to message #12363] Mon, 17 May 2004 21:10 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9104
Registered: November 2002
Location: California, USA
Senior Member
Taking it one step further, to create synonyms for all tables in the kinchow schema using the same table name as schema name, copy and start the following script from user1's schema.

STORE SET saved_settings REPLACE
SET ECHO OFF FEEDBACK OFF HEADING OFF PAGES 0 VERIFY OFF
SPOOL r.sql
SELECT 'CREATE SYNONYM ' || table_name || ' FOR kimchow.' || table_name || ';'
FROM all_tables
WHERE owner = 'KIMCHOW'
/
SPOOL OFF
START saved_settings
START r.sql
Re: create synonym for multiple tables [message #12366 is a reply to message #12365] Mon, 17 May 2004 21:20 Go to previous message
Barbara Boehmer
Messages: 9104
Registered: November 2002
Location: California, USA
Senior Member
Alternatively, you could create a public synonym from kimchow's schema, instead of creating a synonym from user1's schema. Please see the example below and note the required privilege to create a public synonym.

scott@ORA92> CREATE USER user1 IDENTIFIED BY user1
  2  /

User created.

scott@ORA92> CREATE USER kimchow IDENTIFIED BY kimchow
  2  /

User created.

scott@ORA92> GRANT CONNECT, RESOURCE TO user1, kimchow
  2  /

Grant succeeded.

scott@ORA92> <b>GRANT CREATE PUBLIC SYNONYM TO kimchow
  2  /</b>

Grant succeeded.

scott@ORA92> @ CONNECT kimchow/kimchow
scott@ORA92> set termout off
kimchow@ORA92> 
kimchow@ORA92> set termout on
kimchow@ORA92> CREATE TABLE planning
  2    (col1 NUMBER)
  3  /

Table created.

kimchow@ORA92> INSERT INTO planning (col1) VALUES (1)
  2  /

1 row created.

kimchow@ORA92> COMMIT
  2  /

Commit complete.

kimchow@ORA92> GRANT SELECT ON planning TO user1
  2  /

Grant succeeded.

kimchow@ORA92> --STORE  SET saved_settings REPLACE
kimchow@ORA92> --SET    ECHO OFF FEEDBACK OFF HEADING OFF PAGES 0 VERIFY OFF
kimchow@ORA92> --SPOOL   r.sql
kimchow@ORA92> --SELECT 'CREATE PUBLIC SYNONYM ' || table_name || ' FOR kimchow.' 
kimchow@ORA92> --FROM   all_tables
kimchow@ORA92> --WHERE  owner = 'KIMCHOW'
kimchow@ORA92> --/
kimchow@ORA92> --SPOOL  OFF
kimchow@ORA92> --START  saved_settings
kimchow@ORA92> --START  r.sql
<b>kimchow@ORA92> CREATE PUBLIC SYNONYM PLANNING FOR kimchow.PLANNING
  2  /</b>

Synonym created.

kimchow@ORA92> @ CONNECT user1/user1
kimchow@ORA92> set termout off
user1@ORA92> 
user1@ORA92> set termout on
<b>user1@ORA92> SELECT * FROM planning
  2  /</b>

      COL1
----------
         1
Previous Topic: Order BY in VARCHAR2 Column
Next Topic: how to breakup values
Goto Forum:
  


Current Time: Fri Feb 28 09:11:23 CST 2025