IBM DB2 - Database drop commands
Oracle
Thursday, July 8, 2021
IBM DB2 - Database drop commands
IBM DB2 - Database Creation Steps
IBM DB2 - Database Creation Steps
Below provided are the steps I have used to create a database in IBM db2 database engine.
Connect to db2 prompt after installation:
$ db2
(c) Copyright IBM Corporation 1993,2007
Command Line Processor for DB2 Client 10.1.0
You can issue database manager commands and SQL statements from the command prompt. For example:
db2 => connect to sample
db2 => bind sample.bnd
db2 => db2start
DB20000I The DB2START command completed successfully.
db2 =>
Tuesday, July 6, 2021
Space Reclamation Procedure for Oracle tables - Move tablespace
Space Reclamation Procedure for oracle objects: move tablespace
There are various space reclamation procedures available for the oracle database and the tables. Below is one of the methodologies to move all the tables and objects to the new tablespace. Though I have run it successfully in an environment, I remember correcting it while running it.
Pre-requisites:
1. Run it in test environment before running it in any important environment.
2. Have enough space in the new tablespace before initiating the scripts.
3. If a lob segment is not moved, then that particular table's data has to be moved to a new table created.
4. Always connect to the schema to be moved to run the scripts. Must not run as a 'SYS' user.
5. Tablespaces 'TBSDATA' and 'TBSINDEX' were created for the below example.
TEMP TABLES:
CREATE TABLE RECLAIM_SEGMENTS AS SELECT * FROM USER_SEGMENTS; CREATE TABLE COMPLETED_TABLES AS SELECT SEGMENT_NAME FROM USER_SEGMENTS
WHERE 1=2; CREATE TABLE NOT_COMPLETED AS SELECT SEGMENT_NAME FROM USER_SEGMENTS WHERE
1=2; |
PL/SQL PROCEDURE:
SET SERVEROUTPUT ON BEGIN FOR cur_rec IN (SELECT SEGMENT_NAME, SEGMENT_TYPE
FROM RECLAIM_SEGMENTS ) LOOP BEGIN EXECUTE IMMEDIATE 'ALTER TABLE ' || cur_rec.SEGMENT_NAME || ' enable row
movement '; EXECUTE IMMEDIATE 'ALTER TABLE ' || cur_rec.SEGMENT_NAME || ' MOVE
TABLESPACE TBSLIVEDATA parallel 20
nologging '; EXECUTE IMMEDIATE 'ALTER TABLE ' || cur_rec.SEGMENT_NAME || ' MOVE
LOB(XMLRECORD) STORE AS (TABLESPACE TBSDATA) parallel 20 nologging '; BEGIN FOR cur_index in (select INDEX_NAME from DBA_INDEXES where
table_name='' || cur_rec.SEGMENT_NAME
|| '' and INDEX_NAME NOT LIKE
'%SYS%' and INDEX_NAME NOT LIKE
'%LOB%' and owner='USER') LOOP EXECUTE IMMEDIATE 'ALTER INDEX ' || cur_index.INDEX_NAME ||' REBUILD
TABLESPACE TBSINDEX parallel 10
nologging '; END LOOP; END; EXECUTE IMMEDIATE 'INSERT INTO COMPLETED_TABLES VALUES (''' || cur_rec.SEGMENT_NAME ||
''')'; EXECUTE IMMEDIATE 'DELETE FROM RECLAIM_SEGMENTS WHERE
SEGMENT_NAME ='''|| cur_rec.SEGMENT_NAME || ''''; EXECUTE IMMEDIATE 'ALTER TABLE ' || cur_rec.SEGMENT_NAME || ' disable row
movement '; Commit; EXCEPTION WHEN OTHERS THEN EXECUTE IMMEDIATE 'INSERT INTO NOT_COMPLETED VALUES (''' || cur_rec.SEGMENT_NAME ||
''')'; DBMS_OUTPUT.put_line('FAILED: COULD NOT BE ALTERED ' ||
cur_rec.SEGMENT_TYPE || ' "' || cur_rec.SEGMENT_NAME || '"'); END; END LOOP; END; / |
Monday, July 5, 2021
PL/SQL procedure to Drop all the objects from the oracle user
Saturday, November 23, 2019
Steps to create a database using windows cmd prompt:
Thursday, May 17, 2018
Linux script file to create database in Oracle
kasi.sh :
cd $ORACLE_HOME
ORACLEHOME=$ORACLE_HOME
cd ../../..
cd oradata
echo "Please enter the database name"
read name
mkdir $name
cd $name
dir_path=`pwd`;
echo "YOUR FILES WILL BE STORED ON THIS DIRECTORY: $dir_path"
cat > 1.sql <<EOF
startup pfile='$dir_path/init$name.ora' nomount;
create spfile from pfile='$dir_path/init$name.ora';
EOF
cat > init$name.ora <<EOF
pga_aggregate_target=922746880
sga_target=2751463424
compatible='11.2.0.0.0'
control_files='$dir_path/control01.ctl','$dir_path/control02.ctl'
db_block_size=8192
db_name='$name'
diagnostic_dest='$ORACLE_BASE/diag'
processes=600
undo_tablespace='undotbs'
open_cursors=5000
EOF
cat > create$name.sql <<EOF
CREATE DATABASE "$name"
LOGFILE
GROUP 1 '$dir_path/redo01.log' SIZE 50M BLOCKSIZE 512,
GROUP 2 '$dir_path/redo02.log' SIZE 50M BLOCKSIZE 512,
GROUP 3 '$dir_path/redo03.log' SIZE 50M BLOCKSIZE 512
DATAFILE '$dir_path/system01.dbf' size 500m REUSE
SYSAUX DATAFILE '$dir_path/sysaux01.dbf' SIZE 325M REUSE
UNDO TABLESPACE undotbs DATAFILE '$dir_path/undotbs01.dbf' SIZE 900M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
DEFAULT TABLESPACE users DATAFILE '$dir_path/users01.dbf' SIZE 200M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
DEFAULT TEMPORARY TABLESPACE tempts1 TEMPFILE '$dir_path/temptbs01.dbf' SIZE 200M REUSE;
EOF
cat > tablespace.sql << EOF
CREATE TABLESPACE DATA DATAFILE '$dir_path/DATA.DBF' SIZE 500M AUTOEXTEND ON MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
EOF
cat > user.sql << EOF
CREATE USER user1 IDENTIFIED BY pass1 DEFAULT TABLESPACE DATA QUOTA UNLIMITED ON DATA;
grant connect to user1;
grant resource to user1;
grant query rewrite to user1;
grant create synonym to user1;
grant drop any procedure to user1;
grant create any table to user1;
grant select any table to user1;
grant update any table to user1;
grant insert any table to user1;
grant delete any table to user1;
grant drop any table to user1;
grant create any index to user1;
grant alter any index to user1;
grant drop any index to user1;
grant create any view to user1;
grant drop any view to user1;
grant create any directory to user1;
grant exp_full_database to user1;
grant imp_full_database to user1;
@?/rdbms/admin/catblock.sql;
grANT EXECUTE ON DBMS_LOCK TO user1;
EOF
export $ORACLE_SID=$name;
sqlplus -s /nolog << EOF
CONNECT / as sysdba;
whenever sqlerror exit sql.sqlcode;
set echo off
set heading off
@1.sql;
@create$name.sql;
@?/rdbms/admin/catalog.sql;
@?/rdbms/admin/catproc.sql;
conn system/manager;
@?/sqlplus/admin/pupbld.sql;
startup force;
@tablespace.sql;
@user.sql;
shut immediate
EOF
Wednesday, May 16, 2018
DELETE RECORDS USING BULK COLLECT - ORACLE
Pl/Sql :
SET SERVEROUTPUT ON
DECLARE
TYPE t_bulk_del IS TABLE OF TABLE2%ROWTYPE;
l_tab t_bulk_del;
CURSOR bulk_del IS
SELECT RECID FROM TABLE2;
BEGIN
OPEN bulk_del;
LOOP
FETCH bulk_del
BULK COLLECT INTO l_tab LIMIT 100000;
forall vloop in 1 .. l_tab.count
delete from TABLE1 where recid = l_tab(vloop);
commit;
EXIT WHEN l_tab.count = 0;
DBMS_OUTPUT.put_line(l_tab.count || ' rows');
END LOOP;
CLOSE bulk_del;
END;
/