麻烦在Oracle SQL DB中创建表

时间:2014-11-26 14:27:50

标签: sql database oracle

您好我已经创建了表并为它制作了交易。现在我在创建表的视图时遇到了麻烦。我试图找到错误并继续这样做但仍然不顺利。

我是SQL和Stack溢出的新手。 以下代码是创建表和事务,然后是具有错误的视图。

**Create Table**


create TABLE employee

(
EmployeeID varchar2(10) PRIMARY KEY,

EmployeeName varchar2(30) NOT NULL,

Phone number(10) NOT NULL,

JobTitle varchar2(30) NOT NULL

);


create TABLE Airplane

(

AirplaneID varchar2(10) PRIMARY KEY,

Capacity number NOT NULL,

Modle varchar2(10) NOT NULL

);


create TABLE Route

(

FlightID varchar2(10) PRIMARY KEY,

Origin varchar2(20) NOT NULL,

Destination varchar2(20) NOT NULL,

ETD number(10) NOT NULL,

ETA number(10) NOT NULL

);


create TABLE Customer

(

CustomerID varchar2(10) PRIMARY KEY,

CustomerName varchar2(10) NOT NULL,

PhoneNumber number NOT NULL

);


create TABLE Maintenance

(

MaintenanceID varchar2(20) PRIMARY KEY,

MaintenanceDate date NOT NULL,

AirplaneID varchar2(10) NOT NULL,

EmployeeID varchar2(10) NOT NULL,

Location varchar2(30) NOT NULL,

FOREIGN KEY (AirplaneID) REFERENCES Airplane(AirplaneID),

FOREIGN KEY (EmployeeID) REFERENCES employee(EmployeeID)

);

create TABLE Flight

(

FlightID varchar2(10),

FlightDate date,

AirplaneID varchar2(10),

ATD number(10) NOT NULL,

ATA number(10) NOT NULL,

--FOREIGN KEY (FlightID) REFERENCES Flight(FlightID),  --check you can't do this:

FOREIGN KEY (AirplaneID) REFERENCES Airplane(AirplaneID),

CONSTRAINT PK_FlightID PRIMARY KEY (FlightID,FlightDate)

);

create TABLE Reservation

(

ReservationID varchar2(20) PRIMARY KEY,

CustomerID varchar2(10) NOT NULL,

FlightID varchar2(10) NOT NULL,

FlightDate date NOT NULL,

Fare float,

PaymentMethod varchar2(20),

CardNumber number(30) NOT NULL,

ExperationDate date,

check (PaymentMethod = 'Cash' OR PaymentMethod ='Credit' OR PaymentMethod 

='Cheque'),

FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID),

FOReIGN KEY (FlightID,FlightDate) REFERENCES Flight(FlightID,FlightDate)

);

create TABLE CrewAssignment

(

EmployeeID varchar2(10),

FlightID varchar2(10),

FlightDate date NOT NULL,

Role varchar2(20) NOT NULL,

FlightHour number(10) NOT NULL,

FOReIGN KEY (FlightID,FlightDate) REFERENCES Flight(FlightID,FlightDate),

FOReIGN KEY (EmployeeID) REFERENCES employee(EmployeeID) ,

CONSTRAINT PK_CrewAssignment PRIMARY key (FlightID,FlightDate,EmployeeID)

);

交易

insert into Route (FlightID, Origin, Destination, ETD, ETA) values

('FBN001', 'Perth', 'Singapore', '1100', '1600');

insert into Airplane (AirplaneID, Capacity, Modle) values

('FH-FBT', '350', 'Boeing767');



insert into Flight (FlightID, FlightDate, AirplaneID, ATD, ATA) values

( 'FBN001', '20 october 2014', 'FH-FBT', '1105', '1555');

insert into Flight (FlightID, FlightDate, AirplaneID, ATD, ATA) values

( 'FBN001', '20 october 2014', 'FH-FBT', '1105', '1555');

insert into Employee (EmployeeID,EmployeeName,Phone,JobTitle) values

('01', 'Martha McGee','123456', 'Pilot');

insert into Employee (EmployeeID,EmployeeName,Phone,JobTitle) values

('02','Dorothy McDonald','2211', 'Co-Pilot');

insert into Employee (EmployeeID,EmployeeName,Phone,JobTitle) values

('03','Albert','1122', 'Engineer');

insert into Employee (EmployeeID,EmployeeName,Phone,JobTitle) values

('04','Kathy Kelly','5544', 'HeadSteward');

insert into Employee (EmployeeID,EmployeeName,Phone,JobTitle) values

('05', 'Ornella', '3123', 'Steward');

insert into CrewAssignment (EmployeeID,FlightID,FlightDate, Role, FlightHour) values

('01','FBN001', '20 October 2014', 'Flight','5');

insert into CrewAssignment  (EmployeeID,FlightID,FlightDate, Role, FlightHour) values

('02','FBN001', '20 October 2014', 'Flight','5');

insert into CrewAssignment  (EmployeeID,FlightID,FlightDate, Role, FlightHour) values

('03','FBN001', '20 October 2014', 'Flight','5');

insert into CrewAssignment  (EmployeeID,FlightID,FlightDate, Role, FlightHour) values

('04','FBN001', '20 October 2014', 'Non-Flight','0');

insert into CrewAssignment  (EmployeeID,FlightID,FlightDate, Role, FlightHour) values

('05','FBN001', '20 October 2014', 'Non-Flight','0');

insert into Customer (CustomerID,CustomerName,PhoneNumber) Values

('01', 'John Smith', '81393'); 




insert into Reservation (ReservationID,CustomerID,FlightID, FlightDate,Fare,PaymentMethod,CardNumber, ExperationDate) values
('01' , '01', 'FBN001','20 October 2014', '100' , 'Cash' , '44444' , '21 Feb 2019'); 







insert into Employee (EmployeeID,EmployeeName,Phone,JobTitle) values
('06','Laurence Schreiner','007', 'Supervisor');

insert into Maintenance (MaintenanceID,MaintenanceDate, EmployeeID, AirplaneID, Location) values
('004', '21 October 2014', '06', 'FH-FBT', 'Melbourne Airport');

创建视图

ViewA: 约翰史密斯的所有航班预订,包括已飞行的航班,航班的持续时间

Create View ViewA as 
Select Reservation.ReservationID,Flight.FlightID,Flight.FlightDate, Flight.ATA - Flight.ATD as DurationofFlight 
from Reservation, Customer, FLight
where Customer.CustomerName = 'John Smith' and
Customer.CustomerID = Reservation.CustomerID and
Reservation.FlightID = Flight.FlightID and
Reservation.FlightDate = Flight.FlightDate;

ViewB: 2014年10月20日FBN001上未预留/可用座位的数量。

Create view ViewB as 
Select Airplane.Capacity, count(Reservaion.ReservationID)
from Airplane,Reservation
where FlightID='FBN001' and FlightDate='20 October 2014'
and Flight.AirplaneID = Airplane.AirplaneID
and Flight.FlightID = Reservaton.FlightID
and Flight.FlightDate=Reservation.FlightDate;

ViewC: 2014年10月20日FBN001船员的总飞行小时数。

Create vire ViewC as 
select EmployeeID, sum(FlightHour)
from CrewAssignment
where CrewAssignment.EmployeeID in
(select EmployeeNO from CrewAssignment
where FlightID = 'FBN001' and FlightDate = '20 October 2014')
group by EmployeeID;

ViewE: FH-FBT的维护历史记录,包括维护事件的日期和地点,维护是否安排,以及监督员工的姓名和电话号码。

  Create view ViewE as 
select Maintenance.MaintenanceDate, Maintenance.Maintenance.Location,
Employee.EmployeeName,Employee.EmployeeName 
from Maintenance, Employee
where Maintenance.AirplaneID = 'FH-FBT' and
Maintenance.EmloyeeID = Employee.EmployeeID;

这是我为每个表获得的错误

查看A

Error starting at line : 157 in command -
create View ViewA as 
Select Reservation.ReservationID,Flight.FlightID,Flight.FlightDate, Flight.ATA - Flight.ATD as DurationofFlight 
from Reservation, Customer, FLight
where Customer.CustomerName = 'John Smith' and
Customer.CustomerID = Reservation.CustomerID and
Reservation.FlightID = Flight.FlightID and
Reservation.FlightDate = Flight.FlightDate
Error at Command Line : 157 Column : 13
Error report -
SQL Error: ORA-00955: name is already used by an existing object
00955. 00000 -  "name is already used by an existing object"
*Cause:    
*Action:

查看B

Error starting at line : 166 in command -
create view ViewB as 
Select Airplane.Capacity,count(Reservaion.ReservationID)
from Airplane,Reservation
where FlightID='FBN001' and FlightDate='20 October 2014'
and Flight.AirplaneID = Airplane.AirplaneID
and Flight.FlightID = Reservaton.FlightID
and Flight.FlightDate=Reservation.FlightDate
Error at Command Line : 172 Column : 5
Error report -
SQL Error: ORA-00904: "FLIGHT"."FLIGHTDATE": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:

ViewC

Error starting at line : 177 in command -
create view ViewC as 
select EmployeeID, sum(FlightHour)
from CrewAssignment
where CrewAssignment.EmployeeID in
(select EmployeeID from CrewAssignment
where FlightID = 'FBN001' and FlightDate = '20 October 2014')
group by EmployeeID
Error at Command Line : 178 Column : 20
Error report -
SQL Error: ORA-00998: must name this expression with a column alias
00998. 00000 -  "must name this expression with a column alias"
*Cause:    
*Action:

ViewE

Error starting at line : 186 in command -
create view ViewE as 
select Maintainence.MaintainenceDate, Maintainence.maintainence.Description,
Employee.EmployeeName,Employee.EmployeeName 
from Maintenance, Employee
where Maintenance.AirplaneID = 'FH-FBT' and
Maintenance.EmloyeeID = Employee.EmployeeID
Error at Command Line : 191 Column : 1
Error report -
SQL Error: ORA-00904: "MAINTENANCE"."EMLOYEEID": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:

3 个答案:

答案 0 :(得分:0)

您正在使用Creater代替createcreater更改为create,它应该有效。请在create vier而不是create view

的某个时刻检查您的拼写
Create view ViewE as 
select Maintainence.MaintainenceDate, Maintainence.maintainence.Description,
Employee.EmployeeName,Employee.EmployeeName 
from Maintenance, Employee
where Maintenance.AirplaneID = 'FH-FBT' and
Maintenance.EmloyeeID = Employee.EmployeeID;

sum(FlightHour) - 必须用别名

命名

无效的标识符 - 列不存在

现有对象已使用的名称 - 使用create or replace view

任何其他 - 请在尝试严肃的事情之前阅读sql手册;

答案 1 :(得分:0)

有很多问题。一些是:

  1. ViewBFLIGHT子句中引用WHERE,但FLIGHT不在FROM条款中。
  2. ViewC的陈述代表create vire而不是create view
  3. ViewC引用employeeno中的CrewAssignment,但不存在此列。
  4. ViewC中,您使用的是没有别名的聚合函数,因此数据库无法确定列的名称。
  5. ViewE中,EmployeeId缺少“p”。
  6. ViewE引用Maintainence.maintainence.Description。两次指定表名无效。此外,Description表中没有Maintainence列。
  7. 这些都是非常基本的问题,应使用基本的故障排除技巧解决。发布此问题表明完全没有努力解决这些问题。

答案 2 :(得分:0)

View A:
create view ViewA as
select Reservation.ReservationID, Flight.FlightID, Flight.FlightDate, Flight.ATA - Flight.ATD as DurationOFFlight
from Reservation, Customer, Flight
where Customer.CustomerName = 'John Smith' and
Customer.CustomerID = Reservation.CustomerID and
Reservation.FlightID=Flight.FlightID and
Reservation.FlightDate=Flight.FlightDate;

View B:

create view ViewB as
select Airplane.Capacity - Reservation.ReservationID
as RemainingSeats
from Airplane, Reservation, Flight
where
Flight.FlightID = 'FBN001' and Flight.FlightDate='20 October 2014'
and Flight.AirplaneID = Airplane.AirplaneID and
Flight.FlightID = Reservation.FlightID and
Flight.FlightDate = Reservation.FlightDate;


View C:

create view ViewC as
select EmployeeID, sum(FlightHour) as Total_Hours
from CrewAssignment
where CrewAssignment.EmployeeID in
(select EmployeeID from CrewAssignment
where FlightID='FBN001' and FlightDate='20-10-2014')
group by EmployeeID;

View D:

create view ViewD as
select JobTitle, sum(FlightHour) as Total_Hours
from CrewAssignment
where CrewAssignment.EmployeeID in
(select EmployeeID from CrewAssignment
where FlightID='FBN001' and FlightDate='20-10-2014' and EmployeeID= '123456')
group by JobTitle;


View E: 

create view ViewE as
select Maintenance.MaintenanceDate,Maintenance.Location, 
Employee.EmployeeName,Employee.Empphone from Maintenance,Employee 
where Maintenance.AirplaneID = 'FH-FBT' and Maintenance.EmployeeID = Employee.EmployeeID;