在Oracle SQL DB中创建表时遇到问题
您好,我已经创建了表并为其进行了事务处理。现在,我在为表创建视图时遇到问题。我试着找出错误并继续做下去,但仍然做得不好 我不熟悉SQL和堆栈溢出。 下面的代码正在创建表和事务,后面是出现错误的视图在Oracle SQL DB中创建表时遇到问题,sql,database,oracle,Sql,Database,Oracle,您好,我已经创建了表并为其进行了事务处理。现在,我在为表创建视图时遇到问题。我试着找出错误并继续做下去,但仍然做得不好 我不熟悉SQL和堆栈溢出。 下面的代码正在创建表和事务,后面是出现错误的视图 **Create Table** create TABLE employee ( EmployeeID varchar2(10) PRIMARY KEY, EmployeeName varchar2(30) NOT NULL, Phone number(10) NOT NULL, JobT
**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');
创建视图
视图A:
John Smith预订的所有航班,包括已飞行的航班的航班持续时间
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;
视图B:
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;
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;
ViewC:
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;
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;
视图E:
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
维维
您正在使用
Creater
而不是create
将Creater
更改为create
,它应该可以工作。请同时检查您的Spelling,您有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 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;
任何其他-在尝试像这样严肃的事情之前,请阅读sql手册 有很多问题。其中一些是:
ViewB
在WHERE
子句中引用FLIGHT
,但FLIGHT
不在FROM
子句中ViewC
的语句是createvire
而不是createview
ViewC
引用了crewaSignment
中的employeeno
,但不存在此类列ViewC
中,您使用的聚合函数没有别名,因此数据库无法确定列的名称ViewE
中,EmployeeId
缺少“p”ViewE
referencesmaintenance.maintenance.Description
。指定两次表名无效。此外,维护
表中没有说明
列这些都是非常基本的问题,应该使用基本的故障排除技能来解决。发布此问题表明完全没有努力解决这些问题。您应该编辑您的问题并确定您遇到问题的视图。你也应该提到你得到的错误。对不起,我只是第一次在这里发布。我将编辑并包含错误。只需尝试,兄弟仍有错误。我将编辑并发布每个视图的错误。我刚刚编辑并上传了我遇到的错误的问题。看到我的编辑,你会明白的。我建议你使用谷歌,看看有多少关于这些错误的例子!谢谢你的建议。我去试一试,然后麻烦地射杀那个男人。对不起,我犯了个粗心的错误。我对SQL和这里都是新手。无论如何,我已经设法纠正我的观点了。再见,谢谢。
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;