Sunteți pe pagina 1din 33

Oracle Academy

Amazing Books

Oracle Academy 1 Database Programming with SQL


Copyright © 2010, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Introduction

SCENARIO
Cicely Williams inherited her uncle's bookstore after he retired in 2001. In the
last several years, the bookstore's sales have skyrocketed. Cicley can no longer
continue to manage the business without using a database to store customer
records, track inventory and access accounting information. Eventually, Cicely
would like to take the business online, but first she needs an easier way to
manage the details of her business in house.

She needs to be able to:

• search her inventory for book titles, authors, ISBN numbers, book editions,
book subjects and publishers.
• track sales for other merchandise items and display wholesale and retail
prices for all items
• insert, update and delete items from her items inventory
• track customer information for emailing store specials, new books, book
signings, etc.
• place and view customer orders

Recently, Cicely hired a consulting company to determine the information needs


of her business. The consultants developed an Entity Relationship Diagram
detailing the business' data and functionality requirements. Now, Cicely is ready
to have you design an easy to use, but robust application interface for her office.
Having just completed the Oracle Internet Academy Data Modeling and SQL
course, you are confident in your ability to design the application Cicely needs.

Oracle Academy 2 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

INSTRUCTIONS
The Amazing Books Project uses Oracle Application Express SQL Workshop
and the Application Builder tools to build a database for the Amazing Books
bookstore. The project is divided into seven parts. Each part consists of several
steps. Each step has detailed instructions and supporting graphics.

Part 1: Building the tables and adding column constraints.

Part 2: Creating the application's pages, page style and Popup List of Values
(LOVs).

Part 3: Creating the input forms and report regions for the application's
CUSTOMERS, SUBJECTS, PUBLISHERS, ITEM_TYPES pages.

Part 4: Inputting data into the CUSTOMERS, SUBJECTS, PUBLISHERS,


ITEM_TYPES tables.

Part 5: Creating the input forms and report areas for the application’s ORDERS
and ITEMS pages to enable use of the LOVs for inputting data.

Part 6: Inputting data into the Orders and Items table.

Part 7: Formatting report attributes.

Let's begin....

Oracle Academy 3 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Part 1: Building Tables and Adding Constraints


In this section, you use the Object Browser in Oracle Application Express to:

• Build the base tables for the project


• Add primary key constraints
• Add foreign key constraints

Be sure to have a copy of the entity relationship diagram (ERD) and schema
diagrams available as you go through this section. You will need to reference
them as you build the tables.

Step 1: Logging on to Oracle Application Express.

Log on to Oracle Application Express at http://iacademy.oracle.com/

In the Login screen, enter your Workspace, Username, and Password. Click
Login.

Oracle Academy 4 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Step 2: Accessing SQL Workshop and the Object Browser


2.1 Launch SQL Workshop.
In the Home window of Oracle Application Express, doubleclick the SQL
Workshop icon.

The SQL Workshop tools are displayed.

2.2 Use the Object Browser to create the tables for the project.

A. Click the drop-down menu for Object Browser. Select Create-> Table.

Oracle Academy 5 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Step 3: Creating the CUSTOMERS table


3.1 Review the CUSTOMERS entity in the ERD and schema diagram.

CUSTOMERS CUSTOMERS
pk customer_id # customer_id
customer_name * customer_name
address * address
city * city
state_province o
state_province
country *country
email o
email

3.2 Create the columns for the CUSTOMERS table.


Enter the column information in the Create Table window.

A. Enter CUSTOMERS in the Table Name textbox.

B. Create the fields in the table by entering the data in the picture into the
Column Name, Type, Precision and Scale textboxes.

C. According to the ERD, some columns are optional and others are
required. Check Not Null for the columns that must not have a null value
(a NOT NULL constraint will be created when the table is created).

Oracle Academy 6 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Note: Primary Keys will set to NOT NULL by default, but in this case, we
are checking the value anyway.

D. When you are finished, click Next.


3.3 Create the primary key for the CUSTOMERS table.
A primary key allows each row in a table to be uniquely identified.

A. The primary key values for the CUSTOMERS table will be generated from
a sequence. Choose “Populated from a new sequence” for the Primary
Key.

B. Confirm that the Primary Key Constraint Name is CUSTOMERS_PK


and the Sequence Name is CUSTOMERS_SEQ. These are automatically
entered by Application Express.

C. In the Primary Key drop-down menu, choose CUSTOMER_ID.

D. When you are finished, click Next.

Oracle Academy 7 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

3.4 Skip the step of creating a foreign key for the CUSTOMERS table.
The next part of creating a table in Application Express is defining the foreign
key. Since CUSTOMERS is the first table in this project, there are no foreign
keys to reference yet. We will create all the "connections" or foriegn keys after
we have created all the tables. Click Next to continue.

Oracle Academy 8 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

3.5 Skip the step of creating additional constraints for the CUSTOMERS
table.
The next step in the table creation process is to create constraints for the table.
There are no constraints needed for the CUSTOMERS table. Click Finish.

3.6 Confirm the create table request.


The final step in the create table process is to confirm your request to create the
CUSTOMERS table. In the Create Table window, click Create.

Oracle Academy 9 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Note that you can review the SQL code that will be used to create the table by
expanding the SQL section.

3.7 Examine the new table.


The newly created table should look like the sample below.

DO NOT MAKE ANY OTHER CHANGES TO THIS TABLE AT THIS TIME.

3.8 Return to SQL Workshop.

Oracle Academy 10 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Step 4: Creating the ORDERS table


4.1 Review the ORDERS entity in the ERD and data model.

ORDERS
pk order_id ORDERS
fk customer_id # order_id
order_date * order_date
quantity * quantity
fk item_id

4.2 Create the columns for the ORDERS table.


Click the drop-down menu for Object Browser. Select Create -> Table.

Oracle Academy 11 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

4.2 Create the columns for the ORDERS table, continued

Enter the following column definitions.

4.3 Create the primary key for the ORDERS table.


Create a primary key for the ORDERS table using a new sequence. Use the
default constraint and sequence names. Specify ORDER_ID as the primary key.
When you are finished, click Next.

4.4 Skip the step for creating a foreign key.


We will add all "connections" or foriegn keys after we have created all the tables.
Click NEXT.

Oracle Academy 12 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

4.5 Skip the step for creating a constraint for the ORDERS table.
There are no other constraints for this table. Click Finish.
4.6 Confirm the create table request.
The final step in the create table process is to confirm your request to create the
ORDERS table. In the Create Table window, click Create.

Note that you can review the SQL code that will be used to create the table by
expanding the SQL section.

4.7 Examine the new table.

4.8 Return to SQL Workshop.

Oracle Academy 13 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Step 5: Creating the ITEMS table


5.1 Review the ITEMS entity in the ERD and data model.

ITEMS ITEMS
pk item_id # item_id
isbn * isbn
title * title
author * author
fk publisher_id * wholesale_cost
fk subject_id * retail_price
fk item_type_id
wholesale_cost
retail_price

5.2 Create the columns for the ITEMS table.


Note: if you run out of textboxes, click Add Column. When you are completely
done adding all columns, click Next to continue.

Oracle Academy 14 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

5.3 Create the primary key for the ITEMS table.


Create a primary key for the ITEMS table using a new sequence. Use the default
constraint and sequence names. Specify ITEM_ID as the primary key. Click
NEXT to continue.

5.4 Skip the step for creating a foreign key.


We will add all "connections" or foriegn keys after we have created all the tables.
Click NEXT.
5.5 Create constraints for the ITEMS table.
Other than the primary key constraint and foreign key, there are no other
constraints for this table. Click Finish.

5.6 Confirm the create table request.


The final step in the create table process is to confirm your request to create the
ITEMS table. In the Create Table window, click Create.

5.7 Examine the new table.

5.8 Return to SQL Workshop.

Oracle Academy 15 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Step 6: Creating the SUBJECTS table


6.1 Review the SUBJECTS entity in the ERD and data model.

SUBJECTS SUBJECTS
pk subject_id # subject_id
subject * subject

6.2 Create the columns for the SUBJECTS table.


Click Table in the Object Browser CREATE menu.

6.3 Create the primary key for the SUBJECTS table.


Create a primary key for the SUBJECTS table using a new sequence. Use the
default constraint and sequence names. Specify SUBJECT_ID as the primary
key.

Oracle Academy 16 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

6.4 Skip the step for creating a foreign key.


We will add all "connections" or foriegn keys after we have created all the tables.
Click NEXT.
6.5 Create constraints for the SUBJECTS table.
There are no other constraints for this table. Click Finish.
6.6 Confirm the create table request.
The final step in the create table process is to confirm your request to create the
SUBJECT table. In the Create Table window, click Create.
6.7 Examine the new table.

6.8 Return to SQL Workshop.

Oracle Academy 17 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Step 7: Creating the PUBLISHERS table


7.1 Review the PUBLISHERS entity in the ERD and data model.

PUBLISHERS PUBLISHER
pk publisher_id #publisher_id
publisher *publisher

7.2 Create the columns for the PUBLISHERS table.

7.3 Create the primary key for the PUBLISHERS table.


Create a primary key for the PUBLISHERS table using a new sequence. Use the
default constraint and sequence names. Specify PUBLISHER_ID as the primary
key.

7.4 Skip the step for creating a foreign key.


We will add all "connections" or foriegn keys after we have created all the tables.
Click NEXT.

7.5 Create constraints for the PUBLISHERS table.


Other than the primary key constraint, there are no other constraints for this
table. Click Finish.
7.6 Confirm the create table request.
The final step in the create table process is to confirm your request to create the
PUBLISHERS table. In the Create Table window, click Create.

Oracle Academy 18 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

7.7 Examine the new table.

7.8 Return to SQL Workshop.

Oracle Academy 19 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Step 8: Creating the ITEM_TYPES table


8.1 Review the ITEM_TYPES entity in the ERD and data model.

ITEM_TYPES ITEM_TYPES
pk item_type_id # item_type_id
item_type * item_type

8.2: Create the columns for the ITEM_TYPES table.

8.3 Create the primary key for the ITEM_TYPES table.


Create a primary key for the ITEMS table using a new sequence. Use the default
constraint and sequence names. Specify ITEM_TYPE_ID as the primary key.

8.4 Skip the step for creating a foreign key.


We will add all "connections" or foriegn keys after we have created all the tables.
Click NEXT.

8.5 Create constraints for the ITEM_TYPES table.


There are no other constraints for this table. Click Finish.
8.6 Confirm the create table request.
The final step in the create table process is to confirm your request to create the
ITEM_TYPES table. In the Create Table window, click Create.

Oracle Academy 20 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

8.7 Examine the new table.

8.8 Return to SQL Workshop.

Oracle Academy 21 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Step 9: Creating the foreign keys between all tables


9.1 Review the Amazing Books ERD.

9.2 Create the foreign key for the ORDERS table to CUSTOMERS
A. In Object Browser, select Browse -> Tables.

Oracle Academy 22 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

B. Click to SELECT the ORDERS table from the list of tables displayed.

The ORDERS table should appear like the following example.

Oracle Academy 23 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

C. Click on the CONSTRAINTS tab to begin creating the foriegn key between
the ORDERS table and the CUSTOMERS table.

D. Click on the CREATE button to create a new foriegn key constraint for the
ORDERS table.

F. Populate the fields with the following values and then click NEXT to continue.

Oracle Academy 24 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

G. Click FINISH to complete creating the foriegn key.

H. Confirm the foriegn key is now created in the ORDERS table referencing the
CUSTOMER table using the CUSTOMER table's primary key.

H. Return to SQL Workshop.

9.3 Create the foreign key for the ORDERS table to ITEMS.

A. In Object Browser, select Browse -> Tables.

Oracle Academy 25 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

B. Click to SELECT the ORDERS table from the list of tables displayed.

Oracle Academy 26 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

The ORDERS table should appear like the following example.

C. Click on the CONSTRAINTS tab to begin creating the foriegn key between
the ORDERS table and the CUSTOMERS table.

Oracle Academy 27 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

D. Click on the CREATE button to create a new foriegn key constraint for the
ORDERS table.

F. Populate the fields with the following values and then click NEXT to continue.

G. Click FINISH to complete creating the foriegn key.

H. Confirm the foriegn key is now created in the ORDERS table referencing the
ITEMS table using the ITEMS table primary key.

Oracle Academy 28 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

9.4 Create the foreign keys for the ITEMS table to the SUBJECTS,
PUBLISHERS and ITEM_TYPES tables.
A. In Object Browser, select Browse -> Tables.

B. Click to SELECT the ORDERS table from the list of tables displayed.

Oracle Academy 29 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

The ITEMS table should appear like the following example.

C. Click on the CONSTRAINTS tab to begin creating the foriegn key between
the ORDERS table and the CUSTOMERS table.

D. Click on the CREATE button to create a new foriegn key constraint for the
ITEMS table.

Oracle Academy 30 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

F. Create the following 3 foriegn keys by populating the fields with the following
values. Click NEXT to continue creating the foriegn keys until completed.

These are the values to create the foriegn key for the ITEMS table to the
ITEMS_TYPES table.

Oracle Academy 31 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

These are the values to create the foriegn key for the ITEMS table to the
PUBLISHER table.

These are the values to create the foriegn key for the ITEMS table to the
SUBJECTS table.

Oracle Academy 32 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.
Part 1: Building Tables and Adding Constraints

Summary

In Steps 1 through 8, you created six tables: CUSTOMERS, ORDERS,


SUBJECTS, PUBLISHERS, ITEM_TYPES and ITEMS.

In Step 9, you created five foriegn keys. Two foriegn keys in the ORDERS table
which created a connection to CUSTOMERS and ITEMS. We created three
foriegn keys in the ITEMS table creating a connection between the ITEMS table
and SUBJECTS, PUBLISHERS, and ITEM_TYPES respectively.

These tables are the foundation for building any application. In Part 2, we will
create an application which will use the tables you just created. We'll continue to
add functionality to this application throughout this project.

Let's continue...

Oracle Academy 33 Database Programming with SQL


Copyright © 2009, Oracle. All rights reserved.

S-ar putea să vă placă și