Wednesday, September 1, 2010

Oracle New created role is not working

In Oracle the newly created role is not working

I have created a role and grante select access on a table to the role
then granted the role to user
User not able to select on the table whose access is granted through the role.

this is explained and resolved in the example below:


SQL> create user a identified by a default tablespace users temporary tablespace temp quota unlimited on users;

User created.

SQL> grant connect, create table to a;

Grant succeeded.

SQL> conn a/a
Connected.
SQL> create table t (name varchar(15));

Table created.

SQL> conn sys as sysdba
Enter password:
Connected.
SQL> grant insert on a.t to a;

Grant succeeded.


SQL> conn a/a
Connected.
SQL> insert into t (name) values('harvarinder');

1 row created.

SQL> conn sys as sysdba
Enter password:
Connected.
SQL> create user b identified by b default tablespace users temporary tablespace temp quota unlimited on users;

User created.

SQL> grant connect to b;

Grant succeeded.

SQL> create role r;

Role created.

SQL> grant select on a.t to r;

Grant succeeded.

SQL> grant r to b;

Grant succeeded.

SQL> conn b/b
Connected.
SQL> select * from a.t;
select * from a.t
*
ERROR at line 1:
ORA-00942: table or view does not exist


SQL>

SQL> conn sys as sysdba
Enter password:
Connected.

SQL> select * from dba_role_privs where grantee = 'B';

GRANTEE GRANTED_ROLE ADM DEF
------------------------------ ------------------------------ --- ---
B R NO NO
B CONNECT NO YES


SQL> conn b/b;
Connected.
SQL> select * from a.t;
select * from a.t
*
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> conn sys as sysdba
Enter password:
Connected.
SQL> alter user b default role all;

User altered.

SQL> select * from dba_role_privs where grantee = 'B';

GRANTEE GRANTED_ROLE ADM DEF
------------------------------ ------------------------------ --- ---
B R NO YES
B CONNECT NO YES

SQL> conn b/b
Connected.
SQL> select * from a.t;

NAME
---------------
harvarinder

SQL>


The reason for this is that, only default roles get enabled implicitly. Non default roles are not enabled on log on they should be enabled explicitly by

SQL> set role role_name

The above command should enable the role (role_name) for that session and once the session is lost the role is disabled for that use. It must be enabled for next session if we want its benifits.

However if we want it to be enable by default then make that role as default.



Tuesday, August 31, 2010

Delete undo tablespace and create undotablespace

In oracle how to delete the undo tablespace. Whenever I am trying to delete it I am getting the following message

SQL> drop tablespace UNDOTBS1 including contents;
drop tablespace UNDOTBS1 including contents
*
ERROR at line 1:
ORA-30013: undo tablespace 'UNDOTBS1' is currently in use

Below is also is solution if you have delete the undo datafile and after that getting following message on every start up

SQL> startup
ORACLE instance started.

Total System Global Area 544030720 bytes
Fixed Size 1337776 bytes
Variable Size 406849104 bytes
Database Buffers 130023424 bytes
Redo Buffers 5820416 bytes
Database mounted.
ORA-00376: file 13 cannot be read at this time
ORA-01110: data file 13: '/u01/app/oracle/oradata/orcl/undotbs02.dbf'




Following is how you can do this:

SQL> alter system set undo_tablespace = '';

System altered.

SQL> DROP TABLESPACE UNDOTBS1 INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS;

Tablespace dropped.

SQL> create undo tablespace undotbs1 datafile '/u01/app/oracle/oradata/orcl/undotbs01.dbf' size 50M reuse autoextend on maxsize 500M;

Tablespace created.

Wednesday, August 18, 2010

Show all autoextendable tablespaces

In Oracle how to find all the autoextendable tablespaces:

set pages 10000
col tablespace_name format a20
col file_name format a45
col file_mb format 999999
col data_mb format 999999
col free_mb format 999999
col max_mb format 999999
select files.tablespace_name, files.file_name, ceil(files.mb) file_mb, ceil(files.mb - nvl(free.freemb,0)) data_mb,
nvl(free.freemb,0) free_mb, files.maxmb max_mb, autoextensible
from
(select file_id, file_name, tablespace_name, bytes/1024/1024 mb, autoextensible,
decode(autoextensible,'NO',bytes/1024/1024,decode(sign(maxbytes-bytes),-1,bytes/1024/1024,maxbytes/1024/1024)) maxmb
from dba_data_files) files,
(select file_id, sum(bytes)/1024/1024 freemb
from dba_free_space
group by file_id) free
where files.file_id = free.file_id (+)
and files.autoextensible='YES'
order by 1,2;

Show all NON-autoextentable tablespaces

In Oracle how to show all NON-autoextentable tablespacesset pages 10000:
col tablespace_name format a20
col file_name format a45
col file_mb format 999999
col data_mb format 999999
col free_mb format 999999
col max_mb format 999999
select files.tablespace_name, files.file_name, ceil(files.mb) file_mb, ceil(files.mb - nvl(free.freemb,0)) data_mb,
nvl(free.freemb,0) free_mb, files.maxmb max_mb, autoextensible
from
(select file_id, file_name, tablespace_name, bytes/1024/1024 mb, autoextensible,
decode(autoextensible,'NO',bytes/1024/1024,decode(sign(maxbytes-bytes),-1,bytes/1024/1024,maxbytes/1024/1024)) maxmb
from dba_data_files) files,
(select file_id, sum(bytes)/1024/1024 freemb
from dba_free_space
group by file_id) free
where files.file_id = free.file_id (+)
and files.autoextensible='NO'
order by 1,2;

Check if there is a block wait or request

In Oracle check if there is a block wait or request

select * from V$LOCK where REQUEST > 1 or block > 0;

Check the blocking sessions in database

In Oracle how to check what are the blocking sessions in a database:

select s1.username || '@' || s1.machine
|| ' ( SID=' || s1.sid || ' ) is blocking '
|| s2.username || '@' || s2.machine || ' ( SID=' || s2.sid || ' ) ' AS blocking_status
from v$lock l1, v$session s1, v$lock l2, v$session s2
where s1.sid=l1.sid and s2.sid=l2.sid
and l1.BLOCK=1 and l2.request > 0
and l1.id1 = l2.id1
and l2.id2 = l2.id2 ;

Check current sessions and processes

In Oracle how to check current sessions and processes:
set pages 10000 line 200
col username format a15
col machine format a15
col logon_time format a20
col osuser format a10
SELECT NVL(s.username, '(oracle)') AS username,
s.osuser,
s.sid,
s.serial#,
p.spid,
s.lockwait,
s.status,
s.module,
s.machine,
s.program,
TO_CHAR(s.logon_Time,'DD-MON-YYYY HH24:MI:SS') AS logon_time,
s.LAST_CALL_ET
FROM v$session s,
v$process p
WHERE s.paddr = p.addr
AND s.username is not null
ORDER BY s.LAST_CALL_ET, s.logon_Time, s.username, s.osuser;

Show all the users who have the same role

Show all the users who have the same role
select grantee from dba_role_privs where GRANTED_ROLE='&Enter_Role';


Privileges assigned to a user or schema

In Oracle what are the privileges assigned to a user or schema
set pages 10000 line 200
col owner format a20
col table_name format a30
col privilege format a30

select owner, table_name, privilege
from dba_tab_privs
where grantee = '&USER'
union all
select null, null, privilege
from dba_sys_privs
where grantee = '&USER'
union all
select null, null, granted_role
from dba_role_privs
where grantee = '&USER';

Space wise what are the top 10 objects

What are the top 10 consumers of space in a tablespace:


col owner for a7
col segment_name for a30
col MBytes for 9,999.99
select *
from (select owner, segment_name, sum(bytes)/1024/1024 MBytes
from dba_segments
where tablespace_name='SYSTEM'
group by owner, segment_name
order by 3 desc)
where rownum<11;

Oracle remove the parameter from spfile

How to remove the parameter from the spfile.

I was getting following in the alert log:

Deprecated system parameters with specified values:
remote_os_authent
End of deprecated system parameter listing

that means there is a deprecated system parameter which we are using. At the same time I was getting the following message while startup:

SQL> startup
ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
ORACLE instance started.

Total System Global Area 544030720 bytes
Fixed Size 1337776 bytes
Variable Size 406849104 bytes
Database Buffers 130023424 bytes
Redo Buffers 5820416 bytes
Database mounted.
Database opened.


Steps to resolve the issue are:
SQL> alter system reset remote_os_authent scope=spfile sid='*';

System altered.

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area 544030720 bytes
Fixed Size 1337776 bytes
Variable Size 406849104 bytes
Database Buffers 130023424 bytes
Redo Buffers 5820416 bytes
Database mounted.
Database opened.
SQL>

Creating user

To create a user:
create user username identified by password default tablespace TSName temporary tablespace TEMPTSName quota 10M on TSName ;

or
create user username identified by password default tablespace TSName temporary tablespace TEMPTSName quota unlimited on TSName ;


If you forgot to add the quota for users on tablespace then:
alter user quota quota unlimited on TSName ;

Number of DML Operations on a database

DML operation on database:

set lines 160;
set pages 999;
select *
from (SELECT
to_char(first_time,'YYYY-MON-DD') day,
to_char(sum(decode(to_char(first_time,'HH24'),'00',1,0)),'999') "00",
to_char(sum(decode(to_char(first_time,'HH24'),'01',1,0)),'999') "01",
to_char(sum(decode(to_char(first_time,'HH24'),'02',1,0)),'999') "02",
to_char(sum(decode(to_char(first_time,'HH24'),'03',1,0)),'999') "03",
to_char(sum(decode(to_char(first_time,'HH24'),'04',1,0)),'999') "04",
to_char(sum(decode(to_char(first_time,'HH24'),'05',1,0)),'999') "05",
to_char(sum(decode(to_char(first_time,'HH24'),'06',1,0)),'999') "06",
to_char(sum(decode(to_char(first_time,'HH24'),'07',1,0)),'999') "07",
to_char(sum(decode(to_char(first_time,'HH24'),'08',1,0)),'999') "08",
to_char(sum(decode(to_char(first_time,'HH24'),'09',1,0)),'999') "09",
to_char(sum(decode(to_char(first_time,'HH24'),'10',1,0)),'999') "10",
to_char(sum(decode(to_char(first_time,'HH24'),'11',1,0)),'999') "11",
to_char(sum(decode(to_char(first_time,'HH24'),'12',1,0)),'999') "12",
to_char(sum(decode(to_char(first_time,'HH24'),'13',1,0)),'999') "13",
to_char(sum(decode(to_char(first_time,'HH24'),'14',1,0)),'999') "14",
to_char(sum(decode(to_char(first_time,'HH24'),'15',1,0)),'999') "15",
to_char(sum(decode(to_char(first_time,'HH24'),'16',1,0)),'999') "16",
to_char(sum(decode(to_char(first_time,'HH24'),'17',1,0)),'999') "17",
to_char(sum(decode(to_char(first_time,'HH24'),'18',1,0)),'999') "18",
to_char(sum(decode(to_char(first_time,'HH24'),'19',1,0)),'999') "19",
to_char(sum(decode(to_char(first_time,'HH24'),'20',1,0)),'999') "20",
to_char(sum(decode(to_char(first_time,'HH24'),'21',1,0)),'999') "21",
to_char(sum(decode(to_char(first_time,'HH24'),'22',1,0)),'999') "22",
to_char(sum(decode(to_char(first_time,'HH24'),'23',1,0)),'999') "23"
from
v$log_history
GROUP by
to_char(first_time,'YYYY-MON-DD'))
order by to_date(day,'YYYY-MON-DD');

Undo used per session

UNDO used per session:

set pages 100 lines 160
col sid_serial for a20
col orauser for a15
col program for a30
col undoseg for a15
col undo for a10
SELECT TO_CHAR(s.sid)||','||TO_CHAR(s.serial#) sid_serial,
NVL(s.username, 'None') orauser,
s.program,
r.name undoseg,
t.used_ublk * TO_NUMBER(x.value)/1024||'K' "Undo"
FROM sys.v_$rollname r,
sys.v_$session s,
sys.v_$transaction t,
sys.v_$parameter x
WHERE s.taddr = t.addr
AND r.usn = t.xidusn(+)
AND x.name = 'db_block_size';

Check when was materialised view got refreshed

Checking when was the MV got refreshed last time:

alter session set nls_date_format = 'DD-MON-YY HH24:MI:SS';

column Owner format a14
column master_owner format a14

select distinct owner,
name mview,
master_owner master_owner,
last_refresh
from dba_mview_refresh_times;

set long 4000
alter session set nls_date_format='DD-MON-YYYY HH24:MI:SS';
select query, rewrite_enabled, refresh_mode rmode, refresh_method rmethod,
last_refresh_date, last_refresh_type rtype, staleness, compile_state,
build_mode bmode, fast_refreshable ref_fast
from dba_mviews where mview_name = '';

Tuesday, August 17, 2010

All Roles to a user(s)

Show all the roles assigned to a user(s):
select
lpad(' ', 2*level) || granted_role "Roles, Privileges of USER(s)"
from
(
/* THE USERS */
select
null grantee,
username granted_role
from
dba_users
where
username like upper('%&enter_username%')
/* THE ROLES TO ROLES RELATIONS */
union
select
grantee,
granted_role
from
dba_role_privs
/* THE ROLES TO PRIVILEGE RELATIONS */
union
select
grantee,
privilege
from
dba_sys_privs
)
start with grantee is null
connect by grantee = prior granted_role;

Space used by Tablespaces

Find the space used by Tablespaces:
set pages 10000 line 200
col tablespace_name format a20
col file_name format a50
col file_mb format 999999
col data_mb format 999999
col free_mb format 999999
col max_mb format 999999
select distinct files.tablespace_name, sum(ceil(files.mb)) file_mb, sum(ceil(files.mb - nvl(free.freemb,0))) data_mb,
sum(nvl(free.freemb,0)) free_mb, sum(files.maxmb) max_mb, round(sum(files.maxmb)-sum(ceil(files.mb - nvl(free.freemb,0)))) gth_cap,
round(sum(ceil(files.mb - nvl(free.freemb,0)))/sum(files.maxmb)*100) PCT_USED
from
(select file_id, file_name, tablespace_name, bytes/1024/1024 mb, autoextensible,
decode(autoextensible,'NO',bytes/1024/1024,decode(sign(maxbytes-bytes),-1,bytes/1024/1024,maxbytes/1024/1024)) maxmb
from dba_data_files) files,
(select file_id, sum(bytes)/1024/1024 freemb
from dba_free_space
group by file_id) free
where files.file_id = free.file_id (+)
group by files.tablespace_name
order by 1,2;

Find all user and their status in Oracle

To view current users in Oracle database and their current status:

select username, account_status from dba_users;