You are here: Foswiki>Applications Web>AppHowToMain>AppHowToOracleSql (13 Apr 2020, JuttaFitzek)Edit Attach

Quick Reference: Oracle SQL

Useful Queries

Some usefull Oracle sql queries:
To start a session as sysdba: sqlplus sys@tnsname as sysdba;
To start a sysdba session under Windows (9iAS): sqlplus "/as sysdba"
To list all tables in current schema: SELECT table_name FROM user_tables;
or, all tables current user has access to: SELECT table_name FROM all_tables;
To list all schemas: SELECT username FROM all_users ORDER BY username;
To turn pause on: SET PAUSE ON;
To list top n rows of a table in order: SELECT * FROM (SELECT * FROM t ORDER BY c) WHERE ROWNUM <= n;
Show current database: SELECT * FROM global_name;
Use database: CONNECT schema/password@tnsname;
Show who I am: SHOW USER;
Describe table: DESC tablename;
Set display rows: SET PAGESIZE 66;
Read field constraints: SELECT constraint_name,search_condition FROM user_constraints WHERE table_name='tablename';
Copy table from foreign host to here: COPY FROM user@tnsname CREATE tablename USING SELECT * FROM tablename;
Start SQLPLUS without login: SQLPLUS /NOLOG
Change a user's password: ALTER USER user IDENTIFIED BY password;
Unlock an account ALTER USER user ACCOUNT UNLOCK;
Empty the current users recyclebin PURGE RECYCLEBIN;
Restore a deleted table from the recyclebin FLASHBACK TABLE <tab_name> TO BEFORE DROP [RENAME TO <tab_neu>];
Flashback one or several tables FLASHBACK TABLE user.tab_name1, user.tab_name1 TO TIMESTAMP (SYSTIMESTAMP – INTERVAL '10' MINUTE);
Flashback a database by one day SQL> SHUTDOWN IMMEDIATE
SQL> STARTUP MOUNT
SQL> FLASHBACK DATABASE TO TIMESTAMP (sysdate-1/24);
The database is now in an older consistent state and is only accessible with OPEN RESETLOGS:
SQL> ALTER DATABASE OPEN RESETLOGS;
Delete all leftover beamprocesses that once belonged to a supercycle
declare 
type id_table is table of beamprocesses.BEAMPROCESS_ID%type index by binary_integer; 
found_bp_id id_table;

begin 
dbms_output.ENABLE(10000);

select beamprocess_id 
bulk collect into found_bp_id 
from beamprocesses bp 
where bp.CONTEXT_TYPE = 'SC' 
and not exists ( 
  select null from supercycle_beamprocesses sbp where sbp.beamprocess_id = bp.beamprocess_id 
);

for i in found_bp_id.first .. found_bp_id.last loop 
  dbms_output.PUT_LINE(found_bp_id(i)); 
  settings_management.delete_beamprocess(found_bp_id(i)); 
end loop;

end; /

---------------------------------------------------------

declare 
type id_table is table of beamprocesses.BEAMPROCESS_ID%type index by binary_integer; 
l1 beamprocesses.BEAMPROCESS_ID%type;
i beamprocesses.BEAMPROCESS_ID%type;
found_bp_id id_table;

begin 
dbms_output.ENABLE(10000);

select beamprocess_id 
bulk collect into found_bp_id 
from beamprocesses bp 
where bp.CONTEXT_TYPE = 'SC' 
and not exists ( 
  select null from supercycle_beamprocesses sbp where sbp.beamprocess_id = bp.beamprocess_id 
);


l1 := found_bp_id.first;
while (l1 is not null)
loop
dbms_output.PUT_LINE(l1); 
l1 := found_bp_id.next(l1);
--settings_management.delete_beamprocess(l1); 
end loop;

end;
delete all obsolete device types delete from device_types_og where device_type_id in (
select device_type_id from device_types_og dt where not exists (select * from properties_og p, parameter_types_og pt where p.property_id = pt.property and p.device_type = dt.device_type_id)
and not exists (select * from devices where device_type_id = dt.device_type_id)
and src = 'LSA');
grant all privileges to LSA; For test database purposes: grant all privileges to another user, here to the user LSA.

Synonym creation

Execute the script below to create the actual script for creating synonyms ("Run script output as script" after running the script in SQL developer). Also be aware that you need to execute the script from the user "LSA" for the users "LSA_APPS" and "LSA_PUB" and from both of the users. Don't forget to set the correct values for the "l_access_rights" and "l_to_user" parameters in the script.
-- drop an (re)create synonyms for the lsa_data tablespace
SET serveroutput ON;
DECLARE 
  l_access_rights VARCHAR2(1) := 'W'; -- can be 'W' for write or 'R' for read access
  l_from_user VARCHAR2(30) := 'LSA'; -- table owner of the real tables
  l_to_user VARCHAR2(30) := 'LSA_APPS'; -- user that should receive the synonyms
BEGIN
  DBMS_OUTPUT.enable(1000000);
  -- drop all the existing synonyms for l_to_user that point to tables from l_from_user
  FOR x IN
    (SELECT 'DROP SYNONYM "' || l_to_user || '"."' || synonym_name || '";' stmt
     FROM
      (SELECT synonym_name
       FROM all_synonyms
       WHERE owner = l_to_user
       AND table_owner = l_from_user)
    )
  LOOP
    DBMS_OUTPUT.PUT_LINE(x.stmt);
  END LOOP;

  -- (re)create synonyms for l_to_user that point to tables and views from l_from_user
  FOR x IN

      (SELECT DISTINCT OBJECT_NAME, OBJECT_TYPE
       FROM all_objects
       WHERE object_type in ('TABLE', 'VIEW', 'SEQUENCE', 'PROCEDURE', 'FUNCTION', 'PACKAGE', 'TYPE')
       AND owner = l_from_user
       ORDER BY object_name)
    
  LOOP
  DBMS_OUTPUT.PUT_LINE('CREATE OR REPLACE SYNONYM "' || l_to_user || '"."' || x.OBJECT_NAME || '" FOR "' || l_from_user || '"."' || x.OBJECT_NAME || '";');
    IF x.object_type IN ('TABLE','VIEW') THEN
      IF l_access_rights = 'W' THEN
        DBMS_OUTPUT.PUT_LINE('GRANT SELECT, UPDATE, INSERT, DELETE ON "' || l_from_user || '"."' || x.OBJECT_NAME || '" TO "' || l_to_user || '";');
      ELSE
        DBMS_OUTPUT.PUT_LINE('GRANT SELECT ON "' || l_from_user || '"."' || x.OBJECT_NAME || '" TO "' || l_to_user || '";');
      END IF;
    ELSIF x.object_type IN ('SEQUENCE') THEN
     IF l_access_rights = 'W' THEN
        DBMS_OUTPUT.PUT_LINE('GRANT SELECT, ALTER ON "' || l_from_user || '"."' || x.OBJECT_NAME || '" TO "' || l_to_user || '";');
      ELSE
        DBMS_OUTPUT.PUT_LINE('GRANT SELECT ON "' || l_from_user || '"."' || x.OBJECT_NAME || '" TO "' || l_to_user || '";');
      END IF;
    ELSIF x.object_type IN ('PROCEDURE','FUNCTION','PACKAGE') THEN
      IF l_access_rights = 'W' THEN
        DBMS_OUTPUT.PUT_LINE('GRANT EXECUTE ON "' || l_from_user || '"."' || x.OBJECT_NAME || '" TO "' || l_to_user || '";');
      ELSE
        DBMS_OUTPUT.PUT_LINE('REVOKE EXECUTE ON "' || l_from_user || '"."' || x.OBJECT_NAME || '" FROM "' || l_to_user || '";');
      END IF;
    END IF;
  END LOOP;

  DBMS_OUTPUT.new_line();
END;
/

Create a read only database user

Create the user (here we use lsaread) giving it the right to "connect" through the oracle web interface.
After that execute:

grant select any table to lsaread;
alter user lsaread quota unlimited on users;

-- JuttaFitzek - 06 Jan 2015
Topic revision: r4 - 13 Apr 2020, JuttaFitzek
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback