Error loading a file in oracle - 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:-)

Related

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 do i resolve ORA-00904: "CI"."CATALOG_ITEM_ID": invalid identifier Error at Line: 2 Column: 52

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

ORA-02253: constraint specification not allowed here

create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000),
date_logged date
CONSTRAINT abc FOREIGN KEY (voter_ID) REFERENCES voters(voter_ID)
)
The table works when i create it without date element. But when i add the date element to it says:
ORA-02253: constraint specification not allowed here
The table works when i create it without date element
create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000), -- comma
CONSTRAINT abc FOREIGN KEY (voter_ID) REFERENCES voters(voter_ID)
)
You have to add , before constraint:
create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000),
date_logged date, -- here
CONSTRAINT abc FOREIGN KEY (voter_ID) REFERENCES voters(voter_ID)
)
I would also reconsider datatype of log_id/voter_id as (NUMBER/INTEGER).
Try this:
create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000) CONSTRAINT abc REFERENCES voters(voter_ID)
)

ORA-00911: invalid character

I am trying to write my first table create script with column definitions. I have tried several things to get this error to go away, but to no avail. Please take a look to see if their is anything obvious in my code below:
CREATE TABLE CD_TYPE (
CD_TYPE VARCHAR2(4) PRIMARY KEY,
CD_FORMAT VARCHAR2(10)
);
CREATE TABLE MANUFACTURER (
MANUFACTURER_NUM NUMBER(3) PRIMARY KEY,
MANUFACTURER_NAME VARCHAR2(30)
);
CREATE TABLE CD_TITLE (
CD_NUM NUMBER(4) PRIMARY KEY,
TITLE VARCHAR2(30),
MANUFACTURER_NUM VARCHAR2(30) FOREIGN KEY,
CD_TYPE VARCHAR2(4) FOREIGN KEY,
ACQUIRED_DATE DATE,
ORIGINAL CHAR(1)
);
CREATE TABLE CD_SN (
CD_NUM NUMBER(4) PRIMARY KEY FOREIGN KEY,
SERIAL_NUM VARCHAR2(30) PRIMARY KEY,
NUM_LIC_REMAIN NUMBER(2)
);
The problem is with the columns reading
MANUFACTURER_NUM VARCHAR2(30) FOREIGN KEY,
You forgot to specify what table these columns refer to.
You to that with a REFERENCES clause:
create table cd_title (
cd_num number(4) primary key,
title varchar2(30),
manufacturer_num REFERENCES MANUFACTURER,
cd_type REFERENCES CD_TYPE,
acquired_date date,
original char(1)
);
Alternatively, you can specify the name for the foreign key with a CONSTRAINT ... FOREIGN KEY (...) REFERENCES ... clause:
create table cd_title (
cd_num number(4) primary key,
title varchar2(30),
manufacturer_num,
cd_type,
acquired_date date,
original char(1),
--
CONSTRAINT cd_title_fk1 FOREIGN KEY (manufacturer_num) REFERENCES manufacturer,
CONSTRAINT cd_title_fk2 FOREIGN KEY (cd_type ) REFERENCES cd_type
)

Missing left parenthesis

Missing left parenthesis on line 9..can't see the error!
create table EDITIONS(
ID number NOT NULL,
ID_VOLUME number,
Publishing varchar2(20),
Year number(4),
Language varchar2(10),
Pages number(4),
CONSTRAINT pk_editions PRIMARY KEY,
constraint fk_editions_volume foreign key (id_volume)
references volume(id),
constraint editions_publishing,
constraint editions_year,
constraint editions_language,
constraint editions_pages,
);
Try something like this:
create table EDITIONS (
ID number NOT NULL primary key
ID_VOLUME number,
Publishing varchar2(20),
Year number(4),
Language varchar2(10),
Pages number(4),
constraint fk_editions_volume foreign key (id_volume) references volume(id)
);
I assume you are using Oracle because of the varchar2(). You should tag questions with the correct database.

Resources