Oracle: using substitution variables in sqlplus

Get the value:

ACCEPT my_password CHAR PROMPT ‘Password:  ’ HIDE
ACCEPT birthday DATE FORMAT ‘dd/mm/yyyy’ DEFAULT ’01/01/1950′ PROMPT ‘Enter birthday date:  ’

Declaring the variable

DEFINE the_answer = 42

Undefine the variable

UNDEFINE the_answer

How to remember the result of the query

COLUMN the_date new_value the_rundate noprint;
SELECT TO_CHAR(SYSDATE, ‘DDMMYYYY_HH24MI’) the_date FROM dual;

SELECT ‘&the_rundate’ FROM dual ;

Save the variables to the file

store SET myvars.txt CREATE
store [...]

Oracle: disable all constraints referencing the table

BEGIN
FOR cur IN (SELECT fk.owner, fk.constraint_name , fk.table_name
    FROM all_constraints fk, all_constraints pk
    WHERE fk.CONSTRAINT_TYPE = ‘R’ AND
    pk.owner = ‘&which_owner’ AND
    fk.R_CONSTRAINT_NAME = pk.CONSTRAINT_NAME
           AND pk.TABLE_NAME = ‘&which_table’
        ) LOOP
        EXECUTE IMMEDIATE ‘ALTER TABLE ‘||cur.owner||’.'||cur.table_name||
      [...]

Oracle: info about the corrupted block

SELECT * FROM dba_extents
WHERE  file_id = &file_id
AND    &block_id BETWEEN block_id AND block_id + blocks [...]

Oracle: using recycling bin

Information about the objects in recycle bin:

SELECT object_name,
 original_name, TYPE,
 can_undrop , can_purge ,
 droptime
FROM recyclebin

or

show recyclebin

Restore

flashback TABLE mytable TO before DROP;

Clear recycle [...]

mysql: rownum functionality

Here are some examples how to use “rownum” functionality (similar to Oracle) in mysql

UPDATE mytable
SET col1 = ‘somevalue’
ORDER BY col2
LIMIT 300

rownum analog:

SELECT @rownum:=@rownum+1 rownum, mytable.*
FROM (SELECT @rownum:=0) [...]

mySQL: size of the database

SELECT table_schema "Database",
 sum( data_length + index_length ) / 1024 / 1024 "Size (MB)",
 sum( data_free )/ 1024 / 1024 "Free (MB)"
FROM information_schema.TABLES
GROUP BY table_schema ;

This will work in mySQL 5.0.2 and newer. Use SHOW TABLE STATUS command for [...]

Oracle: flashback usage

– To save the flashback information for the last 30 minutes:
ALTER SYSTEM SET UNDO_RETENTION = 1800;


SELECT * FROM some_table AS OF TIMESTAMP (’2008-10-08 06:31:58′, ‘YYYY-MM-DD HH24:MI:SSS’);

SELECT * FROM some_table AS OF SCN 12345678;

– dbms_flashback could be also used
EXECUTE dbms_flashback.enable_at_time [...]

Oracle: information about ASM

Get the information about ASM disks:

SELECT GROUP_NUMBER, DISK_NUMBER, TOTAL_MB/1024 GB, NAME FROM v$asm_disk;

Get the information about ASM diskgroups (including used space and the free space):

SELECT GROUP_NUMBER, NAME, TOTAL_MB/1024 TOTAL_GB, FREE_MB/1024 FREE_GB [...]

Oracle: file needs recovery (offline mode)

select d.file# f#, d.name, d.status, h.status
from v$datafile d, v$datafile_header h
where d.file# = h.file#
and (d.status not in (’SYSTEM’,'ONLINE’) or h.status != ‘ONLINE’ );

If there are such files, the recovery is necessary:

restore the file from the backup
recover datafile ‘&the_file_name’ ;
alter database datafile ‘&the_file_name’ online;

Another possibility (if there are a lot of files):

restore the files from the backup
recover tablespace [...]