I am trying to create a trigger where is takes a several number of ratings from a table called FEEDBACK, and it creates an Average Rating on the MEMBER table. I want the trigger to update the average rating of each person whenever a new feedback is inserted.
----------- These are my tables -----------
create table Member_T(
MemberID Varchar2 (10) primary key,
MemberFirstName Varchar2 (20) NOT NULL,
MemberLastName Varchar2 (20) NOT NULL,
MemberMidleName Varchar2 (10),
MemberEmail Varchar2 (50) NOT NULL,
MemberPassword Varchar2 (20) NOT NULL,
MemberAdress Varchar2 (50) ,
MemberCity Varchar2 (20) ,
MemberState char (2) ,
MemberCountry Varchar2 (20) ,
MemberZipCode number (5,0),
MemberPhone Varchar2 (12) ,
MemberAverageRating number (3,1) check (MemberAverageRating >= 0.0 AND MemberAverageRating <= 5.0));
create table Feedback_T(
FeedbackID Varchar2 (10) primary key,
FeedbackMemberGiverID Varchar2 (10) references Member_T(MemberID),
MemberReceiverID Varchar2 (10) references Member_T(MemberID),
MemberRating number (3,1) check (MemberRating >= 0.0 AND MemberRating <= 5.0),
MemberComment Varchar2 (500),
MemberFeedbackDate Date Default(sysdate));
----------- This is my trigger -----------
create or replace trigger updateRating
after insert
on Feedback_T
for each row
Declare
rating Feedback_T.MemberRating%type;
receiver Feedback_T.MEMBERRECEIVERID%type;
averageRating Member_T.MemberAverageRating%type;
begin
select AVG(MemberRating), count(MEMBERRECEIVERID)
into rating, receiver
from Feedback_T
where Feedback_T.FEEDBACKID = :new.FEEDBACKID;
update Member_T
set averageRating = rating / receiver
where Member_T.MemberID = :new.MemberID;
end;
----------- I get this error -----------
TRIGGER UPDATERATING compiled
Errors: check compiler log
If you run "show errors" from the sql prompt, you will see your errors, or compile this in SQLDeveloper, which will show you the errors. Your problem is:
where Member_T.MemberID = :new.MemberID;
Where there is no bind variable called :new.MemberID, since MemberID is not a column in the Feedback_T table that the trigger is for. Maybe you meant to reference FeedbackMemberGiverID or MemberReceiverID?
Related
CREATE TABLE suppliers
(
supplier_ID NUMBER not null,
supplier_Name varchar2 (50) not null,
supplier_Addr varchar2 (100),
supplier_Town varchar2 (30),
supplier_State varchar2 (30),
supplier_Pcode varchar2 (15) not null,
supplier_Phone1 varchar2 (15),
supplier_Phone2 varchar2 (15),
supplier_Contact varchar2 (50),
supplier_Fax varchar2 (15),
supplier_Email varchar2 (40),
supplier_Renew DATE,
supplier_Creditlimit NUMBER,
supplier_Comments varchar2 (500),
PRIMARY KEY (supplier_ID)
);
INSERT INTO suppliers (supplier_ID, supplier_Name, supplier_Addr, supplier_Town, supplier_State, supplier_Pcode, supplier_Phone1, supplier_Phone2, supplier_Contact, supplier_Fax, supplier_Email, supplier_Renew, supplier_Creditlimit,supplier_Comments)
VALUES (010203, 'DSHK COMPANY', 'G 7 JLN TUNKU HASSAN ', 'Seremban', 'Negeri Sembilan', 70000, 604-42449268, 09-4265050, 'DSHK CO', 04-2224568, 'DSHKCOMPANY#GMAIL.COM', 2024-08-23, 1000, 'COMPANY WILL CALL BEFORE SENDING THE GOODS');
I keep getting missing comma error for insert statement. Please guide me, tq
You need to make the phone numbers into strings as you have defined them as varchar.
70000,604-42449268,09-4265050
->
'70000', '604-42449268', '09-4265050'
I think you need to do tha same for date... that is 2024-08-23 -> '2024-08-23'
Assuming that you are using an Oracle database since you are using VARCHAR2 data types then use string and date literals for VARCHAR2 and DATE data types respectively:
insert into suppliers (
supplier_ID,
supplier_Name,
supplier_Addr,
supplier_Town,
supplier_State,
supplier_Pcode,
supplier_Phone1,
supplier_Phone2,
supplier_Contact,
supplier_Fax,
supplier_Email,
supplier_Renew,
supplier_Creditlimit,
supplier_Comments
) values (
010203,
'DSHK COMPANY',
'G 7 JLN TUNKU HASSAN ',
'Seremban',
'Negeri Sembilan',
'70000',
'604-42449268',
'09-4265050',
'DSHK CO',
'04-2224568',
'DSHKCOMPANY#GMAIL.COM',
DATE '2024-08-23',
1000,
'COMPANY WILL CALL BEFORE SENDING THE GOODS'
);
If you don't then, for example, 604-42449268 will be parsed as 604 subtract 42449268 which gives the result −42448664.
Do not use a string literal such as '2024-08-23' for the date as Oracle will implicitly try to convert the string literal to a date using the NLS_DATE_FORMAT session paramter. The default Oracle DATE format depends on the territory you are in and for most territories that implicit conversion will fail. Either use a DATE literal or explicitly call TO_DATE('2024-08-23', 'YYYY-MM-DD') with a format model.
db<>fiddle here
Regarding the create statement: NUMBER is not a valid type. You can use e.g. int(11) unsigned.
Also, instead of varchara(500) you can use text. So the create statement should be:
CREATE TABLE suppliers
(
supplier_ID int(11) not null,
supplier_Name varchar (50) not null,
supplier_Addr varchar (100),
supplier_Town varchar (30),
supplier_State varchar (30),
supplier_Pcode varchar (15) not null,
supplier_Phone1 varchar (15),
supplier_Phone2 varchar (15),
supplier_Contact varchar (50),
supplier_Fax varchar (15),
supplier_Email varchar (40),
supplier_Renew DATE,
supplier_Creditlimit int(11),
supplier_Comments text,
PRIMARY KEY (supplier_ID)
);
For your insert statement you must enclose all non-numerical values in quotes, like this:
INSERT INTO suppliers (supplier_ID, supplier_Name, supplier_Addr, supplier_Town, supplier_State, supplier_Pcode, supplier_Phone1, supplier_Phone2, supplier_Contact, supplier_Fax, supplier_Email, supplier_Renew, supplier_Creditlimit,supplier_Comments)
VALUES (010203, 'DSHK COMPANY', 'G 7 JLN TUNKU HASSAN ', 'Seremban', 'Negeri Sembilan', 70000, '604-42449268', '09-4265050', 'DSHK CO', '04-2224568', 'DSHKCOMPANY#GMAIL.COM', '2024-08-23', 1000, 'COMPANY WILL CALL BEFORE SENDING THE GOODS');
I have a problem, I don't understand why do I have this error
Here's the code
CREATE TABLE Deposit
( ac_no Int(15),
customer_name Varchar(35),
branch_name Varchar(30),
Amount Int(10,2),
credit_date Date
);
Because integers don't have size nor precision.
Also, use VARCHAR2 instead of VARCHAR (that's not an error, but Oracle recommends so).
SQL> CREATE TABLE deposit
2 (
3 ac_no INT,
4 customer_name VARCHAR2 (35),
5 branch_name VARCHAR2 (30),
6 amount INT,
7 credit_date DATE
8 );
Table created.
SQL>
As #Littlefoot already said, INT data type has no precision or scale. If you want precision and scale in your numbers, then use number data type instead.
SQL> CREATE TABLE deposit
2 (
3 ac_no number(15),
4 customer_name VARCHAR2 (35),
5 branch_name VARCHAR2 (30),
6 amount number(10,2),
7 credit_date DATE
8 );
Table created.
SQL>
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).
Using: Application Express 18.1.0.00.45
Please note: I am very new to Oracle Apex and SQL.
For a project I have to create an Application straight in Apex.
I was trying to create a table that works with a Primary Key that auto-increments itself.
Yesterday I created my first Application with a page for user input in a table, a page with table display and filter option and was playing around with forms, dashboard and authentication methods.
I removed the app after playing to start the "Real Work", but I my enthusiasm went quickly away when I realized that I am doing something very wrong but am not sure what.. :)
After lots of googling / reading etc, I am still not sure what is wrong..
See below the code:
-- Create Sequence
CREATE SEQUENCE seq_odm_for_pk
START WITH 1
INCREMENT BY 1
CACHE 100;
-- Create Table for User Input
CREATE TABLE ODM_Progress_v1 (
-- General Details:
ID int NOT NULL AUTO_INCREMENT,
TRAINEE varchar(50) NOT NULL, --1
COACH varchar(50) NOT NULL, --2
STATUS varchar(50) NOT NULL, --3
REGION varchar(5) NOT NULL, --4
-- Actions:
ACTION_TAKEN varchar(100) NOT NULL, --5
ACTION_DETAILS varchar(250), --6
ACTIONED_BY varchar(50) NOT NULL, --7
ACTIONED_DATE DATE NOT NULL, --8
-- Constraints that perform checks for each column:
CONSTRAINT CHK_GeneralDetails CHECK (TRAINEE!=COACH AND (STATUS IN('New', 'In Progress', 'Completed')) AND (REGION IN('EMEA', 'APAC', 'AMER'))),
-- Set Primary Key (Trainee+Coach):
CONSTRAINT PK_ODMProgress PRIMARY KEY (TRAINEE,REGION,ID)
);
-- Create Trigger
CREATE trigger_for_pk_odm_progress
BEFORE INSERT ON ODM_Progress_v1
FOR EACH ROW
WHEN (new.ID is null)
BEGIN
select seq_odm_for_pk.nextval into :new.ID from DUAL;
-- :new.PK_ODMProgress := seq_odm_for_pk.nextval;
END;
The script finishes running with 3 errors, see below:
CREATE OR REPLACE SEQUENCE seq_odm_for_pk START WITH 1
INCREMENT BY 1 CACHE 100
ORA-00922: missing or invalid option
CREATE TABLE ODM_Progress_v1 ( -- General Details: ID int NOT
NULL AUTO_INCREMENT, TRAINEE varchar(50) NOT NULL, --1 COACH
varchar(50) NOT NULL, --2 STATUS varchar(50) NOT NULL, --3
REGION varchar(5) NOT NULL, --4 -- Actions: ACTION_TAKEN
varchar(100) NOT NULL, --5 ACTION_DETAILS varchar(250), --6
ACTIONED_BY varchar(50) NOT NULL, --7 ACTIONED_DATE DATE NOT NULL,
--8 -- Constraints that perform checks for each column: CONSTRAINT CHK_GeneralDetails CHECK (TRAINEE!=COACH AND (STATUS
IN('New', 'In Progress', 'Completed')) AND (REGION IN('EMEA', 'APAC',
'AMER'))), -- Set Primary Key (Trainee+Coach): CONSTRAINT
PK_ODMProgress PRIMARY KEY (TRAINEE,REGION,ID) )
ORA-00907: missing right parenthesis
CREATE OR REPLACE trigger_for_pk_odm_progress BEFORE INSERT ON
ODM_Progress_v1 FOR EACH ROW WHEN (new.ID is null) BEGIN SELECT
seq_odm_for_pk.nextval INTO :new.ID FROM DUAL; --
:new.PK_ODMProgress := seq_odm_for_pk.nextval; END;
ORA-00922: missing or invalid option
Can you please help me unravel this (to me, complete) mystery?
The final application should at least contain 1 table with primary key and sequence (created from scratch, see above) and have at least 2 pages, one is for data input, the other is for data display with use tabs or navigation menu.
Thank you in advance!
That code looks OK, more or less. Here you go:
SQL> CREATE SEQUENCE seq_odm_for_pk
2 START WITH 1
3 INCREMENT BY 1
4 CACHE 100;
Sequence created.
Table: I'm on 11g which doesn't support auto-incremented columns, so I removed that clause:
SQL> CREATE TABLE ODM_Progress_v1 (
2 -- General Details:
3 ID int NOT NULL AUTO_INCREMENT,
4 TRAINEE varchar(50) NOT NULL, --1
5 COACH varchar(50) NOT NULL, --2
6 STATUS varchar(50) NOT NULL, --3
7 REGION varchar(5) NOT NULL, --4
8 -- Actions:
9 ACTION_TAKEN varchar(100) NOT NULL, --5
10 ACTION_DETAILS varchar(250), --6
11 ACTIONED_BY varchar(50) NOT NULL, --7
12 ACTIONED_DATE DATE NOT NULL, --8
13 -- Constraints that perform checks for each column:
14 CONSTRAINT CHK_GeneralDetails CHECK (TRAINEE!=COACH AND (STATUS IN('New', 'In Progress', 'Completed')) AND (REG
ION IN('EMEA', 'APAC', 'AMER'))),
15 -- Set Primary Key (Trainee+Coach):
16 CONSTRAINT PK_ODMProgress PRIMARY KEY (TRAINEE,REGION,ID)
17 );
ID int NOT NULL AUTO_INCREMENT,
*
ERROR at line 3:
ORA-00907: missing right parenthesis
SQL> l3
3* ID int NOT NULL AUTO_INCREMENT,
SQL> c/auto_increment//
3* ID int NOT NULL ,
SQL> /
Table created.
Trigger contains an error in line #1: it is not "trigger_for" but "trigger for" (no underscore):
SQL> CREATE trigger_for_pk_odm_progress
2 BEFORE INSERT ON ODM_Progress_v1
3 FOR EACH ROW
4 WHEN (new.ID is null)
5 BEGIN
6 select seq_odm_for_pk.nextval into :new.ID from DUAL;
7 -- :new.PK_ODMProgress := seq_odm_for_pk.nextval;
8 END;
9 /
CREATE trigger_for_pk_odm_progress
*
ERROR at line 1:
ORA-00901: invalid CREATE command
SQL> l1
1* CREATE trigger_for_pk_odm_progress
SQL> c/er_/er /
1* CREATE trigger for_pk_odm_progress
SQL> l
1 CREATE trigger for_pk_odm_progress
2 BEFORE INSERT ON ODM_Progress_v1
3 FOR EACH ROW
4 WHEN (new.ID is null)
5 BEGIN
6 select seq_odm_for_pk.nextval into :new.ID from DUAL;
7 -- :new.PK_ODMProgress := seq_odm_for_pk.nextval;
8* END;
SQL> /
Trigger created.
SQL>
So:
sequence is OK, but - for vast majority of cases - a simple create sequence seq_odm_for_pk; is enough
for CREATE TABLE remove AUTO_INCREMENT (if you aren't on 12c)
trigger: remove underscore
Now, depending on where you executed those commands, you might have got errors. If you ran them in Apex SQL Workshop, run them one-by-one (and keep only one command in the window). Doing so, it should be OK.
Also, I've noticed that you used VARCHAR datatype - switch to VARCHAR2.
Finally, there's no use in constraining primary key columns with the NOT NULL clause - primary key will enforce it by default.
As of Apex itself: the way you described it, you should create an Interactive Report; the Wizard will create a report (to view data), along with a form (to insert/modify/delete data).
The query you ran in your script is not the same as you posted in your code, as can be read in error text.
Code for creating your sequence as you wrote it in your code should be fine:
CREATE SEQUENCE seq_odm_for_pk
START WITH 1
INCREMENT BY 1
CACHE 100;
As of 11g you cannot use AUTO_INCREMENT in Oracle when creating table. It is not even necessary since you're having a trigger populating your :new.ID with nextval from sequence. So, in your CREATE TABLE remove AUTO_INCREMENT for your ID and everything should be fine.
While creating a trigger you omitted TRIGGER keyword (CREATE OR REPLACE TRIGGER trigger_for_pk_odm_progress).
Also, I'm not sure if you did or did not put END; at the end of your create trigger command. if not, put it.
I hope that helped :)
I have this new problem after creating the Subscriber table of Subscriber_T type and its function i cant seem to insert any value in Subscrber table. The error, i keep getting is column not allowed here. But where???
These are the scripts and an insert record.
CREATE OR REPLACE TYPE Surnames_T AS OBJECT (
Surname varchar (20)
);
/
CREATE OR REPLACE TYPE listSurnames_T
AS Varray(4) of Surnames_T;
/
CREATE OR REPLACE TYPE listTels as object(
Tel number (12)
);
/
CREATE OR REPLACE TYPE listTels_T as Varray(5) of listTels;
/
CREATE OR REPLACE TYPE ADDRESS_T AS OBJECT (
NUM number (6),
STREET varchar (20),
TOWN varchar (20)
);
/
CREATE or replace type TAddress
as table of Address_T;
/
create or replace type Subscriber_T as object(
num_s number(6),
sName varchar(30),
surname listSurnames_T,
Adds TAddress,
DateOfBirth date,
phoneNo listTels_T,
member function Age return number
);
/
create table Subscribers of Subscriber_T(
CONSTRAINT subscriber_pk primary key (num_s)
)
nested table Adds store as Tab_Adds;
/
show errors
create or replace type body Subscriber_T as
member function Age return number is
calc_age number;
dob date;
diff date;
begin
select S.dateOfBirth into dob
from Subscribers S
where deref(S.num_s) = self
diff := sysdate - dob
calc_age := to_Char(diff, 'YYYY')
return cal_age;
end;
end;
/
show errors
insert into Subscribers values (34, Chloe, listSurnames_T(Surnames_T('Dave'), Surnames_T('Camille'), Surnames_T('Jones')),
TAddress(Address_T(10, 'ave Foch', 'Ravenwood'), Address_T(30, 'rue des pole', 'England')),
'10-11-1976',
listTels_T(listTels(5839550456), listTels(6834734567)));
I'm guessing the function is raising this problem but it shows no error on compilation.
Some issues:
You don't need to do a SELECT in the member function age.
You haven't quoted the name Chloe so its not a string.
You haven't converted the '10-11-1976' to a date.
You should not use VARCHAR as a datatype - use VARCHAR2 instead.
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE OR REPLACE TYPE Surnames_T AS OBJECT (
Surname varchar2 (20)
)
/
CREATE OR REPLACE TYPE listSurnames_T
AS Varray(4) of Surnames_T
/
CREATE OR REPLACE TYPE listTels as object(
Tel number (12)
)
/
CREATE OR REPLACE TYPE listTels_T as Varray(5) of listTels
/
CREATE OR REPLACE TYPE ADDRESS_T AS OBJECT (
NUM number (6),
STREET varchar2 (20),
TOWN varchar2 (20)
)
/
CREATE or replace type TAddress as table of Address_T
/
create or replace type Subscriber_T as object(
num_s number(6),
sName varchar2(30),
surname listSurnames_T,
Adds TAddress,
DateOfBirth date,
phoneNo listTels_T,
member function Age return number
)
/
create or replace type body Subscriber_T as
member function Age return number is
begin
return FLOOR( MONTHS_BETWEEN( SYSDATE, self.DateOfBirth )/12 );
end;
end;
/
create table Subscribers of Subscriber_T(
CONSTRAINT subscriber_pk primary key (num_s)
)
nested table Adds store as Tab_Adds
/
insert into Subscribers values (
34,
'Chloe',
listSurnames_T(
Surnames_T('Dave'),
Surnames_T('Camille'),
Surnames_T('Jones')
),
TAddress(
Address_T(10, 'ave Foch', 'Ravenwood'),
Address_T(30, 'rue des pole', 'England')
),
TO_DATE( '10-11-1976', 'DD-MM-YYYY' ),
listTels_T(
listTels(5839550456),
listTels(6834734567)
)
)
/
Query 1:
SELECT s.age() FROM subscribers s
Results:
| S.AGE() |
|---------|
| 37 |
Edit
I'm not sure why you are wrapping a VARCHAR2 in the Surnames_T type or a NUMBER in the ListTels type. You can omit these and just do:
CREATE OR REPLACE TYPE listSurnames_T
AS Varray(4) of VARCHAR2(20)
/
CREATE OR REPLACE TYPE listTels_T as Varray(5) of NUMBER(12)
/
and:
insert into Subscribers values (
34,
'Chloe',
listSurnames_T(
'Dave',
'Camille',
'Jones'
),
TAddress(
Address_T(10, 'ave Foch', 'Ravenwood'),
Address_T(30, 'rue des pole', 'England')
),
TO_DATE( '10-11-1976', 'DD-MM-YYYY' ),
listTels_T(
5839550456,
6834734567
)
)
/