Insert my data from Relational Tables to Object-Relational Tables - oracle

I tried with the command insert into select... but it didn't work. How can I load the data from the Relational tables to the Object-Relational tables?
Example for table Categories:
insert into CATEGORIES_NEW select * from Categories;
The above worked,but the similar way doesn't work for the other tables.
Below are all the tables:
Relational Tables
CREATE TABLE Customers (
CUSTOMERID NUMBER NOT NULL,
FIRSTNAME VARCHAR2 (20) NOT NULL,
LASTNAME VARCHAR2 (20) NOT NULL,
ADDRESS1 VARCHAR2 (40) NOT NULL,
ADDRESS2 VARCHAR2 (40),
CITY VARCHAR2 (20) NOT NULL,
STATE VARCHAR2 (20) NOT NULL,
ZIP VARCHAR2 (20) NOT NULL,
COUNTRY VARCHAR2 (20) NOT NULL,
REGION VARCHAR2 (20) NOT NULL,
EMAIL VARCHAR2 (20) NOT NULL,
PHONE VARCHAR2 (20) NOT NULL,
CREDITCARDTYPE VARCHAR2 (20) NOT NULL,
CREDITCARD VARCHAR2 (20) NOT NULL,
CREDITCARDEXPIRATION VARCHAR (10) NOT NULL,
USERNAME VARCHAR2 (20) NOT NULL,
"PASSWORD" VARCHAR2 (20) NOT NULL,
AGE NUMBER NOT NULL,
INCOME NUMBER NOT NULL,
GENDER VARCHAR2 (8) NOT NULL,
PRIMARY KEY (CUSTOMERID));
CREATE TABLE Orders (
ORDERID NUMBER NOT NULL,
ORDERDATE DATE NOT NULL,
CUSTOMERID NUMBER NOT NULL REFERENCES Customers,
NETAMOUNT NUMBER NOT NULL,
TAX NUMBER NOT NULL,
TOTALAMOUNT NUMBER NOT NULL,
PRIMARY KEY (ORDERID));
CREATE TABLE Categories (
"CATEGORY" NUMBER NOT NULL,
CATEGORYNAME VARCHAR (20) NOT NULL,
PRIMARY KEY ("CATEGORY"));
CREATE TABLE Products (
PROD_ID NUMBER NOT NULL ,
"CATEGORY" NUMBER NOT NULL REFERENCES Categories,
TITLE VARCHAR (40) NOT NULL,
ACTOR VARCHAR (40) NOT NULL,
PRICE VARCHAR (40) NOT NULL,
PRIMARY KEY (PROD_ID));
CREATE TABLE Orderlines (
ORDERLINEID NUMBER NOT NULL,
ORDERID NUMBER NOT NULL REFERENCES Orders,
PROD_ID NUMBER NOT NULL REFERENCES Products,
QUANTITY NUMBER NOT NULL,
ORDERDATE VARCHAR2 (80) NOT NULL,
PRIMARY KEY (ORDERLINEID,ORDERID));
Object-Relational Tables
CREATE TYPE Customer_obj AS OBJECT (
CUSTOMERID NUMBER,
CUST_NAME NAME,
CUST_ADDRESS ADDRESS_TYPE,
CUST_CONTACT CONTACT_INFO,
CUST_CREDITCARD CREDITCARD_INFO,
CUST_PERSONALINFO PERSONAL_INFO
);
CREATE TYPE NAME AS OBJECT (
FIRSTNAME VARCHAR2(80) ,
LASTNAME VARCHAR2(80)
);
CREATE TYPE ADDRESS_TYPE AS OBJECT (
ADDRESS VARCHAR2(280) ,
CITY VARCHAR2(80) ,
STATE VARCHAR2(20),
ZIP VARCHAR2(40) ,
COUNTRY VARCHAR2(40) ,
REGION VARCHAR2(40)
);
CREATE TYPE CONTACT_INFO AS OBJECT (
EMAIL VARCHAR2(40),
PHONE VARCHAR2(40)
);
CREATE TYPE CREDITCARD_INFO AS OBJECT (
CREDITCARDTYPE VARCHAR2(40) ,
CREDITCARD VARCHAR2(40) ,
CREDITCARDEXPIRATION VARCHAR2(40)
);
CREATE TYPE PERSONAL_INFO AS OBJECT (
USERNAME VARCHAR2(80) ,
PASSWORD VARCHAR2(80) ,
AGE NUMBER ,
INCOME NUMBER ,
GENDER VARCHAR(20)
);
CREATE TABLE CUSTOMERS_NEW OF Customer_obj(
PRIMARY KEY (CUSTOMERID));
CREATE TYPE Categories_obj AS Object(
CATEGORY NUMBER ,
CATEGORYNAME VARCHAR2(80) );
CREATE TABLE CATEGORIES_NEW OF Categories_obj(
PRIMARY KEY (CATEGORY));
CREATE TYPE Orders_obj AS Object(
ORDERID NUMBER ,
ORDERDATE VARCHAR2(400),
CUSTOMERID NUMBER,
NETAMOUNT NUMBER ,
TAX NUMBER,
TOTALAMOUNT NUMBER
);
CREATE TABLE ORDERS_NEW OF Orders_obj(
PRIMARY KEY (ORDERID),
FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMERS_NEW
);
CREATE TYPE Products_obj AS Object(
PROD_ID NUMBER ,
CATEGORY NUMBER ,
TITLE VARCHAR2 (40) ,
ACTOR VARCHAR2 (40) ,
PRICE VARCHAR2 (40)
);
CREATE TABLE PRODUCTS_NEW OF Products_obj(
PRIMARY KEY (PROD_ID),
FOREIGN KEY (CATEGORY) REFERENCES CATEGORIES_NEW
);
CREATE TYPE Orderlines_obj AS Object(
CUST_ID_INFO ID_INFO,
QUANTITY NUMBER,
ORDERDATE VARCHAR2 (80)
);
CREATE TYPE ID_INFO AS Object (
ORDERLINEID NUMBER ,
ORDERID NUMBER ,
PROD_ID NUMBER
);
CREATE TABLE ORDERLINES_NEW OF Orderlines_obj(
PRIMARY KEY (CUST_ID_INFO.ORDERLINEID,CUST_ID_INFO.ORDERID),
FOREIGN KEY (CUST_ID_INFO.ORDERID) REFERENCES ORDERS_NEW,
FOREIGN KEY (CUST_ID_INFO.PROD_ID) REFERENCES PRODUCTS_NEW
);

Object tables are tricky, and probably not worth the effort. But anyway.
You're working with objects, which means you need to instantiate them. When the object table is defined on a type which just has scalar attributes Oracle is clever enough to implicitly instantiate the object. But when the Type is complex, with attributes which are themselves Types ypu need to instantiate them explicitly.
Here is the insert for CUSTOMERS_NEW
insert into CUSTOMERS_NEW
select Customer_obj (
c.CUSTOMERID ,
c.FIRSTNAME||' '||c.LASTNAME ,
ADDRESS_TYPE(
c.ADDRESS1||' ' ||c.ADDRESS2 ,
c.CITY ,
c.STATE ,
c.ZIP ,
c.COUNTRY ,
c.REGION
),
CONTACT_INFO(
c.EMAIL,
c.PHONE
),
CREDITCARD_INFO (
c.CREDITCARDTYPE ,
c.CREDITCARD ,
c.CREDITCARDEXPIRATION
),
PERSONAL_INFO (
c.USERNAME,
c.PASSWORD,
c.AGE ,
c.INCOME ,
c.GENDER
)
)
from customers ;

Related

TRIGGERS_function

please help me to find the solution i have a project .
so my project is about a mobile repair store , we have some tables :
customers, mobiles, repairjob
//customer table
CREATE TABLE CUSTOMERS (
CUSTOMER_CPR NUMBER(9) PRIMARY KEY,
FIRST_NAME VARCHAR2(30) NOT NULL,
MID VARCHAR2(30)
LAST_NAME VARCHAR2(30) NOT NULL,
EMAIL VARCHAR2(30) NOT NULL UNIQUE,
HOME_TEL NUMBER(8) UNIQUE,
HOUSE_NO VARCHAR2(30) NOT NULL,
ROAD_NO VARCHAR2(30) NOT NULL,
BLOCK_NO VARCHAR2(30) NOT NULL,
CITY VARCHAR2(30) NOT NULL
);
//mobiles table
CREATE TABLE MOBILES (
MOB_ID NUMBER(8,0) PRIMARY KEY,
MODEL VARCHAR2(30) NOT NULL,
MOB_DECRIPTION VARCHAR2(100),
SERIAL_NUM VARCHAR2(30) UNIQUE,
MAKE VARCHAR2(50),
customer_cpr number(9),
);
//repair job table
CREATE TABLE REPAIR JOB (
JOB_NUM NUMBER primary key,
customer_cpr number(9)
EMP_ID NUMBER(8,0) NOT NULL,
DATE_RECEIVED DATE,
DATE_TO_RETURN DATE,
ITEM_ID NUMBER
);
so my question is to make a trigger function , we have assumed that the customer cannot have more than 3 repair jobs or can't fix more than 3 mobiles at one time until it finishes all. so how i make a triiger function ?

Table using Foreign Key Oracle

I am having a problem linking two tables in oracle 11g express addition. I have looked on this site for assistance, but I am unable to find what I need.
My BOOKS table was loaded up using the following statement:
CREATE TABLE STATES(
ST VARCHAR2(2) PRIMARY KEY,
STATES VARCHAR2(20) NOT NULL
);
The CUSTOMERS table that I am trying to load is written as such:
CREATE TABLE CUSTOMERS(
CUST_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
STREET_ADDRESS_1 VARCHAR2(30) NOT NULL,
STREET_ADDRESS_2 VARCHAR2(20),
CITY VARCHAR2(20) NOT NULL,
ST VARCHAR2(2),
ZIP_CODE VARCHAR2(10) NOT NULL,
PHONE_NUMBER_1 VARCHAR2(20)NOT NULL,
PHONE_NUMBER_2 VARCHAR2(20),
EMAIL VARCHAR2(40) NOT NULL,
CREDIT_LIMIT NUMBER(7,2) NOT NULL,
FOREIGN KEY ST REFERENCES STATES(ST)
);
However, when I run the statement I get the following error:
ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:
I believe that the error is in the FOREIGN KEY portion of the statement, as I can load the table without that statement.
You have syntax error while declaring foreign key constraint
Correct one:
CREATE TABLE CUSTOMERS(
CUST_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
STREET_ADDRESS_1 VARCHAR2(30) NOT NULL,
STREET_ADDRESS_2 VARCHAR2(20),
CITY VARCHAR2(20) NOT NULL,
ST VARCHAR2(2),
ZIP_CODE VARCHAR2(10) NOT NULL,
PHONE_NUMBER_1 VARCHAR2(20)NOT NULL,
PHONE_NUMBER_2 VARCHAR2(20),
EMAIL VARCHAR2(40) NOT NULL,
CREDIT_LIMIT NUMBER(7,2) NOT NULL,
CONSTRAINT FK_CUSTOMERS FOREIGN KEY (ST) REFERENCES STATES(ST)
);
or
CREATE TABLE CUSTOMERS(
CUST_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
STREET_ADDRESS_1 VARCHAR2(30) NOT NULL,
STREET_ADDRESS_2 VARCHAR2(20),
CITY VARCHAR2(20) NOT NULL,
ST VARCHAR2(2),
ZIP_CODE VARCHAR2(10) NOT NULL,
PHONE_NUMBER_1 VARCHAR2(20)NOT NULL,
PHONE_NUMBER_2 VARCHAR2(20),
EMAIL VARCHAR2(40) NOT NULL,
CREDIT_LIMIT NUMBER(7,2) NOT NULL,
FOREIGN KEY (ST) REFERENCES STATES(ST)
);

PL SQL trigger When Insert a table insert into another Table

I attached two table below I need a trigger when insert do_chd table in same time insert in do_chd_hist table And DO_MST table to DO_MST_HIST TAbLE
create table do_mst
( fdo_no varchar2(14) not null,
fdo_date date not null,
code_db varchar2(11),
db_name varchar2(25),
code_sdb varchar2(11),
sdb_name varchar2(25),
code_sst varchar2(11),
sst_name varchar2(25),
constraint fdo_no_pk primary key(fdo_no)
);
create table do_mst_hist
( fdo_no varchar2(14) not null,
fdo_date date not null,
code_db varchar2(11),
db_name varchar2(25),
code_sdb varchar2(11),
sdb_name varchar2(25),
code_sst varchar2(11),
sst_name varchar2(25),
constraint fdo_no_pk primary key(fdo_no)
);
create table do_chd
( fdo_no varchar2(14) not null,
itemcode varchar2(11) not null,
name varchar(27) not null,
unit_price varchar2(11),
req_qty number(11) not null,
total_price number(14),
unit varchar2(7),
constraint fdo_no_fk foreign key(fdo_no)
references do_mst(fdo_no)
);
create table do_chd_hist
( fdo_no varchar2(14) not null,
itemcode varchar2(11) not null,
name varchar(27) not null,
unit_price varchar2(11),
req_qty number(11) not null,
total_price number(14),
unit varchar2(7),
constraint fdo_no_fk foreign key(fdo_no)
references do_mst(fdo_no)
);
Can anybody help me to write this trigger?
While I agree with what being said in the comments, I don't think there's a reason to think twice about writing up a create trigger syntax, and I'm happy to show you the solution to your problem:
CREATE OR REPLACE TRIGGER do_chd_insert_trg
AFTER INSERT do_chd
FOR EACH ROW
DECLARE
sal_diff number;
BEGIN
insert into do_chd_hist
values (new.fdo_no, new.itemcode, new.name, new.unit_price, new.req_try, new.total_price, new.unit);
END;
/
You can use this syntax to generate the trigger to the second table, after changing the respectable column names.
You can read more about oracle trigger syntax here -
https://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm

Oracle Batch Database

I'm trying to run my databases but STUDENTS AND BATCHES tables are giving the error: 'ORA-00942: table or view does not exist'. I've tried dropping them in order, but I can't tell what exactly the problem is.
Forgive me if the answer is obvious, I'm new to batches
DROP TABLE students;
DROP TABLE batches;
DROP TABLE courses;
DROP TABLE faculty;
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
Create your faculty and courses tables first. Since batches depends on one of those tables, you would want to have those tables created first.
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
Example: http://sqlfiddle.com/#!4/91909c/1 shows the sequence of table creation

Oracle SQL Foreign Key Issues

I am trying to create tables as well as references but keep getting the following error:
ERROR at line 4:
ORA-00907: missing right parenthesis
...and have searched high and low with no clear way on how to resolve this. I have the following:
drop table financing_plans;
CREATE TABLE financing_plans (plan_id CHAR(10) PRIMARY KEY,
institution VARCHAR2(15) NOT NULL,
Loan_type VARCHAR2(10) NOT NULL,
min_down NUMBER(10,2) NOT NULL,
max_loan_amount NUMBER(10,2) NOT NULL,
Percentage NUMBER(10,2) NOT NULL,
Max_term NUMBER(10,25) NOT NULL);
drop table sale_financings;
CREATE TABLE sale_financings (sale_id CHAR(10) PRIMARY KEY,
Down_pay VARCHAR2(25) NOT NULL,
Loan_term VARCHAR2(18) NOT NULL,
FOREIGN KEY ("plan_uid") REFERENCES financing_plans(plan_id)
FOREIGN KEY ("sale_uid") REFERENCES sales(sale_id));
drop table sales;
CREATE TABLE sales (sale_id CHAR(10) PRIMARY KEY,
Salesperson_ID VARCHAR2(25) NOT NULL,
Cust_ID VARCHAR2(10) NOT NULL,
VIN VARCHAR2(10) NOT NULL,
Gross_sale_price NUMBER(10,2) NOT NULL,
Mileage NUMBER(10,2) NOT NULL,
sale_date DATE,
Vehicle_status VARCHAR2(10) NOT NULL);
Any solution, anyone?
Thanks,
You have to create columns for the foreign key at first, and then create the foreign keys:
CREATE TABLE financing_plans (
plan_id CHAR(10) PRIMARY KEY
, institution VARCHAR2(15) NOT NULL
, Loan_type VARCHAR2(10) NOT NULL
, min_down NUMBER(10,2) NOT NULL
, max_loan_amount NUMBER(10,2) NOT NULL
, Percentage NUMBER(10,2) NOT NULL
, Max_term NUMBER(10,25) NOT NULL
);
CREATE TABLE sales (
sale_id CHAR(10) PRIMARY KEY
, Salesperson_ID VARCHAR2(25) NOT NULL
, Cust_ID VARCHAR2(10) NOT NULL
, VIN VARCHAR2(10) NOT NULL
, Gross_sale_price NUMBER(10,2) NOT NULL
, Mileage NUMBER(10,2) NOT NULL
, sale_date DATE
, Vehicle_status VARCHAR2(10) NOT NULL
);
CREATE TABLE sale_financings (
sale_id CHAR(10) PRIMARY KEY
, Down_pay VARCHAR2(25) NOT NULL
, Loan_term VARCHAR2(18) NOT NULL
, plan_id char(10) not null
, sale_uid char(10) not null
, CONSTRAINT constraint_name_fk FOREIGN KEY (plan_id) REFERENCES financing_plans(plan_id)
, constraint constraint_name_fk2 foreign key (sale_uid) references sales(sale_id)
);
It looks like you're creating the FK reference to sales(sale_id) in sale_financings before you create the sales table. Also, I would check to be sure table exits before dropping it.

Resources