How can I insert the data to the cars table if it's referenced to the customer table?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How can I insert the data to the cars table if it's referenced to the customer table?


CREATE TYPE Customer_t AS OBJECT
(
cid char(6),
name varchar(15),
birthdate date,
phone char(10),
address varchar(50)
);

CREATE TABLE Customer OF Customer_t
(
cid PRIMARY KEY
);

CREATE TYPE Car_t AS OBJECT
(
regno char(9),
make varchar(12),
model varchar(10),
mdate date,
owner REF Customer_t,
value number(8,2)
);
/

CREATE TABLE Cars OF Car_t (regno PRIMARY KEY );

INSERT INTO Cars (regno, make, model, mdate, owner, value)
VALUES ('car1', 'german','honda',
TO_DATE('2000/12/16 12:00:00', 'yyyy/mm/dd hh:mi:ss'),
'c1', 8000 );



Error :



SQL Error: ORA-00932: inconsistent datatypes: expected REF got CHAR
00932. 00000 - "inconsistent datatypes: expected %s got %s"





By the way, varchar2 is the standard short string type in Oracle. char is just a weird ANSI compatibility feature there to waste space and create bugs.
– William Robertson
Aug 8 at 23:16



varchar2


char




1 Answer
1



First, you need to have a customer already created, in order to reference it. Then you include the reference in the INSERT statement. See below:


INSERT


CREATE TYPE Customer_t AS OBJECT (
cid char(6),
name varchar(15) ,
birthdate date ,
phone char(10),
address varchar(50)
);

CREATE TABLE Customer OF Customer_t (
cid PRIMARY KEY );

insert into customer values (customer_t('cl1234', 'Peter',
to_date('1990-05-04', 'YYYY-MM-DD'), '1234567890',
'123 Maple St'));

CREATE TYPE Car_t AS OBJECT (
regno char(9),
make varchar(12) ,
model varchar(10) ,
mdate date,
owner REF Customer_t,
value number(8,2)
);

CREATE TABLE Cars OF Car_t (
regno PRIMARY KEY );

insert into cars values (
car_t('reg123456', 'honda', 'accord',
to_date('2000-12-16 12:00:00', 'YYYY-MM-DD HH24:MI:SS'),
(select ref(cu) from customer cu where cid = 'cl1234'),
8000)
);





Thank you The Impaler
– user536263
Aug 9 at 8:24






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard