Search This Blog
Wednesday, December 28, 2011
Revoke object granted to PUBLIC for a specific USER
FROM table_privileges
WHERE grantee = 'PUBLIC'
AND OWNER='UM'
ORDER BY owner, table_name;
When a new table is created , a SELECT priveleges on that TABLE is added to a ROLE.
====================================================================================
--Create the tigger and the hr_s role under sys.
CREATE OR REPLACE TRIGGER ddl_trig
AFTER DDL
ON DATABASE
DECLARE
main_sql_text varchar2(100);
job_name_ varchar2(30);
job_type_ varchar2(30);
job_action_ varchar2(4000);
sql_text varchar2(400);
job_id_ varchar2(30);
start_date_ timestamp;
BEGIN
if (ora_sysevent='CREATE' and ora_dict_obj_type='TABLE' and ora_dict_obj_owner='HR' )then
sql_text := 'grant select on '||ora_dict_obj_owner||'.'||ora_dict_obj_name||' to hr_s';
select to_char(systimestamp,'DDMMYYYYHH24MISS') into job_id_ from dual;
select SYSTIMESTAMP + interval '1' minute into start_date_ from dual;
main_sql_text:=''''||sql_text||'''';
job_name_:='job'||job_id_;
job_type_:='PLSQL_BLOCK';
job_action_:='begin execute immediate('||main_sql_text||'); end;';
execute immediate ('begin DBMS_SCHEDULER.create_job (job_name=> :a,job_type=> :b,job_action => :c,start_date=> :d,repeat_interval => NULL,end_date=> NULL,enabled=>TRUE,comments=> NULL); end;') using job_name_,job_type_,job_action_,start_date_;
end if;
END ddl_trig;
/
--Grant the role on a privelges users.
Tuesday, December 27, 2011
Prepare Oracle Linux 6 for Installing Oracle 11gR2 (rpm packages))
by Michael Georgiou
============================================================
mpfr-2.4.1-6
cpp-4.4.4-13
ppl-0.10.2-11
gcc-4.4.4-13
cloog-ppl-0.15.7-1.2
binutils-2
glibc-2
nss-softokn-freebl-3
glibc-2
nss-softokn-freebl-3
compat-libstdc++-33
glibc-common-2
glibc-devel-2
glibc-headers-2
elfutils-libelf-0
elfutils-libelf-devel-0
gcc-4
gcc-c++-4
ksh-
libaio-0
libaio-devel-0
libgcc-4
libstdc++-4
libstdc++-devel-4
make-3.81mpfr-2.4.1-6
cpp-4.4.4-13
ppl-0.10.2-11
gcc-4.4.4-13
cloog-ppl-0.15.7-1.2
numactl-devel-2
sysstat-9
compat-libstdc++-33
compat-libcap
All packages are available :/media/OL6.0 i386 Disc 1 20110203/Server/Packages
Friday, December 23, 2011
SQL Performance
TYPE rc IS REF CURSOR;
l_rc rc;
l_dummy all_objects.object_name%TYPE;
l_start number DEFAULT dbms_utility.get_time;
BEGIN
FOR i IN 1 .. 1000
LOOP
OPEN l_rc FOR
'select object_name
from all_objects
where object_id = ' || i;
FETCH l_rc INTO l_dummy;
CLOSE l_rc;
dbms_output.put_line(l_dummy);
END LOOP;
dbms_output.put_line(ROUND((dbms_utility.get_time-l_start)/100, 2) ||' Seconds...' );
END;
/
Thursday, December 22, 2011
Create schema statistics using DBMS_STATS
(
ownname=>'HR',
cascade=>TRUE, ---Indexes are included
method_opt=>'FOR ALL INDEXED COLUMNS',--Create Histograms
estimate_percent => dbms_stats.auto_sample_size, --Takes Sample size for the tables and Indexes
degree => 2 --Parallel degree 2
);
Wednesday, December 21, 2011
Create Service in a single Instance database
exec DBMS_SERVICE.START_SERVICE('TPCH','ORCL');
TPCH =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = TPCH)
)
)
sqlplus system/xxxxxx@TPCH
Monday, November 14, 2011
Package persistent stage - Example
--------------------------------------------------------------------------------------- Every time ( in the same session ) that a procedure EMP is executed it returns the next 30 rows from table employees until the cursor returns all rows.
CREATE OR REPLACE PACKAGE EMP AS
/* TODO enter package declarations (types, exceptions, methods etc) here */
cursor c is select * from employees;
return_rows number:=30;
procedure PRINTEMPLOYEES ;
END EMP;
----------------------------------------------------------------------------------------
CREATE OR REPLACE PACKAGE BODY EMP AS
procedure PRINTEMPLOYEES
as
cr c%ROWTYPE;
fetched number:=0;
BEGIN
IF NOT c%ISOPEN THEN
OPEN c;
END IF;
loop
fetch c into cr;
EXIT WHEN c%ROWCOUNT > return_rows OR c%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(cr.last_name);
end loop;
return_rows:=return_rows+30;
END PRINTEMPLOYEES;
END EMP;
/
----------------------------------------------------------------------------------------
Friday, November 11, 2011
Check Alert.log for ORA- errors using PLSQL
create or replace procedure write_file
( DIR IN VARCHAR2,filename IN VARCHAR2,text IN varchar2)
is
file utl_file.file_type;
begin
file:=utl_file.fopen(DIR, filename,'a');
utl_file.put_line(file,text);
utl_file.new_line(file);
utl_file.fclose(file);
end;
/
create or replace
procedure read_file ( DIR IN VARCHAR2,filename IN VARCHAR2, report IN VARCHAR2)
is
file utl_file.file_type; --file handler
buffer char(1000);
--EOF boolean:=false;
begin
if not utl_file.is_open(file) then
file := utl_file.fopen(DIR,filename,'R');
end if;
loop
begin
utl_file.get_Line(file,buffer,32767);
--dbms_output.put_line(buffer);
if instr(buffer,'ORA-')=1 then
---dbms_output.put_line(buffer);
write_file(DIR, report,buffer);
end if;
exception
when no_data_found then
exit;
end;
end loop;
-- To run everynight at midnight starting tonight
exec dbms_job.submit
(:v_JobNo, 'proc1;', TRUNC(SYSDATE)+1, 'TRUNC(SYSDATE)+1');
How to call c shared library (.so) from PLSQL
The example uses Oracle Linux 5 32bits , Oracle 11gR2 EE
Call C
csum
int csum(int a, int b)
{
return (a+b);
}
gcc -c -fPIC csum.c -o csum.o
gcc -shared -Wl,-soname,csum.so.1 -o csum.so.1.0.1 csum.o
mv csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib
ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so
ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so.1
create or replace library calc_sum as '/oracle/product/11.2.0/dbhome_1/lib/csum.so';
/
create or replace function getCSUM( a binary_integer,b binary_integer) return binary_integer
as language c
library calc_sum
name "csum";
/
select getCSUM(10,2) from dual;
How to Call C and Java from PLSQL
{
return (a+b);
}
gcc -shared -Wl,-soname,csum.so.1 -o csum.so.1.0.1 csum.o
ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so
ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so.1
/
as language c
library calc_sum
name "csum";
/
ALTER JAVA CLASS "JSUM"
RESOLVER (("C:\app\Administrator\product\11.2.0\dbhome_1\jdk\bin\*" pm)(* public))
RESOLVE;
PLSQL Large objects example
Large objects exercise
As sys create the oracle directory EMP_DATA. Grant the appropriate privileges to user HR to read and write on this directory.
Modify your employees table (
Write a procedure to print your table's date. For the resume print its length,for photo just print the photo size.
Solution
As sys:
create or replace directory IMG as 'C:\Libraries\Pictures';
grant read,write on directory IMG to hr;
As hr:
drop table michael_emp purge;
create table michael_emp
(
emp_id number primary key,
emp_name varchar2(30) not null
);
insert into michael_emp values ( 1,'mmmmm');
insert into michael_emp values ( 2,'ggggg');
commit;
alter table michael_emp add emp_resume clob;
alter table michael_emp add emp_photo bfile;
create or replace procedure updateEMP
as
cursor c is select emp_id,emp_name from michael_emp FOR UPDATE;
cr c%ROWTYPE;
begin
for cr in c loop
update michael_emp set emp_resume=RPAD(cr.emp_name||' resume is: ',32000,'.') ,
emp_photo=bfilename('EMP_DATA','anonymous.jpg')
where current of c;
end loop;
commit;
end;
CREATE OR REPLACE PROCEDURE PRINTEMP AS
cursor c is select * from michael_emp ;
cr c%ROWTYPE;
BEGIN
for cr in c loop
dbms_lob.fileopen(cr.emp_photo);
dbms_output.put_line('Employee ID:'||cr.emp_id||' Name:'||cr.emp_name||' Resume size: '||dbms_lob.getlength(cr.emp_resume) ||' Photo size: '||dbms_lob.getlength(cr.emp_photo));
dbms_lob.fileclose(cr.emp_photo);
end loop;
END PRINTEMP;
Create a small plsql parser
CREATE OR REPLACE FUNCTION pl_parser ( SQL_ IN VARCHAR2) RETURN varchar2
AS
cursor_name INTEGER;
ecode NUMBER;
emesg VARCHAR2(200);
BEGIN
cursor_name := dbms_sql.open_cursor;
DBMS_SQL.PARSE(cursor_name,SQL_,DBMS_SQL.NATIVE);
return 0;
EXCEPTION WHEN OTHERS THEN
ecode := SQLCODE;
emesg := SQLERRM;
return SQLERRM;
END;
/
Using PLSQL Collections -- Simple Example with Nested Table
PLSQL Fine Grained Control Example
Using PLSQL Collections -- Simple Example with VARRAY
Sunday, October 16, 2011
Summarizing RAC's service control utility ( SRVCTL) 10gR2
Summarizing RAC's service control utility ( SRVCTL) 10gR2
by Michael Georgiou
Srvctl is the main Oracle Real Application Cluster (RAC) utility which administer RAC objects. RAC objects are databases, instances, asm, nodeapps and services. The purposes of the srvctl is divided in three categories:
- Oracle cluster database configuration tasks
- General Oracle cluster database administration tasks
- Node level tasks
Oracle cluster database configuration tasks
Add, remove modify databases, instances, services
Set and unset environment from databases, instances, services
General Oracle cluster database administration tasks
Start, stop, enable, disable and get statuses for databases, instances, services
Relocate a service back to the preferred instance.
Node level tasks
Add, remove modify node level applications
Set and unset environment from node level applications
Start , stop applications ( Applications can be Virtual IP Adress, Listeners, Oracle notification services and Oracle enterprise manager) .
Srvctl commands
- add
- config
- enable
- disable
- start
- stop
- modify
- relocate
- status
- getenv
- setenv
- unsetenv
- remove
Commands description
add
database
instance
service
asm
nodeapps
examples:
srvctl add database -d ORCL -o $ORACLE_HOME
srvctl add instance -d ORCL -i ORCL1 -n racnode1
srvctl add service -d ORCL -s ORCLSRV -r racnode1 -a racnode2
srvctl add asm -n racnode1 -i asm1 -o $ORACLE_HOME
srvctl add nodeapps -n racnode1 -o $ORACLE_HOME \
-A 192.168.0.1/255.255.255.0
config
database
database
service
asm
nodeapps
listener
examples:
srvctl config database -ORCL
srvctl config -d ORCL -s ORCLSRV
srvctl config asm -n noderac1
srvctl config nodeapps -n noderac1
srvctl config listener -n noderac1
enable|disable
database
instance
service
asm
examples:
srvctl enable database -d ORCL
srvctl enable instance -d ORCL -i ORCL1,ORCL2
srvctl enable service -d ORCL -s ORCLSRV
srvctl enable asm -n noderac1 -i asm1
srvctl disable database -d ORCL
srvctl disable instance -d ORCL -i ORCL2,ORCL3
srvctl disable service -d ORCL -s ORCLSRV
srvctl disable asm -n noderac1 -i asm1
start|stop
database
database
service
asm
nodeapps
listener
examples:
srvctl start database -d ORCL -o open
srvctl start instance -d ORCL -i ORCL1,ORCL2
srvctl start service -d ORCL -s ORCLSRV
srvctl start asm -n noderac1 -i asm1
srvctl start nodeapps -n noderac1
srvcrl start listener -n noderac1
srvctl stop database -d ORCL -o immediate -c sys/****@ORCL1
srvctl stop instance -d ORCL -i ORCL1,ORCL2
srvctl stop service -d ORCL -s ORCLSRV
srvctl stop asm -n noderac1 -i asm1
srvctl stop nodeapps -n noderac1
srvcrl stop listener -n noderac1
modify
database
instance
service
nodeapps
Use modify database when:
-change ORACLE_HOME location
-change spfile location
-change the database role (primary,physical_standby,logical_standby)
-change startup option
-change management policy(automatic,manual
Use modify instance when:
-relocate a database instance
-establish a dependency between an asm instance and database instance
Use modify service when:
-Move service from one instance to another
-Changes which instances to be preferred and available
examples:
srvctl modify database -d ORCL -r logical_standby
srvctl modify instance -d ORCL -i ORCL1 -s ASM1
srvctl modify service -d ORCL -s ORCLSRV -r ORCL2 -a ORCL1
srvctl modify nodeapps -n noderac1 -A 192.168.100.2/255.255.255.0/etch0
More service examples:
-Move a service from instance ORCL1 to ORCL2
srvctl modify service -d ORCL -s ORCLSRV -i ORCL1 -t ORCL2
-Change an available instance to be a preferred instance
srvctl modify service -d ORCL -s ORCLSRV -i ORCL1 -r
relocate
service
example:
srvctl relocate service -d ORCL -s ORCLSRV -i ORCL1 -t ORCL3
status
database
instance
service
nodeapps
asm
examples:
srvctl status database -d ORCL -f
srvctl status instance -d ORCL -i ORCL1
srvctl status service -d ORCL -s ORCLSRV
srvctl status asm -n noderac1
srvctl status nodeapps -n noderac1
getenv
database
instance
service
nodeapps
examples:
srvctl getenv database -d ORCL
srvctl getenv instance -d ORCL -i ORCL1
srvctl getenv service -d ORCL -s ORCLSRV
srvctl getenv nodeapps -n noderac1
setenv
database
instance
service
nodeapps
examples:
srvctl setenv database -d ORCL -t LANG=EN
srvctl setenv instance -d ORCL -i ORCL1 -t LANG=EN
srvctl setenv service -d ORCL -s ORCLSRV -t CLASSPATH=/usr/local/jdk/jre/rt.jar
srvctl setenv nodeapps -n noderac1 -t CLASSPATH=/usr/local/jdk/jre/rt.jar
unsetenv
database
instance
service
nodeapps
examples:
srvctl unsetenv database -d ORCL -t LANG
srvctl unsetenv instance -d ORCL -i ORCL1 -t LANG=EN
srvctl unsetenv service -d ORCL -s ORCLSRV -t CLASSPATH
srvctl unsetenv nodeapps -n noderac1 -t CLASSPATH
remove
database
instance
service
nodeapps
asm
examples:
srvctl remove database -d ORCL
srvctl remove instance -d ORCL -i ORCL1
srvctl remove service -d ORCL -s ORCLSRV
srvctl remove service -d ORCL -s ORCLSRV -i ORCL3,ORCL4
srvctl remove nodeapps -n noderac1
srvctl remove asm -n noderac1
Note : use crs_stat -t or crs_stat -u for validating commands
Table: Available commands per RAC's object
Command | Database | Instance | Service | ASM | Nodeapps |
add | √ | √ | √ | √ | √ |
config | √ | | √ | √ | √ |
enable|disable | √ | √ | √ | √ | |
start|stop | √ | √ | √ | √ | √ |
modify | √ | √ | √ | | √ |
relocate | | | √ | | |
Status | √ | √ | √ | √ | √ |
getenv | √ | √ | √ | | √ |
setenv | √ | √ | √ | | √ |
unsetenv | √ | √ | √ | | √ |
remove | √ | √ | √ | √ | √ |
For more details please read oracle's reference Oracle database clusterware and oracle real application clusters administration and deployment appendix E.