In order to perform many of the tasks described in the following sections, you should set up a test environment. For example, you need a database to test your application's SQL code.
A testing environment should include the following:
If you must create a test database, write a small server application that calls the CREATE DATABASE API, or use the command line processor. Refer to the Command Reference for information about the command line processor, or the Administrative API Reference for information about the CREATE DATABASE API.
To design the test tables and views needed, first analyze the data needs of the application. To create a table, you need the CREATETAB authority and the CREATEIN privilege on the schema. Refer to the information on the CREATE TABLE statement in the SQL Reference for alternative authorities.
List the data the application accesses and describe how each data item is
accessed. For example, suppose the application being developed accesses
the TEST.TEMPL, TEST.TDEPT, and TEST.TPROJ tables.
You could record the type of accesses as shown in Table 1.
Table 1. Description of the Application Data
Table or View Name | Insert Rows | Delete Rows | Column Name | Data Type | Update Access |
---|---|---|---|---|---|
TEST.TEMPL | No | No |
EMPNO LASTNAME WORKDEPT PHONENO JOBCODE |
CHAR(6) VARCHAR(15) CHAR(3) CHAR(4) DECIMAL(3) |
Yes Yes Yes |
TEST.TDEPT | No | No |
DEPTNO MGRNO |
CHAR(3) CHAR(6) |
|
TEST.TPROJ | Yes | Yes |
PROJNO DEPTNO RESPEMP PRSTAFF PRSTDATE PRENDATE |
CHAR(6) CHAR(3) CHAR(6) DECIMAL(5,2) DECIMAL(6) DECIMAL(6) |
Yes Yes Yes Yes Yes |
When the description of the application data access is complete, construct the test tables and views that are needed to test the application:
In this example, create a test view of the TDEPT table using the CREATE VIEW SQL statement.
If the database schema is being developed along with the application, the definitions of the test tables might be refined repeatedly during the development process. Usually, the primary application cannot both create the tables and access them because the database manager cannot bind statements that refer to tables and views that do not exist. To make the process of creating and changing tables less time-consuming, consider developing a separate application to create the tables. Of course you can always create test tables interactively using the Command Line Processor (CLP).
Use any of the following methods to insert data into a table:
For information about the INSERT statement, refer to the SQL Reference. For information about the IMPORT, LOAD, and RESTORE utilities, refer to the Administration Guide.
The following SQL statements demonstrate a technique you can use to populate your tables with randomly generated test data. Suppose the table EMP contains four columns, ENO (employee number), LASTNAME (last name), HIREDATE (date of hire) and SALARY (employee's salary) as in the following CREATE TABLE statement:
CREATE TABLE EMP (ENO INTEGER, LASTNAME VARCHAR(30), HIREDATE DATE, SALARY INTEGER);
Suppose you want to populate this table with employee numbers from 1 to a number, say 100, with random data for the rest of the columns. You can do this using the following SQL statement:
INSERT INTO EMP -- generate 100 records WITH DT(ENO) AS (VALUES(1) UNION ALL SELECT ENO+1 FROM DT WHERE ENO < 100 ) (1) -- Now, use the generated records in DT to create other columns -- of the employee record. SELECT ENO, (2) TRANSLATE(CHAR(INTEGER(RAND()*1000000)), (3) CASE MOD(ENO,4) WHEN 0 THEN 'aeiou' || 'bcdfg' WHEN 1 THEN 'aeiou' || 'hjklm' WHEN 2 THEN 'aeiou' || 'npqrs' ELSE 'aeiou' || 'twxyz' END, '1234567890') AS LASTNAME, CURRENT DATE - (RAND()*10957) DAYS AS HIREDATE, (4) INTEGER(10000+RAND()*200000) AS SALARY (5) FROM DT; SELECT * FROM EMP;
The following is an explanation of the above statement:
For sample programs that are helpful in generating random test data, please see the fillcli.sqc and fillsrv.sqc sample programs in the sqllib/samples/c subdirectory.
You may also want to consider prototyping any user-defined functions (UDF) you are developing against the test data. For more information on why and how you write UDFs, see Writing User-Defined Functions (UDFs) and Methods and User-Defined Functions (UDFs) and Methods.