Useful shortcuts for vi editor

Friday 30 March 2018

What is implicit commit?

After every DDL operation, implicit commit occurs such as while defining a new table.

e.g.
CREATE TABLE HR.EXAMPLE(ABC char(1));

Also we call it as "auto commit" operation.

blob vs bfile data types

BLOB (binary large objects) stores unstructured binary large objects. It is often used for graphic images, video clips, and sounds. The BLOB data type stores the content inside the Oracle database.

BFILE (binary file) is also used to large binary file stored outside the database. Oracle can read the file only, not modify it. Oracle requires appropriate operating system level read permissions on the file.

Ref: Oracle SQL Interactive Workbook (Alice Rischert)

How to do CTAS in Oracle?

CTAS stands for Create Table As Select

e.g.
SQL>
CREATE TABLE HR.EMPLOYEES_NEW
AS
   SELECT * FROM HR.EMPLOYEES;

-> By default, only "NOT_NULL" constraints copy to new table (HR.EMPLOYEES_NEW) if it has (HR.EMPLOYEES). (PK, FK, index, etc. does not copy)

CTAS is faster because UNDO and REDO data does not generate. (It use NOLOGGING AND PARALEL methods)

NULLS LAST keyword usage

-> HR.EMPLOYEES will be test table.
SQL> 
DESC HR.EMPLOYEES;

e.g.
To populate all data
SQL> 
SELECT * FROM HR.EMPLOYEES;

To manipulate data by "COMMISSION_PCT" column descending order.
SQL> 
  SELECT *
    FROM HR.EMPLOYEES
ORDER BY COMMISSION_PCT DESC;

-> This is wrong query cause "NULL" columns show on top. Therefore, "NULLS LAST" keyword should be used.
SQL>
  SELECT *
    FROM HR.EMPLOYEES
ORDER BY COMMISSION_PCT DESC NULLS LAST;

ORA-01495 solution

Action
SQL>
ANALYZE TABLE HR.EMPLOYEES LIST CHAINED ROWS INTO CHAINED_ROWS;

Error
ORA-01495: specified chain row table not found

Solution
SQL>
CREATE TABLE HR.CHAINED_ROWS
(
   OWNER_NAME          VARCHAR2 (30),
   TABLE_NAME          VARCHAR2 (30),
   CLUSTER_NAME        VARCHAR2 (30),
   PARTITION_NAME      VARCHAR2 (30),
   SUBPARTITION_NAME   VARCHAR2 (30),
   HEAD_ROWID          ROWID,
   ANALYZE_TIMESTAMP   DATE
);

Friday 23 March 2018

ORA-02270 solution

Preparation
SQL>
create table A (id number, soid number);

SQL>
create table B (id number);

Action
SQL> 
alter table A
add constraint SOID_B_FK
foreign key (soid)
references B (id);

Error
ORA-02270: no matching unique or primary key for this column-list

Reason
Parent table (B) does not have any primary key for matching with child table (A)

Solution
SQL>
alter table B
add constraint ID_PK 
primary key (id);

Friday 16 March 2018

ORA-00959 solution

Action
SQL> 
CREATE TABLE HR.EXAMPLE(ABC char(1));

Error
ORA-00959: tablespace '_$deleted$40$0' does not exist

Reason
Defined default tablespace is not available any more for HR user.

Solution
SQL>
ALTER USER HR DEFAULT TABLESPACE NEW_USERS;

Friday 9 March 2018

ORA-02449 solution

Preparation
SQL>
-- A table is child table
create table A (id number, soid number);

SQL>
-- B table is parent table
create table B (id number); 

SQL>
alter table B
add constraint ID_B_PK 
primary key (id);

SQL>
alter table A
add constraint ID_A_PK 
primary key (id);

SQL>
alter table A
add constraint SOID_B_FK
foreign key (soid)
references B (id);

Action
SQL> 
drop table B;

Error
ORA-02449: unique/primary keys in table referenced by foreign keys

Solution
SQL>
drop table B cascade constraints;

Result
By "cascade constraints" keywords, only SOID_B_FK foreign key removed from Table A, ID_A_PK primary key remains for Table A.

Friday 2 March 2018

ORA-01720 solution

Preparation
"employees" table exists in HR schema

"v_employees" view exists in HR2 schema;
SQL> grant select on hr.employees_new to hr2;
SQL> create view hr2.v_employees as select * from hr.employees;

User wants to see H2.V_EMPLOYEES view with HR3 user.
Action
SQL> grant select on hr2.v_employees to hr3;

Error
ORA-01720: grant option does not exist for 'HR.EMPLOYEES'

Solution
Append grant option to HR.EMPLOYEES table for HR2 user
SQL> grant select on hr.employees to hr2 with grant option;

Then, grant select permission to HR3 user
SQL> grant select on hr2.v_employees to hr3;

Result
SQL> select * from HR2.v_employees;