How do i resolve ORA-00904: "CI"."CATALOG_ITEM_ID": invalid identifier Error at Line: 2 Column: 52 - oracle

I need to create a query that will display title, publisher, ISBN, release date, number of pages, and whether it's carried in any library ("Yes" or "No") For each book in the catalog and sort results by title. I can't have any duplicates either.
The query I have is
select distinct ci.title, ci.publisher, ci.RELEASE_DATE, b.ISBN, b.pages from
catalog_item ci, book b
left join physical_item pi on pi.CATALOG_ITEM_ID = ci.CATALOG_ITEM_ID
left join branch b on b.branch_id = pi.branch_id
left join library l on b.library_id=l.library_id
order by ci.TITLE
;
I get the following error
ORA-00904: "CI"."CATALOG_ITEM_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 2 Column: 52
I don't see how it is invalid
My DDL is as follows:
CREATE TABLE CUSTOMER (Customer_ID NUMBER PRIMARY KEY, Customer_Firstname
VARCHAR2(30), Customer_Lastname VARCHAR2(30), Customer_Street VARCHAR2(30),
Customer_City VARCHAR2(30), Customer_State VARCHAR2(20), Customer_Zip
VARCHAR2(10));
CREATE TABLE BRANCH (Branch_ID NUMBER PRIMARY KEY, Branch_Name VARCHAR2(30),
Branch_Phone VARCHAR2 (30), Branch_Address VARCHAR2(30), LIBRARY_ID NUMBER);
CREATE TABLE PHYSICAL_ITEM (Physical_Item_ID NUMBER PRIMARY KEY, Branch_ID NUMBER,
Catalog_Item_ID NUMBER, Copy_Number NUMBER, Date_Purchased DATE);
CREATE TABLE CATALOG_ITEM (Catalog_Item_ID NUMBER PRIMARY KEY, Title VARCHAR2 (30),
Description VARCHAR2(30), Publisher VARCHAR2 (30), Release_Date DATE, Type
VARCHAR2(30));
CREATE TABLE DVD (Catalog_Item_ID NUMBER, Length VARCHAR2(30));
CREATE TABLE BOOK (Catalog_Item_ID NUMBER, ISBN VARCHAR2(13), Pages NUMBER);
CREATE TABLE TRANSACTION (Transaction_ID NUMBER PRIMARY KEY, Date_checkout DATE,
Date_Due DATE, Date_Returned DATE, Library_Card_ID NUMBER, Physical_Item_ID NUMBER);
CREATE TABLE LIBRARY (Library_ID NUMBER PRIMARY KEY, Library_Name VARCHAR2 (30),
Library_Phone VARCHAR2(30), Library_Address VARCHAR2(30));
CREATE TABLE LIBRARY_CARD (Library_Card_ID NUMBER PRIMARY KEY, Library_ID NUMBER,
Customer_ID NUMBER, Card_Number VARCHAR2(30), PIN VARCHAR2(8), Date_Expire DATE);
ALTER TABLE BRANCH ADD CONSTRAINT Library_ID_FK FOREIGN KEY (Library_ID) REFERENCES
LIBRARY (Library_ID);
ALTER TABLE PHYSICAL_ITEM ADD CONSTRAINT Branch_ID_FK FOREIGN KEY (Branch_ID)
REFERENCES BRANCH (Branch_ID);
ALTER TABLE PHYSICAL_ITEM ADD CONSTRAINT Catalog_Item_ID_FK FOREIGN KEY
(Catalog_Item_ID) REFERENCES CATALOG_ITEM (Catalog_Item_ID);
ALTER TABLE TRANSACTION ADD CONSTRAINT Library_Card_ID_FK FOREIGN KEY
(Library_Card_ID) REFERENCES LIBRARY_CARD (Library_Card_ID);
ALTER TABLE TRANSACTION ADD CONSTRAINT Physical_Item_ID_FK FOREIGN KEY
(Physical_Item_ID) REFERENCES PHYSICAL_ITEM (Physical_Item_ID);
ALTER TABLE LIBRARY_CARD ADD CONSTRAINT Library_ID_FK1 FOREIGN KEY (Library_ID)
REFERENCES LIBRARY (Library_ID);
ALTER TABLE LIBRARY_CARD ADD CONSTRAINT Customer_ID_FK FOREIGN KEY (Customer_ID)
REFERENCES CUSTOMER (Customer_ID);
ALTER TABLE DVD ADD CONSTRAINT Catalog_Item_ID_FK1 FOREIGN KEY (Catalog_Item_ID)
REFERENCES CATALOG_ITEM (Catalog_Item_ID);
ALTER TABLE BOOK add CONSTRAINT Catalog_Item_ID_FK2 FOREIGN KEY (Catalog_Item_ID)
REFERENCES CATALOG_ITEM (Catalog_Item_ID);

Related

ORA-02264: name already used by an existing constraint how to get this solved?

CREATE TABLE report(
report_id NUMBER(5),
description VARCHAR2(200) NOT NULL,
status VARCHAR2(200) NOT NULL,
bicycle_id NUMBER(5),
cust_id NUMBER(5),
staff_id NUMBER(5),
CONSTRAINT ad_reportid_pk PRIMARY KEY (report_id),
CONSTRAINT ad_bicycle_fk FOREIGN KEY (bicycle_id) REFERENCES bicycle(bicycle_id),
CONSTRAINT ad_custid_fk FOREIGN KEY (cust_id) REFERENCES customer(cust_id),
CONSTRAINT ad_staffid_fk FOREIGN KEY (staff_id) REFERENCES staff(staff_id)
);
Above is the SQL script when I'm creating a table and it thrown me error with ORA-02264. I am neither drop the table nor creating that table. Tried to run this script
SELECT * FROM user_constraints WHERE CONSTRAINT_NAME = 'ad_bicycleid_fk'
and it showed no data.
All object names are stored in uppercase unless you mention it in double quotes while creating an object.
Try this:
SELECT * FROM user_constraints
WHERE upper(CONSTRAINT_NAME) = upper('ad_bicycleid_fk');
Here's an example which shows one option you might do that.
First, "dummy" tables which are being referenced by columns in the report table (why do I need them? Because create table report would fail otherwise):
SQL> create table bicycle
2 (bicycle_id number(5) constraint pk_bic primary key);
Table created.
SQL> create table customer
2 (cust_id number(5) constraint pk_cus primary key);
Table created.
SQL> create table staff
2 (staff_id number(5) constraint pk_sta primary key);
Table created.
The report table:
SQL> create table report
2 (report_id number(5),
3 description varchar2(200) not null,
4 status varchar2(200) not null,
5 bicycle_id number(5),
6 cust_id number(5),
7 staff_id number(5),
8 --
9 constraint pk_rep primary key (report_id),
10 constraint fk_rep_bic foreign key (bicycle_id)
11 references bicycle (bicycle_id),
12 constraint fk_rep_cus foreign key (cust_id)
13 references customer (cust_id),
14 constraint fk_rep_sta foreign key (staff_id)
15 references staff (staff_id)
16 );
Table created.
SQL>
Everything is OK.
Note the way I named constraints, especially foreign keys whose names show - at least approximately - which table references which another table, e.g. FK_REP_BIC - it is a Foreign Key from table REPort and it references the BICycle table. I'm not saying that you must do it that way, but - Google a little bit, read about good practices and find the one you prefer the most.
As of the constraint you couldn't find:
SQL> select owner, constraint_type, table_name
2 from user_constraints
3 where constraint_name = 'FK_REP_BIC';
OWNER C TABLE_NAME
--------------- - ---------------
SCOTT R REPORT
SQL>

Alter Table , adding a Foreign key constraint at a column ORA-02253

Hello there i am studying for the Oracle Certification of SQL Associate .
And trying to do some Examples .
I have an issue where i cannot find easily a reference on this .
create table employees
(employee_id number NOT NULL,
first_name varchar(20),
last_name varchar(30),
constraint employee_pk primary key (employee_id));
create table employee_notes
(employee_notes_id number,
employee_notes varchar(500),
constraint pk_employee_notes primary key (employee_notes_id));
create sequence employee_notes_seq start with 1 increment by 1
Now i want to add a new column at employee_notes table with a foreign key constraint .
I can't find out in syntax where is the problem .
****alter table employee_notes
add employee_id number
constraint fk_employee_notes foreign key (employee_id) references employees (employee_id);****
i get this error
ORA-02253: constraint specification not allowed her
I also tried to alter the table and add column and then the constraint but cannot
alter table employee_notes
add employee_id number;
--
alter table employee notes
add constraint fk_employee_notes foreign key (employee_id) references employees (employee_id);
ORA-02253: constraint specification not allowed here
I would like to know how i can do this
and why this syntax is wrong :)
You did something wrong because - it works OK:
SQL> CREATE TABLE employees
2 (
3 employee_id NUMBER NOT NULL,
4 first_name VARCHAR (20),
5 last_name VARCHAR (30),
6 CONSTRAINT employee_pk PRIMARY KEY (employee_id)
7 );
Table created.
SQL>
SQL> CREATE TABLE employee_notes
2 (
3 employee_notes_id NUMBER,
4 employee_notes VARCHAR (500),
5 CONSTRAINT pk_employee_notes PRIMARY KEY (employee_notes_id)
6 );
Table created.
SQL> ALTER TABLE employee_notes ADD employee_id NUMBER;
Table altered.
SQL> ALTER TABLE employee_notes ADD CONSTRAINT fk_employee_notes
2 FOREIGN KEY (employee_id)
3 REFERENCES employees (employee_id);
Table altered.
SQL>
When you use ALTER TABLE ... ADD in order to add a column and a constraint in one statement, do the following:
-- notice the () and the comma!
alter table employee_notes
add (
employee_id number
, constraint fk_employee_notes
foreign key (employee_id) references employees (employee_id)
) ;
That should do the trick. See dbfiddle. The syntax is similar to CREATE TABLE, where you'd also write all column names, data types etc in (), separated by commas.

Create table field with foreign key constraint

I want to create a table department:
COLUMN NAME DATATYPE SIZE CONSTRAINT
dept_id number 4 Primary key
prod_id number 4 Foreign key
I tried this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id number(4) foreign key);
It shows error. How can I add a foreign key constraint to this table?
A foreign key defines a relationship between your table DEPARTMENT and another table with a primary key. It means, you cannot create a row in DEPARTMENT with a PROD_ID of 1234 unless there is a pre-existing row in the designated parent table with a value of 1234 as its primary key.
So do you have such an existing parent table? if so you need to include its name in the foreign key definition. Otherwise you must create it.
Let's say the parent table is PRODUCT:
create table product (
prod_id number(4) primary key
, name varchar2(32) not null
);
Then you can create DEPARTMENT with a foreign key like this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id references PRODUCT );
Yep, that's all the syntax you need: it automatically creates a column PROD_ID with the same datatype and precision as the primary key column of the referenced table. More verbose syntax is available. Read the Oracle SQL documentation to find out more.
I assume that the other table is named other_table_name and that it contains a primary key named prod_id.
CREATE Department (
dept_id number(4) primary key,
prod_id number(4) REFERENCES other_table_name (prod_id)
);
or a different syntax
CREATE Department (
dept_id number(4) primary key,
prod_id number(4)
...
CONSTRAINT fk_prod_id
FOREIGN KEY (prod_id)
REFERENCES other_table_name (prod_id)
);

how to create a foreign key in Oracle

How to link the MgrId in ManagerProject to EmpId in the Employee table ?
This is wat I tried :
CREATE TABLE Employee(EmpId varchar2(5),
EmpName varchar2(25),
DeptId varchar2(3),
Salary Number(8),
Constraint PK_addn primary key (EmpId, DeptId),
Constraint fk_Department foreign key (DeptId) references Department (DeptId));
But the second table failed to be created :
CREATE TABLE ManagerProject(ProjId varchar2(4),
MgrId varchar2(5),
StartDate Date,
EndDate Date,
Constraint fk_managerproject foreign key (MgrId) references Employee (EmpId),
Constraint PK_Managerproject Primary key(ProjId, MgrId, StartDate));
It displays
ORA-02270: no matching unique or primary key for this column-list
The error message says that you are trying to create a FK referencing a column on which there is no Unique or Primary Key constraint.
Assuming that you don't want to add the column DeptId to ManagerProject, you need to add a unique key on employee:
alter table Employee add constraint empId_UK unique ( empId)
But this strongly depends on what your schema should be.
If you want to add the column DeptId to ManagerProject, you will need to edit your FK to both use EmpId and DeptId in referencing employee.

Error loading a file in oracle

im working with oracle right now, but im getting a problem when a load this .sql file
CREATE TABLE TIENDA(
ID_TIEND NUMBER,
NOMB_TIEND VARCHAR2(40),
RIF VARCHAR2 (15),
TELF VARCHAR2 (15),
FAX VARCHAR2 (15),
CAPACIDAD_PROD NUMBER,
ID_CIUD NUMBER,
CONSTRAINT ID_TIEND_PK PRIMARY KEY (ID_TIEND),
);
CREATE TABLE CATEGORIA(
ID_CAT NUMBER,
NOMB_CAT VARCHAR2(20),
CONSTRAINT ID_CAT_PK PRIMARY KEY (ID_CAT)
);
CREATE TABLE SUBCATEGORIA(
ID_SUB NUMBER,
NOMB_SUB VARCHAR2(20),
ID_CAT NUMBER,
CONSTRAINT ID_SUB_PK PRIMARY KEY (ID_SUB),
);
CREATE TABLE MARCA(
ID_MARCA NUMBER,
NOMB_MARCA VARCHAR2 (20),
CONSTRAINT ID_MARCA_PK PRIMARY KEY (ID_MARCA)
);
CREATE TABLE PROVEEDOR(
ID_PROV NUMBER,
NOMBRE VARCHAR2(30),
RIF VARCHAR2(15),
TELF VARCHAR2(15),
ID_CIUD NUMBER,
CONSTRAINT ID_PROV_PK PRIMARY KEY (ID_PROV),
);
CREATE TABLE ESTADO(
ID_EST NUMBER,
NOMB_EST VARCHAR2(20),
SIGLAS VARCHAR2 (2),
CONSTRAINT ID_EST_PK PRIMARY KEY (ID_EST)
);
CREATE TABLE CIUDAD(
ID_CIUD NUMBER,
NOMB_CIUD VARCHAR2(20),
SIGLAS VARCHAR2(2),
ID_EST NUMBER,
CONSTRAINT ID_CIUD_PK PRIMARY KEY (ID_CIUD),
);
CREATE TABLE PROVEE(
FECHA_REC DATE,
FECHA_ENV DATE,
CANT NUMBER,
ID_PROV NUMBER,
ID_ALM NUMBER,
ID_PROD NUMBER,
COSTO_PROD FLOAT,
COSTO_ENV FLOAT,
COSTO_TOTAL FLOAT,
CONSTRAINT FECHA_PK PRIMARY KEY (FECHA_REC,FECHA_ENV),
);
CREATE TABLE ABASTECE (
FECHA_REC DATE,
FECHA_DESC DATE,
ID_ALM NUMBER,
ID_TIEND NUMBER,
ID_PROD NUMBER,
CANT NUMBER,
CONSTRAINT FECHA_PK PRIMARY KEY (FECHA_REC,FECHA_DESC),
);
CREATE TABLE PRODUCTO(
ID_PROD NUMBER,
NOMBRE_PROD VARCHAR2(30),
ID_MARCA NUMBER,
ID_SUB NUMBER,
PVP FLOAT,
CONSTRAINT ID_PROD_PK PRIMARY KEY (ID_PROD),
);
CREATE TABLE TIENDA(
ID_TIEND NUMBER,
NOMB_TIEND VARCHAR2(40),
RIF VARCHAR2 (15),
TELF VARCHAR2 (15),
FAX VARCHAR2 (15),
CAPACIDAD_PROD NUMBER,
ID_CIUD NUMBER,
CONSTRAINT ID_TIEND_PK PRIMARY KEY (ID_TIEND),
);
CREATE TABLE ALMACEN(
ID_ALM NUMBER,
NOMB_ALM VARCHAR2(40),
RIF VARCHAR2(15),
TELF VARCHAR2(15),
DIMENSIONES VARCHAR2(15),
CAPACIDAD_PROD NUMBER,
ID_CIUD NUMBER,
CONSTRAINT ID_ALM_PK PRIMARY KEY (ID_ALM),
);
CREATE TABLE TIENE(
FECHA DATE,
ID_TIEND NUMBER,
ID_PROD NUMBER
CANT_VEND NUMBER,
CANT_EXIST NUMBER,
NOPA NUMBER,
NMRD NUMBER,
CONSTRAINT FECHA_PK PRIMARY KEY (FECHA),
);
CREATE TABLE ALMACENA(
FECHA DATE,
ID_ALM NUMBER,
ID_PROD NUMBER,
CANT_DESP NUMBER,
CANT_EXIST NUMBER,
NOPAL NUMBER,
NMRS NUMBER,
CONSTRAINT FECHA_PK PRIMARY KEY (FECHA),
);
ALTER TABLE SUBCATEGORIA
ADD CONSTRAINT ID_SUB_FK
FOREIGN KEY (ID_CAT)
REFERENCES CATEGORIA(ID_CAT);
ALTER TABLE CIUDAD
ADD CONSTRAINT ID_EST_FK
FOREIGN KEY (ID_EST)
REFERENCES ESTADO(ID_EST);
ALTER TABLE PROVEE
ADD CONSTRAINT ID_PROV_FK
FOREIGN KEY (ID_PROV)
REFERENCES PROVEEDOR(ID_PROV);
ALTER TABLE PROVEE
ADD CONSTRAINT ID_ALM_FK
FOREIGN KEY (ID_ALM)
REFERENCES ALMACEN(ID_ALM);
ALTER TABLE PROVEE
ADD CONSTRAINT ID_PROD_FK
FOREIGN KEY (ID_PROD)
REFERENCES PRODUCTO(ID_PROD);
ALTER TABLE ABASTECE
ADD CONSTRAINT ID_TIEND_FK
FOREIGN KEY (ID_TIEND)
REFERENCES TIENDA(ID_TIEND);
ALTER TABLE ABASTECE
ADD CONSTRAINT ID_PROD_FK
FOREIGN KEY (ID_PROD)
REFERENCES PRODUCTO(ID_PROD);
ALTER TABLE ABASTECE
ADD CONSTRAINT ID_ALM_FK
FOREIGN KEY (ID_ALM)
REFERENCES ALMACEN(ID_ALM);
ALTER TABLE PRODUCTO
ADD CONSTRAINT ID_SUBC_FK
FOREIGN KEY (ID_SUB)
REFERENCES SUBCATEGORIA(ID_SUB);
ALTER TABLE TIENDA
ADD CONSTRAINT ID_CIUD_FK
FOREIGN KEY (ID_CIUD)
REFERENCES CIUDAD(ID_CIUD);
ALTER TABLE ALMACEN
ADD CONSTRAINT ID_CIUD_FK
FOREIGN KEY (ID_CIUD)
REFERENCES CIUDAD(ID_CIUD);
ALTER TABLE PROVEEDOR
ADD CONSTRAINT ID_CIUD_FK
FOREIGN KEY (ID_CIUD)
REFERENCES CIUDAD(ID_CIUD);
ALTER TABLE TIENE
ADD CONSTRAINT ID_TIEND_FK
FOREIGN KEY (ID_TIEND)
REFERENCES TIENDA(ID_TIEND);
ALTER TABLE TIENE
ADD CONSTRAINT ID_PROD_FK
FOREIGN KEY (ID_PROD)
REFERENCES PRODUCTO(ID_PROD);
ALTER TABLE ALMACENA
ADD CONSTRAINT ID_ALM_FK
FOREIGN KEY (ID_ALM)
REFERENCES ALMACEN(ID_ALM);
ALTER TABLE ALMACENA
ADD CONSTRAINT ID_PROD_FK
FOREIGN KEY (ID_PROD)
REFERENCES PRODUCTO(ID_PROD);
When i load it in the terminal with this command "SQL> start D:\lab2.sql" this error comes out
A little help? thanks.
In some of your commands - e.g. the first one - you seem to have removed a line before the ending parenthesis. But you have not at the same time removed the trailing comma from the preceding line.
I cant say if this will solve all your errors - but certainly some:-)

Resources