What are temporary tables? When are they useful? are temporary tables? When are they useful? •...
Transcript of What are temporary tables? When are they useful? are temporary tables? When are they useful? •...
What are temporary tables? When are they useful?
• Temporary tables exists solely for a particular session, orwhose data persists for the duration of the transaction.
• The temporary tables are generally used to supportspecialized rollups or specific application processingrequirements.
• Unlike a permanent table, a space is not allocated to atemporary table when it is created.
• Space will be dynamically allocated for the table as rows areinserted.
• The CREATE GLOBAL TEMPORARY TABLE command is used tocreate a temporary table in Oracle.
Barjinder Singh
Syntax of TEMPORARY TABLE command
• CREATE GLOBAL TEMPORARY TABLE <table_name> (<columns details>
) ON COMMIT {PRESERVE|DELETE} ROWS;
Barjinder Singh
External tables
• External tables allow Oracle to query data that is stored outside the database in flat files. The ORACLE_LOADER driver can be used to access any data stored in any format that can be loaded by SQL*Loader.
• Create the external table using the CREATE TABLE..ORGANIZATION EXTERNAL syntax.
Barjinder Singh
External tables
• Create a directory object pointing to the location of the files.
• create or replace directory <directory name> as 'd:\';
Barjinder Singh
External table
• create directory dn as 'd:\';
Create database table and insert data into it• create table tt1(c1 int);• insert into tt1 values(101);Create external table• create table exttable• (c) • organization external• (type ORACLE_DATAPUMP• default directory dn• location('exttab.dat'))as select c1 from tt1;Query to external table• select * from exttable;
Barjinder Singh
Querying Data From Flat Files in Oracle• CREATE OR REPLACE DIRECTORY ext_tab_data AS 'E:/data';
• CREATE TABLE countries_ext (• country_code VARCHAR2(5),• country_name VARCHAR2(50),• country_language VARCHAR2(50)• )• ORGANIZATION EXTERNAL (• TYPE ORACLE_LOADER• DEFAULT DIRECTORY ext_tab_data• ACCESS PARAMETERS (• RECORDS DELIMITED BY NEWLINE• FIELDS TERMINATED BY ','• MISSING FIELD VALUES ARE NULL• (• country_code CHAR(5),• country_name CHAR(50),• country_language CHAR(50)• )• )• LOCATION ('Countries1.txt','Countries2.txt')• )• PARALLEL 5• REJECT LIMIT UNLIMITED;
Barjinder Singh
Create a table using a subquery
• Create table <new table name> as select <colname1>, <colname2>…. From <existing table name>
• CREATE TABLE dept80AS SELECT empolyee_id,last_name,salary*12,hire date FROM employess WHERE department_id=80
Barjinder Singh
ANY operator
• The ANY comparison condition is used to compare a value to a list or subquery. It must be preceded by =, !=, >, <, <=, >= and followed by a list or subquery.
• SELECT empno, sal• FROM emp• WHERE sal > ANY (2000, 3000, 4000);• -- Transformed to equivalent statement without ANY.
• SELECT empno, sal• FROM emp• WHERE sal > 2000 OR sal > 3000 OR sal > 4000;
Barjinder Singh
ALL operator
• The ALL comparison condition is used to compare a value to a list or subquery. It must be preceded by =, !=, >, <, <=, >= and followed by a list or subquery.
• SELECT empno, sal• FROM emp• WHERE sal > ALL (2000, 3000, 4000);• -- Transformed to equivalent statement without ALL.
• SELECT empno, sal• FROM emp• WHERE sal > 2000 AND sal > 3000 AND sal > 4000;
Barjinder Singh
Oracle Built in Functions
There are two types of functions in Oracle.
1) Single Row Functions: Single row or Scalar functions return a value for every row that is processed in a query.
2) Group Functions: These functions group the rows of data based on the values returned by the query. The group functions are used to calculate aggregate values like total or average, which return just one total or one average value after processing a group of rows.
Barjinder Singh
• There are four types of single row functions. They are:
1) Numeric Functions: These are functions that accept numeric input and return numeric values.2) Character or Text Functions: These are functions that accept character input and can return both character and number values.
3) Date Functions: These are functions that take values that are of datatypeDATE as input and return values of datatype DATE, except for the MONTHS_BETWEEN function, which returns a number.
4) Conversion Functions: These are functions that help us to convert a value in one form to another form. For Example: a null value into an actual value, or a value from one datatype to another datatype like TO_CHAR, TO_NUMBER, TO_DATE etc.
• You can combine more than one function together in an expression. This is known as nesting of functions.
Barjinder Singh
• What is a DUAL Table in Oracle?This is a single row and single column dummy table provided by oracle. This is used to perform mathematical calculations without using a table.
• Select * from DUAL
Barjinder Singh
• Numeric Functions:
• Numeric functions are used to perform operations on numbers. They accept numeric values as input and return numeric values as output. Few of the Numeric functions are:
Function Name Examples Return Value
ABS (x)ABS (1)ABS (-1)
1-1
CEIL (x)CEIL (2.83)CEIL (2.49)CEIL (-1.6)
33-1
FLOOR (x)FLOOR (2.83)FLOOR (2.49)FLOOR (-1.6)
22-2
TRUNC (x, y)
ROUND (125.456, 1)ROUND (125.456, 0)ROUND (124.456, -1)
125.4125120
ROUND (x, y)
TRUNC (140.234, 2)TRUNC (-54, 1)TRUNC (5.7)TRUNC (142, -1)
140.23545140Barjinder Singh
• Character or Text Functions:
• Character or text functions are used to manipulate text strings. They accept strings or characters as input and can return both character and number values as output.
Function Name Examples Return Value
LOWER(string_value) LOWER('Good Morning') good morning
UPPER(string_value) UPPER('Good Morning') GOOD MORNING
INITCAP(string_value) INITCAP('GOOD MORNING') Good Morning
LTRIM(string_value, trim_text) LTRIM ('Good Morning', 'Good) Morning
RTRIM (string_value, trim_text) RTRIM ('Good Morning', ' Morning') Good
TRIM (trim_text FROM string_value) TRIM ('o' FROM 'Good Morning') Gd Mrning
SUBSTR (string_value, m, n) SUBSTR ('Good Morning', 6, 7) Morning
LENGTH (string_value) LENGTH ('Good Morning') 12
LPAD (string_value, n, pad_value) LPAD ('Good', 6, '*') **Good
RPAD (string_value, n, pad_value) RPAD ('Good', 6, '*') Good**Barjinder Singh
• Date Functions:
• These are functions that take values that are of datatype DATE as input and return values of datatypes DATE, except for the MONTHS_BETWEEN function, which returns a number as output.
Function Name Examples Return Value
ADD_MONTHS ( )
ADD_MONTHS ('16-Sep-81', 3)
16-Dec-81
MONTHS_BETWEEN( )
MONTHS_BETWEEN ('16-Sep-81', '16-Dec-81')
3
NEXT_DAY( )NEXT_DAY ('01-Jun-08', 'Wednesday')
04-JUN-08
LAST_DAY( )LAST_DAY ('01-Jun-08')
30-Jun-08
NEW_TIME( )NEW_TIME ('01-Jun-08', 'IST', 'EST')
31-May-08
Barjinder Singh
• Conversion Functions:
• These are functions that help us to convert a value in one form to another form. For Ex: a null value into an actual value, or a value from one datatype to another datatype like NVL, TO_CHAR, TO_NUMBER, TO_DATE.
• Few of the conversion functions available in oracle are:
Function Name
Return Value
TO_CHAR (x [,y])
Converts Numeric and Date values to a character string value. It cannot be used for calculations since it is a string value.
TO_DATE (x [, date_format])
Converts a valid Numeric and Character values to a Date value. Date is formatted to the format specified by 'date_format'.
NVL (x, y)If 'x' is NULL, replace it with 'y'. 'x'and 'y' must be of the same datatype.Barjinder Singh
Function Name ExamplesReturn Value
TO_CHAR ()
TO_CHAR (3000, '$9999')TO_CHAR (SYSDATE, 'Day, Month YYYY')
$3000Monday, June 2008
TO_DATE ()TO_DATE ('01-Jun-08')
01-Jun-08
NVL () NVL (null, 1) 1
Barjinder Singh
Examples
• select TO_CHAR (30, '$99') from dual;
• select TO_DATE (SYSDATE) from dual;
• SELECT TO_CHAR (SYSDATE, 'Day, Month YYYY') from dual
Barjinder Singh
INTERSECT operator
• The SQL INTERSECT operator is used to return the results of 2 or more SELECT statements. However, it only returns the rows selected by all queries or data sets. If a record exists in one query and not in the other, it will be omitted from the INTERSECT results.
• SELECT expression1, expression2, ... expression_n
• FROM tables
• [WHERE conditions]
• INTERSECT
• SELECT expression1, expression2, ... expression_n
• FROM tables
• [WHERE conditions];
Barjinder Singh
UNION ALL Operator
• The SQL UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It does not remove duplicate rows between the various SELECT statements (all rows are returned).
• Each SELECT statement within the UNION ALL must have the same number of fields in the result sets with similar data types.
• What is the difference between UNION and UNION ALL?
• UNION removes duplicate rows.
• UNION ALL does not remove duplicate rows.
Barjinder Singh
Syntax
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION ALL
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
Barjinder Singh
MINUS operator
• The SQL MINUS operator is used to return all rows in the first SELECT statement that are not returned by the second SELECT statement. Each SELECT statement will define a dataset. The MINUS operator will retrieve all records from the first dataset and then remove from the results all records from the second dataset.
• SELECT expression1, expression2, ... expression_n
• FROM tables
• [WHERE conditions]
• MINUS
• SELECT expression1, expression2, ... expression_n
• FROM tables
• [WHERE conditions];
Barjinder Singh
EXISTS Condition with the SELECT Statement
• The SQL EXISTS condition is used in combination with a subquery and is considered to be met, if the subquery returns at least one row. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
• SELECT *
• FROM emp
• WHERE EXISTS
• (SELECT *
• FROM dept
• WHERE emp.ecode = dept.eno);
• www.techonthenet.com/sql/exists.php
Barjinder Singh
IF THEN ELSE logic in a SELECT statement
SELECT ename, CASE WHEN sal = 1000 THEN 'Minimum wage'
WHEN sal > 1000 THEN 'Over paid'
ELSE 'Under paid'
END AS "Salary Status"
FROM emp;
-----------------------------------------------------------------------------------
SELECT ename, esal, CASE WHEN esal = 17000 THEN 'Minimum wage'
WHEN esal > 18000 THEN 'Over paid'
ELSE 'Under paid'
END AS "Salary Status"
FROM emp;
Barjinder Singh
Display names of all constraints for a table in Oracle SQL
• select constraint_name,constraint_type
• from user_constraints
• where table_name = 'DEPT';
• -------------------------------------------------------------------------------------
• R = Referential Constraint/Foreign Key constraint
• C = Check Constraint
• P = Primary Key
• U = Unique Key
• ------------------------------------------------------------------------------
• select * from user_constraints where table_name='k12'
Barjinder Singh
On Delete Cascade Option
• create table k2 ( rno int references k1(rno) on delete cascade, dname int);
• drop table k1 cascade constraint;
To drop Foreign Key• alter table k12 drop constraint SYS_C0010212;
Barjinder Singh