Designing, Modeling and Generating Database Tables

JDeveloper provides a set of modeling tools which allow you to visually create tables on a diagram. You can create new tables directly on a database diagram, or import existing tables from a database schema, or work with offline tables in the navigator and then drag them on a diagram to work with them further. Once you have finished modeling the tables, you can generate the changes directly to the database, or create a DDL file to be run later.

You can create and edit database components directly on a database diagram. This is called in-place editing. For instance, you can create or edit columns, or edit keys and constraints on a modeled table, or draw foreign key relationships between tables directly on the diagram. You can import tables from a live database connection by dragging them directly onto a diagram and reconcile your changes with the database and create the DDL to generate the changes back to the database.

 

Topics

This tutorial discusses the following:

Overview
Prerequisites
Step 1 - Create the Model Project

Step 2 - Create a Database Diagram

Step 3 - Import Tables from a Database Connection
Step 4 - Edit Objects on the Diagram
Step 5- Create a New Table
Step 6- Generate to a Database
Step 7- Customizing a Database Diagram
Summary

1 hour

Move your mouse over this icon to show all screenshots. You can also move your mouse over each individual icon to see only the screenshot associated with it.


Overview

The tutorial guides you through the process of importing two tables from the Human Resources schema on the database. This will give you two offline table definitions which you can edit and manipulate on a diagram. You will also create an offline table directly onto the diagram.

Once you have finished working with the offline tables, you will create the DDL to create the new tables in the new schema.

Back to Topic List

You want to create some tables for a new schema. A couple of the tables are similar to those already used in the Human Resources schema, and you also want a new table to contain some additional information.

Prerequisites

Back to Topic List

In order for this tutorial to work successfully, you will need to have performed the following:

1.

Install Oracle JDeveloper 10g.

 

2.

Install the Sample Schemas and create a connection to the HR or HR8 schema. See Installing the Sample Schemas and Establishing a Database Connection tutorial.

Step 1- Create the Model Project

When you work in JDeveloper, you organize your work in projects within application workspaces. JDeveloper provides a number of predefined templates which allow you to create a workspace and projects that are configured for developing the types of application you are working on. The templates provide the basic range of technologies that are needed, and you create your working environment by selecting the one that fits your needs, then configuring it to add any additional technologies you are going to use. The options that are available to you in the New Gallery and for some context menu operations depend on your choice of template for that workspace.

In this tutorial you are going to choose the default web application template, and customize it to make JDeveloper's database functionality available.

Back to Topic List

1.

In the Applications Navigator, right-click Applications and choose New from the context menu.

Move your mouse over this icon to see the image


2.

In the Create Application Workspace dialog, rename the application Application1 to DBModeling. Notice that the directory name changes to match the new name.

Move your mouse over this icon to see the image

 

3 .

Before selecting the application template, you are going to customize it. Click Manage Templates to open the Manage Application Templates dialog.

Move your mouse over this icon to see the image

 

4 .

Under the Web Application [Default] node, select Data Model. Locate Database in the available technologies list and move it to the selected technologies list, then click OK.
Make sure that the application template is Web Application [Default], and click OK.

Move your mouse over this icon to see the image

 

5 .

The DBModeling workspace is created and displayed as a node in the navigator.
Save your work by clicking on the Save All button. You should save your work at regular intervals as you work through the rest of this tutorial.

Move your mouse over this icon to see the image

 

Step 2- Create a Database Diagram

In this step you will create a database diagram.

Back to Topic List

1.

With the Model project selected in the Application Navigator, right-click and choose New from the context menu. This opens the New Gallery.

 

Move your mouse over this icon to see the image

 

2.

In the Categories list, select Offline Database Objects in the Categories list, then select Database Diagram from the Items list. Click OK to open the Create Database Diagram dialog.

Move your mouse over this icon to see the image


3 .

Accept the defaults and click OK. A new database diagram opens. You should see the Component Palette, which shows the elements available for you to use on this diagram. If you can't see it, open it by choosing Component Palette from the View menu.

Move your mouse over this icon to see the image


Now that you have a blank database diagram, you can directly import tables from the database connection by dragging them onto the diagram. Importing the tables creates the offline table definitions which you will work with before creating the DDL to create new tables in a database.

Back to Topic List

1.

To import tables from the connection you have to the database, click the Connections tab so that the Connections Navigator is visible, or choose View | Connection Navigator. Expand the Database, and HR nodes to see all the database elements available through this connection.

Move your mouse over this icon to see the image

 


2.

Filter the list to make it easier to work with. Select the HR node, then click Filter. In the Filter Object Types dialog, move all the object types except Tables from the list of displayed object types to the list of available object types. Click OK.

Move your mouse over this icon to see the image

 

3 .

Expand the Tables node and select DEPARTMENTS, hold down the Ctrl key and select EMPLOYEES.

Move your mouse over this icon to see the image

 

4 .

Drag DEPARTMENTS and EMPLOYEES onto the diagram. In the Create from Tables dialog, ensure that Offline Tables is selected and click OK. JDeveloper connects to the database and creates offline table definitions based on the tables you have selected. There may be a short delay while this occurs.

Move your mouse over this icon to see the image

 

5 .

The offline tables and foreign keys are created and displayed on the diagram. You can see a thumbnail view of the diagram by clicking the Thumbnail tab in the Structure pane. The default position of this pane is at the lower left corner of JDeveloper. If you can't find it, choose View | Thumbnail.


Move your mouse over this icon to see the image

 

6 .

The offline tables also appear in the Application Navigator, in the Model project under the Database Objects node. Notice that they are grouped in an offline schema called model. The offline schema is a container for offline database objects.

 

Move your mouse over this icon to see the image

 

JDeveloper's modeling tools allow you to edit offline tables directly on the diagram, called in-place editing. You can change the name of a table, create new columns, edit existing columns, and edit constraints on the table.

Back to Topic List

1.

In the second compartment of the diagrammed table, click in the LAST_NAME column to select it, then click again so you can edit the line, and change the field size to 30.
The changes are made when you press Enter or move the focus off the table.
If you double click you will bring up the Edit Offline Table dialog. If this happens, cancel it and click on the table again, pause, and click again to edit the line.

Move your mouse over this icon to see the image

 


2.

In the third compartment, select the check constraint that says <<Check>> EMP_SALARY_MIN:salary > 0 and change it to EMP_SALARY_MIN:salary > 10.

Move your mouse over this icon to see the image

 

3 .

Now try comparing in line editing with editing using the Edit Offline Tables dialog.

Open the Edit Offline Tables dialog by doing one of the following:
Right clicking the table you want to edit in the diagram and choose Properties from the context menu.
Right clicking the table you want to edit in the Application Navigator and choose Properties from the context menu.

Use the in line editing from the diagram
Clicking on the component you wish to edit from modeled table on the diagram.


Move your mouse over this icon to see the image

 

In addition to importing existing tables from a database, you can create new tables directly on the diagram.

Back to Topic List

1.

In the component palette, click on Table, then click on the diagram. A new offline table is created on the diagram, and it appears in the Application Navigator in the model schema. Resize it so that it is easier to work with.

Move your mouse over this icon to see the image

 


2.

Change the default name of the table to REGIONS. In the second compartment of the modeled table, create two columns by typing REGION_ID : CHAR(2) on one line, then REGION_NAME : VARCHAR2(40) on the next.

Move your mouse over this icon to see the image

 

3 .

In the third compartment, you can see that a primary key has been automatically created from the first column you created. We'll use this to link it to the Departments table.

Move your mouse over this icon to see the image

 

4 .

Make sure you can see both the Departments and Regions tables in the modeler. Use the Thumbnail pane and move the shape that shows the visible area to include both tables, or you can use click the Zoom Out button at the bottom right of the diagram pane.
In the Component Palette, click on Mandatory Foreign Key, then click on the Regions table, then the Departments table. Whenever you create a foreign key in JDeveloper, you draw it from the table with the primary key to the table with the foreign key. In other words, create the foreign key beginning from the "master" and ending at the "detail" side of the relationship.
Click OK.

Move your mouse over this icon to see the image

 

5 .

The foreign key relationship is displayed on the diagram, and the table with the foreign key, Departments, now has an extra column named REGIONS_REGION_ID : CHAR(2).

Move your mouse over this icon to see the image

 

In the previous steps, you have seen how to import tables from a database connection, as well as how to create new tables on a database diagram. In this step you will create a SQL file containing the DDL which you could use to create the tables in a database.

JDeveloper also allows you to generate directly to a database, or to reconcile your changes against a database.

Back to Topic List

1.

To generate a SQL script:
On the diagram, hold down the Ctrl key and select all three tables. You'll find that that this works best if you click on an area within the table shape, but not on a line of text.
Right click and choose Generate | Data Definition Language from the context menu. This invokes the Generate SQL from Offline Database Objects wizard.
Alternatively, you could select the tables in the Application Navigator and choose Generate or Reconcile Objects from the context menu.

 

Move your mouse over this icon to see the image


2.

If the first page of the wizard is displayed, review the information and click Next.
On the Select Objects page, check that the Departments, Employees and Regions tables are in the list of selected objects and click Next.

 

Move your mouse over this icon to see the image

3 .

On the Generate Options page, ensure that just the following are selected:

CREATE (or replace) the chosen objects
Generate SQL scripts for the operation

These will ensure that a script is created, and that it contains CREATE statements, rather than UPDATE statements.

Move your mouse over this icon to see the image

 

4 .

On the SQL Script Options page, change the SQL file name from untitled.sql to dbmodeling.sql.
Select Generate SQL*Plus extensions. Click Next, then Finish.
The file is dbmodeling.sql created in the default folder <jdev_home>/jdev/mywork/<workspace>/<project>/database, which in this case is <jdev_home>/jdev/mywork/DBModeler/Model/database.

The SQL script is listed in the Application Navigator in the Model project under the Resources node. Double-click on dbmodeling.sql to open it in JDeveloper's SQL editor and view the CREATE and ALTER statements.

 

Move your mouse over this icon to see the image

 

This part of the tutorial shows you how you can customize database diagrams to suit your way of working. Try one or more of the following tasks.

Back to Topic List

1.

To change the way the diagram is viewed:

Zoom in and out using the buttons at the bottom right corner of the diagram.
You can see a lot of detail, or reduce the amount of detail shown. Right click on the diagram and choose View as | Compact to see the difference. Remember to change back to View as | Standard before proceeding to the next step of the tutorial.


Move your mouse over this icon to see the image

 


2.

To resize and move diagram elements:

Move one of the tables and the foreign keys move as well. If you end up with unnecessary points on the foreign keys, right click on a line and choose Straighten Lines from the context menu. Alternatively you can let JDeveloper automatically lay out the tables by choosing Lay Out Shapes from the context menu, followed by one of the available options.

Move your mouse over this icon to see the image

 

3 .

To change the colors of the diagram elements:

Change the default colors that are used in the creation of new diagram elements, by choosing Preferences form the Tools menu. Select the Diagrams node, then Diagram, then Database. Continue selecting the nodes under database to change the color, font and shape of foreign keys and tables.
Change the colors of individual existing diagram elements by right clicking a table or other element and choosing Display Properties from the context menu of the table or other element to open the Display Properties dialog. You can also change the font, the color of lines, fills and fonts, and the elements that are displayed.

Move your mouse over this icon to see the image

 

 

In this tutorial, you've learned how to:

Create a new application workspace
Create an empty database diagram.
Start modeling database objects by importing tables from a live database connection.

Edit tables on the diagram using in-place editing.

Create a new offline table on the diagram from the component palette.
Create a foreign key relationship between two tables.
Use the JDeveloper functionality to change the look of your diagram.
Generate a SQL file from your work which you could run against a database connection to create the tables in the database.


Move your mouse over this icon to hide all screenshots