Oracle PL/SQL Tutorial/User Privilege/Synonyms — различия между версиями

Материал из SQL эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 10:05, 26 мая 2010

create a synonym for a view

SQL>
SQL> create table emp(
  2           emp_id                integer         primary key
  3          ,lastname               varchar2(20)    not null
  4          ,firstname              varchar2(15)    not null
  5          ,midinit                varchar2(1)
  6          ,street                 varchar2(30)
  7          ,city                   varchar2(20)
  8          ,state                  varchar2(2)
  9          ,zip                    varchar2(5)
 10          ,shortZipCode                   varchar2(4)
 11          ,area_code              varchar2(3)
 12          ,phone                  varchar2(8)
 13          ,company_name           varchar2(50));
Table created.
SQL>
SQL>
SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values
  2                      (1,"Jones","Joe","J","1 Ave","New York","NY","11202","1111","212", "221-4333","Big Company");
1 row created.
SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values
  2                      (2,"Smith","Sue","J","1 Street","New York","NY","11444","1111","212", "436-6773","Little Company");
1 row created.
SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values
  2                      (3,"X","Peggy","J","1 Drive","New York","NY","45502","2222","212", "234-4444","Medium Company");
1 row created.
SQL>
SQL> create or replace view phone_list as
  2  select emp_id, firstname || " " || midinit || ". " || lastname as name,"(" || area_code || ")" || phone as telephone#
  3  from emp;
View created.
SQL>
SQL>
SQL>
SQL> desc phone_list
 Name                                                                                                  Null?    Type
 ----------------------------------------------------------------------------------------------------- -------- --------------------------------------------------------------------
 EMP_ID                                                                                                NOT NULL NUMBER(38)
 NAME                                                                                                   VARCHAR2(39)
 TELEPHONE#                                                                                             VARCHAR2(13)
SQL> select * from phone_list;
    EMP_ID NAME                                    TELEPHONE#
---------- --------------------------------------- -------------
         1 Joe J. Jones                            (212)221-4333
         2 Sue J. Smith                            (212)436-6773
         3 Peggy J. X                              (212)234-4444
3 rows selected.
SQL> create synonym phones for phone_list;
Synonym created.
SQL> desc phones
 Name                                                                                                  Null?    Type
 ----------------------------------------------------------------------------------------------------- -------- --------------------------------------------------------------------
 EMP_ID                                                                                                NOT NULL NUMBER(38)
 NAME                                                                                                   VARCHAR2(39)
 TELEPHONE#                                                                                             VARCHAR2(13)
SQL> select * from phones;
    EMP_ID NAME                                    TELEPHONE#
---------- --------------------------------------- -------------
         1 Joe J. Jones                            (212)221-4333
         2 Sue J. Smith                            (212)436-6773
         3 Peggy J. X                              (212)234-4444
3 rows selected.
SQL>
SQL> select view_name from user_views;
VIEW_NAME
------------------------------
EMP_HQ
V
AVG_SAL
EMPDEPT_V
DEPT_SAL
ALL_ORACLE_ERRORS
INVENTORY_VIE
TOP_EMP
EMP_BONUS
SHARED
PHONE_LIST
11 rows selected.
SQL>
SQL> select synonym_name, table_name from user_synonyms;
SYNONYM_NAME                   TABLE_NAME
------------------------------ ------------------------------
PHONES                         PHONE_LIST
1 row selected.
SQL>
SQL>
SQL> drop synonym phones ;
Synonym dropped.
SQL>
SQL>
SQL>
SQL> drop table emp;
Table dropped.


Creating a public synonym

SQL>
SQL> CREATE TABLE product (
  2       product_name     VARCHAR2(25) PRIMARY KEY,
  3       product_price    NUMBER(4,2),
  4       quantity_on_hand NUMBER(5,0),
  5       last_stock_date  DATE
  6       );
Table created.
SQL>
SQL> INSERT INTO product VALUES ("Product 1", 99,  1,    "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 2", 75,  1000, "15-JAN-02");
1 row created.
SQL> INSERT INTO product VALUES ("Product 3", 50,  100,  "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 4", 25,  10000, null);
1 row created.
SQL> INSERT INTO product VALUES ("Product 5", 9.95,1234, "15-JAN-04");
1 row created.
SQL> INSERT INTO product VALUES ("Product 6", 45,  1,    TO_DATE("December 31, 2008, 11:30 P.M.","Month dd, YYYY, HH:MI P.M."));
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE PUBLIC SYNONYM product FOR product;
Synonym created.
SQL>
SQL> drop PUBLIC SYNONYM product;
Synonym dropped.
SQL>
SQL>
SQL> drop table product;
Table dropped.
SQL>
SQL>


Creating a synonym for a table

SQL>
SQL>
SQL> CREATE TABLE product (
  2       product_name     VARCHAR2(25) PRIMARY KEY,
  3       product_price    NUMBER(4,2),
  4       quantity_on_hand NUMBER(5,0),
  5       last_stock_date  DATE
  6       );
Table created.
SQL>
SQL> INSERT INTO product VALUES ("Product 1", 99,  1,    "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 2", 75,  1000, "15-JAN-02");
1 row created.
SQL> INSERT INTO product VALUES ("Product 3", 50,  100,  "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 4", 25,  10000, null);
1 row created.
SQL> INSERT INTO product VALUES ("Product 5", 9.95,1234, "15-JAN-04");
1 row created.
SQL> INSERT INTO product VALUES ("Product 6", 45,  1,    TO_DATE("December 31, 2008, 11:30 P.M.","Month dd, YYYY, HH:MI P.M."));
1 row created.
SQL>
SQL>
SQL>
SQL> SELECT * FROM prod;
SELECT * FROM prod
              *
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL>
SQL> CREATE SYNONYM prod FOR product;
Synonym created.
SQL>
SQL> SELECT * FROM prod;
PRODUCT_NAME              PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC
------------------------- ------------- ---------------- ---------
Product 1                            99                1 15-JAN-03
Product 2                            75             1000 15-JAN-02
Product 3                            50              100 15-JAN-03
Product 4                            25            10000
Product 5                          9.95             1234 15-JAN-04
Product 6                            45                1 31-DEC-08
6 rows selected.
SQL>
SQL>
SQL> drop SYNONYM prod;
Synonym dropped.
SQL>
SQL> drop table product;
Table dropped.
SQL>


Describe a synonym

SQL>
SQL> create table employees(
  2    empno      NUMBER(4)
  3  , ename      VARCHAR2(8)
  4  , init       VARCHAR2(5)
  5  , job        VARCHAR2(8)
  6  , mgr        NUMBER(4)
  7  , bdate      DATE
  8  , msal       NUMBER(6,2)
  9  , comm       NUMBER(6,2)
 10  , deptno     NUMBER(2) ) ;
Table created.
SQL>
SQL>
SQL> insert into employees values(1,"Jason",  "N",  "TRAINER", 2,   date "1965-12-18",  800 , NULL,  10);
1 row created.
SQL> insert into employees values(2,"Jerry",  "J",  "SALESREP",3,   date "1966-11-19",  1600, 300,   10);
1 row created.
SQL> insert into employees values(3,"Jord",   "T" , "SALESREP",4,   date "1967-10-21",  1700, 500,   20);
1 row created.
SQL> insert into employees values(4,"Mary",   "J",  "MANAGER", 5,   date "1968-09-22",  1800, NULL,  20);
1 row created.
SQL> insert into employees values(5,"Joe",    "P",  "SALESREP",6,   date "1969-08-23",  1900, 1400,  30);
1 row created.
SQL> insert into employees values(6,"Black",  "R",  "MANAGER", 7,   date "1970-07-24",  2000, NULL,  30);
1 row created.
SQL> insert into employees values(7,"Red",    "A",  "MANAGER", 8,   date "1971-06-25",  2100, NULL,  40);
1 row created.
SQL> insert into employees values(8,"White",  "S",  "TRAINER", 9,   date "1972-05-26",  2200, NULL,  40);
1 row created.
SQL> insert into employees values(9,"Yellow", "C",  "DIRECTOR",10,  date "1973-04-27",  2300, NULL,  20);
1 row created.
SQL> insert into employees values(10,"Pink",  "J",  "SALESREP",null,date "1974-03-28",  2400, 0,     30);
1 row created.
SQL>
SQL> create synonym e for employees;
Synonym created.
SQL>
SQL> describe e
 Name                                                  Null?    Type
 ----------------------------------------------------- -------- ------------------------------------
 EMPNO                                                          NUMBER(4)
 ENAME                                                          VARCHAR2(8)
 INIT                                                           VARCHAR2(5)
 JOB                                                            VARCHAR2(8)
 MGR                                                            NUMBER(4)
 BDATE                                                          DATE
 MSAL                                                           NUMBER(6,2)
 COMM                                                           NUMBER(6,2)
 DEPTNO                                                         NUMBER(2)
SQL>
SQL> drop table employees;
Table dropped.
SQL>
SQL> drop synonym e;
Synonym dropped.


Dropping a public synonym

SQL>
SQL> CREATE TABLE product (
  2       product_name     VARCHAR2(25) PRIMARY KEY,
  3       product_price    NUMBER(4,2),
  4       quantity_on_hand NUMBER(5,0),
  5       last_stock_date  DATE
  6       );
Table created.
SQL>
SQL> INSERT INTO product VALUES ("Product 1", 99,  1,    "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 2", 75,  1000, "15-JAN-02");
1 row created.
SQL> INSERT INTO product VALUES ("Product 3", 50,  100,  "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 4", 25,  10000, null);
1 row created.
SQL> INSERT INTO product VALUES ("Product 5", 9.95,1234, "15-JAN-04");
1 row created.
SQL> INSERT INTO product VALUES ("Product 6", 45,  1,    TO_DATE("December 31, 2008, 11:30 P.M.","Month dd, YYYY, HH:MI P.M."));
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE PUBLIC SYNONYM product FOR product;
Synonym created.
SQL>
SQL> drop PUBLIC SYNONYM product;
Synonym dropped.
SQL>
SQL>
SQL> drop table product;
Table dropped.


Dropping a synonym

SQL>
SQL>
SQL> CREATE TABLE product (
  2       product_name     VARCHAR2(25) PRIMARY KEY,
  3       product_price    NUMBER(4,2),
  4       quantity_on_hand NUMBER(5,0),
  5       last_stock_date  DATE
  6       );
Table created.
SQL>
SQL> INSERT INTO product VALUES ("Product 1", 99,  1,    "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 2", 75,  1000, "15-JAN-02");
1 row created.
SQL> INSERT INTO product VALUES ("Product 3", 50,  100,  "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 4", 25,  10000, null);
1 row created.
SQL> INSERT INTO product VALUES ("Product 5", 9.95,1234, "15-JAN-04");
1 row created.
SQL> INSERT INTO product VALUES ("Product 6", 45,  1,    TO_DATE("December 31, 2008, 11:30 P.M.","Month dd, YYYY, HH:MI P.M."));
1 row created.
SQL>
SQL>
SQL>
SQL> SELECT * FROM prod;
SELECT * FROM prod
              *
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL>
SQL> CREATE SYNONYM prod FOR product;
Synonym created.
SQL>
SQL> SELECT * FROM prod;
PRODUCT_NAME              PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC
------------------------- ------------- ---------------- ---------
Product 1                            99                1 15-JAN-03
Product 2                            75             1000 15-JAN-02
Product 3                            50              100 15-JAN-03
Product 4                            25            10000
Product 5                          9.95             1234 15-JAN-04
Product 6                            45                1 31-DEC-08
6 rows selected.
SQL>
SQL>
SQL> drop SYNONYM prod;
Synonym dropped.
SQL>
SQL> drop table product;
Table dropped.
SQL>


Public Synonyms

Even though a public synonym has been created for schemaName.tableName, a user still needs object privileges on that table to actually access the table.



CREATE PUBLIC SYNONYM products FOR schemaName.tableName;


Query a table by selecting its synonym

SQL>
SQL> create table employees(
  2    empno      NUMBER(4)
  3  , ename      VARCHAR2(8)
  4  , init       VARCHAR2(5)
  5  , job        VARCHAR2(8)
  6  , mgr        NUMBER(4)
  7  , bdate      DATE
  8  , msal       NUMBER(6,2)
  9  , comm       NUMBER(6,2)
 10  , deptno     NUMBER(2) ) ;
Table created.
SQL>
SQL>
SQL> insert into employees values(1,"Jason",  "N",  "TRAINER", 2,   date "1965-12-18",  800 , NULL,  10);
1 row created.
SQL> insert into employees values(2,"Jerry",  "J",  "SALESREP",3,   date "1966-11-19",  1600, 300,   10);
1 row created.
SQL> insert into employees values(3,"Jord",   "T" , "SALESREP",4,   date "1967-10-21",  1700, 500,   20);
1 row created.
SQL> insert into employees values(4,"Mary",   "J",  "MANAGER", 5,   date "1968-09-22",  1800, NULL,  20);
1 row created.
SQL> insert into employees values(5,"Joe",    "P",  "SALESREP",6,   date "1969-08-23",  1900, 1400,  30);
1 row created.
SQL> insert into employees values(6,"Black",  "R",  "MANAGER", 7,   date "1970-07-24",  2000, NULL,  30);
1 row created.
SQL> insert into employees values(7,"Red",    "A",  "MANAGER", 8,   date "1971-06-25",  2100, NULL,  40);
1 row created.
SQL> insert into employees values(8,"White",  "S",  "TRAINER", 9,   date "1972-05-26",  2200, NULL,  40);
1 row created.
SQL> insert into employees values(9,"Yellow", "C",  "DIRECTOR",10,  date "1973-04-27",  2300, NULL,  20);
1 row created.
SQL> insert into employees values(10,"Pink",  "J",  "SALESREP",null,date "1974-03-28",  2400, 0,     30);
1 row created.
SQL>
SQL>
SQL>
SQL> create synonym e for employees;
Synonym created.
SQL>
SQL>
SQL> select * from e;
     EMPNO ENAME    INIT  JOB             MGR BDATE           MSAL       COMM     DEPTNO
---------- -------- ----- -------- ---------- --------- ---------- ---------- ----------
         1 Jason    N     TRAINER           2 18-DEC-65        800                    10
         2 Jerry    J     SALESREP          3 19-NOV-66       1600        300         10
         3 Jord     T     SALESREP          4 21-OCT-67       1700        500         20
         4 Mary     J     MANAGER           5 22-SEP-68       1800                    20
         5 Joe      P     SALESREP          6 23-AUG-69       1900       1400         30
         6 Black    R     MANAGER           7 24-JUL-70       2000                    30
         7 Red      A     MANAGER           8 25-JUN-71       2100                    40
         8 White    S     TRAINER           9 26-MAY-72       2200                    40
         9 Yellow   C     DIRECTOR         10 27-APR-73       2300                    20
        10 Pink     J     SALESREP            28-MAR-74       2400          0         30
10 rows selected.
SQL>
SQL> drop table employees;
Table dropped.
SQL>
SQL> drop synonym e;
Synonym dropped.
SQL>


Seeing SYNONYM in the Oracle data dictionary

SQL>
SQL>
SQL> CREATE TABLE product (
  2       product_name     VARCHAR2(25) PRIMARY KEY,
  3       product_price    NUMBER(4,2),
  4       quantity_on_hand NUMBER(5,0),
  5       last_stock_date  DATE
  6       );
Table created.
SQL>
SQL> INSERT INTO product VALUES ("Product 1", 99,  1,    "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 2", 75,  1000, "15-JAN-02");
1 row created.
SQL> INSERT INTO product VALUES ("Product 3", 50,  100,  "15-JAN-03");
1 row created.
SQL> INSERT INTO product VALUES ("Product 4", 25,  10000, null);
1 row created.
SQL> INSERT INTO product VALUES ("Product 5", 9.95,1234, "15-JAN-04");
1 row created.
SQL> INSERT INTO product VALUES ("Product 6", 45,  1,    TO_DATE("December 31, 2008, 11:30 P.M.","Month dd, YYYY, HH:MI P.M."));
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE PUBLIC SYNONYM product FOR product;
Synonym created.
SQL>
SQL>
SQL> SELECT table_name, SUBSTR(comments, 1, 45) FROM dict WHERE SUBSTR(comments, 1, 7) <> "Synonym" and rownum < 50;

SQL>
SQL> drop PUBLIC SYNONYM product;
Synonym dropped.
SQL>
SQL> drop table product;
Table dropped.
SQL>


Synonyms

You can access tables in another schema by specifying the schema name followed by the table.

You can avoid having to enter the schema name by creating a synonym for a table.



CREATE SYNONYM customers FOR store.customers;