ORA-02330: datatype specification not allowed - sql

I tried creating a an object table after successfully creating an object type but i got the error
'datatype specification not allowed'.
Please what am i doing wrong. I am lost at this point.
CREATE OR REPLACE Type Route_t AS Object(
Route_ID CHAR(3),
Route_descr VARCHAR(30),
city VARCHAR(10),
Stop_no NUMBER(3),
Stop_meal VARCHAR(10),
Route_ticket ticket_nt_type,
Route_schedule schedule_nt_type
);
Create Table Route_Tab of Route_t
(primary key (Route_ID),
Nested Table Route_ticket Store As Route_ticket_NTab,
Nested Table Route_Schedule Store As Route_Schedule _NTab;
);

The NESTED TABLE storage clauses must come after the parenthesis. Below is a fully functional example.
--Drop table and types to reset environment.
/*
drop table route_tab;
drop type route_t;
drop type ticket_nt_type;
drop type schedule_nt_type;
*/
--Create nested types.
create or replace type ticket_nt_type as table of varchar2(100);
create or replace type schedule_nt_type is table of varchar2(100);
--Create object.
CREATE OR REPLACE Type Route_t AS Object(
Route_ID CHAR(3),
Route_descr VARCHAR(30),
city VARCHAR(10),
Stop_no NUMBER(3),
Stop_meal VARCHAR(10),
Route_ticket ticket_nt_type,
Route_schedule schedule_nt_type
);
--ORIGINAL version that throws "ORA-02330: datatype specification not allowed".
Create Table Route_Tab of Route_t
(primary key (Route_ID),
Nested Table Route_ticket Store As Route_ticket_NTab,
Nested Table Route_Schedule Store As Route_Schedule _NTab;
);
--NEW version that runs on my system.
Create Table Route_Tab of Route_t
(primary key (Route_ID))
Nested Table Route_ticket Store As Route_ticket_NTab,
Nested Table Route_Schedule Store As Route_Schedule_NTab;

Related

Can I change an attribute name from a table derived from a type?

Folowing the Object-Relational Database model, I wanted to create the tables or_doctor and or_recepcionist derived from the type t_employee.
Here, follows the type structure:
DROP TYPE t_employee FORCE;
CREATE OR REPLACE TYPE t_employee AS OBJECT (
num_employee INTEGER,
name_employee VARCHAR2(50),
birthdate_employee DATE
);
And here, the tables' structure:
DROP TABLE or_doctor CASCADE CONSTRAINTS;
CREATE TABLE or_doctor OF t_employee (
PRIMARY KEY (num_employee),
name_employee NOT NULL,
birthdate_employee NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
DROP TABLE or_recepcionist CASCADE CONSTRAINTS;
CREATE TABLE or_recepcionist OF t_employee (
PRIMARY KEY (num_employee),
name_employee NOT NULL,
birthdate_employee NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
Doing so, the attributes names, on both tables, will end up with "employee". Could I change the attribute name so they are specific in each table at the moment I'm creating the table?
E.G.:
Table or_doctor: num_doct, name_doct, birthdate_doct.
Table or_recepcionist: num_recep, name_recep, birthdate_recep.
As a frame challenge, don't add a suffix to your identifiers then you don't need to worry about the suffix being incorrect:
CREATE TYPE t_employee AS OBJECT (
num INTEGER,
name VARCHAR2(50),
birthdate DATE
);
CREATE TABLE or_doctor OF t_employee (
PRIMARY KEY (num),
name NOT NULL,
birthdate NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
CREATE TABLE or_receptionist OF t_employee (
PRIMARY KEY (num),
name NOT NULL,
birthdate NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
If you try to rename the column:
ALTER TABLE or_doctor RENAME COLUMN name TO name_doctor;
Then you will get the error:
ORA-23291: Only base table columns may be renamed
If you are using object-derived tables then you appear to be stuck with the identifiers from the object; so, make the object names generic so that they are appropriate in every place they are going to be used.

Is there way to create trigger for get ref value when insert data?

I create table using types in oracle. I have data file in csv. I need to get ref value for patient table when inserting data.
CREATE TYPE Location_t as OBJECT(
L_id varchar(6),
lon NUMBER(9,6),
lat NUMBER(9,6),
country varchar(26),
Province varchar(26)
)
/
create type patient_t as object(
p_id varchar(10),
ReportedDate date,
Deadts int,
recover int,
confirmed int,
loc ref location_t
)
create table Location of location_t
(L_id PRIMARY KEY)
/
CREATE TABLE PATIENT of patient_t
(p_id PRIMARY KEY,
loc references Location)
Location table data is already inserted and i need to get ref code for patient.
location table
patient table

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

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') )

I am trying to create THE TABLE ,ORA-00902: invalid datatype error..what's wrong here

CREATE TYPE project_type AS OBJECT
(
ID NUMBER,
DESCRIPTION VARCHAR(10),
EMPLOYESS_NO VARCHAR(10),
TYPE VARCHAR(10)
);
CREATE TABLE project OF project_type
(
CONSTRAINT unique_ex_ID
UNIQUE(ID)
);
Type is a reserved keyword, so I think it's barfing on TYPE VARCHAR. Change to EMPLOYEE_TYPE and see if that works

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) );