Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Wednesday, June 22, 2016



Oracle SQL WITH Clause
       subquery factoring    or       Materializing subqueries

                               (Simplify complex SQL Subquery)


 Oracle offers three types of materialization:
Global Temporary Tables 
 The table definition is permanent.
Materialized Views 
 The definition and the data are permanent.
 The WITH clause 
 The materialized subquery data is persistent through the query.

-         It’s called Subquery Factoring  or Named Subquery
-         Allows you to give a sub-query block a name and call subquery with this name.
-         The SQL WITH clause is very similar to the use of Global temporary tables.
-         This technique often used to improve query speed for complex subqueries and make simplify complex SQL.
-         You can use subquery factoring (SQL WITH) when a subquery is executed multiple times within main Query.
-         You can create any number of named factoring queries.

Syntax
WITH
<alias_name> AS (sql_subquery_statement)
SELECT column_list FROM <alias_name>[,tablename]
[WHERE <join_condition>]



Use SQL WITH  as table Alias

(Change Table alias before use it in main query)

WITH
E AS (SELECT * FROM EMP WHERE SAL>1200)
SELECT * FROM E WHERE E.DEPTNO = 10
UNION
SELECT * FROM E WHERE E.DEPTNO = 90

 (Use SQL WITH as multiple Subqueries)
WITH
EM AS (SELECT * FROM EMP),
DP AS (SELECT DEPTNO FROM DEPT WHERE LOC = 'DALLAS')
SELECT * FROM EM,DP WHERE EM.DEPTNO IN (DP.DEPTNO)


(Make simplify complex SQL)

WITH
     m AS  (
                SELECT TO_CHAR (SYSDATE, 'mm') g_month FROM DUAL
                ),
     d AS (
                SELECT TO_CHAR (SYSDATE, 'dd')   g_day FROM DUAL
                ),
     y AS (
                SELECT TO_CHAR (SYSDATE, 'yyyy') g_year FROM DUAL
              )
  SELECT g_day || '/' || g_month || '/' || g_year dat
  FROM m, d, y






Wednesday, December 9, 2015

Oracle Toad Team Coding

Oracle Toad Team Coding


Sure you can use source control and Team work server with Toad IDE that helps Developers and database Administrators to manage code changes in Oracle Database with confidence of no overwriting the changes of other team members .
Developers and Admins can work with same Procedures , Functions , packages triggers and Types and check-in – check-out their source code …Developer can reserve his source code to prevent any other developer to overwrite same source code .

Setup Team Coding:
-      Login in Toad with DBA privileges (system , sys ..)
-      Setup will create a new database schema in Database server with “ Toad ” User Name (recommended to leave default name Toad) witch will be as metadata provides the functionality to team work .
-      This schema will be shared with all Work team without risk of override the      changes made by other developers .

--------------------------------------------------------------------------------------------------------
Remember : Developers could not edit source code unless 
check-out File or database objects
Source code will be locked after Checked-in
--------------------------------------------------------------------------------------------------------
 Let us Start
Steps:

Login to TOAD as sysdba (use DBA privileges )










Select Configure Team Coding: 
Utilities menu > Team Coding > Configure Team Coding

Following message will appear. Click OK:

Click on Install Team Coding:

It is preferable to install Team Coding objects in standard TOAD schema. Select the tablespaces for the Team coding metadata. The database role TC_ADMIN_ROLE is used for:
  1. 1.     Edit Team Coding Configuration
  1. 2.     Edit Code Collections
  1. 3.     Freeze Team Coding Objects
  1. 4.     Break another user’s Team Coding lock
-      Let us Test Team coding to record editing source code 
 







The script to generate the Team Coding Objects will be displayed on screen. Click Execute:

Enter the password for the TOAD database user:

Installation for Team Coding is completed (took less than a minute):

Now the database is configured to use Team Coding across all the schemas. Above steps are needed to be completed only once per database. Click on “Enable Team Coding for this Database”:

This setup is the basic setup, where working on a single object at a time gives enough control. Once you check out any object, it is available in Team Coding Dashboard.

-       Create new Procedure


-       Now press Check-in to lock your code and prevent update
-       

-                
-          Press Check in from toolbar or Team coding menu to lock your code 

Record comment on your code before check-in




-                                            To update source code – press chek-out



-                                     Previewed and open team coding – Edit History
-      




-       Now as you see , any edit in Database objects from any member in your team will record in team Coding 

Thursday, October 22, 2015

Get Information about the largest Top 10 Tables in DB

Get Information about the largest Top 10 Tables in DB

Sometimes you need to query about the largest Tables or    DB Objects   
To resolve and monitor Server free spaces and resources

Open SQLPLUS 
Write this simple code (User should be with DBA privileges)



/> SELECT * FROM
(
     SELECT
          SEGMENT_NAME,
          SEGMENT_TYPE,
          BYTES/1024/1024 MEGA_BYTES,
          TABLESPACE_NAME,
          OWNER
     FROM
          DBA_SEGMENTS     
          WHERE 
                   (SEGMENT_TYPE = :TYP OR :TYP IS NULL)
          AND (OWNER               = :OWNR OR :OWNR IS NULL)
          ORDER BY MEGA_BYTES DESC
)
WHERE
     ROWNUM <= 10



REGIONS
TABLE
0.0625
USERS
HR
LOCATIONS
TABLE
0.0625
USERS
HR
DEPARTMENTS
TABLE
0.0625
USERS
HR
JOBS
TABLE
0.0625
USERS
HR
EMPLOYEES
TABLE
0.0625
USERS
HR
JOB_HISTORY
TABLE
0.0625
USERS
HR
PS_TXN
TABLE
0.0625
USERS
HR



Get Information about Current Database Version.


Get Information about Current Database Version.

If you need to open Service request , you have to know what is your database version .

Open SQLPLUS 
Write this simple code (User should be with DBA privileges)



/> select * from v$version;


Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
CORE       11.1.0.7.0                Production
TNS for Linux: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production

IF you need to know your current privileges

/> SELECT GRANTED_ROLE FROM DBA_ROLE_PRIVS WHERE
GRANTEE = 'HR'

RESOURCE
CONNECT





O racle  SQL WITH Clause         subquery factoring     or         Materializing   subqueries                                 (Sim...