Coding a COUNT in a VIEW in Oracle - oracle

I am stuck in a programming problem in Oracle, that I am trying to solve. I am suppose to create a View that will give me the Number of unreserved/Available seats on FBN001 ON 27TH October 2016
This is the Flight Table:
CREATE TABLE Flight(
Flight_ID VARCHAR(25),
Route_ID VARCHAR(25),
Airplane_ID VARCHAR(10),
Departure_Time DATE,
Arrival_Time DATE,
CONSTRAINT FlightIDPK PRIMARY KEY(Flight_ID),
CONSTRAINT RouteIDFK FOREIGN KEY(Route_ID) REFERENCES Route(Route_ID),
CONSTRAINT AirplaneIDFK FOREIGN KEY(Airplane_ID) REFERENCES Airplane(Airplane_ID));
This is the Airplane Table:
CREATE TABLE Airplane(
Airplane_ID VARCHAR(10),
Capacity NUMBER(5) NOT NULL,
Model VARCHAR(15) NOT NULL,
CONSTRAINT AirplaneIDPK PRIMARY KEY(Airplane_ID));
This is the Booking Table:
CREATE TABLE Booking(
Booking_ID NUMBER(10),
Flight_ID VARCHAR(25),
Customer_ID NUMBER(10),
Payment_Method VARCHAR(10) NOT NULL,
CreditCard_Details NUMBER(18),
CONSTRAINT BookingIDPK PRIMARY KEY(Booking_ID),
CONSTRAINT FlightIDFK FOREIGN KEY(Flight_ID) REFERENCES Flight(Flight_ID),
CONSTRAINT CustomerIDFK FOREIGN KEY(Customer_ID) REFERENCES Customer(Customer_ID));
I thought i would use the COUNT function to count the Bookings
CREATE VIEW ViewB AS(
SELECT Flight.Route_ID, Flight.Departure_Time, Airplane.Capacity, COUNT(Flight_ID) AS NumberOfBooking
FROM Flight, Airplane, Booking
WHERE Flight.Airplane_ID = Airplane.Airplane_ID
AND Flight.Route_ID = 'FBN001'
GROUP BY Flight.Route_ID, Airplane.Capacity);
But that did not work,
It is giving me the Error
Error report -
SQL Error: ORA-00918: column ambiguously defined
00000 - "column ambiguously defined"
*Cause:
*Action:
What could be the solution?
Thank you

You didn't add a statement to properly join the booking table:
CREATE VIEW ViewB AS(
SELECT Flight.Route_ID, Flight.Departure_Time, Airplane.Capacity, COUNT(Flight_ID) AS NumberOfBooking
FROM Flight, Airplane, Booking
WHERE Flight.Airplane_ID = Airplane.Airplane_ID
AND Flight.Flight_ID=Booking.Flight_ID
AND Flight.Route_ID = 'FBN001'
GROUP BY Flight.Route_ID, Airplane.Capacity);

Related

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

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.

Coding a SUM in a View in Oracle

I am stuck in a progaramming problem in Oracle, that I am trying to solve.
I am suppose to create a View that will give me the total hours flown EVER, for crew of the FBN001 on the 27th october 2016
This is the Flight Table:
CREATE TABLE Flight(
Flight_ID VARCHAR(25),
Route_ID VARCHAR(25),
Airplane_ID VARCHAR(10),
Departure_Time DATE,
Arrival_Time DATE,
CONSTRAINT FlightIDPK PRIMARY KEY(Flight_ID),
CONSTRAINT RouteIDFK FOREIGN KEY(Route_ID) REFERENCES Route(Route_ID),
CONSTRAINT AirplaneIDFK FOREIGN KEY(Airplane_ID) REFERENCES Airplane(Airplane_ID));
And this is the Crew Table:
CREATE TABLE Crew(
Crew_ID NUMBER(10),
Flight_ID VARCHAR(25),
Role VARCHAR(25) NOT NULL,
Employee_ID NUMBER(10),
Hours NUMBER(10),
CONSTRAINT CrewIDPK PRIMARY KEY(Crew_ID),
CONSTRAINT EmployeeIDFK FOREIGN KEY(Employee_ID) REFERENCES Employee(Employee_ID));
I thought i would use the SUM Function:
CREATE VIEW ViewC AS(
SELECT Flight.Route_ID, Crew.Employee_ID, SUM(Hours)AS TotalHoursFlew
FROM Crew, Flight
WHERE Crew.Flight_ID = Flight.Flight_ID
AND Route_ID = 'FBN001');
but that isnt working?
what could be the solution?
Thank you
You need a GROUP BY:
CREATE VIEW ViewC AS(
SELECT Flight.Route_ID, Crew.Employee_ID, SUM(Hours)AS TotalHoursFlew
FROM Crew, Flight
WHERE Crew.Flight_ID = Flight.Flight_ID
AND Route_ID = 'FBN001'
GROUP BY Flight.Route_ID, Crew.Employee_ID);

Triggers in oracle PL/SQL

I am trying to do a trigger that updates a sum of all credits on update of the grade or a insert of a new one. That's my tables
create table Department (
Department_ID varchar(4) not null,
Name varchar(25) unique,
Department_Head_ID varchar(9),
College_ID varchar(4),
Credits_To_Degree NUMBER(3),
constraint pkDepartment primary key (Department_ID));
create table Enrollment (
Student_ID varchar(9) not null,
Course_ID varchar(5) not null,
Registered_Date date,Grade NUMBER,
Status varchar(4),constraint pkEnrollment primary key
(Student_ID, Course_ID));
create table Student (
Student_ID varchar(9) not null,
First_Name varchar(25),
Last_Name varchar(25),
Phone char(11),
Birth_Date date,
Street varchar(100),
Zip_Code char(5),
Department varchar(4),
Credits integer,
Eligible char(4), constraint pkStudent primary key
(Student_ID),constraint fkDeptId foreign key (Department)
references Department(Department_ID));
ALTER TABLE Department ADD FOREIGN KEY (Department_Head_ID)
REFERENCES Faculty(Faculty_Id) INITIALLY DEFERRED;
ALTER TABLE Department ADD FOREIGN KEY(College_ID) REFERENCES
College(College_ID) INITIALLY DEFERRED;
the trigger:
create or replace TRIGGER Credits
after INSERT OR UPDATE
ON enrollment
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
BEGIN
UPDATE STUDENT
SET CREDITS=(SELECT SUM(c.CREDITS) FROM COURSE c,ENROLLMENT e
WHERE c.COURSE_ID = e.COURSE_ID
and e.STUDENT_ID = :new.student_id
and e.GRADE>= 60 )
WHERE STUDENT.STUDENT_ID=:new.student_id;
END;
The trigger compiles but when i change value of the grade a get this error
UPDATE "FELIX"."ENROLLMENT" SET GRADE = '60' WHERE ROWID = 'AAAGUSAABAAALKJAAF' AND ORA_ROWSCN = '3540016'
ORA-20003: An error has occurred while performing credits trigger
ORA-06512: at "FELIX.CREDITS", line 5
ORA-04088: error during execution of trigger 'FELIX.CREDITS'
One error saving changes to table "FELIX"."ENROLLMENT":
Row 6: ORA-20003: An error has occurred while performing credits trigger
ORA-06512: at "FELIX.CREDITS", line 5
ORA-04088: error during execution of trigger 'FELIX.CREDITS'
line 5 is a FOR EACH ROW command
I figure out that the problem is in the :new.student_id. But how could i get the id of the row that the triggers fires upon.
Thanks in advance for the help.

Employee/History - Part of composite key as foreign key

I've got 2 entities:
1) EMPLOYEES (Parent)
CREATE TABLE EMPLOYEES (
employee_id NUMBER (3) NOT NULL,
first_name VARCHAR (20) NOT NULL,
last_name VARCHAR (20) NOT NULL,
job_title VARCHAR (20) NOT NULL,
employee_type VARCHAR (1) NOT NULL,
salary NUMBER (5),
hourly_pay NUMBER (5,2),
bonus_pay NUMBER (5,2),
CONSTRAINT employee_pk PRIMARY KEY(employee_id));
2) EMPLOYEE_HISTORY (Child)
CREATE TABLE EMPLOYEE_HISTORY (
start_date DATE NOT NULL,
employee_id NUMBER (3) NOT NULL,
end_date DATE,
job_title VARCHAR (10) NOT NULL,
hourly_rate NUMBER (5,2) NOT NULL,
CONSTRAINT employee_history_pk PRIMARY KEY(start_date, employee_id));
I'm trying to create:
ALTER TABLE employee_history
ADD CONSTRAINT employee_history_fk
FOREIGN KEY (employee_id)
REFERENCES employee_history(employee_id);
When I do this, I get an error
ORA-02270: no matching unique or primary key for this column-list
My guess is that I cannot create the constraint on just employee_id because I have a composite key in my child table. I understand when an employee gets put into the database, the parent table is filled out and the "start date" should be filled out along with everything else. However, I do not understand how this would work if I had start_date in my parent table as well. I would be able to create my constraint, yes, but how will I be able to keep a record of changes in start_date if my start_date was inputted at the time of when the employee was entered into the database.I thought about using job_title as a primary key instead of start_date because it's present in both tables, but what happens when an employee gets promoted and demoted again? Won't a duplicate value constraint come up when the same employee_id and job_title is getting inserted?
Your references clause needs to reference the parent table. Not the child table
ALTER TABLE employee_history
ADD CONSTRAINT employee_history_fk
FOREIGN KEY (employee_id)
REFERENCES employee(employee_id); -- employee not employee_history
The SQL you posted is trying to create a self-referential foreign key where employee_history is both the parent and the child. That doesn't make sense in this case.

Resources