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

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

Related

'SET' must be a type

I created the following types :
CREATE TYPE Adress AS Object (Street varchar2(50), PostalC number, Ville varchar2(50));
CREATE TYPE PhoneNumber;
A person can have a set of PhoneNumbers, when I try to create the Type Person :
CREATE TYPE Person AS Object (FirstName varchar2(50), LastName varchar2(50), Adr Adress, Mobile SET(PhoneNumber));
I get the following error :
Errors: TYPE PERSON Line/Col: 0/0 PL/SQL: Compilation unit analysis
terminated Line/Col: 1/90 PLS-00488: 'SET' must be a type
If you want an array of phone numbers for each person, define the phone number type as a nested table:
create or replace type address as object (
street varchar2(50), postalc number, ville varchar2(50)
);
/
create or replace type phonenumber as table of varchar2(20);
/
create or replace type person as object (
firstname varchar2(50), lastname varchar2(50),
adr address, mobile phonenumber
);

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 do I fix my my table so I don't get that the invalid datatype message?

I am trying to create a table in SQL and every time it I get the following error message:
ORA-00902: invalid datatype
SQL> create table BUSINESS (
2 B_IDINTEGER PRIMARY KEY,
3 B_CITYchar(20) not null,
4 B_NAMECHAR (20) NOT NULL,
5 B_CATEGORY(S) CHAR (25),
6 B_ACCTCHAR (25)
7 );
B_CITYchar(20) not null,
*
ERROR at line 3:
ORA-00902: invalid datatype
It is supposed to say table created but I don't know what is wrong with line 3.
You have several errors in your code. Try something like this:
create table BUSINESS (
B_ID INTEGER PRIMARY KEY,
B_CITY varchar2(20) not null,
B_NAME varchar2(20) NOT NULL,
B_CATEGORY varchar2(25),
B_ACCT varchar2(25)
);
Note that you should generally use variable length strings unless you know the value has a fixed length (which might be true of b_acct but is not true for b_city).

Oracle SQL - Table Create Issue

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.

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