WINDOWS
e.g.
SQL>
sqlplus / as sysdba;
SQL>
SET LINESIZE 200;
SET PAGESIZE 100;
-- define a output file for writing
SPOOL 'C:\output_SQL.txt';
SELECT owner,
object_name,
created,
timestamp
FROM DBA_OBJECTS
WHERE STATUS = 'INVALID'
ORDER BY OBJECT_NAME;
-- stop to write
SPOOL OFF;
LINUX
e.g.
SQL>
sqlplus / as sysdba;
SQL>
SET LINESIZE 200;
SET PAGESIZE 100;
--define a output file for writing
SPOOL '/tmp/output_SQL.txt';
SELECT owner,
object_name,
created,
timestamp
FROM DBA_OBJECTS
WHERE STATUS = 'INVALID'
ORDER BY OBJECT_NAME;
-- stop to write
SPOOL OFF;
-> to see output file
$ cat /tmp/output_SQL.txt
Friday, 6 April 2018
Tuesday, 3 April 2018
What are pencil hardness grading scales?
2H -> H -> F -> HB -> B -> 2B
H means Hard (hard-tipped)
B means Bold (soft-tipped)
F means Fine
Info: from left to right, writing is thicker on the paper.
H means Hard (hard-tipped)
B means Bold (soft-tipped)
F means Fine
Info: from left to right, writing is thicker on the paper.
Generally, it would say that H (Hard) is designed for drafting and S (Soft) is designed for writing.
Labels:
pencil,
pencil thickness grades
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.
e.g.
CREATE TABLE HR.EXAMPLE(ABC char(1));
Also we call it as "auto commit" operation.
Labels:
auto commit,
implicit commit,
oracle
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)
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)
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;
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;
Labels:
desc,
hr,
NULLS LAST,
oracle,
sql
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
);
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
);
Labels:
chained rows,
ORA-01495
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);
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);
Labels:
child table,
foreign key,
ORA-02270,
oracle,
parent table,
primary key
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;
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;
Labels:
ORA-00959,
oracle,
tablespace
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.
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.
Labels:
cascade constranits,
ORA-02449,
oracle
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;
"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;
Friday, 23 February 2018
How to use SQL Tuning Advisor in Oracle SQL Developer?
Oracle SQL Developer -> Connections -> Right mouse click on desired database connection name (e.g. XE) -> Click "Open SQL Worksheet" -> Input and select SQL code block -> Right mouse click then select "SQL Tuning Advisor" or press "CTRL+F12" -> Lastly, analyze the result.
Labels:
oracle sql developer,
SQL Tuning Advisor
How to take screenshot on top window quickly?
e.g.
Alt key + Print Screen key -> Then CTRL + V in "mspaint.exe" -> Save it.
Alt key + Print Screen key -> Then CTRL + V in "mspaint.exe" -> Save it.
Labels:
screenshot,
windows
How to see both Oracle VM Virtual Box 32bit/64bit options?
e.g. If there are only 32 bit Linux distro in the OS list, you should enable virtualization support in BIOS
* BIOS -> CMOS Setup Utility -> Virtualization Technology -> [Enabled]
* BIOS -> CMOS Setup Utility -> Virtualization Technology -> [Enabled]
Labels:
bios,
linux,
oracle,
oracle virtual box
Friday, 16 February 2018
e.g. bulk insert
SET TIMING ON
/
CREATE TABLE HR.EMPLOYEES_TEST
AS
SELECT *
FROM HR.EMPLOYEES
WHERE ROWNUM < 0;
/
DECLARE
TYPE TABLE_TYP IS TABLE OF HR.EMPLOYEES%ROWTYPE;
TABLE_ROWS TABLE_TYP := TABLE_TYP ();
CURSOR TEST_CURSOR
IS
SELECT * FROM HR.EMPLOYEES;
V_COUNT NUMBER;
BEGIN
SELECT COUNT (1) INTO V_COUNT FROM HR.EMPLOYEES;
DBMS_OUTPUT.PUT_LINE ('TOTAL NUMBER OF ROWS: ' || V_COUNT);
OPEN TEST_CURSOR;
LOOP
FETCH TEST_CURSOR BULK COLLECT INTO TABLE_ROWS LIMIT 10;
FORALL A IN TABLE_ROWS.FIRST .. TABLE_ROWS.LAST
INSERT INTO HR.EMPLOYEES_TEST
VALUES TABLE_ROWS (A);
COMMIT;
DBMS_OUTPUT.PUT_LINE (
'Looping, TEST_CURSOR%rowcount = ' || TEST_CURSOR%ROWCOUNT);
EXIT WHEN TEST_CURSOR%NOTFOUND;
--EXIT WHEN TEST_CURSOR%ROWCOUNT > 1000000;
END LOOP;
DBMS_OUTPUT.PUT_LINE (
'Exited, TEST_CURSOR%rowcount = ' || TEST_CURSOR%ROWCOUNT);
CLOSE TEST_CURSOR;
END;
/
/
CREATE TABLE HR.EMPLOYEES_TEST
AS
SELECT *
FROM HR.EMPLOYEES
WHERE ROWNUM < 0;
/
DECLARE
TYPE TABLE_TYP IS TABLE OF HR.EMPLOYEES%ROWTYPE;
TABLE_ROWS TABLE_TYP := TABLE_TYP ();
CURSOR TEST_CURSOR
IS
SELECT * FROM HR.EMPLOYEES;
V_COUNT NUMBER;
BEGIN
SELECT COUNT (1) INTO V_COUNT FROM HR.EMPLOYEES;
DBMS_OUTPUT.PUT_LINE ('TOTAL NUMBER OF ROWS: ' || V_COUNT);
OPEN TEST_CURSOR;
LOOP
FETCH TEST_CURSOR BULK COLLECT INTO TABLE_ROWS LIMIT 10;
FORALL A IN TABLE_ROWS.FIRST .. TABLE_ROWS.LAST
INSERT INTO HR.EMPLOYEES_TEST
VALUES TABLE_ROWS (A);
COMMIT;
DBMS_OUTPUT.PUT_LINE (
'Looping, TEST_CURSOR%rowcount = ' || TEST_CURSOR%ROWCOUNT);
EXIT WHEN TEST_CURSOR%NOTFOUND;
--EXIT WHEN TEST_CURSOR%ROWCOUNT > 1000000;
END LOOP;
DBMS_OUTPUT.PUT_LINE (
'Exited, TEST_CURSOR%rowcount = ' || TEST_CURSOR%ROWCOUNT);
CLOSE TEST_CURSOR;
END;
/
Labels:
bulk insert,
oracle
[ADVICE] Free Database Monitoring Tool
You may use following free tool instead of Oracle EM (Enterprise Manager)
- ASH Viewer (Active Session History)
Labels:
ash viewer,
enterprise manager,
oracle
Friday, 9 February 2018
Naming conventions for database objects
index -> [SHORT_TABLE_NAME]_[SHORT_COLUMN_NAME]_[INDEX_TYPE]
e.g. EMP_DEPARTMENT_IX
constraint -> [SHORT_TABLE_NAME]_[SHORT_COLUMN_NAME]_[CONSTRAINT_TYPE]
e.g.
NOT NULL const. -> EMP_EMAIL_NN
FOREIGN KEY (Referential Integrity) const.
Source table: EMPLOYEES
Column name of source table: DEPARTMENT_ID
-> EMP_DEPT_FK
trigger -> [TABLE_NAME]_TRG
e.g. HR.EMPLOYEES_TRG
sequence -> [TABLE_NAME]_SEQ
e.g. HR.EMPLOYEES_SEQ
type -> [TABLE_NAME]_TYP
e.g. HR.EMPLOYEES_TYP
e.g. EMP_DEPARTMENT_IX
constraint -> [SHORT_TABLE_NAME]_[SHORT_COLUMN_NAME]_[CONSTRAINT_TYPE]
e.g.
NOT NULL const. -> EMP_EMAIL_NN
FOREIGN KEY (Referential Integrity) const.
Source table: EMPLOYEES
Column name of source table: DEPARTMENT_ID
-> EMP_DEPT_FK
trigger -> [TABLE_NAME]_TRG
e.g. HR.EMPLOYEES_TRG
sequence -> [TABLE_NAME]_SEQ
e.g. HR.EMPLOYEES_SEQ
type -> [TABLE_NAME]_TYP
e.g. HR.EMPLOYEES_TYP
Labels:
db objects,
name format,
oracle
Friday, 2 February 2018
How to debug code in Toad?
e.g.
Toad for Oracle -> Database -> Schema Browser -> Click HR -> Click Procedures -> Double click to "PROC_EXEC" procedure -> Set breakpoint or Add Watch (optional) -> Press Shift+F7 or Click Debug-Trace Into -> Confirm -> Click Execute
Toad for Oracle -> Database -> Schema Browser -> Click HR -> Click Procedures -> Double click to "PROC_EXEC" procedure -> Set breakpoint or Add Watch (optional) -> Press Shift+F7 or Click Debug-Trace Into -> Confirm -> Click Execute
How to grap and drop table columns in Toad?
e.g.
Toad for Oracle -> Type HR.EMPLOYEES then Press F4 in Editor -> Select desired columns by CTRL key-> Drag and drop to Editor by mouse -> Columns should show in Editor
Toad for Oracle -> Type HR.EMPLOYEES then Press F4 in Editor -> Select desired columns by CTRL key-> Drag and drop to Editor by mouse -> Columns should show in Editor
Labels:
drag and drop,
toad
Friday, 26 January 2018
optimizer_index_cost_adj parameter
Purpose
The purpose of optimizer_index_cost_adj parameter is to force use index even the cost is high. (decides large-table full-table scans or index range scans)
Usage
Current setting:
SQL> show parameter optimizer_index_cost_adj;
-- default is 100
-- ranges between 1 and 10000
(options like -> 10 | 100 | 1000)
Change setting permanently:
add this into init.ora file;
*.optimizer_index_cost_adj=10
-- "10" is to force it uses index
-- "100" is the default value
-- "1000" is not to force and it tries to different method (large-table full-table) to search data
Then restart the instance to perform.
Change setting for current instance - temporary:
SQL> alter system set optimizer_index_cost_adj = 10;
Result
SQL> show parameter optimizer_index_cost_adj;
-- output; 10
The purpose of optimizer_index_cost_adj parameter is to force use index even the cost is high. (decides large-table full-table scans or index range scans)
Usage
Current setting:
SQL> show parameter optimizer_index_cost_adj;
-- default is 100
-- ranges between 1 and 10000
(options like -> 10 | 100 | 1000)
Change setting permanently:
add this into init.ora file;
*.optimizer_index_cost_adj=10
-- "10" is to force it uses index
-- "100" is the default value
-- "1000" is not to force and it tries to different method (large-table full-table) to search data
Then restart the instance to perform.
Change setting for current instance - temporary:
SQL> alter system set optimizer_index_cost_adj = 10;
Result
SQL> show parameter optimizer_index_cost_adj;
-- output; 10
Labels:
optimizer_index_cost_adj,
oracle
Subscribe to:
Posts (Atom)