Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CREATE OR REPLACE v/s Dropping a function and re-creating it

What is the difference between the two? In both the cases? what happens to the privileges granted on this function? Are automatically revoked in both the cases and have to be provided again while re-creating? Kindly explain.

like image 943
user182944 Avatar asked Jul 13 '13 09:07

user182944


People also ask

What is the difference between create and replace in Oracle?

The big difference between create, and create or replace, is that create or replace will always replace an existing object of the same type. You need to be really sure that that is what you want to do. A lot of tools out there will automatically use create or replace when you use the tool to make a new procedure.

Why we use create or replace?

The OR REPLACE option on the CREATE TABLE statement can be used to change an existing table definition. Using CREATE OR REPLACE TABLE lets you consolidate the master definition of a table into one statement.

Why we use create or replace in Oracle?

With Oracle CREATE OR REPLACE, the OR REPLACE clause allows the compiler to replace a procedure if a procedure of the same name is already in the database schema. This aspect of the Oracle CREATE OR REPLACE procedure is handy during development as you do not have to drop the procedure each time before recreating it.

Why we write create or replace in Plsql block?

[OR REPLACE] option allows the modification of an existing procedure. The optional parameter list contains name, mode and types of the parameters. IN represents the value that will be passed from outside and OUT represents the parameter that will be used to return a value outside of the procedure.


Video Answer


2 Answers

When an object is dropped everything associated with it is dropped too, including privileges. This is not true of CREATE OR REPLACE.

SQL> create procedure p1 is
  2  begin
  3      null;
  4  end;
  5  /

Procedure created.

SQL> grant execute on p1 to xyz;

Grant succeeded.

SQL> select * from user_tab_privs_made
  2  /

GRANTEE                        TABLE_NAME                     GRANTOR                        PRIVILEGE                     GRA HIE
------------------------------ ------------------------------ ------------------------------ ---------------------------------------- --- ---
XYZ                            P1                             APC                            EXECUTE                       NO  NO

SQL> create or replace procedure p1 is
  2    n pls_integer;
  3  begin
  4   n := 1;
  5  end;
  6  /

Procedure created.

SQL> select * from user_tab_privs_made
  2  /

GRANTEE                        TABLE_NAME                     GRANTOR                        PRIVILEGE                     GRA HIE
------------------------------ ------------------------------ ------------------------------ ---------------------------------------- --- ---
XYZ                            P1                             APC                            EXECUTE                       NO  NO

SQL> drop procedure p1;

Procedure dropped.

SQL> create or replace procedure p1 (p in out pls_integer) is
  2  begin
  3    p := p+1;
  4  end;
  5  /

Procedure created.

SQL> select * from user_tab_privs_made
  2  /

no rows selected

SQL> 
like image 155
APC Avatar answered Sep 21 '22 03:09

APC


I believe if you replace a function, the privileges stay intact, although I usually like to create public synonyms and grant privileges to those.

The docs say:

REPLACE is an optional keyword used in object definitions (DDL) to override the older objet definition with a new one. It retains the access privileges of the object during the definition modification process. If the object is dropped and recreated, however, its privileges are lost.

like image 28
GolezTrol Avatar answered Sep 20 '22 03:09

GolezTrol