ORA-00955 during SQL Table creation - sql

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" ( ...

Related

Getting an error of invalid identifier when try to create relationship between two tables in Oracle SQL developer? [duplicate]

This question already has an answer here:
A CREATE statement with quoted fields in Oracle
(1 answer)
Closed 1 year ago.
This is my device table
CREATE TABLE "DEVICE" (
"IMEI_Number" varchar(15),
"Device_Model" varchar(30),
"Device_Description" varchar(500),
"Assigned_Sim_Number" varchar(11),
"Activation_Date" timestamp,
"Deactivation_Date" timestamp,
"Manufacturer_ID" int,
"Customer_ID" int,
PRIMARY KEY ("IMEI_Number")
);
Then I have the manufacturer table which is
CREATE TABLE "MANUFACTURER" (
"Manufacturer_ID" int,
"Manufacturer_Name" varchar(30),
PRIMARY KEY ("Manufacturer_ID")
);
and I trying to create a relationship between these and getting the ORA-00904: "MANUFACTURER_ID": invalid identifier
My relationship code is
ALTER TABLE DEVICE
ADD FOREIGN KEY (Manufacturer_ID) REFERENCES MANUFACTURER(Manufacturer_ID);
That's just awful. Don't use double quotes when creating objects in Oracle as you'll have to use them every time you reference those objects, and match letter case every time.
SQL> alter table device add constraint fk_mf foreign key ("Manufacturer_ID")
2 references manufacturer ("Manufacturer_ID");
Table altered.
SQL>
A better option would be
SQL> create table device (
2 imei_number varchar2(15),
3 device_model varchar2(30),
4 device_description varchar2(500),
5 assigned_sim_number varchar2(11),
6 activation_date timestamp,
7 deactivation_date timestamp,
8 manufacturer_id int,
9 customer_id int,
10 primary key (imei_number)
11 );
Table created.
SQL> create table manufacturer (
2 manufacturer_id int,
3 manufacturer_name varchar2(30),
4 primary key (manufacturer_id)
5 );
Table created.
SQL> alter table device add constraint fk_mf foreign key (manufacturer_id)
2 references manufacturer (manufacturer_id);
Table altered.
SQL>
(Note also VARCHAR2 datatype; use that instead of VARCHAR).
By default, Oracle stores names in uppercase into data dictionary, but you can reference them using any case you want (upper, lower, mixed). If you do use double quotes, then you have to use their names exactly as during creation phase.
The columns in the tables you've created are surrounded by double-quotes, making their names case-sensitive, while the alter statement does not use quotes, and thus can't match the case-sensitive column name.
The best practice would be to drop these quotes and make the column names case-insensitive. If this is not an option, you could use the same quotes in the alter statement too:
ALTER TABLE DEVICE
ADD FOREIGN KEY ("Manufacturer_ID") REFERENCES MANUFACTURER("Manufacturer_ID");
-- Here ---------^---------------^--------------------------^---------------^

creating a table with Foreign key in it gives error ORA-00904: : invalid identifier in oracle 10g [duplicate]

This question already has answers here:
Creating table via SQL Command Line, invalid identifier
(3 answers)
Closed 3 years ago.
Image of the code with table, query and error
I have oracle 10g installed on my computer. I have created a table in it named STUDENT and this STUDENT table has a primary key called RNO and now I want to create another table named FEE and make this RNO key into a foreign key in FEE table with the following query:
CREATE TABLE FEE ( RNO NUMBER(2), Amount number(20) Not Null, Date varchar2(10) Not Null, Receipt Number(10) Not Null, CONSTRAINT FEEFK FOREIGN KEY (RNO) REFERENCES STUDENT (RNO));
Now I have done all I could to correct it but just can't seem to find any problem or error with this query above. The Query gives the following error in Oracle 10g:
ORA-00904: : invalid identifier
Column name can't be DATE, it is reserved for datatype. Rename it to, say, CDATE.
SQL> CREATE TABLE student (rno NUMBER (2) PRIMARY KEY);
Table created.
SQL> CREATE TABLE FEE
2 (
3 RNO NUMBER (2),
4 Amount NUMBER (20) NOT NULL,
5 cDate VARCHAR2 (10) NOT NULL,
6 Receipt NUMBER (10) NOT NULL,
7 CONSTRAINT FEEFK FOREIGN KEY (RNO) REFERENCES STUDENT (RNO)
8 );
Table created.
SQL>
Use double qoutes "Date" or rename your column Date as some other name like DateColumn as Date is a reserved name fpr date types in oracle

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

Oracle creating Table using super class attribute as primary key

I have a sales_person_type defined as follows:
CREATE OR REPLACE TYPE sales_person_type UNDER person_type (
salesAppointments sales_person_appointments
);
this is a subclass of the person_type defined as such:
CREATE OR REPLACE TYPE person_type AS OBJECT (
personID NUMBER,
forename VARCHAR2(30),
surname VARCHAR2(20),
dateOfBirth DATE
) NOT FINAL;
I'm trying to create a table of sales persons and specifying the primary key as the super class attribute like this:
CREATE TABLE sales_person_table OF sales_person_type (
PRIMARY KEY (personID),
OBJECT IDENTIFIER IS PRIMARY KEY)
NESTED TABLE salesAppointments STORE AS sale_appointment_table (
(PRIMARY KEY(NESTED_TABLE_ID, appointmentID))
ORGANIZATION INDEX COMPRESS)
RETURN AS LOCATOR
But I'm getting this error:
SQL Error: ORA-02330: datatype specification not allowed
02330. 00000 - "datatype specification not allowed"
*Cause: An attempt was made to specify the data type in the column
constraint specification of an object table.
I'm think it's because im trying to assign the primary key as the super class attibute? Is this the correct syntax for this?
Cheers.
EDIT:
Works now, thanks Dimitry.
Check the syntax of CREATE TABLE for OID clause:
SQL> CREATE OR REPLACE TYPE person_type AS OBJECT (
2 personID NUMBER,
3 forename VARCHAR2(30),
4 surname VARCHAR2(20),
5 dateOfBirth DATE
6 ) NOT FINAL;
7 /
SQL> create or replace type sale_appointment is object
2 (
3 appointmentID integer
4 );
5 /
Тип создан.
SQL> create type sales_person_appointments as table of sale_appointment;
2 /
SQL> CREATE OR REPLACE TYPE sales_person_type UNDER person_type (
2 salesAppointments sales_person_appointments
3 );
4 /
SQL> CREATE TABLE sales_person_table OF sales_person_type (
2 PRIMARY KEY (personID)
3 )
4 OBJECT IDENTIFIER IS PRIMARY KEY
5 NESTED TABLE salesAppointments STORE AS sale_appointment_table (
6 (PRIMARY KEY(NESTED_TABLE_ID, appointmentID))
7 ORGANIZATION INDEX COMPRESS)
8 RETURN AS LOCATOR
9 /
Table created.