Friday 12 April 2013

How do you list all the objects available in given database?

Run the below any of the query, it will display the all objects in given database.
SELECT * FROM DBC.TABLES WHERE DATABASENAME=<'DATABASENAME'>
OR
Run the normal help command
HELP DATABASE '<DATABASENAME>'

Thursday 11 April 2013

How to rename columns using views ?

I have a emp table.I want to display columns empno as emp_number,ename as emp_name,job as emp_job,mgr as emp_manager,hiredate as emp_hiredate,sal as emp_salary,comm as emp_commission,deptno as emp_department in view. Renaming columns in views will help increase security of sensitive tables and hiding columns under alias names.In this scenario we have to create view like below.

empnoenamejobmgrhiredatesalcommdeptno
7788SCOTTANALYST756619/04/19873000020
7782CLARKMANAGER783909/06/19812450010
7521WARDSALESMAN769822/02/1981125050030
7698BLAKEMANAGER783901/05/19812850030
7902FORDANALYST756603/12/19813000020
7839KINGPRESIDENT017/09/19815000010
7900JAMESCLERK769803/12/1981950030
7654MARTINSALESMAN769828/09/19811250140030
7934MILLERCLERK778223/01/19821300010
7566JONESMANAGER783902/04/19812975020
7844TURNERSALESMAN769808/09/1981500030
7499ALLENSALESMAN769820/02/1981160030030
7876ADAMSCLERK778823/05/19871100020
CREATE VIEW EMP_V (emp_number,emp_name,emp_job,emp_manager,emp_hiredate,emp_salary, emp_commission,emp_department)
AS SELECT 
EMPNO
,ENAME
,JOB
,MGR
,HIREDATE
,SAL
,COMM
,DEPTNO FROM EMP;
run the below query, only alias columns are displayed.

emp_numberemp_nameemp_jobemp_manageremp_hiredateemp_salaryemp_commissionemp_department
7788SCOTTANALYST756619/04/19873000.00020
7782CLARKMANAGER783909/06/19812450.00010
7521WARDSALESMAN769822/02/19811250.00500.0030
7698BLAKEMANAGER783901/05/19812850.00030
7902FORDANALYST756603/12/19813000.00020
7839KINGPRESIDENT017/09/19815000.00010
7900JAMESCLERK769803/12/1981950.00030
7654MARTINSALESMAN769828/09/19811250.001400.0030
7934MILLERCLERK778223/01/19821300.00010
7566JONESMANAGER783902/04/19812975.00020
7844TURNERSALESMAN769808/09/1981500.000.0030
7499ALLENSALESMAN769820/02/19811600.00300.0030
7876ADAMSCLERK778823/05/19871100.00020

Tuesday 9 April 2013

What is View ?

VIEW:
  • It is just window for a table
  • It is the logical object for the physical collection of tables
Mainly we use views for
  • Limiting the no of rows and columns
  • reducing network
  • better security and encapsulation
Restrictions on view:
  • An index cannot be created on a view
  • A view cannot be contain on ORDER BY clause
  • Derived and aggregated columns must be assigned a alias name
  • A view cannot be used for UPDATE operations, if it contains
  • Data from more then one table (join view)
  • The same column specified twice
  • Derived columns (sal/12)
  • A Distinct clause
  • A group by clause
Creating View : 
CREATE VIEW <view_name> AS 
SELECT <column-name> [AS <alias-name>]
           [....,<column-name> [AS <alias-name>]]
           FROM <table-name>
           [WHERE <conditional-tests>]
           [WITH CHECK OPTION];
OR
CREATE VIEW may be abbreviated as CV
CV <view-name> [(<alias-name>, <alias-name>,...)]  AS
SELECT <column-name> [AS <alias-name>]
           [....,<column-name> [AS <alias-name>]]
           FROM <table-name>
           [WHERE <conditional-tests>]
           [WITH CHECK OPTION];
The employee table is used to demonstrate the use of views
Emp table - contains 13 rows.

empnoenamejobmgrhiredatesalcommdeptno
7788SCOTTANALYST756619/04/19873000020
7782CLARKMANAGER783909/06/19812450010
7521WARDSALESMAN769822/02/1981125050030
7698BLAKEMANAGER783901/05/19812850030
7902FORDANALYST756603/12/19813000020
7839KINGPRESIDENT017/09/19815000010
7900JAMESCLERK769803/12/1981950030
7654MARTINSALESMAN769828/09/19811250140030
7934MILLERCLERK778223/01/19821300010
7566JONESMANAGER783902/04/19812975020
7844TURNERSALESMAN769808/09/1981500030
7499ALLENSALESMAN769820/02/1981160030030
7876ADAMSCLERK778823/05/19871100020
The following creates a view to return the emp table in deptno 20. It limits the view to and empno,ename,and sal. This view restricts columns that is job,mgr,hiredate,comm and rows not for deptno 20.
CREATE VIEW emp_20_v as 
SELECT empno as empolyeenumber,ename as employeename,sal*12 as "Annual Salary" from emp where deptno=20

empolyeenumberemployeenameAnnual Salary
7788SCOTT36000
7566JONES35700
7902FORD36000
7876ADAMS13200
In the above view creation, we written alias name all columns in select statement. But this is another way to written alias names in crate view statement. Let us see how we write,
CREATE VIEW EMP_10_V (EMP_NO,EMP_NAME,EMP_SAL) AS
SELECT EMPNO,ENAME,SAL FROM EMP 
WHERE DEPTNO=10 
These two tables are used in the following examples

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7369SMITHCLERK790217/12/1980800020
7499ALLENSALESMAN769820/02/1981160030030
7521WARDSALESMAN769822/02/1981125050030
7566JONESMANAGER783902/04/19812975020
7654MARTINSALESMAN769828/09/19811250140030
7698BLAKEMANAGER783901/05/19812850030
7782CLARKMANAGER783909/06/19812450010
7788SCOTTANALYST756619/04/19873000020
7839KINGPRESIDENT017/11/19815000010
7844TURNERSALESMAN769808/09/19811500030
7876ADAMSCLERK778823/05/19871100020
7900JAMESCLERK769803/12/1981950030
7902FORDANALYST756603/12/19813000020
7934MILLERCLERK778223/01/19821300010

DEPTNODNAMELOC
10ACCOUNTINGNEW YORK
20RESEARCHDALLAS
30SALESCHICAGO
40OPERATIONSBOSTON
The following view performs the join
create view emp_dept_v as

select ename as emp_name

,sal as emp_sal

,dname as dept_name

,loc as dept_loc from emp inner join dept

on emp.deptno=dept.deptno

The next select references the view to perform the join 

select * from emp_dept_v order by emp_sal

emp_nameemp_saldept_namedept_loc
TURNER500SALESCHICAGO
JAMES950SALESCHICAGO
ADAMS1100RESEARCHDALLAS
WARD1250SALESCHICAGO
MARTIN1250SALESCHICAGO
MILLER1300ACCOUNTINGNEW YORK
ALLEN1600SALESCHICAGO
CLARK2450ACCOUNTINGNEW YORK
BLAKE2850SALESCHICAGO
JONES2975RESEARCHDALLAS
FORD3000RESEARCHDALLAS
SCOTT3000RESEARCHDALLAS
KING5000ACCOUNTINGNEW YORK
Another common use for views is to summarize data. Instead of creating an actual table and storing the data twice.
The following creates a view to perform the aggregation of emp by each department.

CREATE VIEW AGG_V AS
SELECT DEPTNO
,SUM(SAL) TOTALSAL
,AVG(SAL) AVARAGESAL
,MIN(SAL) MINIMUMSAL
,MAX(SAL) MAXIMUSAL 
FROM EMP
GROUP BY DEPTNO
The view can then be used to aggregate the columns that are created as a result of an aggregate,as seen below
SEL SUM(TOTALSAL) FROM AGG_V
SEL AVG(AVARAGESAL) FROM AGG_V
Deleting View : 
DROP VIEW [<database-name>]<View-name>
It removes the view name, column names and SELECT from the DD
Modifying View:
The syntax of the REPLACE VIEW follows
REPLACE VIEW [<database-name>]<view-name>
AS SELECT <column-name>
                 [...<column-name>]
FROM <table-name>
[WHERE <conditional-tests>]
[WITH CHECK OPTION];
REPLACE VIEW AGG_V AS
SELECT DEPTNO
,SUM(SAL) TOTALSAL
,AVG(SAL) AVARAGESAL
,MIN(SAL) MINIMUMSAL
,MAX(SAL) MAXIMUSAL
FROM EMP
GROUP BY DEPTNO
WHERE DEPTNO BETWEEN 10 AND 20
Notice that the keyword REPLACE appears instead of the original CREATE and the WHERE clause is changed form the original CREATE VIEW statement.










Monday 8 April 2013

How to check the table size in Teradata ?

The following syntax is used to find the table size in Teradata 

SyntaxSelect databasename,tablename,sum(currentperm) from   dbc.tablesize where databasename='<databasename>' and  tablename='<tablename>' group by databasename,tablename;

While running the above query, type your databasename in place of <databasename> and type your tablename in place of <tablename>