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)
);
Related
all the details of the references tables are filled.My Create Statement is :-
Im Using Oracle 10g express Edition
create table Sales_order
(
S_order_no varchar2(6) Primary key check( S_order_no like '0%'),
S_order_date Date ,
Client_no Varchar2(25) references as CLIENT_MASTER ,
Dely_add Varchar2(6) ,
Salesman_no Varchar2(6) references as SALES_MASTER ,
Dely_type Char(1) check( Dely_type in ('p','f')) default 'f',
Billed_yn Char(1),
Dely_date Date check ( Dely_date < s_order_date),
Order_status Varchar2(10) check (Order_status in ('in process','fulfilled','back order','canceled'))
)
I got the oracle error "ORA-00903: invalid table name"
A few errors.
as is invalid while referencing other tables
default should be before check constraint (line #8)
column-level check constraint can't reference other columns in the table (line 10), but table-level can (as #a_horse_with_no_name showed on a link in the comment and I replicated here). Alternatively, you could use a trigger.
SQL> create table client_master (client_no varchar2(25) primary key);
Table created.
SQL> create table sales_master (salesman_no varchar2(25) primary key);
Table created.
SQL> create table sales_order
2 (
3 s_order_no varchar2(6) primary key check( s_order_no like '0%'),
4 s_order_date date ,
5 client_no varchar2(25) references client_master ,
6 dely_add varchar2(6) ,
7 salesman_no varchar2(6) references sales_master ,
8 dely_type char(1) default 'f' check( dely_type in ('p','f')) ,
9 billed_yn char(1),
10 dely_date date,
11 order_status varchar2(10) check (order_status in ('in process','fulfilled','back order','canceled')),
12 check ( dely_date < s_order_date)
13 );
Table created.
SQL>
I'm fairly new to SQL and I am trying to create a trigger to calculate overtime worked by subtracting hours contracted (GRADE_HOURS) in the COMPANY_GRADE table from hours worked (TIMESHEET_HOURS) in the COMPANY_TIMESHEET table. This will then go into the TIMESHEET_OT column of the COMPANY_TIMESHEET table whenever a row is inserted into the COMPANY_TIMESHEET table.
The tables concerned are as follows:
CREATE TABLE COMPANY_TIMESHEET
(
timesheet_ID number(3) constraint timesheet_pk primary key,
Timesheet_emp number(3) constraint timesheet_empnotnull not null references company_employee,
Timesheet_wc date constraint timesheet_wcnotnull not null,
Timesheet_hours number(2),
Timesheet_OT number(2) default 0,
Timesheet_approved number(3) references company_employee
);
CREATE TABLE COMPANY_GRADE
(
grade_ID number(3) constraint grade_pk primary key,
Grade_rate number(5,2) constraint grade_ratenotnull not null,
Grade_hours number(2)
) ;
CREATE TABLE COMPANY_EMPLOYEE
(
emp_ID number(3) constraint emp_pk primary key,
Emp_firstname varchar2(50) constraint emp_firstnamenotnull not null,
Emp_surname varchar2(50),
Emp_department number(2) constraint employeeFKdepartment references company_department,
emp_street varchar2(50),
emp_town varchar2(50),
emp_district varchar2(50),
Emp_grade number(3) default 4 constraint checkempgrade check (Emp_grade between 1 and 9) references company_grade,
Emp_site varchar2(30) default 'LONDON'
);
I would appreciate any help as I have been trying for many hours now only to be met by error after error.
I have tried numerous variations of this as a starting block to try and pass the hours contracted into a variable to then subtract from another variable:
CREATE OR REPLACE TRIGGER trg_ot
BEFORE INSERT ON company_timesheet
FOR EACH ROW
DECLARE t_contracted NUMBER;
BEGIN
SELECT grade_hours INTO t_contracted
FROM company_grade
WHERE company_employee.emp_id = :new.timesheet_emp;
END;
/
CREATE OR REPLACE TRIGGER trg_ot
BEFORE INSERT ON company_timesheet
FOR EACH ROW
DECLARE t_contracted NUMBER;
BEGIN
SELECT g.grade_hours
INTO t_contracted
FROM company_grade g
INNER JOIN
company_employee e
ON ( e.emp_grade = g.grade_id )
WHERE e.emp_id = :new.timesheet_emp;
:new.timesheet_ot := :new.timesheet_hours - t_contracted;
END;
/
I've been stuck at creating tables.
Here is the code I am stuck at how to make the code work in loan_type to write office worker or non office worker
create table loaner
(
loan_id number(5) primary key,
loan_type VARCHAR2 (16),
loan_start_date date,
loan_end_date date,
)
create table office_worker
(
worker_id number(5) primary_key,
loan_id number(5) references loaner(loan_id),
worker_name varchar2(50)
)
create table nonoffice_worker
(
nonworker_id number(5) primary_key,
loan_id number(5) references loaner(loan_id),
nonworker_name varchar2(50)
);
commit;
You can't create a constrain to check that with the existing table structures. A common way to do it is like this:
create table loaner (
loan_id number(5) primary key,
loan_type VARCHAR2 (16),
loan_start_date date,
loan_end_date date,
constraint loaner_uk unique (loan_id, loan_type)
);
create table office_worker (
worker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
worker_name varchar2(50),
constraint office_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint office_worker_loan_type_chk check (loan_type = 'OFFICE')
);
create table nonoffice_worker (
nonworker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
nonworker_name varchar2(50),
constraint nonoffice_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint nonoffice_worker_loan_type_chk check (loan_type = 'NONOFFICE')
);
That is:
Create a redundant UNIQUE constraint in (load_id, loan_type) in the first table.
Add loan_type to the subtype tables and base the foreign key on (loan_id, loan_type).
Add a check constraint to each subtype table to ensure the correct loan_type is used.
You can also add a check constraint to your table, but you must check that column loan_type only contains desired values(office worker or non office worker), else this won't work:
alter table loaner add (CONSTRAINT chk_loan_type CHECK
(loan_type='office worker' or loan_type='non office worker'));
I keep getting this error when I attempt to create tables. This is what I have so far:
CREATE TABLE Club
( Club_ID NUMBER(7) NOT NULL
, Club_Name VARCHAR2(35) NOT NULL
, Street_Addr VARCHAR2(40) NOT NULL
, City VARCHAR2(20) NOT NULL
, State CHAR(2) NOT NULL
, Zipcode NUMBER(7) NOT NULL
, Phone NUMBER(10) NOT NULL
, CONSTRAINT club_pk PRIMARY KEY (Club_ID)
)
CREATE TABLE Member
( Member_ID NUMBER(10) NOT NULL
, First_Name VARCHAR2(20) NOT NULL
, Last_Name VARCHAR2(30) NOT NULL
, Street_Addr VARCHAR2(40) NOT NULL
, City VARCHAR2(20) NOT NULL
, State CHAR(2) NOT NULL
, Zipcode NUMBER(7) NOT NULL
, Phone NUMBER(10) NOT NULL
, CONSTRAINT member_pk PRIMARY KEY (Member_ID)
) ;
I don't get any errors from each individually, but as soon as I put them together like this I run into problems. Adding a semicolon between the two CREATE TABLE sections causes a different error ("ORA-00911: invalid character"). What should I be doing differently?
Thanks!
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.