Skip to main content

Oracle SQL Tips


1.      Using SQL dummy table

This query will subtract first number from second. Dual table is used as dummy table to complete the sql statement, do nothing though.
select 36649-36598 from dual

2.      Audit details


get audit details
select * from DBA_AUDIT_TRAIL WHERE OBJ_NAME='course_material'

get audit count
SELECT * FROM all_tab_modifications  where TABLE_NAME = 'COURSE_MATERIAL'

3.      Constraint details


get constraint details
select * from ALL_CONSTRAINTS where CONSTRAINT_NAME = 'SYS_C0012069';

get column name of constratint
SELECT * FROM all_cons_columns where CONSTRAINT_NAME = 'SYS_C0012069';

see what constraints are referencing a table
SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "tabnam";

disable constraints
alter table "COURSE" DISABLE constraint "FK_PARENT_ID";

get referecing table names

 select owner,constraint_name,constraint_type,table_name,r_owner,r_constraint_name
  from all_constraints
 where constraint_type='R'
 AND OWNER ='CTGPORTAL_CERT'
   and r_constraint_name in (select constraint_name
                               from all_constraints
                              where constraint_type in ('P','U')
                                and table_name='COURSE');         
          
delete with foreign key constraints
delete from mytable     

4.      Accessible Tables


get the list of oracle tables can access
select * from all_tables order by tablespace_name, table_name

5.      Get active users in the db


SELECT * FROM v$session
WHERE username IS NOT NULL and status = 'ACTIVE'
ORDER BY username ASC; 

6.      Get triggers linked to a table

SELECT * FROM USER_TRIGGERS WHERE TABLE_NAME = 'COURSE_MEMBERSHIP';

7.      Rename column name

alter table a rename column id to new_id;

8.      Create new schema user

create user ctgportal identified by ctgportal ;
grant connect, unlimited tablespace, imp_full_database to ctgportal;

set dba role to new user to have full db access

9.      Get current schema


 select sys_context('userenv','current_schema') x from dual

10. Read via a datalink


Read from SMS database course table
    SELECT * From COURSE@smsportal    

11. Get first 10 rows


select * from COURSE where rownum <10

12. Get child tables


Select a.table_name, b.table_name, a.CONSTRAINT_NAME, b.CONSTRAINT_NAME
from sys.all_constraints a
inner join sys.all_constraints b on a.constraint_name=b.r_constraint_name
where  a.owner = 'CTGPORTAL'
and b.r_owner = 'CTGPORTAL'
and a.table_name = 'COURSE'

13. Common sql


Records created for last 180 minutes
select Entity_id  from TRANSACTION_TRACKER where date_created >  sysdate - interval '180' minute;

14. Creating a sequeance


DDL for Sequence CatalogChangeLogID_SEQ
   CREATE SEQUENCE  "CatalogChangeLogID_SEQ"  MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER  NOCYCLE ;

15. Joins


Get records not in table 2
SELECT  *
FROM    common
LEFT JOIN
        table1 t1
ON      t1.common_id = common.common_id
WHERE   t1.common_id IS NULL


    LEFT JOIN / IS NULL:

    SELECT  *
    FROM    common
    LEFT JOIN
            table1 t1
    ON      t1.common_id = common.common_id
    WHERE   t1.common_id IS NULL

    NOT EXISTS:

    SELECT  *
    FROM    common
    WHERE   NOT EXISTS
            (
            SELECT  NULL
            FROM    table1 t1
            WHERE   t1.common_id = common.common_id
            )

    NOT IN:

    SELECT  *
    FROM    common
    WHERE   common_id NOT IN
            (
            SELECT  common_id
            FROM    table1 t1
            )

16. creating a global temp table


CREATE GLOBAL TEMPORARY TABLE Table_name3
    (namea varCHAR(20),
     address varCHAR(20),
     phone varCHAR(20))
  ON COMMIT PRESERVE ROWS;

17. SQL Plus #connecting to a remote server

  
 sqlplus "ctgportal_cert/ctgportal_cert7@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=10.52.215.11)(Port=1521))(CONNECT_DATA=(SID=vuptdev)))"

     18.Alter Session


alter session set current_schema = ctgportal ;

Comments

Popular posts from this blog

Oracle Database 12c installation on Ubuntu 16.04

This article describes how to install Oracle 12c 64bit database on Ubuntu 16.04 64bit. Download software  Download the Oracle software from OTN or MOS or get a downloaded zip file. OTN: Oracle Database 12c Release 1 (12.1.0.2) Software (64-bit). edelivery: Oracle Database 12c Release 1 (12.1.0.2) Software (64-bit)   Unpacking  You should have following two files downloaded now. linuxamd64_12102_database_1of2.zip linuxamd64_12102_database_2of2.zip Unzip and copy them to \tmp\databases NOTE: you might have to merge two unzipped folders to create a single folder. Create new groups and users Open a terminal and execute following commands. you might need root permission. groupadd -g 502 oinstall groupadd -g 503 dba groupadd -g 504 oper groupadd -g 505 asmadmin Now create the oracle user useradd -u 502 -g oinstall -G dba,asmadmin,oper -s /bin/bash -m oracle You will prompt to set to password. set a momorable password and write it down. (mine is orac

DBCA : No Protocol specified

when trying to execute dbca from linux terminal got this error message. now execute the command xhost, you probably receiving No protocol specified xhost:  unable to open display ":0" issue is your user is not allowed to access the x server. You can use xhost to limit access for X server for security reasons. probably you are logged in as oracle user. switch back to default user and execute xhost again. you should see something like SI:localuser:nuwan solution is adding the oracle to access control list xhost +SI:localuser:oracle now go back to oracle user and try dbca it should be working

Java Head Dump Vs Thread Dump

JVM head dump is a snapshot of a JVM heap memory in a given time. So its simply a heap representation of JVM. That is the state of the objects. JVM thread dump is a snapshot of a JVM threads at a given time. So thats what were threads doing at any given time. This is the state of threads. This helps understanding such as locked threads, hanged threads and running threads. Head dump has more information of java class level information than a thread dump. For example Head dump is good to analyse JVM heap memory issues and OutOfMemoryError errors. JVM head dump is generated automatically when there is something like OutOfMemoryError has taken place.  Heap dump can be created manually by killing the process using kill -3 . Generating a heap dump is a intensive computing task, which will probably hang your jvm. so itsn't a methond to use offetenly. Heap can be analysed using tools such as eclipse memory analyser. Core dump is a os level memory usage of objects. It has more informaiton t