How to overcome a persistent oracle 'invalid identifier' error on a basic insert? - sql

I am trying to create a basic table using subtypes and insert some data into this in Oracle Express 11g.
My table is successfully created but i am having issues with inserting data.
The result of my insert statement always throws back an error 'SQL Error: ORA-00904: "BRANCH_PHONE": invalid identifier'.
The column which shows up in the error message is always the column which is at the end of the insert statement, despite the column existing in the table. I have tried the following code:
create type addressType as object(
street varchar2(20),
city varchar2(20),
postCode varchar2(8))
not final
/
create type branchType as object(
branchID int,
branch_address addressType,
branch_phone int(11))
not final
/
create table Branch of branchType(
constraint branch_pk primary key(branchID));
/
insert into Branch values (
branchID('2364'),
addressType('12 Rooster','Atlantis','A13 4UG'),
branch_phone('01316521311'));
I would really appreciate any ideas.

I made some changes, including changing the branch_phone to varchar2. A Phone number, while is "numbers" is not a data type of number. it is a string of characters. Also you were passing branchID as a string, but you are declaring it as a number, so changed that also. BranchID and branch_phone are primitive data types, so no constructor needed.
create type addressType as object(
street varchar2(20),
city varchar2(20),
postCode varchar2(8))
not final
/
create type branchType as object(
branchID int,
branch_address addressType,
branch_phone varchar2(11))
not final
/
create table Branch of branchType(
constraint branch_pk primary key(branchID));
/
insert into Branch values (
branchtype(2364,
addressType('12 Rooster','Atlantis','A13 4UG'),
'01316521311') )

Related

Displaying Oracle SQL Types

I am having a hard time getting an output from a table.
Here is the table creation
CREATE OR REPLACE TYPE FULL_MAILING_ADDRESS AS OBJECT
( STREET VARCHAR2(80),
CITY VARCHAR2(80),
STATE CHAR(2),
ZIP VARCHAR2(10));
CREATE TABLE CUSTOMER
(
FULL_ADDRESS FULL_MAILING_ADDRESS
);
INSERT INTO CUSTOMER VALUES (FULL_MAILING_ADDRESS('55 SOUTH','ARLINGTON','VA','2222'));
when I do select on the table I don't get the values instead I get
[ORACLE.FULL_MAILING_ADDRESS]

ORA-00936: missing expression error when inserting values

I spend lot of time searching where i made the mistake but i was unable to find it when its going to insert the last record there is a error message showing "ORA-00936: missing expression" How to solve this please help me
create type pearson_types as object(
name varchar2(50),
sysID char(6)
)NOT FINAL;
create type doctor_types under pearson_types(
regNo char(10),
specialization varchar2(25)
)
create table doctor of doctor_types(
regNo primary key
)
create type hospVisits_types as object(
hosChg float,
vDate varchar2(20),
refDoc REF doctor_types,
docChg float
)
create type hospvisits_tbl_types as table of hospVisits_types
create type phone_arr as VARRAY(3) of char(10)
create type patient_types under pearson_types
(
id char(10),
dob varchar(20),
phone phone_arr,
hospVisits hospvisits_tbl_types
)
create table patients of patient_types(
id primary key
)nested table hospVisits store as Hospital_tables
alter table Hospital_tables add scope for (refDoc) is doctor
insert into doctor values ('Dr.k.perera','D001','1223441234','Gynecologist');
insert into doctor values ('Dr.p.weerasingha','D002','1234421131','Dermatalogist');
insert into doctor values ('Prof .S. Fernando','D003','2342111322','Pediatrician');
insert into doctor values ('Dr.k.Sathgunanathan','D004','2344114344','Pediatrician');
insert into patients values('Sampath Weerasingha','P001','732821122V','23-JAN-73',phone_arr('0332124222'),hospvisits_tbl_types(hospVisits_types(50.00,'24-MAY-06',select ref (a) from doctor a where a.regNo='1223441234',500.00)))
Add parentheses to SELECT statements inside a SQL statement:
insert into patients values(
'Sampath Weerasingha','P001','732821122V','23-JAN-73',phone_arr('0332124222'),
hospvisits_tbl_types(hospVisits_types(50.00,'24-MAY-06',
( -- ADD ME
select ref (a) from doctor a where a.regNo='1223441234'
) -- ADD ME
,500.00))
);

Oracle 11g creating table

hi i'm using oracle 11g to create a table with Object-Relational Features but its not creating the table for some reason
here what i have
create type Name as object (
firstname varchar2(20),
surname varchar2(20))
final
create type Address as object (
street varchar2(20),
city varchar2(20),
postal_code varchar2(8))
not final
and for the table
create table people (
(pname Name,
paddress Address,
dateOfBirth date);
yet its not creating the table, i know its probably something simple and straight forward but i just cant seam to get it to create the table, if somebody could point me in the right direction to get it to create the table that would be great
also when i try create the table i get the following error
ORA-00904: "%s: invalid identifier"
*Cause:
*Action:
Vendor code 904Error at line:2 colimn:2
You can try this:
CREATE TYPE Name as object (firstname varchar2(20), surname varchar2(20)) FINAL;
CREATE TYPE Address as object (street varchar2(20), city varchar2(20), postal_code varchar2(8)) NOT FINAL;
CREATE TABLE people (pname Name, paddress Address, dateOfBirth date);
DEMO
try this
create type Name as object (
firstname varchar2(20),
surname varchar2(20))
final );
create type Address as object (
street varchar2(20),
city varchar2(20),
postal_code varchar2(8))
not final);

oracle11g sql: Warning: Type created with compilation errors

I'm trying to create a supertype customer service and subtype agent and supervisor, so they can inherent values however when I try to run this in oracle sql: a message comes up
Warning: Type created with compilation errors.
What is wrong with the code below?
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint supervisor_pk primary key(csID) );
You can use show errors in SQL*Plus or SQL Developer, or select * from user_errors, to see the error details.
Since you've shown six commands and the warning is about one of the first three (since it refers to type), and they appear OK independently apart from the constraint pointed put in comments, it looks like the whole script is being imterpreted as one command. It depends on your client settings, but you probably just need to seperate the commands with a / to cause them to execute. Becuase types can include PL/SQL the ; isn't treated as a statement seperator. So:
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
/
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
/
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
/
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint customer_service_pk primary key(csID) );

How do I insert data into object tables that have refs to others?

I'm new in Oracle and I really don't have a clear idea how to do this.
The database is this one...
CREATE OR REPLACE TYPE personUdt4 AS OBJECT(
pid varchar(11),
firstName varchar(20),
lastName varchar(20),
dob date)
NOT FINAL;
/
CREATE OR REPLACE TYPE locationUdt4 AS OBJECT(
street varchar(30),
bldg varchar(5),
room varchar(5))
NOT FINAL;
/
CREATE TYPE departmentUdt4;
/
CREATE TYPE studentUdt4;
/
CREATE TYPE facultyUdt4;
/
CREATE OR REPLACE TYPE campusClubUdt4 AS OBJECT(
cId number,
name varchar(50),
location locationUdt4,
phone varchar(12),
advisor REF facultyUdt4,
members REF studentUdt4)
NOT FINAL;
/
CREATE OR REPLACE TYPE facultyUdt4 UNDER personUdt4(
rank varchar(10),
advisorOf REF campusClubUdt4,
worksIn REF departmentUdt4,
chairOf REF departmentUdt4)
NOT FINAL;
/
CREATE OR REPLACE TYPE studentUdt4 UNDER personUdt4(
status varchar(10),
memberOf REF campusClubUdt4,
major REF departmentUdt4)
NOT FINAL;
/
CREATE OR REPLACE TYPE studentUdtList4 AS VARRAY(1000) of studentUdt4;
/
CREATE OR REPLACE TYPE facultyUdtList4 AS VARRAY(50) of facultyUdt4;
/
CREATE OR REPLACE TYPE departmentUdt4 AS OBJECT(
code varchar(3),
name varchar(40),
deptChair REF facultyUdt4,
MEMBER FUNCTION getStudents RETURN studentUdtList4,
MEMBER FUNCTION getFaculty RETURN facultyUdtList4)
NOT FINAL;
/
CREATE TYPE BODY departmentUdt4 as
member function getStudents return studentUdtList4
end func;
member function getFaculty return facultyUdtList4
end func;
end;
/
CREATE TABLE person4 OF personUdt4(
primary key (pid));
/
CREATE TABLE faculty4 OF facultyUdt4;
/
CREATE TABLE student4 OF studentUdt4;
/
CREATE TABLE department4 OF departmentUdt4(
primary key (code));
/
CREATE TABLE campusClub4 OF campusClubUdt4(
primary key (cid)
);
INSERT INTO student4
(pid, firstname, lastname, dob, status, memberOf, major)
VALUES
('10','alex','smith','31-may-98','FRESH', '10', '11');
COMMIT;
It'll be great if someone can help me D:
(This is all based on my very limited knowledge of Oracle's object-relational technology. Some of it may be wrong, and there's likely a simpler way to do this.)
As I understand it, a REF must point to an actual row. You must create some data before you can create some data. Since there are so many circular references you'll have to go back later and update everything. But hopefully this is enough to at least get you started.
--Create some rows to reference.
insert into campusClub4(cid) values(1);
insert into department4(code) values('A');
--Insert regular columns first.
insert into student4(pid, firstName, lastName, dob, status)
values('10', 'alex', 'smith', to_date('31-MAY-1998', 'DD-MON-YYYY'), 'FRESH');
--Add references with an update.
update student4
set memberOf = (select ref(campusClub) from campusClub4 campusClub where cid = 1)
,major = (select ref(department) from department4 department where code = 'A')
where pid = '10';
--Verify data
select pid, firstname, lastname, dob, status, deref(memberOf), deref(major) from student4;
--This would be a simpler method, but it doesn't work and I don't understand why.
insert into student4(pid, firstName, lastName, dob, status)
values('10', 'alex', 'smith', to_date('31-MAY-1998', 'DD-MON-YYYY'), 'FRESH'
,(select ref(campusClub) from campusClub4 campusClub where cid = 1)
,(select ref(department) from department4 department where code = 'A')
);
But I strongly recommend that you NEVER DO THIS. Inserting data into a table should not be this difficult. Object Relational databases are probably a bad idea. And Oracle's implementation of it sucks. You'll get ORA-600 errors and "invalid" tables all over the place with this stuff (e.g. I just got an ORA-600 from cross joining campusClub4 and department4 with only one row in each). And nobody will know how to use your data.