In a hierarchical data model, data are organized into a tree-like structure. The structure allows repeating information using parent/child relationships: each parent can have many children but each child only has one parent. so all the relationships are 1:N.
An example of the hierarchical data model would be if an organization had records of employees in a table called "Employees". In the table there would be attributes/columns such as First Name, Last Name, Job Name and Wage. The company also has data about the employee’s children in a separate table called "Children" with attributes such as First Name, Last Name, and DOB. The Employee table represents a parent segment and the Children table represents a Child segment in the hierarchical model.
LDAP model, file systems, Windows registry, XML documents are some well known examples where hierarchical data model is used.
Primary key for default value
Interesting question, can a attribute which is primary key or part of primary key have default value?
Sounds like a stupid questions. How can a primary key have a default value. but look at the table definition below. This executes in Oracle without any issue:
create table test_key_default(COL1 varchar2(10) DEFAULT 'ABCD' PRIMARY KEY, COL2 VARCHAR2(10));
Lets try inserting some values:
INSERT INTO TEST_KEY_DEFAULT(COL1,COL2) VALUES ('TEST1','TEST1');
INSERT INTO TEST_KEY_DEFAULT(COL2) VALUES ('TEST2');
INSERT INTO TEST_KEY_DEFAULT(COL2) VALUES ('TEST3');
Only the third statement fails, because of the obvious reasons.
This whole discussion might sound stupid but it has two points:
1. You can create a primary key with default value. There is no restriction at Oracle Parser/Compiler level. It will not give any error while creating the table.
2. It will make lot of sense, if you use a default value in one column of composite primary key, specially when table is shared amongst many projects. Some project miht be using part of primary key, others might be using primary key completely.
Sounds like a stupid questions. How can a primary key have a default value. but look at the table definition below. This executes in Oracle without any issue:
create table test_key_default(COL1 varchar2(10) DEFAULT 'ABCD' PRIMARY KEY, COL2 VARCHAR2(10));
Lets try inserting some values:
INSERT INTO TEST_KEY_DEFAULT(COL1,COL2) VALUES ('TEST1','TEST1');
INSERT INTO TEST_KEY_DEFAULT(COL2) VALUES ('TEST2');
INSERT INTO TEST_KEY_DEFAULT(COL2) VALUES ('TEST3');
Only the third statement fails, because of the obvious reasons.
This whole discussion might sound stupid but it has two points:
1. You can create a primary key with default value. There is no restriction at Oracle Parser/Compiler level. It will not give any error while creating the table.
2. It will make lot of sense, if you use a default value in one column of composite primary key, specially when table is shared amongst many projects. Some project miht be using part of primary key, others might be using primary key completely.
To find out space consumed by each tablespace in a database
Use following query to find out how much database space is cosumed by which tablespace in a database. With slight modifications the query can also show the space in MB:
select nvl(b.tablespace_name,nvl(a.tablespace_name,'UNKOWN')) name
, Gbytes_alloc Gbytes
, (Gbytes_alloc-nvl(Gbytes_free,0)) used
, nvl(Gbytes_free,0) free
, ((Gbytes_alloc-nvl(Gbytes_free,0))/Gbytes_alloc)*100 pct_used
, nvl(largest,0) largest
from
( select sum(bytes)/1024/1024/1024 Gbytes_free
, max(bytes)/1024/1024/1024 largest
, tablespace_name
from sys.dba_free_space
group by tablespace_name ) a
, ( select sum(bytes)/1024/1024/1024 Gbytes_alloc
, tablespace_name from sys.dba_data_files
group by tablespace_name ) b
where a.tablespace_name (+) = b.tablespace_name
order by 1
select nvl(b.tablespace_name,nvl(a.tablespace_name,'UNKOWN')) name
, Gbytes_alloc Gbytes
, (Gbytes_alloc-nvl(Gbytes_free,0)) used
, nvl(Gbytes_free,0) free
, ((Gbytes_alloc-nvl(Gbytes_free,0))/Gbytes_alloc)*100 pct_used
, nvl(largest,0) largest
from
( select sum(bytes)/1024/1024/1024 Gbytes_free
, max(bytes)/1024/1024/1024 largest
, tablespace_name
from sys.dba_free_space
group by tablespace_name ) a
, ( select sum(bytes)/1024/1024/1024 Gbytes_alloc
, tablespace_name from sys.dba_data_files
group by tablespace_name ) b
where a.tablespace_name (+) = b.tablespace_name
order by 1
To get list of underlying tables in a view
To get a list of tables being used in a view use following SQL query. This query is specially useful when you have to do tyhe verification for many views:
select * from all_dependencies where referenced_type='TABLE' and type='VIEW' and owner='RSKVIEW'
select * from all_dependencies where referenced_type='TABLE' and type='VIEW' and owner='RSKVIEW'
Do You Know
Do you know?
1. You can not alter a dblink in Oracle.
2. Unless "" (Quotes) are used to define passwords, passwords are not case sensitive in Oracle
3. In a partitioned table you can truncate one partition.
4. You can do a bulk load in a table with no constrains and indexes.
5. Grants for view is not derived from grants on underlying tables.
1. You can not alter a dblink in Oracle.
2. Unless "" (Quotes) are used to define passwords, passwords are not case sensitive in Oracle
3. In a partitioned table you can truncate one partition.
4. You can do a bulk load in a table with no constrains and indexes.
5. Grants for view is not derived from grants on underlying tables.
Database Jobs Running from a Schema
To find out what all jobs are running from a particular database schema:
SELECT S.PROCESS UNIX_PROCESS_ID
, P.SPID
, S.SID
, S.SERIAL#
, S.OSUSER
, S.USERNAME
, V.SQL_TEXT
, TO_CHAR( TRUNC(SYSDATE) + ( SYSDATE - S.LOGON_TIME ), 'hh24:mi:ss' ) DURATION
, S.LAST_CALL_ET/60 LAST_CALL_MIN
, S.STATUS
FROM V$PROCESS P
, V$SESSION S
, V$SQLTEXT V
WHERE S.PADDR = P.ADDR
AND S.USERNAME = 'SCOTT'
AND S.SQL_HASH_VALUE=V.HASH_VALUE
ORDER BY S.SID,V.PIECE;
SELECT S.PROCESS UNIX_PROCESS_ID
, P.SPID
, S.SID
, S.SERIAL#
, S.OSUSER
, S.USERNAME
, V.SQL_TEXT
, TO_CHAR( TRUNC(SYSDATE) + ( SYSDATE - S.LOGON_TIME ), 'hh24:mi:ss' ) DURATION
, S.LAST_CALL_ET/60 LAST_CALL_MIN
, S.STATUS
FROM V$PROCESS P
, V$SESSION S
, V$SQLTEXT V
WHERE S.PADDR = P.ADDR
AND S.USERNAME = 'SCOTT'
AND S.SQL_HASH_VALUE=V.HASH_VALUE
ORDER BY S.SID,V.PIECE;
To Run a DB script in backgroup
Step 1: Create the DB script - test.sql:
spool TEST.log
set verify on
set time on
set timing on
set echo on
set feedback on
select to_char(sysdate,'DD_MON_YYYY_hh24:mi') install_date from dual;
set define on
alter session set NLS_DATE_FORMAT = 'DD-MON-RRRR HH24:MI:SS';
-- Write your Oracle here. To call a script use following syntax
-- @Alter_User.sql
SELECT * FROM DUAL;
select to_char(sysdate,'DD_MON_YYYY_hh24:mi') install_date from dual;
set verify on
spool off
Step 2: Run it @ prompt using following command:
nohup sqlplus USer/Password@U121 @test.sql &
Step 3: above step will return process id. Note it down.
To check for status of excution use following command:
ps -aef grep
spool TEST.log
set verify on
set time on
set timing on
set echo on
set feedback on
select to_char(sysdate,'DD_MON_YYYY_hh24:mi') install_date from dual;
set define on
alter session set NLS_DATE_FORMAT = 'DD-MON-RRRR HH24:MI:SS';
-- Write your Oracle here. To call a script use following syntax
-- @Alter_User.sql
SELECT * FROM DUAL;
select to_char(sysdate,'DD_MON_YYYY_hh24:mi') install_date from dual;
set verify on
spool off
Step 2: Run it @ prompt using following command:
nohup sqlplus USer/Password@U121 @test.sql &
Step 3: above step will return process id. Note it down.
To check for status of excution use following command:
ps -aef grep
Truncating one partition in a partitioned table
Use following syntax to truncate a partition in a table. This performs far better than DELETE FROM (obviously!):
ALTER TABLETRUNCATE PARTITION ;
ALTER TABLE
To Find Out Space Consumed by a Table
Step 1: Analyze the table.
Step 2: run the following query which will tell you average space consumption for one row in the table.
SELECT TABLE_NAME,
NUM_ROWS,
AVG_ROW_LEN
FROM DBA_TABLES
WHERE OWNER = 'SCOTT'
AND TABLE_NAME = 'EMPLOYEES'
Step 3: Multiply estimated number of rows with AVG_ROW_LEN. This is the required space in bytes.
Step 2: run the following query which will tell you average space consumption for one row in the table.
SELECT TABLE_NAME,
NUM_ROWS,
AVG_ROW_LEN
FROM DBA_TABLES
WHERE OWNER = 'SCOTT'
AND TABLE_NAME = 'EMPLOYEES'
Step 3: Multiply estimated number of rows with AVG_ROW_LEN. This is the required space in bytes.
Gathering Database Statistics
Oracle recommends DBMS_STATS package as preferred method of gathering object statistics. Oracle list a number of benefits to using it including parallel execution, long term storage of statistics and transfer of statistics between servers.
A simple statement to gather statistics will look like:
EXEC DBMS_STATS.gather_table_stats('SCOTT', 'EMPLOYEES');
You have many options to choose from. A statement with frequently used options is:
execute dbms_stats.gather_table_stats(ownname=>'SCOTT', tabname=>'EMPLOYEES',estimate_percent=>50, method_opt=>'FOR ALL INDEXED COLUMNS SIZE AUTO', granularity=>'ALL', CASCADE => TRUE);
A simple statement to gather statistics will look like:
EXEC DBMS_STATS.gather_table_stats('SCOTT', 'EMPLOYEES');
You have many options to choose from. A statement with frequently used options is:
execute dbms_stats.gather_table_stats(ownname=>'SCOTT', tabname=>'EMPLOYEES',estimate_percent=>50, method_opt=>'FOR ALL INDEXED COLUMNS SIZE AUTO', granularity=>'ALL', CASCADE => TRUE);
To Find Available Space in a Tablespace
Use following query:
SELECT TABLESPACE_NAME,SUM(BYTES)/(1024*1024) AVAILABLE_IN_MB
FROM DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME
SELECT TABLESPACE_NAME,SUM(BYTES)/(1024*1024) AVAILABLE_IN_MB
FROM DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME
To check when DB stats last gathered
To check when DB stats last gathered:
Run this query:
SELECT * FROM sys.dba_tables
WHERE TABLE_NAME = 'EMPLOYEES'
AND OWNER = 'SCOTT'
check the attribute - LAST_ANALYZED.
this is the timestamp when this table was last analyzed.
Oracle recommends re-gathering stats on a table when # of rows in a table change by 10-15%.
Run this query:
SELECT * FROM sys.dba_tables
WHERE TABLE_NAME = 'EMPLOYEES'
AND OWNER = 'SCOTT'
check the attribute - LAST_ANALYZED.
this is the timestamp when this table was last analyzed.
Oracle recommends re-gathering stats on a table when # of rows in a table change by 10-15%.
Subscribe to:
Posts (Atom)