Skip to content

Instantly share code, notes, and snippets.

@mujahidk
mujahidk / user-constraints.sql
Created May 16, 2014 14:17
Oracle: All user constraints.
-- Oracle: All user constraints
select * from user_constraints
@mujahidk
mujahidk / drop-constraints.sql
Created May 16, 2014 14:30
Oracle: Drop all Foreign key and Check constraints
SET SERVEROUTPUT ON
BEGIN
FOR const IN ( SELECT TABLE_NAME, CONSTRAINT_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE IN ('R', 'C') /*Foreign Key and Check constraints.*/ )
LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '||const.TABLE_NAME ||' DROP CONSTRAINT '||const.CONSTRAINT_NAME;
DBMS_OUTPUT.PUT_LINE('Dropped constraint '||const.CONSTRAINT_NAME||' of table '||const.TABLE_NAME);
END LOOP;
END;
@mujahidk
mujahidk / oracle_guid.sql
Created May 16, 2014 15:12
Oracle: System GUID
-- Oracle Sys GUID
SELECT SYS_GUID() FROM DUAL;
@mujahidk
mujahidk / row-count-all-user-tables-column-id.sql
Last active July 21, 2017 13:43
Oracle: Row count of all tables which has ID column
SET SERVEROUTPUT ON
DECLARE ROW_COUNT NUMBER(10);
BEGIN
-- All user tables with column ID.
FOR TAB IN (SELECT TABLE_NAME FROM USER_TAB_COLUMNS WHERE COLUMN_NAME = 'ID' ORDER BY TABLE_NAME)
LOOP
EXECUTE IMMEDIATE 'SELECT COUNT(1) FROM ' || TAB.TABLE_NAME INTO ROW_COUNT;
DBMS_OUTPUT.PUT_LINE( TAB.TABLE_NAME || ',' || ROW_COUNT );
END LOOP;
END;
@mujahidk
mujahidk / oracle-invalid-db-objects.sql
Created May 20, 2014 17:37
Oracle: Script to find invalid objects, errors and way to compile
-- Query to find the invalid user objects in Oracle DB
SELECT OBJECT_NAME,
OBJECT_TYPE,
STATUS
FROM USER_OBJECTS
WHERE object_type IN ('PROCEDURE', 'TRIGGER', 'TABLE', 'VIEW', 'FUNCTION', 'INDEX')
AND STATUS ='INVALID';
--Query to find database view errors in the Oracle DB.
SELECT * FROM ALL_ERRORS WHERE TYPE = 'VIEW' ORDER BY SEQUENCE ASC;
@mujahidk
mujahidk / oracle-list-invalid-objects.sql
Created May 20, 2014 22:33
Oracle: Script to list the invalid objects in the DB
-- List the invalid objects from the Oracle DB
DECLARE
iCount INTEGER := 0;
objects VARCHAR2(4000 CHAR);
new_line VARCHAR2(2):= CHR(13) || CHR(10);
BEGIN
FOR usr_objects IN
(
SELECT object_name, object_type, status
@mujahidk
mujahidk / oracle-day2day.sql
Last active August 29, 2015 14:02
Oracle day2day db information queries
-- db version and other information
SELECT * FROM V$VERSION;
SELECT * FROM PRODUCT_COMPONENT_VERSION;
SELECT SYS_CONTEXT('USERENV','SERVICE_NAME') FROM DUAL;
SELECT * FROM GLOBAL_NAME;
@mujahidk
mujahidk / oracle-recyclebin-on-off.sql
Created June 26, 2014 19:01
Oracle recyclebin on/off and purge
-- Tunring off and on oracle recycle bin
ALTER SYSTEM SET RECYCLEBIN = OFF;
ALTER SYSTEM SET RECYCLEBIN = ON;
-- Purge oracle recycle bin
PURGE RECYCLEBIN;
@mujahidk
mujahidk / regex-number-space-letter.txt
Created July 1, 2014 16:37
Select empty space between a number and letter
//Select empty space between a number and letter
(?<=\d)\s+(?=\w)
//Or, the above will match the line endings too
(?<=\d)[ ]+(?=\w)
@mujahidk
mujahidk / drop-create-schema.sql
Created July 2, 2014 21:31
Drop and create oracle user/schema
--connection as dba
connect <system>/<password> as sysdba;
--drop oracle user/schema
drop user <user_name> cascade;
--create a new user and schema
create user <user_name> identified by <password>;
--grant dba, other roles and privileges