The following walkthrough describes how to create a sample project, add folders and files into it, deploy the project, compile it and export it to a single script. A sample project will contain scripts, that create database objects, that were have created manually in previous walkthroughs.
To create a new project select New, Blank Project on the File menu. A project will appear in the Project Explorer.
Folders help to organize related scripts and queries into groups, so, first of all, let’s create a folder structure for the new project. There will be three folders in the root folder: Security, Database Objects, and Data. Folders for every database object type that is created in the project will be in the Database Objects folder. To create such folder structure perform the following steps.
Right-click the project node in Project Explorer and choose Add Folder on the shortcut menu
or
Select the project node in Project Explorer and choose Add Folder on the Project menu.
Now, you have a folder structure for our project created. Note that project folders are not real Windows folders, information about these folders is stored in a project file. When you create or delete folders, corresponding Windows folders are not created or deleted.
Now, let’s create required SQL script files and add them to the project. Let’s learn how to add existing files to the project first. Existing files can be added to a project in two ways. Perform following actions to create files and add them to a project.
Type or paste following code into the SQL document:
CREATE TABLE DEMOBASE.DEPT (
DEPTNO NUMBER(38),
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT PK_DEPT PRIMARY KEY (DEPTNO))
Right-click on the Tables folder in Project Explorer and choose Add Existing File on the shortcut menu
or
Click on the Tables folder in Project Explorer and choose Add Existing File on the Project menu.
Now, the file is added to the project. Perform following actions, that demonstrate it.
Type or paste following code into the SQL document:
CREATE TABLE DEMOBASE.EMP (
EMPNO NUMBER(38) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(38),
HIREDATE DATE,
SAL FLOAT(126),
COMM FLOAT(126),
DEPTNO NUMBER(38))
Right-click on the Tables folder in Project Explorer and choose Add Existing File on the shortcut menu
or
Click on the Tables folder in Project Explorer and choose Add Existing File on the Project menu.
You can add not only existing files to the project, but to create new files, that will belong to the project too. You can use file templates when adding a new file to project. Perform following actions to create an SQL file for creating the view STAFF.
Right-click the Views folder node in Project Explorer and choose Add New File on the shortcut menu
or
Click the Views folder node in Project Explorer and choose Add New File on the shortcut menu.
Type or paste following query text after the AS keyword.
SELECT
emp.ename AS name,
emp.job,
emp.sal AS salary,
dept.dname AS department
FROM emp, dept
WHERE emp.deptno = dept.deptno
(+)
Save the document with the name View Staff.
To complete the project you should add some more scripts to it. Create and add to the project the following scripts in the same way as previous ones.
Emp data to the Data folder
INSERT INTO demobase.emp VALUES (7839, 'KING', 'PRESIDENT', NULL, '17/11/1981', 5000, 0, 10);
INSERT INTO demobase.emp VALUES (7566, 'JONES', 'MANAGER', 7839, '15/04/1981', 2975, 0, 20);
INSERT INTO demobase.emp VALUES (7698, 'BLAKE', 'MANAGER', 7839, '01/05/1981', 2850, 0, 30);
INSERT INTO demobase.emp VALUES (7782, 'CLARK', 'MANAGER', 7839, '09/06/1981', 2450, 0, 10);
INSERT INTO demobase.emp VALUES (7788, 'SCOTT', 'ANALYST', 7566, '19/08/1987', 3000, 0, 20);
INSERT INTO demobase.emp VALUES (7844, 'TURNER', 'SALESMAN', 7698, '08/09/1981', 1500, 0, 30);
INSERT INTO demobase.emp VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '28/09/1981', 1250, 1400, 30);
INSERT INTO demobase.emp VALUES (7876, 'ADAMS', 'CLERK', 7788, '13/07/1987', 1100, 0, 20);
INSERT INTO demobase.emp VALUES (7900, 'JAMES', 'CLERK', 7698, '03/12/1981', 950, 0, 30);
INSERT INTO demobase.emp VALUES (7902, 'FORD', 'ANALYST', 7566, '03/12/1981', 3000, 0, 20);
INSERT INTO demobase.emp VALUES (7369, 'SMITH', 'CLERK', 7902, '17/12/1980', 800, 0, 20);
INSERT INTO demobase.emp VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '20/02/1981', 1600, 300, 30);
INSERT INTO demobase.emp VALUES (7521, 'WARD', 'SALESMAN', 7698, '22/02/1981', 1250, 500, 30);
INSERT INTO demobase.emp VALUES (7934, 'MILLER', 'CLERK', 7782, '23/01/1982', 1300, 0, 10);
Dept data to the Data folder
INSERT INTO demobase.dept(deptno, dname, loc) VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO demobase.dept(deptno, dname, loc) VALUES (20, 'RESEARCH', 'DALLAS');
INSERT INTO demobase.dept(deptno, dname, loc) VALUES (30, 'SALES', 'CHICAGO');
Sequence Seq_Empno to the Sequences folder
CREATE SEQUENCE DEMOBASE.SEQ_EMPNO START WITH 8000
Trigger Trig_Empinsert to the Triggers folder
CREATE OR REPLACE TRIGGER DEMOBASE.TRIG_EMPINSERT
BEFORE INSERT ON DEMOBASE.EMP
REFERENCING NEW AS NEWVALUES OLD AS OLD FOR EACH ROW
BEGIN
SELECT SEQ_EMPNO.NEXTVAL INTO :NewValues.EMPNO FROM DUAL;
SELECT EmpNo INTO :NewValues.MGR FROM Demobase.Emp WHERE EName='Jones';
END;
User Demobase to the Security folder
GRANT CONNECT, RESOURCE, UNLIMITED TABLESPACE TO DEMOBASE IDENTIFIED BY TEST;
Function RaiseSalary to the Procedures folder
CREATE FUNCTION DEMOBASE.RAISESALARY (
Department IN VARCHAR, Rate IN NUMBER
)
RETURN NUMBER
AS
d_No NUMBER;
e_Affected NUMBER;
BEGIN
SELECT DeptNo INTO d_No FROM Dept WHERE Dname=Department;
UPDATE Emp SET Sal=Sal*Rate WHERE DeptNo=d_No;
SELECT Count(*) INTO e_Affected FROM Emp WHERE DeptNo=d_No;
RETURN e_Affected;
END;
After you added these scripts to project, save the project and all unsaved files for future use. To do it perform the following actions.
Right-click the project node in Project Explorer and choose Save NewProject1 on the shortcut menu
or
Select the project node in Project Explorer and choose Save NewProject1 or Save NewProject1 As on the File menu.
Project deployment is a very convenient feature for executing project scripts in specific order called the deployment order. With the sample project, that was created in this walkthrough you can create entire Demobase database using project deployment. At first you should set the project deployment order. User Demobase should be created first. Then you should execute scripts, that create tables and fill them with data. Then you can create a sequence and a trigger. After this other scripts can be performed in any order or may be not performed. So, the deployment order must be following.
Perform following actions to set such order.
All the project scripts will be executed using the project connection. If the project does not have a connection, it can not be deployed. So, you should assign a connection to the project. It must be a connection to account of a user that have administrative rights, because project contains script, that creates a user. So, create such a connection or use the existing one. To assign a connection to a project perform following steps.
Select required connection from the list and click OK.
or
Right-click the connection in Database Explorer and select Assign to Project on the shortcut menu.
Before deploying the project you have to drop old demobase schema, that was created in the previous walkthroughs. You can do it in two ways. The first is using Security Manager and the second is executing the DROP USER statement.
To delete a user using Security Manager, perform the following steps:
Select the user DEMOBASE and click the Delete button on the Security Manager toolbar
or
Right-click the user DEMOBASE and select Delete on the shortcut menu.
To delete user by executing the DROP USER statement, perform the following actions:
In the SQL editor, type the following:
DROP USER DEMOBASE CASCADE
After deploying the project you can add database object shortcuts to it. To add a database object shortcut drag the database object from the project connection in Database Explorer to the project explorer or select Add to Project and then select Demobase on the shortcut menu of the object node in Database Explorer. Database object shortcuts can be used for fast opening of object editors by double-clicking the shortcuts. You can also easily find the object in Database Explorer by right-clicking its shortcut in Project Explorer and selecting Find in Database Explorer on the shortcut menu.
After deploying project, you can compile it. Compiling project means compiling all database source objects and views, that correspond to the project DDL scripts. So, all deployed source objects and views will be compiled during project compilation. Project will be compiled through the project connection. In case of the Demobase project following objects will be compiled if they were deployed: view STAFF, trigger TRIG_EMPINSERT, function RAISESALARY.
To compile project, on the Project menu, select Compile. You can also compile project with debug information for debugging project source objects.