Wednesday, May 29, 2013

Create And Use SYNONYM In Oracle Database.

############ CREATING A TABLE FOR TESTING SYNONYM #############

SQL> CREATE TABLE TOP_N_ANALYSIS (ID NUMBER, NAME VARCHAR2(30), SALARY NUMBER);

Table created.

############  INSERTING DATA INTO TABLE #############################

SQL> INSERT INTO TOP_N_ANALYSIS(ID, NAME, SALARY) VALUES(10,'RAJIB', 5000);

1 row created.

SQL> INSERT INTO TOP_N_ANALYSIS(ID, NAME, SALARY) VALUES(20,'JOHN', 4000);

1 row created.

SQL> INSERT INTO TOP_N_ANALYSIS(ID, NAME, SALARY) VALUES(30,'KING', 9000);

1 row created.

SQL> COMMIT;

Commit complete.

########## SHOW DATA FROM TABLE ################################

SQL> SELECT * FROM TOP_N_ANALYSIS;

        ID NAME                               SALARY
---------- ------------------------------ ----------
        10 RAJIB                                5000
        20 JOHN                                 4000
        30 KING                                 9000

Synonym created.

########## SHOW DATA FROM SYNONYM IN HERE YOU GET ERROR ############

SQL> SELECT * FROM TOP_N;
SELECT * FROM TOP_N
              *
ERROR at line 1:
ORA-00942: table or view does not exist

########## CREATE SYNONYM FOR TOP_N_ANALYSIS TABLE #################

SQL> CREATE SYNONYM TOP_N FOR TOP_N_ANALYSIS;

Synonym created.

########## SHOW DATA FROM SYNONYM ############

SQL> SELECT * FROM TOP_N;

        ID NAME                               SALARY
---------- ------------------------------ ----------
        10 RAJIB                                5000
        20 JOHN                                 4000
        30 KING                                 9000

########## DROP SYNONYM ############

SQL> DROP SYNONYM TOP_N;

Synonym dropped.

SQL> SELECT * FROM TOP_N;
SELECT * FROM TOP_N
              *
ERROR at line 1:
ORA-00942: table or view does not exist

No comments:

Post a Comment