Supposing i have this hierarchy :
create or replace type tperson as object(
fname varchar2(20),
lname tprenom,
adress tadr,
phone_num varchar2(10),
email varchar2(50)
)not final;
create or replace type tuser under tperson(
username varchar2(20),
password varchar2(20)
);
create table agent(
id_ag int,
infos tuser not null
);
insert into agent values(1,tuser('name',tprenom('bilel','dani','lastname3')
,tadr(3,'ain delfa','miliana','hammama',20),
'2140547854','email#gmail.com','username','password'));
How could i select, update only a single attribute from agent table ?
I've tried that sql but it didn't work :
select infos.fname, infos.lname, infos.adress, infos.phone_num, infos.email,
infos.username, infos.password from agent where id_ag=1;
But i'm getting this error :
invalid identifier 00904. 00000 - "%s: invalid identifier"
What am i missing ?
Thanks for your response.
You have a semicolon before the where that should not be there.
When it comes to accessing the user-defined column, use a table prefix and you should be fine.
Here is the syntax for your SELECT query :
select
ag.infos.fname,
ag.infos.lname,
ag.infos.adress,
ag.infos.phone_num,
ag.infos.email,
ag.infos.username,
ag.infos.password
from agent ag
where ag.id_ag = 1;
If you are looking to perform an UPDATE :
update agent ag
set ag.infos.fname = 'foo', ag.infos.lname = 'bar'
where ag.infos.id_ag = 1
Related
I am working through a homework assignment and am stuck on a problem with stored procedures.
I have the following tables:
create table Restaurant(rID int, name varchar2(100), address varchar2(100), cuisine varchar2(100));
create table Reviewer(vID int, name varchar2(100));
create table Rating(rID int, vID int, stars int, ratingDate date);
For my stored procedure I need to create, I get an input of a restaurant name (unique), reviewer name(unique), star rating, and review date. I need to update the Rating table with the proper information, and add a new reviewer to the Review table if they have not previously existed in the table.
In order to start my stored procedure, I wanted to start with just creating a new table called newReview to get the inputs stored in a new table, before re-writting to update the existing tables.
Here is the newReview Table
CREATE TABLE newReview(
RestaurantName VARCHAR(100),
UserName VARCHAR(100),
Stars Int,
RatingDate Date
)
This is my AddNewReview procedure, which compiles with success:
CREATE OR REPLACE PROCEDURE AddNewReview(
RESTAURANTNAME IN VARCHAR2 (100)
, USERNAME IN VARCHAR2 (100)
, Stars IN NUMBER
, RATINGDATE IN DATE
) AS
BEGIN
INSERT INTO newReview VALUES (RestaurantName, UserName, Stars, RatingDate);
END AddNewReview;
;
However, when I run the stored procedure with inputs as such,
BEGIN
AddNewReview ('Rangoli', 'Sarah M.', 4, '2020-11-21');
END;
I get the following error:
Error starting at line : 20 in command -
BEGIN
AddNewReview ('Rangoli', 'Sarah M.', 4, '2020-11-21');
END;
Error report -
ORA-06550: line 2, column 5:
PLS-00905: object TOCONN22.ADDNEWREVIEW is invalid
ORA-06550: line 2, column 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I have tried defining the date input as DATE '2020-11-21' and also switching the single quote to double. Where am I going wrong, as this is the first stored procedure I am writing.
Try to change Stars data type from NUMBER to Int
AS:
CREATE OR REPLACE PROCEDURE AddNewReview(
RESTAURANTNAME IN VARCHAR2 (100)
, USERNAME IN VARCHAR2 (100)
, Stars IN NUMBER
, RATINGDATE IN DATE
) AS
BEGIN
INSERT INTO newReview VALUES (RestaurantName, UserName, Stars, RatingDate);
END AddNewReview;
;
to
CREATE OR REPLACE PROCEDURE AddNewReview(
RESTAURANTNAME IN VARCHAR2 (100)
, USERNAME IN VARCHAR2 (100)
, Stars IN Int
, RATINGDATE IN DATE
) AS
BEGIN
INSERT INTO newReview VALUES (RestaurantName, UserName, Stars, RatingDate);
END AddNewReview;
;
You need to look up the ids for the insertion:
CREATE OR REPLACE PROCEDURE AddNewReview (
in_RESTAURANTNAME IN VARCHAR2(100),
in_USERNAME IN VARCHAR2(100),
in_Stars IN NUMBER,
in_RATINGDATE IN DATE
) AS
BEGIN
INSERT INTO newReview (rid, vid, stars, RatingDate)
SELECT r.id, rr.id, in_stars, in_RatingDate
FROM restaurant r JOIN
reviewer rr
ON r.name = in_RestaurantName AND
rr.name = in_UserName
END AddNewReview;
This joins to the reference tables to get the ids you need. It will not insert the review if either name does not match. Your question doesn't specify what to do in that case, so that seems like reasonable behavior.
Note that the parameters are named so they don't conflict with column names. And this has listed all columns for the insert -- both are best practices.
Parameters are defined only by name and data type, they do not contain size specification. So your procedure needs:
create or replace procedure addnewreview(
restaurantname in varchar2
, username in varchar2
, stars in int
, ratingdate in date
...
You need to use To_Date function while calling the stored procedure, like below
BEGIN
AddNewReview ('Rangoli', 'Sarah M.', 4, TO_DATE('2020-11-21','YYYY-MM-DD'));
END;
CREATE OR REPLACE TYPE hypothesis AS TABLE OF VARCHAR2(20);
/
CREATE OR REPLACE TYPE Hypoth_pr AS OBJECT (prob_content VARCHAR2(20), pr NUMBER);
/
CREATE OR REPLACE TYPE Hypoth_po AS OBJECT (possib_content VARCHAR2(20), po NUMBER);
/
CREATE OR REPLACE TYPE focal_element AS OBJECT(focal_element_content hypothesis, focal_element_mass NUMBER);
/
CREATE OR REPLACE TYPE bba AS TABLE OF focal_element;
/
CREATE OR REPLACE TYPE Pr_Dist AS TABLE OF Hypoth_pr;
/
CREATE OR REPLACE TYPE po_Dist AS TABLE OF Hypoth_po;
/
CREATE OR REPLACE TYPE confidence_level AS OBJECT(bel NUMBER, pl NUMBER);
/
CREATE TABLE edb (pid VARCHAR2(10) PRIMARY KEY, Disease bba, Symptom Pr_Dist, Age po_Dist, cl CONFIDENCE_LEVEL)
NESTED TABLE Disease STORE AS Diseases (NESTED TABLE focal_element_content STORE AS focal_element_contents_fs),
NESTED TABLE Symptom STORE AS Symptoms
NESTED TABLE Age STORE AS Ages;
/
INSERT INTO edb VALUES ('2', bba(focal_element(hypothesis('Pneumonia',0.2)), focal_element(hypothesis('Cold','Fever'),0.6)),
pr_Dist(Hypoth_pr('Cough',0.5), Hypoth_pr('Headcaches',0.5)),
po_Dist(Hypoth_po('Old',1), Hypoth_po('Teenager',0.7)),
CONFIDENCE_LEVEL(0.4,0.8));
Erreur à la ligne de commande: 26 Colonne: 34
Rapport d'erreur -
Erreur SQL : ORA-02315: nombre d'arguments non valide pour constructeur par défaut
02315. 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.
You wanted to use double focal_element for bba, but using redundant closing parentheses for the first one corrupts the syntax as focal_element(hypothesis('Pneumonia') ,0.2))
-->^
Try Insert statement below :
INSERT INTO edb VALUES ('2',
bba(focal_element(hypothesis('Pneumonia') ,0.2),
focal_element(hypothesis('Cold','Fever'),0.6) ),
pr_Dist(Hypoth_pr('Cough',0.5), Hypoth_pr('Headcaches',0.5)),
po_Dist(Hypoth_po('Old',1), Hypoth_po('Teenager',0.7)),
CONFIDENCE_LEVEL(0.4,0.8));
I already tried to look for this type of error. I do have similar errors on others but different problems. Please help me with this, what is the good approach to this sample oracle query ?
create type Exams (
year char(4),
city varchar(20)
)
create table exams of type Exams
create OR REPLACE type Skills AS OBJECT(
type varchar(20),
ExamSet Exams multiset
);
/
create table skills of Skills;
Here is the error in script output :
LINE/COL ERROR
--------- -------------------------------------------------------------
1/12 PLS-00103: Encountered the symbol "(" when expecting one of the following: ; is authid as compress force compiled wrapped under
Errors: check compiler log
Try :
CREATE OR REPLACE TYPE ExamsType AS OBJECT (
year char(4),
city varchar(20)
);
/
create table Exams OF ExamsType;
Fixes :
create the type AS OBJECT ; this will give you an Abstract Data Type (ADT), that can actually hold attributes - see the oracle docs
you need a semi-colon and a slash at the end of the create type statement
the table cannot have the same name as the type
syntax for CREATE TABLE was wrong
To insert in this table, you would typically use the following statement :
insert into Exams values( ExamsType('2018', 'my adress') );
This will work:
CREATE TABLE new_table
AS (SELECT *
FROM old_table WHERE 1=2);
For your case it goes like this:
CREATE TABLE exams
AS (SELECT *
FROM Exams WHERE 1=2);
for 1=2 it does not select any of the rows but with as it copies the table definition.
I want to insert data into table but got below error..Can any one help me..
CREATE OR REPLACE TYPE TEST_TYP FORCE IS OBJECT
("id" VARCHAR(5000 NULL)
NOT FINAL;
CREATE OR REPLACE TYPE TEST_TAB is table of REF TEST_TYP;
CREATE OR REPLACE TYPE TEST1_TYP FORCE IS OBJECT
("id" VARCHAR2(500) NULL,
"extension" "TEST_TAB" NULL )
NOT FINAL;
CREATE TABLE "TEST_OBJ_TABLE" OF "TEST1_TYP"
NESTED TABLE "extension" STORE AS "Allin"
When I try to insert using this statementL
insert into "TEST_OBJ_TABLE" ("id","extension")
VALUES(
'0FE71A85',
"TEST_TAB"("TEST_TYP"( '0FE71A8'))
);
It throws this error
Error at Command Line : 59 Column : 12
Error report -
SQL Error: ORA-00932: inconsistent datatypes: expected REF SUB_HWOW.TEST_TYP got SUB_HWOW.TEST_TYP
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*Cause:
*Action:
You have to modify object TEST_TYP -> varchar2 has to be max 4000.
You have to store this object in db.
Create object table for TEST_TYP
create table t_for_test_type of TEST_TYP;
To obtain object reference, the object have to be save in db table. In your case:
declare
v_ref_to_test_type1 ref TEST_TYP;
begin
insert into t_for_test_type t values('abcd1') return ref(t) into v_ref_to_test_type1;
end:
Join all parts together.
declare
v_ref_to_test_type1 ref TEST_TYP;
v_ref_to_test_type2 ref TEST_TYP;
begin
insert into t_for_test_type t values('abcd1') return ref(t) into v_ref_to_test_type1;
insert into t_for_test_type t values('abcd2') return ref(t) into v_ref_to_test_type2;
insert into TEST_OBJ_TABLE values (TEST1_TYP('abcd',new TEST_TAB(v_ref_to_test_type1,v_ref_to_test_type2)));
end;
Query table.
select t."id",x.column_value from TEST_OBJ_TABLE t, table(t."extension") x it returns id + ref to other object
Viewing the referenced object.
select t."id",deref(x.column_value) from TEST_OBJ_TABLE t, table(t."extension") x
Note1. You should avoid declaring attribute using double quote. Db becomes case sensitive and it's not normal situation :)
Note2. I don't know why code formatting is not working today
Question. Why are you trying to use such complicated construction?
Changed the original answer. The quotes are doing you no favors - nor was calling your column names id. Removed the REF reference.
CREATE OR REPLACE TYPE TEST_TYP FORCE IS OBJECT
(id1 VARCHAR(4000) NULL)
NOT FINAL;
CREATE OR REPLACE TYPE TEST_TAB is table of TEST_TYP;
CREATE OR REPLACE TYPE TEST1_TYP FORCE IS OBJECT
(id2 VARCHAR2(500) NULL,
extension TEST_TAB NULL )
NOT FINAL;
CREATE TABLE TEST_OBJ_TABLE OF TEST1_TYP
NESTED TABLE extension STORE AS Allin ;
insert into TEST_OBJ_TABLE ( ID2, EXTENSION)
VALUES(
'0FE71A85',
TEST_TAB(TEST_TYP( '0FE71A8'))
);
SELECT * FROM TEST_OBJ_TABLE ;
I want to get the value SESSIONID from this table:
CREATE TABLE SESSIONSLOG(
SESSIONID VARCHAR2(30 ) NOT NULL,
USERNAME VARCHAR2(30 ),
IPADDRESS VARCHAR2(30 ),
LOGINTIME TIMESTAMP(6),
LOGOUTTIME TIMESTAMP(6)
)
/
I tried this SQL query:
SELECT SESSIONID FROM ACTIVESESSIONSLOG
But I get this error message:
ORA-00904: "SESSIONID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 1 Column: 7
How I can fix this?
You're querying the wrong table:
CREATE TABLE SESSIONSLOG
...
SELECT SESSIONID FROM ACTIVESESSIONSLOG
ACTIVESESSIONSLOG is not the same as SESSIONSLOG.
You are querying wrong table ... table should be SESSIONSLOG
SELECT SESSIONID FROM SESSIONSLOG
#user1285928 you are not supposed to use " ACTIVESESSIONSLOG " .
Instead Use SESSIONSLOG. Also use the delimiter symbol ;
SELECT SESSIONID FROM SESSIONSLOG;