简体   繁体   中英

How to GRANT privileges to a ROLE on an Oracle SCHEMA

How i can grant some privileges to a ROLE on all tables of a SCHEMA?
I wrote this code, but in SQLDeveloper it gives an error.

CREATE SCHEMA AUTHORIZATION alberto;

CREATE TABLE Cucine (
  tipo varchar(1) primary key,
  descrizione varchar(200) not null
);

CREATE TABLE Quartieri (
  codice varchar(4) primary key,
  nome varchar(100) not null
);

CREATE TABLE Ristoranti (
  codice varchar(5) primary key,
  nome varchar(150) not null,
  indirizzo varchar(250),
  tipocucina varchar(1) references Cucine(tipo),
  codquart varchar(4) references Quartieri(codice)
);

CREATE TABLE CarteDiCredito (
  codcircuito varchar(4) primary key,
  nomecircuito varchar(100) not null
);

CREATE TABLE Convenzioni (
  circuito varchar(4) references CarteDiCredito(codcircuito),
  codristorante varchar(5) references Ristoranti(codice),
  primary key(circuito, codristorante)
);

CREATE ROLE turista;
GRANT SELECT ON ENTE_TURISTICO.* TO turista;
CREATE USER DBAEnte IDENTIFIED BY 12345;
CREATE USER vinni IDENTIFIED BY mosh;
GRANT dba TO DBAEnte;
GRANT turista TO vinni;

What is wrong in this code?

CREATE SCHEMA is a single statement to create multiple objects, you need to remove the semicolons. Also, CREATE SCHEMA only supports tables, views, and grants. You'll need to move the CREATE ROLE and CREATE USER out of the statement. Here's an example from the manual :

CREATE SCHEMA AUTHORIZATION oe
   CREATE TABLE new_product 
      (color VARCHAR2(10)  PRIMARY KEY, quantity NUMBER) 
   CREATE VIEW new_product_view 
      AS SELECT color, quantity FROM new_product WHERE color = 'RED' 
   GRANT select ON new_product_view TO hr; 

To grant SELECT on all tables, you'll need dynamic SQL like this:

begin
  for tables in (select table_name from all_tables where owner = 'ALBERTO') loop
    execute immediate
      'grant select on alberto.'||tables.table_name||' to turista';
  end loop;
end;
/

Use the below code:-

CREATE TABLESPACE dwtblspc LOGGING DATAFILE 'D:\\oraclexe\\app\\oracle\\oradata\\XE\\DWTBLSPC.DBF' SIZE 300M AUTOEXTEND ON NEXT 1048K MAXSIZE UNLIMITED;

CREATE USER prateek IDENTIFIED by prateek DEFAULT TABLESPACE dwtblspc ACCOUNT UNLOCK;

GRANT unlimited tablespace to prateek ;

CREATE ROLE prateek_role;

GRANT CREATE SESSION,ALTER SESSION,CREATE TABLE,CREATE CLUSTER,CREATE SYNONYM,CREATE VIEW,CREATE SEQUENCE,
CREATE DATABASE LINK,CREATE PROCEDURE,CREATE TRIGGER,CREATE TYPE,CREATE OPERATOR,CREATE INDEXTYPE,BACKUP ANY TABLE,
SELECT ANY TABLE,CREATE ANY TABLE,CREATE ANY INDEX,ALTER ANY INDEX,ALTER ANY INDEXTYPE,DROP ANY INDEX,DROP ANY INDEXTYPE to prateek_role;

GRANT prateek_role to prateek ;

Hope it help.

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