0% found this document useful (0 votes)
87 views

ORADemo

The document details the creation of multiple database tables and their structure. It includes tables for items, customers, orders, employees and more. Each table has multiple columns and constraints are defined. Indexes are also created for some tables.

Uploaded by

juan992276377
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

ORADemo

The document details the creation of multiple database tables and their structure. It includes tables for items, customers, orders, employees and more. Each table has multiple columns and constraints are defined. Indexes are also created for some tables.

Uploaded by

juan992276377
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

-- ============================================================

-- Database name : DEMO


-- DBMS name
: ORACLE Version 7.0
-- Created on
: OCT/18/95
-- ============================================================
drop
drop
drop
drop
drop
drop
drop
drop
drop
drop
drop

table
table
table
table
table
table
table
table
table
table
table

ITEM_HISTORY;
ITEM;
ORDER_ITEM;
ORDER_TBL;
VIP_CUSTOMER;
CUSTOMER;
SALESMAN;
EMPLOYEE;
REGION;
STATE;
COUNTRY;

-- ============================================================
-- Table : COUNTRY
-- ============================================================
create table COUNTRY
(
COUNTRY_ID
VARCHAR2(3)
not
CONSTRAINT valid_country CHECK (COUNTRY_ID IN
X', 'UK ', 'ITA', 'FRA')),
COUNTRY_NAME
VARCHAR2(45)
not
LANGUAGE
VARCHAR2(30)
not
constraint UK_COUNTRY unique (COUNTRY_ID)
);

null
('USA', 'CAN', 'ME
null,
null,

-- ============================================================
-- Comments : COUNTRY
-- ============================================================
comment on table COUNTRY is 'COUNTRY TABLE';
comment on column COUNTRY.COUNTRY_ID is 'A two or three letter code that ident
ifies the country (US, FR, CAN, ...)';
comment on column COUNTRY.COUNTRY_NAME is 'The Country Official Name (ie: UNITED
STATES OF AMERICA, FRANCE, CANADA, ...';
comment on column COUNTRY.LANGUAGE
is 'Main Language spoken in the Country (
ex: USA = ENGLISH, FR = FRENCH, ...';
-- ============================================================
-- Table : CUSTOMER
-- ============================================================
create table CUSTOMER
(
CUSTOMER_NBR
VARCHAR2(3)
not null,
COMPANY_NAME
CHAR(30)
not null,
COUNTRY_ID
VARCHAR2(3)
not null
CONSTRAINT valid_customer_country CHECK (COUNTRY_ID IN ('USA', '
CAN', 'MEX', 'UK ', 'ITA')),
STATE_ID
VARCHAR2(2)
not null,
REGION_ID
VARCHAR2(4)
not null,
SALES_MONTH01
NUMBER(9)
not null,
SALES_MONTH02
NUMBER(9,2)
not null,

SALES_MONTH03
NUMBER(9,2)
USER_ID
VARCHAR2(8)
constraint PK_CUSTOMER primary key (CUSTOMER_NBR)

not null,
not null,

);
-- ============================================================
-- Comments : CUSTOMER
-- ============================================================
comment on table CUSTOMER is 'CUSTOMER TABLE';
comment
comment
comment
comment
comment
comment
comment
comment
comment

on
on
on
on
on
on
on
on
on

column
column
column
column
column
column
column
column
column

CUSTOMER.CUSTOMER_NBR
CUSTOMER.COMPANY_NAME
CUSTOMER.COUNTRY_ID
CUSTOMER.STATE_ID
CUSTOMER.REGION_ID
CUSTOMER.SALES_MONTH01
CUSTOMER.SALES_MONTH02
CUSTOMER.SALES_MONTH03
CUSTOMER.USER_ID

is
is
is
is
is
is
is
is
is

'Customer#';
'Company_Name';
'Country_Id';
'State_Id';
'Region_Id';
'Sales_Month01';
'Sales_Month02';
'Sales_Month03';
'User_Id';

-- ============================================================
-- Table : EMPLOYEE
-- ============================================================
create table EMPLOYEE
(
EMPLOYEE_ID
EMPLOYEE_NAME
EMPLOYEE_ADDRESS
MANAGER_ID
BANK_ID
BRANCH_ID
ACCOUNT#
DATE_HIRED
LAST_UPDATE
TE,
constraint PK_EMPLOYEE primary
);

VARCHAR2(5)
VARCHAR2(30)
VARCHAR2(30)
VARCHAR2(5)
VARCHAR2(3)
VARCHAR2(3)
VARCHAR2(10)
DATE
VARCHAR2(10)

not null,
not null,
not null,
not null,
not null,
not null,
not null,
default SYSDATE,
not null default SYSDA

key (EMPLOYEE_ID)

-- ============================================================
-- Comments : EMPLOYEE
-- ============================================================
comment on table EMPLOYEE is 'EMPLOYEE TABLE';
comment
comment
comment
comment
comment
comment
comment
comment

on
on
on
on
on
on
on
on

column
column
column
column
column
column
column
column

EMPLOYEE.EMPLOYEE_ID
EMPLOYEE.EMPLOYEE_NAME
EMPLOYEE.EMPLOYEE_ADDRESS
EMPLOYEE.MANAGER_ID
EMPLOYEE.BANK_ID
EMPLOYEE.BRANCH_ID
EMPLOYEE.ACCOUNT#
EMPLOYEE.LAST_UPDATE

is
is
is
is
is
is
is
is

'Employee_Id';
'Employee_Name';
'Employee_Address';
'Manager_Id';
'Bank#';
'Branch#';
'Account#';
'Last_Update';

-- ============================================================
-- Table : ITEM
-- ============================================================
create table ITEM

(
ITEM_ID
VARCHAR2(3)
not null,
ITEM_DESCRIPTION
VARCHAR2(30)
not null,
UNIT_PRICE
Decimal(9,2)
not null CHECK (UNIT_P
RICE > 0),
QTY_ON_HAND
Decimal(11,0)
not null
CONSTRAINT enough_stock CHECK (QTY_ON_HAND > 0),
DESCRIPTION1
VARCHAR2(50)
not null,
DESCRIPTION2
VARCHAR2(50)
not null,
DESCRIPTION3
VARCHAR2(50)
not null,
ITEM_STATUS
VARCHAR2(02)
CONSTRAINT valid_item_status CHECK (ITEM_STATUS IN ('ENA')),
LAST_UPDATE
VARCHAR2(10)
not null
);
-- ============================================================
-- Index : NDX_ITM
-- ============================================================
create unique index NDX_ITM on ITEM (ITEM_ID);
-- ============================================================
-- Index : NDX_ITM_STATUS
-- ============================================================
create index NDX_ITM_STATUS on ITEM (ITEM_STATUS);
-- ============================================================
-- Comments : ITEM
-- ============================================================
comment on table ITEM is 'ITEM TABLE';
comment
comment
comment
comment
comment
comment
comment
comment

on
on
on
on
on
on
on
on

column
column
column
column
column
column
column
column

ITEM.ITEM_ID
ITEM.ITEM_DESCRIPTION
ITEM.UNIT_PRICE
ITEM.QTY_ON_HAND
ITEM.DESCRIPTION1
ITEM.DESCRIPTION2
ITEM.DESCRIPTION3
ITEM.LAST_UPDATE

is
is
is
is
is
is
is
is

'Item#';
'Item_Description';
'Unit_Price';
'Qty_On_Hand';
'Description1';
'Description2';
'Description3';
'Last_Update';

-- ============================================================
-- Table : ITEM_HISTORY
-- ============================================================
create table ITEM_HISTORY
(
ITEM_ID
VARCHAR2(3)
not null,
OCCURRENCE_DATE
DATE
not null,
UNIT_PRICE
Decimal(9,2)
not null,
constraint PK_ITM_HST primary key (ITEM_ID, OCCURRENCE_DATE)
);
-- ============================================================
-- Index : NDX_ITM_HST
-- ============================================================
create index NDX_ITM_HST on ITEM_HISTORY (OCCURRENCE_DATE, ITEM_ID);

-- ============================================================
-- Comments : ITEM_HISTORY
-- ============================================================
comment on table ITEM_HISTORY is 'ITEM_HISTORY TABLE';
comment on column ITEM_HISTORY.ITEM_ID
is 'Item#';
comment on column ITEM_HISTORY.OCCURRENCE_DATE is 'Occurrence_Date';
comment on column ITEM_HISTORY.UNIT_PRICE
is 'Unit_Price';
-- ============================================================
-- Table : ORDER_ITEM
-- ============================================================
create table ORDER_ITEM
(
ORDER_NBR
ITEM_ID
QUANTITY
UNIT_PRICE
DISCOUNT
);

VARCHAR2(3)
VARCHAR2(3)
NUMBER(5)
Decimal(9,2)
Decimal(5,3)

not
not
not
not
not

null,
null,
null,
null,
null

-- ============================================================
-- Index : NDX_ORD_ITM
-- ============================================================
create unique index NDX_ORD_ITM on ORDER_ITEM (ORDER_NBR, ITEM_ID);
-- ============================================================
-- Comments : ORDER_ITEM
-- ============================================================
comment on table ORDER_ITEM is 'ORDER_ITEM TABLE';
comment
comment
comment
comment
comment
comment

on
on
on
on
on
on

column
column
column
column
column
column

ORDER_ITEM.ORDER_NBR
ORDER_ITEM.ITEM_ID
ORDER_ITEM.QUANTITY
ORDER_ITEM.UNIT_PRICE
ORDER_ITEM.DISCOUNT
ORDER_ITEM.CUSTOMER_NBR

is
is
is
is
is
is

'Order#';
'Item#';
'Quantity';
'Unit_Price';
'Discount';
'Customer#';

-- ============================================================
-- Table : ORDER_TBL
-- ============================================================
create table ORDER_TBL
(
ORDER_NBR
VARCHAR2(3)
INVOICE_NBR
VARCHAR2(3)
CUSTOMER_NBR
VARCHAR2(3)
ORDER_DATE
DATE
SALESMAN_ID
VARCHAR2(3)
COMPANY_NAME
VARCHAR(25)
USER_ID
VARCHAR2(8)
constraint PK_ORD_TBL primary key (ORDER_NBR)
);

not null,
not null,
,
not null,
not null,
not null,
not null,

-- ============================================================
-- Index : NDX_CST1

-- ============================================================
create index NDX_CST1 on ORDER_TBL (CUSTOMER_NBR);
-- ============================================================
-- Index : NDX_CST2
-- ============================================================
create index NDX_CST2 on ORDER_TBL (CUSTOMER_NBR, ORDER_DATE, COMPANY_NAME);
-- ============================================================
-- Comments : ORDER_TBL
-- ============================================================
comment on table ORDER_TBL is 'ORDER_TBL TABLE';
comment
comment
comment
comment
comment
comment

on
on
on
on
on
on

column
column
column
column
column
column

ORDER_TBL.ORDER_NBR
ORDER_TBL.CUSTOMER_NBR
ORDER_TBL.ORDER_DATE
ORDER_TBL.SALESMAN_ID
ORDER_TBL.COMPANY_NAME
ORDER_TBL.USER_ID

is
is
is
is
is
is

'Order#';
'Customer#';
'Order_Date';
'Salesman#';
'Company_Name';
'User_Id';

-- ============================================================
-- Table : REGION
-- ============================================================
create table REGION
(
COUNTRY_ID

VARCHAR2(3)
not null
DEFAULT 'USX',
STATE_ID
VARCHAR2(2)
not null,
REGION_ID
VARCHAR2(4)
not null,
REGION_NAME
VARCHAR2(45)
not null,
constraint pk_region primary key (COUNTRY_ID, STATE_ID, REGION_ID),
constraint uk_region unique (REGION_NAME)

);
-- ============================================================
-- Comments : REGION
-- ============================================================
comment on table REGION is 'REGION TABLE';
comment
comment
comment
comment

on
on
on
on

column
column
column
column

REGION.COUNTRY_ID
REGION.STATE_ID
REGION.REGION_ID
REGION.REGION_NAME

is
is
is
is

'Country_Id';
'State_Id';
'Region_Id';
'Region_Name';

-- ============================================================
-- Table : SALESMAN
-- ========================================================
create table SALESMAN
(
SALESMAN_ID
SALESMAN_NAME
COUNTRY_ID
ASSIGNED_STATE
SALES_TARGET

VARCHAR2(3)
VARCHAR2(30)
VARCHAR2(3)
VARCHAR2(2)
Decimal(10,2)

not
not
not
not
not

null,
null,
null,
null,
null,

EMPLOYEE_ID
VARCHAR2(5)
constraint UK_SALESMAN unique (SALESMAN_ID)

not null,

);
-- ============================================================
-- Index : NDX_SAL_TARGET
-- ============================================================
create index NDX_SAL_TARGET on SALESMAN (SALES_TARGET);
-- ============================================================
-- Comments : SALESMAN
-- ============================================================
comment on table SALESMAN is 'SALESMAN TABLE';
comment
comment
comment
comment
comment
comment

on
on
on
on
on
on

column
column
column
column
column
column

SALESMAN.SALESMAN_ID
SALESMAN.SALESMAN_NAME
SALESMAN.COUNTRY_ID
SALESMAN.ASSIGNED_STATE
SALESMAN.SALES_TARGET
SALESMAN.EMPLOYEE_ID

is
is
is
is
is
is

'Salesman#';
'Salesman_Name';
'Country_ID';
'Assigned_State';
'Sales_Target';
'Employee#';

-- ============================================================
-- Table : STATE
-- ============================================================
create table STATE
(
COUNTRY_ID
VARCHAR2(3)
CONSTRAINT valid_country_for_state CHECK
'CAN', 'MEX', 'UK ', 'ITA')),
STATE_ID
VARCHAR2(2)
STATE_NAME
VARCHAR2(30)
constraint UK_STATE unique (COUNTRY_ID, STATE_ID)
);

not null
(COUNTRY_ID IN ('USA',
not null,
not null,

-- ============================================================
-- Index : NDX_STATE1
-- ============================================================
create index NDX_STATE1 on STATE (COUNTRY_ID);
-- ============================================================
-- Comments : STATE
-- ============================================================
comment on table STATE is 'STATE TABLE';
comment on column STATE.COUNTRY_ID
comment on column STATE.STATE_ID
comment on column STATE.STATE_NAME

is 'Country#';
is 'State#';
is 'State_Name';

-- ============================================================
-- Table : VIP_CUSTOMER
-- ============================================================
create table VIP_CUSTOMER
(
VIP_CUSTOMER_NBR

VARCHAR2(3)

not null,

ASSIGNED_SALESMAN
ADDITIONAL_CREDIT

VARCHAR2(3)
NUMBER(9,2)

not null,
not null

);
-- ============================================================
-- Index : NDX_VIP_CST
-- ============================================================
create unique index NDX_VIP_CST on VIP_CUSTOMER (VIP_CUSTOMER_NBR);
-- ============================================================
-- Comments : VIP_CUSTOMER
-- ============================================================
comment on table VIP_CUSTOMER is 'Vip_Customer Table';
comment on column VIP_CUSTOMER.VIP_CUSTOMER_NBR is 'Vip_Customer#';
comment on column VIP_CUSTOMER.ASSIGNED_SALESMAN is 'Assigned_Salesman';
comment on column VIP_CUSTOMER.ADDITIONAL_CREDIT is 'Additional_Credit';
create table INVOICE
(
INVOICE_NBR
VARCHAR2(3)
ORDER_NBR
VARCHAR2(3)
INVOICE_DATE
DATE
constraint PK_INVOICE primary key (INVOICE_NBR)
);

not null,
not null,
not null,

-- ============================================================
-- Foreign Keys
-- ============================================================
alter table SALESMAN
add constraint FK_SALESMAN_COUNTRY foreign key (COUNTRY_ID, ASSIGNED_STATE)
references STATE (COUNTRY_ID, STATE_ID) on delete CASCADE;
alter table REGION
add constraint FK_REGION_COUNTRY foreign key (COUNTRY_ID)
references COUNTRY (COUNTRY_ID)on delete CASCADE;
alter table STATE
add constraint FK_STATE_COUNTRY foreign key (COUNTRY_ID)
references COUNTRY (COUNTRY_ID) on delete CASCADE;
alter table EMPLOYEE
add constraint FK_EMPLOYEE_EMPLOYEE foreign key (MANAGER_ID)
references EMPLOYEE (EMPLOYEE_ID);
alter table ORDER_ITEM
add constraint FK_ITEM_ORDER_TBL foreign key (ORDER_NBR)
references ORDER_TBL (ORDER_NBR);
alter table ITEM
DISABLE CONSTRAINT enough_stock;
Create or Replace Trigger emp_permit_changes
Before Delete or Insert or update

on employee
Declare
dummy Integer;
Begin
/* If today is a Saturday or Dunday, then return an error */
If (To_Char(Sysdate, 'DY') = 'SAT' or
To_Char(Sysdate, 'DY') = 'SUN')
Then raise_application_error(-20501,
'May not change employee table during weekend');
End If;
/* If today is a holiday, then return an error */
Select Count(*) into dummy from company_holidays
where day = Trunc(Sysdate);
If dummy > 0
Then raise_application_error(-20501,
'May not change employee table during holiday');
End If;
/* If current time before 8:00am or after 6:00pm, then return an error */
If (To_Char(Sysdate, 'HH24') < 8 or
To_Char(Sysdate, 'HH24') > 6)
Then raise_application_error(-20501,
'May only change employee table during working hours');
End If;
End;
/

You might also like