Oracle SQL - Table Create Issue - sql

i am currently trying to create a person table that allows a person to be either a client or a staff member. Currently this is my code :-
Create Or Replace Type Person As OBJECT
(
person_id number(5) not null,
firstname varchar2(15) not null,
surname varchar2(15) not null,
address1 varchar2(70) not null,
address2 varchar2(70),
address3 varchar2(70),
postcode varchar2(9) not null
);
/
Create Or Replace Type Client Under Person
(
marital_status varchar2(10),
no_of_children number(2)
);
/
Create Or Replace Type Staff Under Person
(
job_title varchar2(15) not null,
salary number(4,2) not null,
manager_id number(5) not null
);
/
Create Table Person Of Person
(
person_id Primary Key
);
The object types all compile but when it goes to create the table i get the following error:-
SQL Error: ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
what is causing this error to occur and what can be done to rectify it. Any help is greatly appreciated.
********EDIT**********
I have changed the name of the table to person_tbl and still get the same error. For some reason, this error now appears in the compiler log:-
Error: PL/SQL: Compilation unit analysis terminated
Error(1,18): PLS-00905: object [ConnectionName].PERSON is invalid
I have no idea why it isn't letting me use the Person type as I have triede this method before successfully.

Don't know, why you get these error. You should get the error
00955. 00000 - "name is already used by an existing object"
when trying to create a table with the same name as a type.
Try
Create Table Persons Of Person
(
person_id Primary Key
);

You have a few problems. As #tonirush mentioned, you can't have more than one object in a database with the same name.
Additionally, the person type is compiling with errors (specifically PLS-00218: a variable declared NOT NULL must have an initialization assignment). Until you resolve these errors, you can't build an object table based on the object.
Your subtypes also have compilation errors: PLS-00590: attempting to create a subtype UNDER a FINAL type, but that's not relevant to the inability to create the object table.
As a footnote, the word "object" in this answer (and in Oracle databases, in general) is overloaded. In the first paragraph, I'm speaking of "database objects", which is pretty much anything that is created in the database with a create command. For the rest I'm speaking of "object types" which are object created by create type ... object specifically.

Related

invalide identifier oracle

CREATE type recommendation as object(
descriptions varchar2(200)
);
create type recomand as table of recommendation;
create type traitement_type as object (
id_traitement number(7),
duree varchar2(25),
recome recomand,
description varchar2(250)
);
CREATE TABLE Traitement (primary key(id_traitement))
nested table recome store as Les_recommendations;
i execute these commands and at the last query i got the error :
ORA-00904: : invalid identifier
any solutions ?
Your create table statement doesn't include a column list. It's interpreting "primary key" a column and spaces are not allowed. Look up the proper format for create table statements and PK constraints.

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

How to manipulate objects with inheritance in sql (oracle)?

Supposing that i have that hierarchy, how can i manipulate (add, edit) an agent or a client ?
CREATE TYPE TPRENOM AS varray(3) OF VARCHAR2(20);
CREATE TYPE tadr as object(
cp int not null,
state varchar2(20),
city varchar2(20),
street varchar2(20),
doorNum int
);
create type tperson as object(
fname varchar2(20),
lname tprenom,
adress tadr,
phoneNum varchar2(10),
email varchar2(50)
)not final;
create type tutilisateur under tperson(
username varchar2(20),
password varchar2(20)
);
create table agent(
id_ag int not null,
infos tutilisateur not null
, CONSTRAINT agent_pk PRIMARY KEY
(
ID_ag
)
enable
);
create table client(
id_cl int not null,
infos tperson not null,
num_chec varchar2(30) not null,
CONSTRAINT client_pk PRIMARY KEY
(
ID_cl
)
enable
);
I've tried these, but it didn't work :
insert into agent values(1, tutilisateur( tperson( 'name', tprenom('bilel',
'dani','lastname3'), tadr(3,'state', 'miliana', 'hammama', 20),
'2140547854', 'email#gmail.com'), 'username', 'password'));
insert into client values(0, tperson('name', tprenom('bilel', 'dani',
'lastname3'), tadr(3,'state2','miliana','hammama',20)),'123456789');
That's the error displaying when excecuting the sql above :
Erreur SQL : ORA-02315: incorrect number of arguments for default constructor
00000 - "incorrect number of arguments for default constructor"
*Cause: The number of arguments specified for the default constructor
doesn't match the number of attributes of the object type.
*Action: Specify the correct number of arguments for the default
constructor and retry the operation.
I'm i doing inheritance wrong ?
Thanks for your response
Okay i found the solution, i can't beleive how easy it was,that's how to insert into agent table :
insert into agent
values(1,tutilisateur('name',tprenom('bilel','dani','lastname3'),tadr(3,'ain
delfa','miliana','hammama',20),'2140547854','email#gmail.com','username','password'));
That's how to insert into client table :
insert into client
values(0,tperson('name',tprenom('bilel','dani','lastname3'),tadr(3,'ain
delfa','miliana','hammama',20),'11225','email#gmail.com'),'123456789');

error: ORA-01730: invalid number of column names specified

Please help. I want to create an object view from the ffl tables but i keep getting the above error and can't find any solution.
create table COPY_BOOK (
NUM number(4,0),
DATE_Purchase date,
PRICE number(5,2),
LOAN_code varchar2(20) ,
STATUS varchar2(15) check (STATUS in ('GOOD','DAMAGED')),
CONSTRAINT CP_PK primary key (num) ,
constraint Loan_code_D check (LOAN_CODE in ('NO', 'LOAN'))
);
create or replace type copy_book_t as object(
num number(4, 0),
loan_code varchar2 (20)
);
/
create or replace view Vcopy_book of copy_book_t
with object oid (num)
as select cb.num, cb.date_purchase, cb.price, cb.loan_code, cb.status
from copy_book cb;
/
Is there a problem with the type definition?
From the documentation:
The procedure for defining an object view is:
Define an object type, where each attribute of the type corresponds to an existing column in a relational table.
Write a query that specifies how to extract the data from the relational table. Specify the columns in the same order as the
attributes in the object type.
You have created your object type with two attributes, but you're trying to populate it with five columns from your relational table.
You either need to change your object type to have the same five attributes (demo), or only select the num and loan_code columns in the view (demo).
Incidentally, the documentation also recommends using with object identifier rather than with object oid.

Writing a CREATE TABLE, keep getting errors

Can anyone look at this and tell me what I'm doing wrong?
I've looked up different ways to write it and changed it several times but keep getting different errors. I've pasted the current error, another one I've gotten is "missing left parenthesis" even when I have them all entered.
Error starting at line 1 in command:
CREATE TABLE book(
ISBN VARCHAR(10) PRIMARY KEY,
TITLE VARCHAR(20) NOT NULL,
AUTHORF_NAME VARCHAR(15) NOT NULL,
AUTHORL_NAME VARCHAR(15) NOT NULL,
LIST_PRICE NUMBER(5,2) NOT NULL,
QO_H INTEGER NOT NULL
)
Error at Command Line:1 Column:14
Error report:
SQL Error: ORA-00955: name is already used by an existing object
00955. 00000 - "name is already used by an existing object"
*Cause:
*Action:
You already have a book table in your database. Tablenames must be unique.