Sorry in advance for the large question, but I'm working on a piece of coursework for a data management module using Oracle 12c, I don't have any prior experience in anything like this and I'm very confused as to what to do. The objective is to create a database for a fictional shipping company, I have created an ERD and exported that into a DDL to use for my database queries. I have been able to input data into the Ship table, however when I try to input data into any other tables it come up with Error ORA-00942: table or view does not exist, and I am unsure why. Below is the code exported from the DDL and some sample statements. Thank you in advance for any help that you can give.
CREATE TABLE Consignments
(
Consignment_Number CHAR (6 CHAR) NOT NULL ,
Supplier_Address VARCHAR2 (40 CHAR) ,
Delivery_Address VARCHAR2 (40 CHAR) ,
Volume NUMBER ,
Weight NUMBER ,
Ship_Ship_Number CHAR (4 CHAR) NOT NULL
) ;
ALTER TABLE Consignments ADD CONSTRAINT Consignments_PK PRIMARY KEY (
Consignment_Number ) ;
CREATE TABLE Officers
(
Officer_Number CHAR (6 CHAR) NOT NULL ,
Officer_Name VARCHAR2 (20 CHAR) ,
Officer_Address VARCHAR2 (40 CHAR) ,
Officer_DoB DATE ,
Officer_Rank VARCHAR2 (20 CHAR) ,
Ship_Ship_Number CHAR (4 CHAR) NOT NULL ,
Officers_Officer_Number CHAR (6 CHAR) ,
Officers_Ship_Ship_Number CHAR (4 CHAR)
) ;
ALTER TABLE Officers ADD CONSTRAINT Officers_PK PRIMARY KEY (
Officer_Number, Ship_Ship_Number ) ;
CREATE TABLE Port
(
Port_Number CHAR (3 CHAR) NOT NULL ,
Port_Name VARCHAR2 (20 CHAR) ,
Agent_Name VARCHAR2 (20 CHAR) ,
Arrival_Date DATE ,
Departure_Date DATE
) ;
ALTER TABLE Port ADD CONSTRAINT Port_PK PRIMARY KEY ( Port_Number ) ;
CREATE TABLE Relation_1
(
Voyage_Voyage_Number CHAR (6 CHAR) NOT NULL ,
Port_Port_Number CHAR (3 CHAR) NOT NULL
) ;
ALTER TABLE Relation_1 ADD CONSTRAINT Relation_1_PK PRIMARY KEY (
Voyage_Voyage_Number, Port_Port_Number ) ;
CREATE TABLE Ship
(
Ship_Number CHAR (4 CHAR) NOT NULL ,
Ship_Name CHAR (20 CHAR) ,
Voyage_Voyage_Number CHAR (6 CHAR) NOT NULL
) ;
CREATE UNIQUE INDEX Ship__IDX ON Ship
(
Voyage_Voyage_Number ASC
)
;
ALTER TABLE Ship ADD CONSTRAINT Ship_PK PRIMARY KEY ( Ship_Number ) ;
CREATE TABLE Voyage
(
Voyage_Number CHAR (6 CHAR) NOT NULL ,
Duration NUMBER ,
Ship_Ship_Number CHAR (4 CHAR) NOT NULL
) ;
CREATE UNIQUE INDEX Voyage__IDX ON Voyage
(
Ship_Ship_Number ASC
)
;
ALTER TABLE Voyage ADD CONSTRAINT Voyage_PK PRIMARY KEY ( Voyage_Number ) ;
ALTER TABLE Consignments ADD CONSTRAINT Consignments_Ship_FK FOREIGN KEY (
Ship_Ship_Number ) REFERENCES Ship ( Ship_Number ) ;
ALTER TABLE Relation_1 ADD CONSTRAINT FK_ASS_1 FOREIGN KEY (
Voyage_Voyage_Number ) REFERENCES Voyage ( Voyage_Number ) ;
ALTER TABLE Relation_1 ADD CONSTRAINT FK_ASS_2 FOREIGN KEY (
Port_Port_Number ) REFERENCES Port ( Port_Number ) ;
ALTER TABLE Officers ADD CONSTRAINT Officers_Officers_FK FOREIGN KEY (
Officers_Officer_Number, Officers_Ship_Ship_Number ) REFERENCES Officers (
Officer_Number, Ship_Ship_Number ) ;
ALTER TABLE Officers ADD CONSTRAINT Officers_Ship_FK FOREIGN KEY (
Ship_Ship_Number ) REFERENCES Ship ( Ship_Number ) ;
ALTER TABLE Ship ADD CONSTRAINT Ship_Voyage_FK FOREIGN KEY (
Voyage_Voyage_Number ) REFERENCES Voyage ( Voyage_Number ) ;
ALTER TABLE Voyage ADD CONSTRAINT Voyage_Ship_FK FOREIGN KEY (
Ship_Ship_Number ) REFERENCES Ship ( Ship_Number ) ;
INSERT INTO Officers (Officer_Number, Officer_Name, Officer_Address,
Officer_DoB, Officer_Rank, Ship_Ship_Number)
VALUES (000000, 'J. Cena', ('1 Elder Row, Glasgow'), 1970-01-01, 'Captain',
1111)
Related
can have in this case foreign key duplicated value? Or better just have an index on column?
Here is my table structure:
CREATE TABLE customers(
id INT (10) NOT NULL,
name VARCHAR (50) NOT NULL,
city VARCHAR (50) NOT NULL
);
CREATE TABLE orders(
cus_id INT (10) NOT NULL ,
order_date DATETIME NOT NULL
);
CREATE TABLE products(
id INT (5) NOT NULL,
product_name VARCHAR(50) NOT NULL,
product_price INT(10) NOT NULL
);
But in orderitems table (Where I have stored the ordered products, the customer can have multiple products ordered so the foreign key value (cus_id) can be duplicated)
CREATE TABLE ordered_items(
id INT (10) NOT NULL,
cus_id INT (10) NOT NULL,
product_id INT(5) NOT NULL
);
ALTER TABLE customers ADD CONSTRAINT customer_id PRIMARY KEY ( id ) ;
ALTER TABLE orders ADD CONSTRAINT customers_id_fr FOREIGN KEY ( cus_id ) REFERENCES customers ( id );
ALTER TABLE ordered_items ADD CONSTRAINT ordered_items_fr FOREIGN KEY ( cus_id ) REFERENCES customers ( id );
EDIT:
Sorry the ordered_items table have a unique ID column as well.
Yes! you can have multiple value of cus_id inside ordered_items. however, your intention is better served if you replace cus_id by order_id from orders, Thus your relationship would sound like,
a **customer** can have multiple *orders*,
an **order** can have multiple *order items*,
and an **order item** can only have single *product*
your sql would look like this
CREATE TABLE customers(
id INT (10) NOT NULL,
name VARCHAR (50) NOT NULL,
city VARCHAR (50) NOT NULL
);
ALTER TABLE customers ADD CONSTRAINT customer_id PRIMARY KEY ( id ) ;
CREATE TABLE orders(
id INT (5) NOT NULL,
cus_id INT (10) NOT NULL ,
order_date DATETIME NOT NULL
);
ALTER TABLE orders ADD CONSTRAINT order_id PRIMARY KEY ( id ) ;
ALTER TABLE orders ADD CONSTRAINT customers_id_fr FOREIGN KEY ( cus_id ) REFERENCES customers ( id );
CREATE TABLE products(
id INT (5) NOT NULL,
product_name VARCHAR(50) NOT NULL,
product_price DOUBLE NOT NULL
);
ALTER TABLE products ADD CONSTRAINT product_id PRIMARY KEY ( id ) ;
CREATE TABLE ordered_items(
id INT (10) NOT NULL,
order_id INT (10) NOT NULL,
product_id INT(5) NOT NULL
);
ALTER TABLE ordered_items ADD CONSTRAINT ordrItm_id PRIMARY KEY ( id ) ;
ALTER TABLE ordered_items ADD CONSTRAINT ordrItm_order_frK FOREIGN KEY ( order_id ) REFERENCES orders ( id );
ALTER TABLE ordered_items ADD CONSTRAINT ordrItm_prd_frK FOREIGN KEY ( product_id) REFERENCES products ( id );
I am having trouble identifying what is causing the following errors in my code:
2 0.00 CREATE TABLE BRANCH ( BRANCH_ID NUMBER(3) , BRANCH_NAME VARC ORA-00904: "LIBRARY_ID": invalid identifier -
3 0.00 CREATE TABLE COPY ( BOOK_ID NUMBER(3) , BRACH_ID NUMBER(3) , ORA-00904: "BRANCH_ID": invalid identifier -
4 0.00 CREATE TABLE PATRON ( PATRON_ID NUMBER(3) , FIRST_NAME VARCH ORA-02263: need to specify the datatype for this column -
5 0.01 CREATE TABLE CHECKOUT ( CHECKOUT_ID NUMBER(3) , COPY_ID NUMB ORA-00942: table or view does not exist -
6 0.00 CREATE TABLE WAITLIST ( BOOK_ID NUMBER(3) , PATRON_ID NUMBER ORA-00942: table or view does not exist
I cannot pinpoint where the lack of a datatype is or why my identifier is considered invalid. I am still learning and appreciate any help. Thank you guys in advance. Here is my SQL code:
CREATE TABLE LIBRARY_SYSTEM
( LIBRARY_ID NUMBER(3)
, LIBRARY_NAME VARCHAR2(50)
, STREET_ADDRESS VARCHAR2 (60)
, CITY VARCHAR2 (30)
, STATE CHAR (2)
, ZIPCODE NUMBER (5)
, PHONE NUMBER (10)
, MANAGER_NAME VARCHAR2(40)
, CONSTRAINT LIBARY_PK PRIMARY KEY (LIBRARY_ID)
);
CREATE TABLE BRANCH
( BRANCH_ID NUMBER(3)
, BRANCH_NAME VARCHAR2(50)
, STREET_ADDRESS VARCHAR2 (60)
, CITY VARCHAR2 (30)
, STATE CHAR (2)
, ZIPCODE NUMBER (5)
, PHONE NUMBER (10)
, BRANCH_MANAGER VARCHAR2(40)
, LIBARY_ID NUMBER(3)
, CONSTRAINT BRANCH_PK PRIMARY KEY (BRANCH_ID)
, CONSTRAINT BRANCH_LIBARY_FK FOREIGN KEY (LIBRARY_ID) REFERENCES LIBRARY_SYSTEM(LIBRARY_ID)
);
CREATE TABLE COPY
( BOOK_ID NUMBER(3)
, BRACH_ID NUMBER(3)
, CONSTRAINT COPY_BOOK_PK PRIMARY KEY (BOOK_ID, BRANCH_ID)
, CONSTRAINT COPY_BOOK_FK FOREIGN KEY (BOOK_ID) REFERENCES BOOK(BOOK_ID)
, CONSTRAINT COPY_BRANCH_FK FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH(BRANCH_ID)
);
CREATE TABLE PATRON
( PATRON_ID NUMBER(3)
, FIRST_NAME VARCHAR2(40)
, LAST_NAME VARCHAR2(40)
, STREET_ADDRESS VARCHAR2 (60)
, CITY VARCHAR2 (30)
, STATE CHAR (2)
, ZIPCODE NUMBER (5)
, PHONE NUMBER (10)
, FEE_BALANCE
, CONSTRAINT PATRON_PK PRIMARY KEY (PATRON_ID)
);
CREATE TABLE CHECKOUT
( CHECKOUT_ID NUMBER(3)
, COPY_ID NUMBER(3)
, PATRON_ID NUMBER(3)
, OUT_DATE DATE
, DUE_DATE DATE
, RETURN_DATE DATE
, CONSTRAINT CHECKOUT_PK PRIMARY KEY (CHECKOUT_ID)
, CONSTRAINT CHECKOUT_COPY_FK FOREIGN KEY (COPY_ID) REFERENCES COPY(COPY_ID)
, CONSTRAINT CHECKOUT_PATRON_FK FOREIGN KEY (PATRON_ID) REFERENCES PATRON(PATRON_ID)
);
CREATE TABLE WAITLIST
( BOOK_ID NUMBER(3)
, PATRON_ID NUMBER(3)
, BRANCH_ID NUMBER(3)
, ON_DATE DATE
, OFF_DATE DATE
, RETURN_DATE DATE
, CONSTRAINT COPY_BOOK_FK FOREIGN KEY (BOOK_ID) REFERENCES BOOK(BOOK_ID)
--, CONSTRAINT CHECKOUT_PATRON_FK FOREIGN KEY (PATRON_ID) REFERENCES PATRON(PATRON_ID)---
--, CONSTRAINT COPY_BRANCH_FK FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH(BRANCH_ID)
);
--sequences
CREATE SEQUENCE LIBRARY_SEQ INCREMENT BY 1 START WITH 1;
CREATE SEQUENCE BRANCH_SEQ INCREMENT BY 1 START WITH 1;
CREATE SEQUENCE PATRON_SEQ INCREMENT BY 1 START WITH 1;
--triggers
CREATE OR REPLACE TRIGGER INSERT_LIBRARY
BEFORE INSERT ON LIBRARY_SYSTEM
FOR EACH ROW
DECLARE
NEW_ID NUMBER;
BEGIN
SELECT LIBRARY_SEQ.NEXTVAL INTO NEW_ID FROM DUAL;
:NEW.LIBRARY_ID := NEW_ID;
END;
/
COMMIT;
CREATE OR REPLACE TRIGGER INSERT_BRANCH
BEFORE INSERT ON BRANCH
FOR EACH ROW
DECLARE
NEW_ID NUMBER;
BEGIN
SELECT BRANCH_SEQ.NEXTVAL INTO NEW_ID FROM DUAL;
:NEW.BRANCH_ID := NEW_ID;
END;
/
COMMIT;
CREATE OR REPLACE TRIGGER INSERT_PATRON
BEFORE INSERT ON PATRON
FOR EACH ROW
DECLARE
NEW_ID NUMBER;
BEGIN
SELECT PATRON_SEQ.NEXTVAL INTO NEW_ID FROM DUAL;
:NEW.PATRON_ID := NEW_ID;
END;
/
COMMIT;
There seem to be just a few misspellings:
CREATE TABLE BRANCH
( BRANCH_ID NUMBER(3)
, BRANCH_NAME VARCHAR2(50)
, STREET_ADDRESS VARCHAR2 (60)
, CITY VARCHAR2 (30)
, STATE CHAR (2)
, ZIPCODE NUMBER (5)
, PHONE NUMBER (10)
, BRANCH_MANAGER VARCHAR2(40)
**, LIBARY_ID NUMBER(3)**
, CONSTRAINT BRANCH_PK PRIMARY KEY (BRANCH_ID)
, CONSTRAINT BRANCH_LIBARY_FK FOREIGN KEY (LIBRARY_ID) REFERENCES LIBRARY_SYSTEM(LIBRARY_ID)
);
CREATE TABLE COPY
( BOOK_ID NUMBER(3)
, **BRACH_ID** NUMBER(3)
, CONSTRAINT COPY_BOOK_PK PRIMARY KEY (BOOK_ID, BRANCH_ID)
, CONSTRAINT COPY_BOOK_FK FOREIGN KEY (BOOK_ID) REFERENCES BOOK(BOOK_ID)
, CONSTRAINT COPY_BRANCH_FK FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH(BRANCH_ID)
);
Someone could help me to find my error ?
CREATE OR REPLACE TRIGGER Assign_Prix
BEFORE INSERT OR UPDATE
ON DetailCommande
FOR EACH ROW
DECLARE
v_NOECHELLE Integer; v_PRIX Integer;
BEGIN
SELECT NOECHELLE INTO v_NOECHELLE
FROM ECHELLE
WHERE ddebut<=:new.ddebut AND dfin>=:new.dfin;
SELECT Min(Prix) INTO v_Prix
FROM DETAILECHELLE
WHERE noechelle = v_Echelle AND qtemin<=:new.qtemin;
:NEW.PRIX:= v_Prix;
END;
CREATE TABLE Echelle
(
NoEchelle NUMBER NOT NULL,
CodeProduit VARCHAR2 (15) NOT NULL ,
DDebut DATE NOT NULL,
DFin DATE NOT NULL
)
;
ALTER TABLE Echelle
ADD CONSTRAINT Echelle_PK PRIMARY KEY ( NoEchelle ) ;
ALTER TABLE Echelle
ADD CONSTRAINT Echelle_FK_Produit FOREIGN KEY (CodeProduit)
REFERENCES Produit(CodeProduit);
CREATE TABLE DetailEchelle
(
NoEchelle NUMBER NOT NULL ,
NoDetail NUMBER NOT NULL ,
QteMin NUMBER NOT NULL ,
Prix NUMBER NOT NULL
)
;
ALTER TABLE DetailEchelle
ADD CONSTRAINT DetailEchelle_PK PRIMARY KEY ( NoEchelle, NoDetail);
ALTER TABLE DetailEchelle
ADD CONSTRAINT DetailEchelle_FK_Echelle FOREIGN KEY (NoEchelle)
REFERENCES Echelle(NoEchelle);
CREATE TABLE Produit
(
CodeProduit VARCHAR2 (15) NOT NULL ,
CodeCategorie VARCHAR2 (5) NOT NULL ,
Nom VARCHAR2 (30) NOT NULL ,
Description VARCHAR2 (100) ,
CoutFixe NUMBER ,
LimiteSupCF NUMBER ,
Delai NUMBER
)
;
ALTER TABLE Produit
ADD CONSTRAINT Produit_PK PRIMARY KEY ( CodeProduit ) ;
ALTER TABLE Produit
ADD CONSTRAINT Produit_FK_Categorie FOREIGN KEY (CodeCategorie)
REFERENCES Categorie(CodeCategorie);
CREATE TABLE DetailCommande
(
NoCommande NUMBER NOT NULL ,
LigneCommande NUMBER NOT NULL ,
Qte NUMBER NOT NULL ,
Message VARCHAR2 (100) NOT NULL,
CodeProduit VARCHAR2 (15) NOT NULL ,
CodeCouleur VARCHAR2 (4) NOT NULL ,
PrixUnitVendu NUMBER,
CFVendu NUMBER
)
;
ALTER TABLE DetailCommande
ADD CONSTRAINT DetailCommande_PK PRIMARY KEY ( LigneCommande, NoCommande ) ;
ALTER TABLE DetailCommande
ADD CONSTRAINT DetailCommande_FK_Commande FOREIGN KEY (NoCommande)
REFERENCES Commande(NoCommande);
ALTER TABLE DetailCommande
ADD CONSTRAINT DetailCommande_FK_Produit FOREIGN KEY (CodeProduit, CodeCouleur)
REFERENCES ProduitCouleur(CodeProduit, CodeCouleur);
Your trigger is defined on the DetailCommande table. This table does not have a column named ddebut, dfin, qtemin or prix. Your trigger, therefore, cannot reference those columns in the :new pseudo-record since they don't exist in the table.
Unfortunately, if those columns don't exist in the DetailCommande table, I'm hard-pressed to guess at what your trigger is supposed to do so I have no idea what the proper syntax would be.
Create Table Person (
ID integer primary key
, [name] character(15) not null
, address character(15)
, phone integer
);
Create Table Surveyor (
surveyID integer
, certificationDate Datetime
, ID integer
, primary KEY (surveyID,ID)
, FOREIGN KEY (ID) REFERENCES Person
);
Create Table Points (
PntID integer primary key
, N float not null
, E float not null
, Height float not null
);
Create Table Polygon (
polyID integer primary key
, area float not null
, atleastonepoint integer not null
, foreign key (atleastonepoint) references Points(PntID)
, check (area > 0)
);
Create Table Block (
blockID integer
, blockName character (15) not null
, polyID integer
, polyLength float not null
, primary KEY (blockID,polyID)
, onemunicipalAuthority character (15) not null
, foreign key (polyID) references polygon
, check (polyLength > 0)
);
Create Table Parcel (
parcelname character (15)
, blockID integer
, polyID integer
, primary KEY (parcelname,blockID,polyID)
, foreign key (polyID) references polygon
, foreign key (blockID) REFERENCES block
);
ERROR : Relationship must be on the same number of fields with the same data types, when I try to implement the last one , CREATE => Parcel
thanx in advance
Try this command:
Create Table Parcel (
parcelname character (15)
, blockID integer
, polyID integer
, primary KEY (parcelname,blockID,polyID)
, foreign key (polyID) references polygon
, foreign key (blockID,polyID) REFERENCES block
);
I'm trying to write a command to create a trigger that generates the composite primary key. This pk is in turn based on two fk.
I'll write example tables to be more specific.
(Table I'm working on)
CREATE TABLE DB.MESSAGE (
TEXT CLOB NOT NULL,
SUBJECT VARCHAR2(2000) NOT NULL,
MSG_TYPE NUMBER(1) NOT NULL,
MAIL_ID NUMBER(10) NOT NULL
)
;
ALTER TABLE DB.MESSAGE ADD CONSTRAINT MSG_PK PRIMARY KEY ( MSG_TYPE, MAIL_ID ) ;
ALTER TABLE DB.MESSAGE ADD
(
CONSTRAINT MESSAGE_TYPE_ID_FK
FOREIGN KEY ( MSG_TYPE )
REFERENCES DB.TYPES ( TYPE_ID )
);
ALTER TABLE DB.MESSAGE ADD
(
CONSTRAINT MESSAGE_MAIL_FK
FOREIGN KEY ( MAIL_ID )
REFERENCES DB.EML_MAIL ( MAILTO_ID )
);
(Referenced tables)
CREATE TABLE DB.TYPES (
TYPE_ID NUMBER(13) NOT NULL,
NAME VARCHAR2(10) NOT NULL
)
;
CREATE TABLE DB.MAIL (
MAIL_ID NUMBER(10) NOT NULL,
MAIL VARCHAR2(350) NOT NULL
)
;
My query so far
create or replace
TRIGGER DB.TRG_MESSAGE_ID
BEFORE INSERT ON DB.MESSAGE
FOR EACH ROW
BEGIN
IF INSERTING THEN
IF :NEW."MSG_ID" IS NULL THEN
SELECT DB.TYPES.TYPE_ID ??????
INTO :NEW."MSG_ID" FROM dual;
END IF;
END IF;
END;
EDIT: So the thinking behind this question was that there would be a separated column with a concatenations of both keys that compose the composite key.
A friend told me this is wrong, you just put both fields as pk and that's that. Is this true?