oracle : Missing right parenthesis - sql

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>

Related

Column not allowed here pl sql

i am trying to create a procedure to add a new sale, but for some reason i am getting an error that says the column not allowed here
Here is the code:
CREATE OR REPLACE PROCEDURE AddSale
(
In_CID customers.CID%Type,
In_EmpID Employees.EmpID%Type,
In_car_num cars.car_num%Type,
In_Pay_M Sales.Pay_method%Type,
In_Payment_duration Sales.Pay_duration%Type,
In_Payment_type Sales.S_type%Type,
In_sh_id Sales.sh_id%Type,
)
IS
number := 0;
Id_sale number;
BEGIN
INSERT INTO Sales(Sales_ID, CID, EmpID, car_num, S_time,
S_type, Pay_method, Pay_duration, sh_id)
VALUES (Sales_ID_Seq.nextval, In_CID, In_EmpID, In_car_num, SYSDATE,
In_Payment_type, In_Payment_M, In_Payment_duration, In_sh_id);
And here is the table definition:
create table Sales
(
Pay_duration number(15),
Car_num number(15),
Sales_ID number(15),
S_type varchar2(15),
CID number(15),
Pay_method varchar2(32),
S_time timestamp,
EmpID number(15),
Sh_id number(15),
discount number(5,3),
Disc_status varchar2(20),
ApprovedBy number(15)
);
I've formatted the code in your request. You will see that proper formatting is a great help when coding.
In your declaration you have:
In_Pay_M
In your insert statement you have:
In_Payment_M
Then, there is a comma too many after your last parameter:
In_sh_id Sales.sh_id%Type,
)

Oracle SQL assign specific values

So I am working on my coursework and I am sort of stuck as to what to do for this one part. The question is this :
Flatpack(FlatpackID, Name, Colour, Type, UnitPrice)
FlatpackID should be generated by the DBMS
Name has at most 20 characters and should be not null
Colour is optional
Type is one of (Office, Kitchen, Bedroom, General)
UnitPrice should be between 5.00 and 500.00
Okay so the one I need help with is the one that is in bold/italic i.e. "Type is one of (Office, Kitchen, Bedroom, General")
How exactly am I declaring this within my
CREATE TABLE FLATPACK (
);
I asked and I was told it is only allowed those values and nothing else.
Any help would be greatly appreciated! Thanks
One method is a check constraint:
constraint chk_flatpack_type check ( Type in ('Office', 'Kitchen', 'Bedroom', 'General') );
Another option is to set up foreign key constraint to a reference table, and have the reference table only contain these values.
This is one option (having types restricted by a check constraint):
SQL> CREATE TABLE flatpack
2 (
3 flatpackid NUMBER CONSTRAINT pk_fp PRIMARY KEY,
4 name VARCHAR2 (20) NOT NULL,
5 colour VARCHAR2 (20),
6 TYPE VARCHAR2 (20)
7 CONSTRAINT ch_ty CHECK
8 (TYPE IN ('Office',
9 'Kitchen',
10 'Bedroom',
11 'General')),
12 unitprice NUMBER CONSTRAINT ch_pr CHECK (unitprice BETWEEN 5 AND 500)
13 );
Table created.
SQL>
Another, better (why? More flexible, as you can add any type you want, without altering the table) option is to create a table which will be referenced by the TYPE column:
SQL> CREATE TABLE flatpack_type (TYPE VARCHAR2 (20) CONSTRAINT pk_ty PRIMARY KEY);
Table created.
SQL> CREATE TABLE flatpack
2 (
3 flatpackid NUMBER CONSTRAINT pk_fp PRIMARY KEY,
4 name VARCHAR2 (20) NOT NULL,
5 colour VARCHAR2 (20),
6 TYPE VARCHAR2 (20)
7 CONSTRAINT fk_fp_ty REFERENCES flatpack_type (TYPE),
8 unitprice NUMBER CONSTRAINT ch_pr CHECK (unitprice BETWEEN 5 AND 500)
9 );
Table created.
SQL> insert into flatpack_type --> valid values
2 select 'Office' from dual union all
3 select 'Kitchen' from dual union all
4 select 'Bedroom' from dual union all
5 select 'Genral' from dual;
4 rows created.
As of the ID, you could use an identity column (if on 12c or higher), or a standard option for lower versions - a trigger which uses a sequence:
SQL> create sequence seq_fp;
Sequence created.
SQL> create or replace trigger trg_bi_fp
2 before insert on flatpack
3 for each row
4 begin
5 :new.flatpackid := seq_fp.nextval;
6 end;
7 /
Trigger created.
SQL>

SQL : can not create a table

I do the following:
use oracle developer
create table loan
(
barcode number (20) not null ,
borrowernumber number (7) ,
loancurrentdate date ,
loanreturndate date ,
loanreserveorder number (20) ,
paymentdate date ,
borrower_borrowernumber number not null
);
alter table loan add constraint loan_pk primary key (barcode) ;
Cannot create a table, then change it ...
create table loan
(
loanno INT not null,
loandate date not null,
loanreturndate date null,
loanreserve number (20) null,
paymentdate date null,
);
alter table loan add constraint loan_pk primary key (loanno) ;
I get the following error. Why am I getting this?
Error report
SQL Error: ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
It also shows "invalid identifier"
Error report:
SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
Or should I delete paymentdate?
After delete the wrong comma, the table created.
Both examples you posted are correct (apart from that superfluous comma at the end of the second CREATE TABLE statement). This are SQL*Plus examples, but - as SQL Developer emulates it well, I believe that they should work just fine there too.
The first one:
SQL> create table loan
2 (
3 barcode number (20) not null ,
4 borrowernumber number (7) ,
5 loancurrentdate date ,
6 loanreturndate date ,
7 loanreserveorder number (20) ,
8 paymentdate date ,
9 borrower_borrowernumber number not null
10 );
Table created.
SQL> alter table loan add constraint loan_pk primary key (barcode) ;
Table altered.
The second one:
SQL> drop table loan;
Table dropped.
SQL> create table loan
2 (
3 loanno INT not null,
4 loandate date not null,
5 loanreturndate date null,
6 loanreserve number (20) null,
7 paymentdate date null
8 );
Table created.
SQL> alter table loan add constraint loan_pk primary key (loanno) ;
Table altered.
SQL>
What do you exactly mean by saying "Cannot create a table, then change it ..." after the first CREATE TABLE? Any error? If so, which one?
"ORA-00942: table or view does not exist" is probably raised by ALTER TABLE as you can't alter it if it doesn't exist (but the mystery is why you can't create it).
"ORA-00904: : invalid identifier" means that you used a column name which doesn't exist in the table, such as
SQL> alter table loan modify xxx number;
alter table loan modify xxx number
*
ERROR at line 1:
ORA-00904: "XXX": invalid identifier
If possible, do the same as I did - copy/paste SQL*Plus session so that we could see what you did and how Oracle responded.
This should work
drop table loan;
create table loan
(
loanno INT not null,
loandate date not null,
loanreturndate date null,
loanreserve number (20) null,
paymentdate date null
);

ORA-00955 during SQL Table creation

I have been getting the error during a table creation. I know it means that the table name needs to change, but I don't see any object with the same name. A copy of the .lst is below.
Thanks
SQL> CREATE TABLE CUSTOMERtable
2 (
3 CUSTOMERID INT NOT NULL,
4 CUSTNAME VARCHAR2 (50) NOT NULL,
5 ADDRESS VARCHAR2 (100) NOT NULL,
6 PHONENUMBER VARCHAR2 (10) NOT NULL,
7 CONSTRAINT IDS_CUST_PK PRIMARY KEY (CUSTOMERID)
8 );
CREATE TABLE CUSTOMERtable
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL>
SQL> CREATE TABLE RENTALStable
2 (
3 RENTALID INT NOT NULL,
4 OUTDATE DATE NOT NULL,
5 INDATE DATE NOT NULL,
6 LATEFEE INT,
7 DAMAGEFEE INT,
8 REWINDFEE INT,
9 ID_CUSTOMER INT,
10 CONSTRAINT RentalsTable_IDS_PK PRIMARY KEY (RENTALID),
11 FOREIGN KEY (ID_CUSTOMER) REFERENCES CUSTOMERtable(CUSTOMERID)
12 );
Table created.
This should find the object that's creating the problem:
select *
from user_objects
where object_name = 'CUSTOMERTABLE'
Notice that your statement, even if you write CUSTOMERtable ( upper and lower case), will try to create a table named CUSTOMERTABLE (upper case).
If you want to keep two objects with the same names but different case (and it seems not a good idea to me) you should use double quotes:
CREATE TABLE "CUSTOMERtable" ( ...

Average Rating Trigger ORACLE SQL

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?