154 WebSphere Studio Application Developer Version 5 Programming Guide <simpleType> <restriction base="string"> <length value="3"/> </restriction> </simpleType> </element> <element name="FIRSTNAME"> <simpleType> <restriction base="string"> <length value="30"/> </restriction> </simpleType> </element> <element name="LASTNAME"> <simpleType> <restriction base="string"> <length value="30"/> </restriction> </simpleType> </element> <element name="USERID"> <simpleType> <restriction base="string"> <length value="8"/> </restriction> </simpleType> </element> <element name="PASSWORD"> <simpleType> <restriction base="string"> <length value="8"/> </restriction> </simpleType> </element> <element name="ADDRESS"> <simpleType> <restriction base="base64Binary"> <length value="2000"/> </restriction> </simpleType> </element> </sequence> </complexType> </element> </schema> Notice the Graph page of the XML schema editor (Figure 6-14). Expand the boxes by clicking the + icon. Chapter 6. Developing database applications 155 Figure 6-14 XML schema editor: graph Study the Outline view as well. It shows the structure of the XML schema file. If you want, you can make changes to the XML file and generate a new DDL file by selecting Generate -> DDL (context). Creating database objects Application Developer provides support for creating new databases, new schemas, and new tables. Create database To create a new database you have to have a project. If you have not already done so, you should now create a new simple project called ItsoProGuideDatabase. To create database objects you have to switch to the Data perspective and open the Data Definition view. 156 WebSphere Studio Application Developer Version 5 Programming Guide To create a database select the ItsoProGuideDatabase project and New -> New database definition . The Database creation dialog is displayed (Figure 6-15). Figure 6-15 Database definition dialog Here you specify the name of the new database and the vendor type. When you later generate the database DDL it will conform to the database type that you select. Click Finish to create the new database definition. Database schemas are a way of providing a logical classification of objects in the database. Some of the objects that a schema may contain include tables, views, aliases, indexes, triggers, and structured types. The support for schemas varies between database types; some require them, and some have no support for them. The schema options available to you depend on the database type that you chose when the database was created. If the database type does not support schemas at all, this option will not be available, and the tables and other objects will be created directly under the database node. Important: Database definitions created within Application Developer are not automatically created in the database system. You have to export the DDL and use the appropriate database tool to create the objects, or you can submit the DDL from Application Developer (see “Define the database schema in a database system” on page 163). Chapter 6. Developing database applications 157 Create schema To add a schema to the database, select the database created in the previous step and New -> New schema definition . The Schema Definition dialog is displayed (Figure 6-16). Figure 6-16 Schema definition dialog Select a name (ITSO) for the schema and click Finish to create it. Expand the new schema in the Data Definition view and you will see the types of objects that can be added to it. Tables Views Aliases Indexes Triggers Structured types Stored procedures User-defined functions Create table We will now look at how to create a new table in the schema. Application Developer provides a wizard for defining table columns as well as primary and foreign keys. To create a table, select the schema created in the previous step Note: In the current release of Application Developer, tables, views, stored procedures, and user-defined functions can be created. The other types of objects are not supported. 158 WebSphere Studio Application Developer Version 5 Programming Guide and New -> New table definition . The Create table wizard is displayed (Figure 6-17). Figure 6-17 Table definition wizard: table name Here you give the table a name and an optional comment. On the next page you define the columns of the table (Figure 6-18). Figure 6-18 Table definition wizard: columns Chapter 6. Developing database applications 159 Click Add Another to add a column to the table and define the column properties. The exact properties available depend on the database type. For more information about the properties available, you can consult the documentation provided by the database vendor. In our case, in addition to the CUSTOMERID field, we add FIRSTNAME and LASTNAME. These two additional fields are of type CHARACTER with string length 30 and For bit data not checked (Figure 6-17). Figure 6-19 Table definition wizard: columns The next page of the wizard lets you define the primary key of the table (Figure 6-20). 160 WebSphere Studio Application Developer Version 5 Programming Guide Figure 6-20 Table definition wizard: primary key You select the items you want from the Source Columns and add them to the primary key by clicking > . On the final page of the wizard you can define any foreign key constraints that you want to apply. In our case, we do not have another table defined, so we do not add a foreign key (Figure 6-21). Figure 6-21 Table definition wizard: foreign keys Chapter 6. Developing database applications 161 Clicking Finish creates the table and keys as defined. If you want, you can generate the DDL for the table you have just created. To do so, select the table in the Data Definition view and Generate DDL and the Generate SQL DDL dialog opens (Figure 6-22). Figure 6-22 Generate DDL dialog The options available are to create the DDL with or without the schema name, whether to place delimiters around identifiers or not, whether or not to generate DROP statements, and whether to open an editor on the generated file. The generated DDL file is shown in Figure 6-23. Figure 6-23 Generated DDL file for the customer table Generated by Relational Schema Center on Mon Mar 17 01:56:34 PST 2003 CREATE TABLE ITSO.CUSTOMER (CUSTOMERID INTEGER NOT NULL, FIRSTNAME CHARACTER(30) NOT NULL, LASTNAME CHARACTER(30) NOT NULL); ALTER TABLE ITSO.CUSTOMER ADD CONSTRAINT C4956569 PRIMARY KEY (CUSTOMERID); COMMENT ON TABLE ITSO.CUSTOMER IS 'Table containing customer details'; 162 WebSphere Studio Application Developer Version 5 Programming Guide You can use the generated DDL to create the table in the database system with the help of the appropriate tool provided by the database vendor. You can also execute the DDL on a database server. See “Define the database schema in a database system” on page 163. Create table with foreign key Now we will create a second table with a foreign key. Use the same wizard as above to create a CUSTADDRESS table with the following columns: ADDRESSID—as an INTEGER and also a key column CITY—as a CHARACTER 50 COUNTRY—as a CHARACTER 50 CUSTOMERID—as an INTEGER On the foreign keys page of the wizard (Figure 6-24), click Add Another to add a foreign key. Figure 6-24 Defining a foreign key Chapter 6. Developing database applications 163 Select ITSO.CUSTOMER as the target table, then select CUSTOMERID in the Source Columns list and click > . This defines the primary key of the ITSO.CUSTOMER table to be the foreign key in the CUSTADDRESS table. Define constraints for referential integrity as RESTRICT for delete and NO ACTION for update. Click Finish and the new table CUSTADDRESS, complete with foreign key has been generated in our model. You can again generate DDL to produce the DDL file for this table (Figure 6-25). Figure 6-25 DDL for address table Define the database schema in a database system To define the ITSOTEST database with its schema and tables in a DB2 system, generate the DDL for the database. Select the ITSOTEST object and Generate DDL (context). Note that the generated file (ITSOTEST.sql) does not contain the DDL for the database object itself, only the schema and the tables are defined. Create the database Create the database, for example, in a DB2 Command Window with the command: db2 create database ITSOTEST Generated by Relational Schema Center on Mon Mar 17 02:09:21 PST 2003 CREATE TABLE ITSO.CUSTADDRESS (ADDRESSID INTEGER NOT NULL, CITY CHARACTER(50) NOT NULL, COUNTRY CHARACTER(50) NOT NULL, CUSTOMERID INTEGER NOT NULL); ALTER TABLE ITSO.CUSTADDRESS ADD CONSTRAINT C0107854 PRIMARY KEY (ADDRESSID); ALTER TABLE ITSO.CUSTADDRESS ADD CONSTRAINT C4412200 FOREIGN KEY (CUSTOMERID) REFERENCES ITSO.CUSTOMER(CUSTOMERID) ON DELETE RESTRICT ON UPDATE NO ACTION; . ItsoProGuideDatabase. To create database objects you have to switch to the Data perspective and open the Data Definition view. 156 WebSphere Studio Application Developer Version 5 Programming Guide To. 154 WebSphere Studio Application Developer Version 5 Programming Guide <simpleType> <restriction base="string"> . 6 -19 Table definition wizard: columns The next page of the wizard lets you define the primary key of the table (Figure 6-20). 160 WebSphere Studio Application Developer Version 5 Programming Guide Figure