Data Base Testing
Data Base Testing
Data Base Testing
Examples
30/05/2009 — Ganesh Raman
Structured Query Language (SQL) is a language that provides an interface to relational database
systems. SQL was developed by IBM in the 1970s for use in System R, and is a de facto standard, as
well as an ISO and ANSI standard. SQL is often pronounced SEQUEL.
In common usage SQL also encompasses DML (Data Manipulation Language), for INSERTs, UPDATEs,
DELETEs and DDL (Data Definition Language), used for creating and modifying tables and other
database structures.
The development of SQL is governed by standards. A major revision to the SQL standard was
completed in 1992, called SQL2. SQL3 support object extensions and will be (partially?) implemented
in Oracle8.
What are the difference between DDL, DML and DCL commands?
TRUNCATE – remove all records from a table, including all spaces allocated for the
records are removed
DELETE – deletes all records from a table, the space for the records remain
SAVEPOINT – identify a point in a transaction to which you can later roll back
SET TRANSACTION – Change transaction options like what rollback segment to use
Choose one of the following queries to identify or remove duplicate rows from a table leaving one
record:
Method 1:
Method 2:
Method 3:
SQL> Delete from my_table where rowid not in(
SQL> group by my_column_name );
Method 4:
Note: If you create an index on the joined fields in the inner loop, you, for all intents purposes,
eliminate N^2 operations (no need to loop through the entire table on each pass by a record).
Create your table with a NOT NULL column (say SEQNO). This column can now be populated with
unique values:
How can I get the time difference between two date columns
select floor(((date1-date2)*24*60*60)/3600)
|| ‘ HOURS ‘ ||
floor((((date1-date2)*24*60*60) -
floor(((date1-date2)*24*60*60)/3600)*3600)/60)
|| ‘ MINUTES ‘ ||
round((((date1-date2)*24*60*60) -
floor(((date1-date2)*24*60*60)/3600)*3600 -
(floor((((date1-date2)*24*60*60) -
floor(((date1-date2)*24*60*60)/3600)*3600)/60)*60)))
from …
sum( decode(sex,’F',1,0)) FEMALE,
count(decode(sex,’M',1,’F',1)) TOTAL
from my_emp_table
group by dept;
A value x will be between values y and z if GREATEST(x, y) = LEAST(x, z). Look at this example:
select f2,
from my_table
group by f2;
For equal size ranges it might be easier to calculate it with DECODE(TRUNC(value/range), 0, rate_0,
1, rate_1, …). Eg.
1, 0.1,
2, 0.2,
from my_table;
SELECT f1 FROM t1
WHERE rowid = (
WHERE rownum <= 10
MINUS
WHERE rownum
Alternatively…
Please note, there is no explicit row order in a relational database. However, this query is quite fun
and may even help in the odd situation.
Can one retrieve only rows X to Y from a table?
SELECT *
FROM tableX
WHERE rowid in (
MINUS
WHERE rownum
Please note, there is no explicit row order in a relational database. However, this query is quite fun
and may even help in the odd situation.
One can easily select all even, odd, or Nth rows from a table using SQL queries like this:
SELECT *
FROM emp
FROM emp);
SELECT *
FROM emp
) temp
WHERE MOD(temp.ROWNUM,4) = 0;
Please note, there is no explicit row order in a relational database. However, these queries are quite
fun and may even help in the odd situation.
Form Oracle8i one can have an inner-query with an ORDER BY clause. Look at this example:
SELECT *
WHERE ROWNUM
SELECT *
FROM my_table a
FROM my_table b
Tree-structured queries are definitely non-relational (enough to kill Codd and make him roll in his
grave). Also, this feature is not often found in other database offerings.
The SCOTT/TIGER database schema contains a table EMP with a self-referencing relation (EMPNO and
MGR columns). This table is perfect for tesing and demonstrating tree-structured queries as the MGR
column contains the employee number of the “current” employee’s boss.
The LEVEL pseudo-column is an indication of how deep in the tree one is. Oracle can handle queries
with a depth of up to 255 levels. Look at this example:
select LEVEL, EMPNO, ENAME, MGR
from EMP
One can produce an indented report by using the level number to substring or lpad() a series of
spaces, and concatenate that to the string. Look at this example:
One uses the “start with” clause to specify the start of the tree. More than one record can match the
starting condition. One disadvantage of having a “connect by prior” clause is that you cannot perform
a join to other tables. The “connect by prior” clause is rarely implemented in the other database
offerings. Trying to do this programmatically is difficult as one has to do the top level query first, then,
for each of the records open a cursor to look for child nodes.
One way of working around this is to use PL/SQL, open the driving cursor with the “connect by prior”
statement, and the select matching records from other tables on a row-by-row basis, inserting the
results into a temporary table for later retrieval.
SELECT *
FROM (SELECT job,
sum(decode(deptno,10,sal)) DEPT10,
sum(decode(deptno,20,sal)) DEPT20,
sum(decode(deptno,30,sal)) DEPT30,
sum(decode(deptno,40,sal)) DEPT40
FROM scott.emp
GROUP BY job)
ORDER BY 1;
JOB DEPT10 DEPT20 DEPT30 DEPT40
ANALYST 6000
CLERK 1300 1900 950
MANAGER 2450 2975 2850
PRESIDENT 5000
SALESMAN 5600
The Oracle decode function acts like a procedural statement inside an SQL statement to return
different values or columns based on the values of other columns in the select statement.
Some examples:
‘F’, ‘Female’,
‘Unknown’)
from employees;
0, ‘a = b’,
‘a
from tableX;
select decode( GREATEST(A,B), A, ‘A is greater than B’, ‘B is greater than A’)…
Note: The decode function is not ANSI SQL and is rarely implemented in other RDBMS offerings. It is
one of the good things about Oracle, but use it sparingly if portability is required.
From Oracle 8i one can also use CASE statements in SQL. Look at this example:
SELECT ename, CASE WHEN sal>1000 THEN ‘Over paid’ ELSE ‘Under paid’ END
FROM emp;
How can one dump/ examine the exact content of a database column?
SELECT DUMP(col1)
FROM tab1
DUMP(COL1)
————————————-
For this example the type is 96, indicating CHAR, and the last byte in the column is 32, which is the
ASCII code for a space. This tells us that this column is blank-padded.
From Oracle8i one can DROP a column from a table. Look at this sample script, demonstrating
the ALTER TABLE table_name DROP COLUMN column_name; command.
With previous releases one can use Joseph S. Testa‘s DROP COLUMN package that can be downloaded
fromhttp://www.oracle-dba.com/ora_scr.htm.
Other workarounds:
rename t1 to t1_base;
rename t2 to t1;
From Oracle8 you can just type “password” from SQL*Plus, or if you need to change another user’s
password, type “password user_name”.
Perform an “ALTER SEQUENCE … NOCACHE” to unload the unused cached sequence numbers from the
Oracle library cache. This way, no cached numbers will be lost. If you then select from the
USER_SEQUENCES dictionary view, you will see the correct high water mark value that would be
returned for the next NEXTVALL call. Afterwards, perform an “ALTER SEQUENCE … CACHE” to restore
caching.
You can use the above technique to prevent sequence number loss before a SHUTDOWN ABORT, or
any other operation that would cause gaps in sequence values.
You can use the SQL*Plus COPY command instead of snapshots if you need to copy LONG and LONG
RAW variables from one location to another. Eg:
COPY TO SCOTT/TIGER@REMOTE -
FROM IMAGES;
Note: If you run Oracle8, convert your LONGs to LOBs, as it can be replicated.